Beispiel #1
0
HandlerResult DomainProperty::tick_handler(LocatedEntity * entity, const Operation & op, OpVector & res)
{
    if (!op->getArgs().empty() && op->getArgs().front()->getName() == "domain") {
        Domain* domain = sInstanceState.getState(entity);
        if (domain) {

            float timeNow = op->getSeconds();

            float timeForNextTick = domain->tick(timeNow, res);
            if (timeForNextTick > 0) {
                scheduleTick(*entity, timeForNextTick);
            }
        }
        return OPERATION_BLOCKED;
    }
    return OPERATION_IGNORED;
}
Beispiel #2
0
 bool operator>(const OpQueEntry& right) const {
     return op->getSeconds() > right->getSeconds();
 }
Beispiel #3
0
HandlerResult UsagesProperty::use_handler(LocatedEntity* e,
                                          const Operation& op, OpVector& res)
{


    auto actor = BaseWorld::instance().getEntity(op->getFrom());
    if (!actor) {
        e->error(op, "Could not find 'from' entity.", res, e->getId());
        return OPERATION_IGNORED;
    }

    if (op->isDefaultFrom()) {
        actor->error(op, "Top op has no 'from' attribute.", res, actor->getId());
        return OPERATION_IGNORED;
    }

    if (!op->getArgs().empty()) {
        auto& arg = op->getArgs().front();
        auto argOp = smart_dynamic_cast<Atlas::Objects::Operation::RootOperation>(arg);
        if (!argOp) {
            actor->error(op, "First arg wasn't an operation.", res, actor->getId());
            return OPERATION_IGNORED;
        }


        if (!argOp->hasAttrFlag(Atlas::Objects::PARENT_FLAG)) {
            actor->error(op, "Use arg op has malformed parent", res, actor->getId());
            return OPERATION_IGNORED;
        }
        auto op_type = argOp->getParent();
        debug_print("Got op type " << op_type << " from arg");

        auto obj = Atlas::Objects::Factories::instance()->createObject(op_type);
        if (!obj.isValid()) {
            log(ERROR, String::compose("Character::UseOperation Unknown op type "
                                       "\"%1\".", op_type));
            return OPERATION_IGNORED;
        }

        auto rop = smart_dynamic_cast<Operation>(obj);
        if (!rop.isValid()) {
            log(ERROR, String::compose("Character::UseOperation Op type "
                                       "\"%1\" but it is not an operation type. ", op_type));
            return OPERATION_IGNORED;
        }
        rop->setFrom(actor->getId());
        rop->setTo(e->getId());
        rop->setSeconds(op->getSeconds());

        if (argOp->getArgs().empty()) {
            actor->error(op, "Use arg op has no arguments; one expected.", res, actor->getId());
            return OPERATION_IGNORED;
        }

        auto arguments = argOp->getArgs().front();

        //Check that there's an action registered for this operation
        auto usagesI = m_usages.find(op_type);
        if (usagesI != m_usages.end()) {
            auto& usage = usagesI->second;

            //Check that the tool is ready
            auto toolReadyAtProp = e->getPropertyType<double>("ready_at");
            if (toolReadyAtProp) {
                if (toolReadyAtProp->data() > BaseWorld::instance().getTime()) {
                    actor->clientError(op, "Tool is not ready yet.", res, actor->getId());
                    return OPERATION_IGNORED;
                }
            }

            //Check if the tools is attached, and if so the attachment is ready
            auto actorReadyAtProp = actor->getPropertyType<MapType>("_ready_at_attached");
            if (actorReadyAtProp) {

                auto plantedOnProp = e->getPropertyClassFixed<PlantedOnProperty>();
                //First check if the tool is attached to the actor at an attach point
                if (plantedOnProp && plantedOnProp->data().entity.get() == actor.get() && plantedOnProp->data().attachment) {
                    auto attachPoint = *plantedOnProp->data().attachment;
                    //Lastly check if there's a value for this attach point.
                    auto attachI = actorReadyAtProp->data().find(attachPoint);
                    if (attachI != actorReadyAtProp->data().end()) {
                        if (attachI->second.isFloat() && attachI->second.Float() > BaseWorld::instance().getTime()) {
                            actor->clientError(op, "Actor is not ready yet.", res, actor->getId());
                            return OPERATION_IGNORED;
                        }
                    }
                }
            }

            //Populate the usage arguments
            std::map<std::string, std::vector<UsageInstance::UsageArg>> usage_instance_args;

            for (auto& param : usage.params) {
                Atlas::Message::Element element;
                if (arguments->copyAttr(param.first, element) != 0 || !element.isList()) {
                    actor->clientError(op, String::compose("Could not find required list argument '%1'.", param.first), res, actor->getId());
                    return OPERATION_IGNORED;
                }

                auto& argVector = usage_instance_args[param.first];

                for (auto& argElement : element.List()) {
                    switch (param.second.type) {
                        case UsageParameter::Type::ENTITY: {
                            if (!argElement.isMap()) {
                                actor->clientError(op, String::compose("Inner argument in list of arguments for '%1' was not a map.", param.first), res, actor->getId());
                                return OPERATION_IGNORED;
                            }
                            //The arg is for an RootEntity, expressed as a message. Extract id and pos.
                            auto idI = argElement.Map().find("id");
                            if (idI == argElement.Map().end() || !idI->second.isString()) {
                                actor->clientError(op, String::compose("Inner argument in list of arguments for '%1' had no id string.", param.first), res, actor->getId());
                                return OPERATION_IGNORED;
                            }

                            auto involved = BaseWorld::instance().getEntity(idI->second.String());
                            if (!involved) {
                                actor->error(op, "Involved entity does not exist", res, actor->getId());
                                return OPERATION_IGNORED;
                            }

                            auto posI = argElement.Map().find("pos");
                            if (posI != argElement.Map().end() && posI->second.isList()) {
                                argVector.emplace_back(EntityLocation(involved, WFMath::Point<3>(posI->second)));
                            } else {
                                argVector.emplace_back(EntityLocation(involved));
                            }
                        }
                            break;
                        case UsageParameter::Type::ENTITYLOCATION:
                            argVector.emplace_back(WFMath::Point<3>(argElement));
                            break;
                        case UsageParameter::Type::POSITION:
                            argVector.emplace_back(WFMath::Point<3>(argElement));
                            break;
                        case UsageParameter::Type::DIRECTION:
                            argVector.emplace_back(WFMath::Vector<3>(argElement));
                            break;
                    }
                }

            }


            UsageInstance usageInstance{usage, actor, e, std::move(usage_instance_args), rop};
            //Check that the usage is valid before continuing
            auto validRes = usageInstance.isValid();
            if (!validRes.first) {
                actor->clientError(op, validRes.second, res, actor->getId());
            } else {
                auto lastSeparatorPos = usage.handler.find_last_of('.');
                if (lastSeparatorPos != std::string::npos) {
                    auto moduleName = usage.handler.substr(0, lastSeparatorPos);
                    auto functionName = usage.handler.substr(lastSeparatorPos + 1);
                    //Py::Module module(moduleName);
                    Py::Module module(PyImport_Import(Py::String(moduleName).ptr()));
                    //PyImport_ReloadModule(module.ptr());
                    auto functionObject = module.getDict()[functionName];
                    if (!functionObject.isCallable()) {
                        actor->error(op, String::compose("Could not find Python function %1", usage.handler), res, actor->getId());
                        return OPERATION_IGNORED;
                    }

                    try {

                        PythonLogGuard logGuard([functionName, actor]() {
                            return String::compose("Usage '%1', entity %2: ", functionName, actor->describeEntity());
                        });
                        auto ret = Py::Callable(functionObject).apply(Py::TupleN(UsageInstance::scriptCreator(std::move(usageInstance))));
                        return ScriptUtils::processScriptResult(usage.handler, ret, res, e);
                    } catch (const Py::BaseException& py_ex) {
                        log(ERROR, String::compose("Python error calling \"%1\" for entity %2", usage.handler, e->describeEntity()));
                        if (PyErr_Occurred()) {
                            PyErr_Print();
                        }
                    }
                }
            }
            return OPERATION_BLOCKED;

        }
    }
    //We couldn't find any suitable task.
    return OPERATION_IGNORED;
}