From a092be42769e9d268af644d7dcf8b692fcadc518 Mon Sep 17 00:00:00 2001 From: Vsevolod Kremianskii Date: Mon, 16 Nov 2020 19:34:22 +0700 Subject: [PATCH] feat: MoveToObject action is walk-by-default --- src/game/action/movetoobject.cpp | 7 ++++++- src/game/action/movetoobject.h | 4 +++- src/game/actionexecutor.cpp | 31 ++++++++++++++++------------ src/game/actionexecutor.h | 4 ++-- src/game/script/routines_actions.cpp | 5 +++-- 5 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/game/action/movetoobject.cpp b/src/game/action/movetoobject.cpp index efd93e9a..d74d2c96 100644 --- a/src/game/action/movetoobject.cpp +++ b/src/game/action/movetoobject.cpp @@ -23,11 +23,16 @@ namespace reone { namespace game { -MoveToObjectAction::MoveToObjectAction(const shared_ptr &object, float distance) : +MoveToObjectAction::MoveToObjectAction(const shared_ptr &object, bool run, float distance) : ObjectAction(ActionType::MoveToObject, object), + _run(run), _distance(distance) { } +bool MoveToObjectAction::getRun() const { + return _run; +} + float MoveToObjectAction::distance() const { return _distance; } diff --git a/src/game/action/movetoobject.h b/src/game/action/movetoobject.h index f5cc77a7..91a07ce3 100644 --- a/src/game/action/movetoobject.h +++ b/src/game/action/movetoobject.h @@ -25,11 +25,13 @@ namespace game { class MoveToObjectAction : public ObjectAction { public: - MoveToObjectAction(const std::shared_ptr &object, float distance); + MoveToObjectAction(const std::shared_ptr &object, bool run, float distance); + bool getRun() const; float distance() const; private: + bool _run { false }; float _distance { 0.0f }; }; diff --git a/src/game/actionexecutor.cpp b/src/game/actionexecutor.cpp index 6ed0db16..73c7e24b 100644 --- a/src/game/actionexecutor.cpp +++ b/src/game/actionexecutor.cpp @@ -93,7 +93,7 @@ void ActionExecutor::executeActions(Object &object, float dt) { void ActionExecutor::executeMoveToPoint(Creature &actor, MoveToPointAction &action, float dt) { glm::vec3 dest(action.point()); - bool reached = navigateCreature(actor, dest, 1.0f, dt); + bool reached = navigateCreature(actor, dest, true, 1.0f, dt); if (reached) { action.complete(); } @@ -102,9 +102,10 @@ void ActionExecutor::executeMoveToPoint(Creature &actor, MoveToPointAction &acti void ActionExecutor::executeMoveToObject(Creature &actor, MoveToObjectAction &action, float dt) { SpatialObject &object = *static_cast(action.object()); glm::vec3 dest(object.position()); + bool run = action.getRun(); float distance = action.distance(); - bool reached = navigateCreature(actor, dest, distance, dt); + bool reached = navigateCreature(actor, dest, run, distance, dt); if (reached) { action.complete(); } @@ -115,7 +116,7 @@ void ActionExecutor::executeFollow(Creature &actor, FollowAction &action, float glm::vec3 dest(object.position()); float distance = action.distance(); - navigateCreature(actor, dest, distance, dt); + navigateCreature(actor, dest, true, distance, dt); } void ActionExecutor::executeDoCommand(Object &actor, CommandAction &action, float dt) { @@ -130,7 +131,11 @@ void ActionExecutor::executeStartConversation(Object &actor, StartConversationAc Creature *creatureActor = dynamic_cast(&actor); SpatialObject &object = static_cast(*action.object()); - bool reached = !creatureActor || action.isStartRangeIgnored() || navigateCreature(*creatureActor, object.position(), kMaxConversationDistance, dt); + bool reached = + !creatureActor || + action.isStartRangeIgnored() || + navigateCreature(*creatureActor, object.position(), true, kMaxConversationDistance, dt); + if (reached) { bool isActorLeader = _game->party().leader()->id() == actor.id(); _game->module()->area()->startDialog(isActorLeader ? object : static_cast(actor), action.dialogResRef()); @@ -138,7 +143,7 @@ void ActionExecutor::executeStartConversation(Object &actor, StartConversationAc } } -bool ActionExecutor::navigateCreature(Creature &creature, const glm::vec3 &dest, float distance, float dt) { +bool ActionExecutor::navigateCreature(Creature &creature, const glm::vec3 &dest, bool run, float distance, float dt) { const glm::vec3 &origin = creature.position(); float distToDest = glm::distance2(origin, dest); @@ -153,7 +158,7 @@ bool ActionExecutor::navigateCreature(Creature &creature, const glm::vec3 &dest, if (path) { uint32_t now = SDL_GetTicks(); if (path->destination == dest || now - path->timeFound <= kKeepPathDuration) { - advanceCreatureOnPath(creature, dt); + advanceCreatureOnPath(creature, run, dt); updatePath = false; } } @@ -164,7 +169,7 @@ bool ActionExecutor::navigateCreature(Creature &creature, const glm::vec3 &dest, return false; } -void ActionExecutor::advanceCreatureOnPath(Creature &creature, float dt) { +void ActionExecutor::advanceCreatureOnPath(Creature &creature, bool run, float dt) { const glm::vec3 &origin = creature.position(); shared_ptr path(creature.path()); size_t pointCount = path->points.size(); @@ -194,8 +199,8 @@ void ActionExecutor::advanceCreatureOnPath(Creature &creature, float dt) { if (distToDest <= 1.0f) { selectNextPathPoint(*path); - } else if (_game->module()->area()->moveCreatureTowards(creature, dest, true, dt)) { - creature.setMovementType(Creature::MovementType::Run); + } else if (_game->module()->area()->moveCreatureTowards(creature, dest, run, dt)) { + creature.setMovementType(run ? Creature::MovementType::Run : Creature::MovementType::Walk); } else { creature.setMovementType(Creature::MovementType::None); @@ -221,7 +226,7 @@ void ActionExecutor::executeOpenDoor(Object &actor, ObjectAction &action, float Creature *creatureActor = dynamic_cast(&actor); Door &door = *static_cast(action.object()); - bool reached = !creatureActor || navigateCreature(*creatureActor, door.position(), 1.0f, dt); + bool reached = !creatureActor || navigateCreature(*creatureActor, door.position(), true, 1.0f, dt); if (reached) { bool isObjectSelf = actor.id() == door.id(); if (!isObjectSelf && door.isLocked()) { @@ -242,7 +247,7 @@ void ActionExecutor::executeCloseDoor(Object &actor, ObjectAction &action, float Creature *creatureActor = dynamic_cast(&actor); Door &door = *static_cast(action.object()); - bool reached = !creatureActor || navigateCreature(*creatureActor, door.position(), 1.0f, dt); + bool reached = !creatureActor || navigateCreature(*creatureActor, door.position(), true, 1.0f, dt); if (reached) { door.close(&actor); action.complete(); @@ -251,7 +256,7 @@ void ActionExecutor::executeCloseDoor(Object &actor, ObjectAction &action, float void ActionExecutor::executeOpenContainer(Creature &actor, ObjectAction &action, float dt) { Placeable &placeable = *static_cast(action.object()); - bool reached = navigateCreature(actor, placeable.position(), 1.0f, dt); + bool reached = navigateCreature(actor, placeable.position(), true, 1.0f, dt); if (reached) { _game->openContainer(&placeable); action.complete(); @@ -261,7 +266,7 @@ void ActionExecutor::executeOpenContainer(Creature &actor, ObjectAction &action, void ActionExecutor::executeOpenLock(Creature &actor, ObjectAction &action, float dt) { Door *door = dynamic_cast(action.object()); if (door) { - bool reached = navigateCreature(actor, door->position(), 1.0f, dt); + bool reached = navigateCreature(actor, door->position(), true, 1.0f, dt); if (reached) { actor.face(*door); actor.playAnimation(Creature::Animation::UnlockDoor); diff --git a/src/game/actionexecutor.h b/src/game/actionexecutor.h index b9e44431..febfdf2d 100644 --- a/src/game/actionexecutor.h +++ b/src/game/actionexecutor.h @@ -44,8 +44,8 @@ private: ActionExecutor(const ActionExecutor &) = delete; ActionExecutor &operator=(const ActionExecutor &) = delete; - bool navigateCreature(Creature &creature, const glm::vec3 &dest, float distance, float dt); - void advanceCreatureOnPath(Creature &creature, float dt); + bool navigateCreature(Creature &creature, const glm::vec3 &dest, bool run, float distance, float dt); + void advanceCreatureOnPath(Creature &creature, bool run, float dt); void selectNextPathPoint(Creature::Path &path); void updateCreaturePath(Creature &creature, const glm::vec3 &dest); diff --git a/src/game/script/routines_actions.cpp b/src/game/script/routines_actions.cpp index 6651a726..a7da0809 100644 --- a/src/game/script/routines_actions.cpp +++ b/src/game/script/routines_actions.cpp @@ -63,12 +63,13 @@ Variable Routines::actionDoCommand(const vector &args, ExecutionContex Variable Routines::actionMoveToObject(const vector &args, ExecutionContext &ctx) { int objectId = args[0].objectId; - float distance = args.size() >= 2 ? args[2].floatValue : 1.0f; + bool run = args.size() >= 2 ? (args[1].intValue != 0) : false; + float distance = args.size() >= 3 ? args[2].floatValue : 1.0f; shared_ptr actor(getObjectById(ctx.callerId, ctx)); if (actor) { shared_ptr object(getObjectById(objectId, ctx)); - unique_ptr action(new MoveToObjectAction(object, distance)); + unique_ptr action(new MoveToObjectAction(object, run, distance)); actor->actionQueue().add(move(action)); } else { warn("Routine: object not found: " + to_string(objectId));