Skip to content

Commit

Permalink
Fixes #231
Browse files Browse the repository at this point in the history
  • Loading branch information
DudeMcDude committed Apr 9, 2016
1 parent 13484ae commit fbe06c9
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 3 deletions.
5 changes: 5 additions & 0 deletions TemplePlus/action_sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2333,6 +2333,11 @@ void ActionSequenceSystem::sequencePerform()
ActnSeq * curSeq = *actSeqCur;
if (combat->isCombatActive() || !actSeqSpellHarmful(curSeq) || !combatTriggerSthg(curSeq) ) // POSSIBLE BUG: I think this can cause spells to be overridden (e.g. when the temple priests prebuff simulataneously with you, and you get the spell effect instead) TODO
{
if (curSeq != *actSeqCur)
{
logger->debug("SequencePerform: Switched sequence slot from combat trigger!");
curSeq = *actSeqCur;
}
logger->debug("SequencePerform: \t {} performing sequence ({})...", description.getDisplayName(curSeq->performer), (void*)curSeq);
if (isSimultPerformer(curSeq->performer))
{
Expand Down
4 changes: 4 additions & 0 deletions TemplePlus/ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,10 @@ int AiSystem::StrategyTabLineParser(TabFileStatus* tabFile, int n, char** string

int AiSystem::AiOnInitiativeAdd(objHndl obj)
{
if (party.IsInParty(obj) && objects.IsPlayerControlled(obj)){
return 0;
}

int critterStratIdx = objects.getInt32(obj, obj_f_critter_strategy);

assert(critterStratIdx >= 0 && critterStratIdx < *aiStrategiesNum);
Expand Down
13 changes: 13 additions & 0 deletions TemplePlus/dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,10 @@ bool Dispatcher::IsValid()
return dispatch.dispatcherValid(this);
}

void Dispatcher::Process(enum_disp_type dispType, D20DispatcherKey key, DispIO* dispIo){
dispatch.DispatcherProcessor(this, dispType, key, dispIo);
}

Dispatcher* _DispatcherInit(objHndl objHnd) {
Dispatcher* dispatcherNew = (Dispatcher *)malloc(sizeof(Dispatcher));
memset(&dispatcherNew->subDispNodes, 0, dispTypeCount * sizeof(SubDispNode*));
Expand Down Expand Up @@ -1045,3 +1049,12 @@ DispIoAttackBonus::DispIoAttackBonus()
this->attackPacket.dispKey = 1;
this->attackPacket.flags = (D20CAF) 0;
}

DispIoBonusAndObj::DispIoBonusAndObj()
{
dispIOType = dispIOTypeSkillLevel;
obj = 0i64;
pad = 0;
returnVal = 0;
bonOut = &bonlist;
}
2 changes: 2 additions & 0 deletions TemplePlus/dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ struct DispIoBonusAndObj : DispIO // type 10
uint32_t pad;
objHndl obj; //optional
BonusList bonlist;
DispIoBonusAndObj();
};
const int TestSizeOfDispIO390h = sizeof(DispIoBonusAndObj); // should be 912 (0x390)

Expand Down Expand Up @@ -483,6 +484,7 @@ struct Dispatcher : temple::TempleAlloc {
CondNode* conditions;
SubDispNode* subDispNodes[dispTypeCount];
bool IsValid();
void Process(enum_disp_type dispTypeInitiativeMod, D20DispatcherKey key, DispIO* dispIo);
};

#pragma endregion
Expand Down
59 changes: 56 additions & 3 deletions TemplePlus/turn_based.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include "obj.h"
#include "temple_functions.h"
#include "d20_obj_registry.h"
#include "gamesystems/gamesystems.h"
#include "gamesystems/objects/objsystem.h"
#include "ai.h"
#include "condition.h"

class TurnBasedReplacements : public TempleFix
{
Expand All @@ -32,7 +36,9 @@ class TurnBasedReplacements : public TempleFix
replaceFunction(0x100DEE40, _turnBasedGetCurrentActor);
replaceFunction(0x100DF310, InitiativeNextActor);
orgPortraitDragChangeInitiative = replaceFunction(0x100DF5A0, PortraitDragChangeInitiative);

replaceFunction<void(__cdecl)(objHndl)>(0x100DF1E0, [](objHndl handle){
tbSys.AddToInitiative(handle);
});
}
} tbReplacements;

Expand Down Expand Up @@ -136,10 +142,57 @@ int TurnBasedSys::GetInitiativeListIdx() const
return party.ObjFindInGroupArray(groupInitiativeList, *turnBasedCurrentActor );
}

void TurnBasedSys::AddToInitiativeGroup(objHndl handle) const
{
party.ObjAddToGroupArray(groupInitiativeList, handle);
}

void TurnBasedSys::ArbitrateInitiativeConflicts() const
{
auto arbConf = temple::GetRef<void(__cdecl)()>(0x100DEFA0);
arbConf();
}

void TurnBasedSys::AddToInitiative(objHndl handle) const
{
auto addToInit = temple::GetRef<void(__cdecl)(objHndl)>(0x100DF1E0);
addToInit(handle);
if (IsInInitiativeList(handle))
return;

auto obj = gameSystems->GetObj().GetObject(handle);
if (critterSys.IsDeadNullDestroyed(handle))
return;
if (critterSys.IsDeadOrUnconscious(handle))
return;

if (!d20Sys.d20Query(handle, DK_QUE_EnterCombat))
return;


aiSys.AiOnInitiativeAdd(handle);

auto initiativeRoll = templeFuncs.RNG(1, 20);
int initiativeMod = 0;
auto dispatcher = obj->GetDispatcher();
if (dispatcher->IsValid()){
DispIoBonusAndObj dispIo;
dispIo.bonOut = &dispIo.bonlist;
dispatcher->Process(dispTypeInitiativeMod, DK_NONE, &dispIo);
initiativeMod = dispIo.bonlist.GetEffectiveBonusSum();
}

obj->SetInt32(obj_f_initiative, initiativeMod + initiativeRoll);
AddToInitiativeGroup(handle);
auto dexScore = objects.StatLevelGet(handle, stat_dexterity);
obj->SetInt32(obj_f_subinitiative, 100 * dexScore);
ArbitrateInitiativeConflicts();
conds.AddTo(handle, "Flatfooted", {});
auto isSurpriseRound = temple::GetRef<BOOL>(0x10BCAD90);
if (isSurpriseRound){
conds.AddTo(handle, "Surprised", {});
}

/*auto addToInit = temple::GetRef<void(__cdecl)(objHndl)>(0x100DF1E0);
addToInit(handle);*/
}

bool TurnBasedSys::IsInInitiativeList(objHndl handle) const // 0x100DEDD0
Expand Down
4 changes: 4 additions & 0 deletions TemplePlus/turn_based.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ struct TurnBasedSys : temple::AddressTable
int InitiativeRefresh(int initiative, int initiativeNext);
void InitiativeListNextActor(); // move initiative to next actor
int GetInitiativeListIdx() const;

void AddToInitiative(objHndl handle) const;
void AddToInitiativeGroup(objHndl handle) const;
void ArbitrateInitiativeConflicts() const;

bool IsInInitiativeList(objHndl handle) const;
};

Expand Down

0 comments on commit fbe06c9

Please sign in to comment.