Skip to content

Commit

Permalink
ENGINES: Implement heartbeat, use native KotOR AI
Browse files Browse the repository at this point in the history
  • Loading branch information
vkremianskii authored and DrMcCoy committed Feb 26, 2019
1 parent ef40916 commit 569ab77
Show file tree
Hide file tree
Showing 23 changed files with 398 additions and 101 deletions.
18 changes: 9 additions & 9 deletions src/engines/kotor/script/function_tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const Functions::FunctionPointer Functions::kFunctionPointers[] = {
{ 5, "PrintObject" , &Functions::printObject },
{ 6, "AssignCommand" , &Functions::assignCommand },
{ 7, "DelayCommand" , &Functions::delayCommand },
{ 8, "ExecuteScript" , 0 },
{ 8, "ExecuteScript" , &Functions::executeScript },
{ 9, "ClearAllActions" , &Functions::clearAllActions },
{ 10, "SetFacing" , 0 },
{ 11, "SwitchPlayerCharacter" , 0 },
Expand Down Expand Up @@ -156,7 +156,7 @@ const Functions::FunctionPointer Functions::kFunctionPointers[] = {
{ 35, "ActionPutDownItem" , 0 },
{ 36, "GetLastAttacker" , 0 },
{ 37, "ActionAttack" , 0 },
{ 38, "GetNearestCreature" , 0 },
{ 38, "GetNearestCreature" , &Functions::getNearestCreature },
{ 39, "ActionSpeakString" , 0 },
{ 40, "ActionPlayAnimation" , 0 },
{ 41, "GetDistanceToObject" , 0 },
Expand Down Expand Up @@ -280,8 +280,8 @@ const Functions::FunctionPointer Functions::kFunctionPointers[] = {
{ 159, "EffectChoke" , 0 },
{ 160, "SetGlobalString" , 0 },
{ 161, "EffectStunned" , 0 },
{ 162, "SetCommandable" , 0 },
{ 163, "GetCommandable" , 0 },
{ 162, "SetCommandable" , &Functions::setCommandable },
{ 163, "GetCommandable" , &Functions::getCommandable },
{ 164, "EffectRegenerate" , 0 },
{ 165, "EffectMovementSpeedIncrease" , 0 },
{ 166, "GetHitDice" , 0 },
Expand Down Expand Up @@ -580,7 +580,7 @@ const Functions::FunctionPointer Functions::kFunctionPointers[] = {
{ 459, "EffectForceShield" , 0 },
{ 460, "EffectDispelMagicAll" , 0 },
{ 461, "SetDialogPlaceableCamera" , 0 },
{ 462, "GetSoloMode" , 0 },
{ 462, "GetSoloMode" , &Functions::getSoloMode },
{ 463, "EffectDisguise" , 0 },
{ 464, "GetMaxStealthXP" , 0 },
{ 465, "EffectTrueSeeing" , 0 },
Expand Down Expand Up @@ -640,7 +640,7 @@ const Functions::FunctionPointer Functions::kFunctionPointers[] = {
{ 519, "IsCreditSequenceInProgress" , 0 },
{ 520, "SWMG_SetLateralAccelerationPerSecond", 0 },
{ 521, "SWMG_GetLateralAccelerationPerSecond", 0 },
{ 522, "GetCurrentAction" , 0 },
{ 522, "GetCurrentAction" , &Functions::getCurrentAction },
{ 523, "GetDifficultyModifier" , 0 },
{ 524, "GetAppearanceType" , 0 },
{ 525, "FloatingTextStrRefOnCreature" , 0 },
Expand Down Expand Up @@ -683,7 +683,7 @@ const Functions::FunctionPointer Functions::kFunctionPointers[] = {
{ 562, "GetFactionLeader" , 0 },
{ 563, "SWMG_SetSpeedBlurEffect" , 0 },
{ 564, "EndGame" , 0 },
{ 565, "GetRunScriptVar" , 0 },
{ 565, "GetRunScriptVar" , &Functions::getRunScriptVar },
{ 566, "GetCreatureMovmentType" , 0 },
{ 567, "AmbientSoundSetDayVolume" , 0 },
{ 568, "AmbientSoundSetNightVolume" , 0 },
Expand Down Expand Up @@ -819,7 +819,7 @@ const Functions::FunctionPointer Functions::kFunctionPointers[] = {
{ 698, "SpawnAvailableNPC" , 0 },
{ 699, "IsNPCPartyMember" , 0 },
{ 700, "ActionBarkString" , 0 },
{ 701, "GetIsConversationActive" , 0 },
{ 701, "GetIsConversationActive" , &Functions::getIsConversationActive },
{ 702, "EffectLightsaberThrow" , 0 },
{ 703, "EffectWhirlWind" , 0 },
{ 704, "GetPartyAIStyle" , 0 },
Expand Down Expand Up @@ -848,7 +848,7 @@ const Functions::FunctionPointer Functions::kFunctionPointers[] = {
{ 727, "GetFirstAttacker" , 0 },
{ 728, "GetNextAttacker" , 0 },
{ 729, "SetFormation" , 0 },
{ 730, "ActionFollowLeader" , 0 },
{ 730, "ActionFollowLeader" , &Functions::actionFollowLeader },
{ 731, "SetForcePowerUnsuccessful" , 0 },
{ 732, "GetIsDebilitated" , 0 },
{ 733, "PlayMovie" , &Functions::playMovie },
Expand Down
18 changes: 9 additions & 9 deletions src/engines/kotor2/script/function_tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const Functions::FunctionPointer Functions::kFunctionPointers[] = {
{ 5, "PrintObject" , &Functions::printObject },
{ 6, "AssignCommand" , &Functions::assignCommand },
{ 7, "DelayCommand" , &Functions::delayCommand },
{ 8, "ExecuteScript" , 0 },
{ 8, "ExecuteScript" , &Functions::executeScript },
{ 9, "ClearAllActions" , &Functions::clearAllActions },
{ 10, "SetFacing" , 0 },
{ 11, "SwitchPlayerCharacter" , 0 },
Expand Down Expand Up @@ -157,7 +157,7 @@ const Functions::FunctionPointer Functions::kFunctionPointers[] = {
{ 35, "ActionPutDownItem" , 0 },
{ 36, "GetLastAttacker" , 0 },
{ 37, "ActionAttack" , 0 },
{ 38, "GetNearestCreature" , 0 },
{ 38, "GetNearestCreature" , &Functions::getNearestCreature },
{ 39, "ActionSpeakString" , 0 },
{ 40, "ActionPlayAnimation" , 0 },
{ 41, "GetDistanceToObject" , 0 },
Expand Down Expand Up @@ -281,8 +281,8 @@ const Functions::FunctionPointer Functions::kFunctionPointers[] = {
{ 159, "EffectChoke" , 0 },
{ 160, "SetGlobalString" , 0 },
{ 161, "EffectStunned" , 0 },
{ 162, "SetCommandable" , 0 },
{ 163, "GetCommandable" , 0 },
{ 162, "SetCommandable" , &Functions::setCommandable },
{ 163, "GetCommandable" , &Functions::getCommandable },
{ 164, "EffectRegenerate" , 0 },
{ 165, "EffectMovementSpeedIncrease" , 0 },
{ 166, "GetHitDice" , 0 },
Expand Down Expand Up @@ -581,7 +581,7 @@ const Functions::FunctionPointer Functions::kFunctionPointers[] = {
{ 459, "EffectForceShield" , 0 },
{ 460, "EffectDispelMagicAll" , 0 },
{ 461, "SetDialogPlaceableCamera" , 0 },
{ 462, "GetSoloMode" , 0 },
{ 462, "GetSoloMode" , &Functions::getSoloMode },
{ 463, "EffectDisguise" , 0 },
{ 464, "GetMaxStealthXP" , 0 },
{ 465, "EffectTrueSeeing" , 0 },
Expand Down Expand Up @@ -641,7 +641,7 @@ const Functions::FunctionPointer Functions::kFunctionPointers[] = {
{ 519, "IsCreditSequenceInProgress" , 0 },
{ 520, "SWMG_SetLateralAccelerationPerSecond", 0 },
{ 521, "SWMG_GetLateralAccelerationPerSecond", 0 },
{ 522, "GetCurrentAction" , 0 },
{ 522, "GetCurrentAction" , &Functions::getCurrentAction },
{ 523, "GetDifficultyModifier" , 0 },
{ 524, "GetAppearanceType" , 0 },
{ 525, "FloatingTextStrRefOnCreature" , 0 },
Expand Down Expand Up @@ -684,7 +684,7 @@ const Functions::FunctionPointer Functions::kFunctionPointers[] = {
{ 562, "GetFactionLeader" , 0 },
{ 563, "SWMG_SetSpeedBlurEffect" , 0 },
{ 564, "EndGame" , 0 },
{ 565, "GetRunScriptVar" , 0 },
{ 565, "GetRunScriptVar" , &Functions::getRunScriptVar },
{ 566, "GetCreatureMovmentType" , 0 },
{ 567, "AmbientSoundSetDayVolume" , 0 },
{ 568, "AmbientSoundSetNightVolume" , 0 },
Expand Down Expand Up @@ -820,7 +820,7 @@ const Functions::FunctionPointer Functions::kFunctionPointers[] = {
{ 698, "SpawnAvailableNPC" , 0 },
{ 699, "IsNPCPartyMember" , 0 },
{ 700, "ActionBarkString" , 0 },
{ 701, "GetIsConversationActive" , 0 },
{ 701, "GetIsConversationActive" , &Functions::getIsConversationActive },
{ 702, "EffectLightsaberThrow" , 0 },
{ 703, "EffectWhirlWind" , 0 },
{ 704, "GetPartyAIStyle" , 0 },
Expand Down Expand Up @@ -849,7 +849,7 @@ const Functions::FunctionPointer Functions::kFunctionPointers[] = {
{ 727, "GetFirstAttacker" , 0 },
{ 728, "GetNextAttacker" , 0 },
{ 729, "SetFormation" , 0 },
{ 730, "ActionFollowLeader" , 0 },
{ 730, "ActionFollowLeader" , &Functions::actionFollowLeader },
{ 731, "SetForcePowerUnsuccessful" , 0 },
{ 732, "GetIsDebilitated" , 0 },
{ 733, "PlayMovie" , &Functions::playMovie },
Expand Down
4 changes: 1 addition & 3 deletions src/engines/kotorbase/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ namespace KotORBase {
Action::Action() : type(kActionInvalid), object(0), range(0.0f) {
}

Action::Action(ActionType _type, Object *_object) :
type(_type),
object(_object) {
Action::Action(ActionType _type) : type(_type), object(0), range(0.0f) {
}

} // End of namespace KotORBase
Expand Down
2 changes: 1 addition & 1 deletion src/engines/kotorbase/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct Action {
float range;

Action();
Action(ActionType type, Object *object);
Action(ActionType type);
};

} // End of namespace KotORBase
Expand Down
2 changes: 1 addition & 1 deletion src/engines/kotorbase/actionexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace Engines {
namespace KotORBase {

void ActionExecutor::executeActions(Creature &creature, Area &area, float dt) {
const Action *action = creature.peekAction();
const Action *action = creature.getCurrentAction();
if (!action)
return;

Expand Down
30 changes: 30 additions & 0 deletions src/engines/kotorbase/area.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "src/engines/kotorbase/area.h"
#include "src/engines/kotorbase/module.h"
#include "src/engines/kotorbase/actionexecutor.h"
#include "src/engines/kotorbase/creaturesearch.h"

#include "src/engines/kotorbase/path/pathfinding.h"
#include "src/engines/kotorbase/path/objectwalkmesh.h"
Expand Down Expand Up @@ -734,6 +735,35 @@ Object *Area::getObjectByTag(const Common::UString &tag) {
return 0;
}

Creature *Area::getNearestCreature(const Object *target, int UNUSED(nth), const CreatureSearchCriteria &criteria) const {
// TODO: Find Nth nearest using all criterias

Creature *result = 0;
float lowestDistance = 0.0f;

float x, y, z;
target->getPosition(x, y, z);
glm::vec3 targetPosition(x, y, z);

for (auto c = _creatures.begin(); c != _creatures.end(); ++c) {
if (*c == target)
continue;

if (!(*c)->matchSearchCriteria(target, criteria))
continue;

(*c)->getPosition(x, y, z);
float dist = glm::distance(targetPosition, glm::vec3(x, y, z));

if ((result == 0) || (dist < lowestDistance)) {
result = *c;
lowestDistance = dist;
}
}

return result;
}

void Area::processCreaturesActions(float dt) {
for (std::vector<Creature *>::iterator c = _creatures.begin();
c != _creatures.end(); ++c) {
Expand Down
2 changes: 2 additions & 0 deletions src/engines/kotorbase/area.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Room;
class Situated;
class Creature;
class Pathfinding;
struct CreatureSearchCriteria;

/** An area in Star Wars: Knights of the Old Republic, holding all objects and
* rooms within, as well as general area properties like the current background
Expand Down Expand Up @@ -136,6 +137,7 @@ class Area : public Object, public Events::Notifyable {
// Object management

Object *getObjectByTag(const Common::UString &tag);
Creature *getNearestCreature(const Object *target, int nth, const CreatureSearchCriteria &criteria) const;

void addCreature(Creature *creature);
void addToObjectMap(Object *object);
Expand Down
42 changes: 28 additions & 14 deletions src/engines/kotorbase/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

#include "src/engines/kotorbase/creature.h"
#include "src/engines/kotorbase/item.h"
#include "src/engines/kotorbase/creaturesearch.h"

#include "src/engines/kotorbase/gui/chargeninfo.h"

Expand All @@ -51,6 +52,7 @@ namespace KotORBase {

Creature::Creature(const Common::UString &resRef) :
Object(kObjectTypeCreature),
_commandable(true),
_walkRate(0.0f),
_runRate(0.0f) {

Expand All @@ -65,6 +67,7 @@ Creature::Creature(const Common::UString &resRef) :

Creature::Creature(const Aurora::GFF3Struct &creature) :
Object(kObjectTypeCreature),
_commandable(true),
_walkRate(0.0f),
_runRate(0.0f) {

Expand All @@ -74,6 +77,7 @@ Creature::Creature(const Aurora::GFF3Struct &creature) :

Creature::Creature() :
Object(kObjectTypeCreature),
_commandable(true),
_walkRate(0.0f),
_runRate(0.0f) {

Expand Down Expand Up @@ -130,12 +134,25 @@ bool Creature::isPartyMember() const {
return _isPC;
}

bool Creature::matchSearchCriteria(const Object *UNUSED(target), const CreatureSearchCriteria &UNUSED(criteria)) const {
// TODO: Implement pattern matching
return false;
}

bool Creature::isCommandable() const {
return _commandable;
}

void Creature::setUsable(bool usable) {
Object::setUsable(usable);
if (_model)
_model->setClickable(isClickable());
}

void Creature::setCommandable(bool commandable) {
_commandable = commandable;
}

Gender Creature::getGender() const {
return _gender;
}
Expand Down Expand Up @@ -680,26 +697,23 @@ void Creature::playHeadAnimation(const Common::UString &anim, bool restart, floa
headChannel->playAnimation(anim, restart, length, speed);
}

void Creature::clearActionQueue() {
_actionQueue.clear();
}
const Action *Creature::getCurrentAction() const {
if (_actions.empty())
return 0;

void Creature::enqueueAction(const Action &action) {
_actionQueue.push_back(action);
return &_actions.front();
}

const Action *Creature::peekAction() const {
if (_actionQueue.empty())
return 0;
void Creature::clearAllActions() {
_actions = std::queue<Action>();
}

return &_actionQueue.front();
void Creature::enqueueAction(const Action &action) {
_actions.push(action);
}

const Action *Creature::dequeueAction() {
Action *action = _actionQueue.empty() ? 0 : &_actionQueue.front();
if (action)
_actionQueue.erase(_actionQueue.begin());
return action;
void Creature::dequeueAction() {
_actions.pop();
}

void Creature::setDefaultAnimations() {
Expand Down

0 comments on commit 569ab77

Please sign in to comment.