Skip to content

Commit

Permalink
Fixes #216
Browse files Browse the repository at this point in the history
  • Loading branch information
DudeMcDude committed Apr 4, 2016
1 parent ec6022e commit f9d9955
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 12 deletions.
58 changes: 56 additions & 2 deletions TemplePlus/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "gamesystems/gamesystems.h"
#include "maps.h"
#include "tutorial.h"
#include "objlist.h"


struct CombatSystemAddresses : temple::AddressTable
Expand Down Expand Up @@ -128,6 +129,11 @@ class CombatSystemReplacements : public TempleFix
{
return combatSys.IsBrawlInProgress();
});

// AddToInitiativeWithinRect
replaceFunction<void(__cdecl)(objHndl)>(0x10062AC0, [](objHndl handle){
combatSys.AddToInitiativeWithinRect(handle);
});

}
} combatSysReplacements;
Expand Down Expand Up @@ -490,6 +496,54 @@ bool LegacyCombatSystem::HasLineOfAttack(objHndl obj, objHndl target)
return 0;
}

void LegacyCombatSystem::AddToInitiativeWithinRect(objHndl handle) const
{

auto obj = gameSystems->GetObj().GetObject(handle);
auto loc = obj->GetLocation();
ObjList objlist;
TileRect trect;
trect.x1 = loc.locx - 18;
trect.x2 = loc.locx + 18;
trect.y1 = loc.locy - 18;
trect.y2 = loc.locy + 18;

auto& unk = temple::GetRef<int>(0x102BE130);
unk = 18; // no idea what this is, looks like part of a 3x6 matrix

objlist.ListRect(trect, OLC_CRITTERS );

for (int i = 0; i < objlist.size() ; i++){
auto resHandle = objlist[i];
if (!resHandle)
break;

// check if the object is ok to act (not dead, OF_OFF, OF_DONTDRAW (to prevent the naughty Co8 critters from getting into combat), destroyed , unconscious)
auto resObj = gameSystems->GetObj().GetObject(resHandle);
if (resObj->GetFlags() & ( OF_OFF | OF_DESTROYED | OF_DONTDRAW ))
continue;

if (critterSys.IsDeadOrUnconscious(resHandle))
{
continue;
}


if (!tbSys.IsInInitiativeList(resHandle)){
auto critFlags = critterSys.GetCritterFlags(resHandle);
if (!(critFlags & OCF_COMBAT_MODE_ACTIVE) && !party.IsInParty(resHandle)) {
aiSys.AiProcess(resHandle); // todo: originally there was a dangling IsPerforming check in the function, should it be added to the conditions??
}
}

auto critFlags = critterSys.GetCritterFlags(resHandle);
if (critFlags & OCF_COMBAT_MODE_ACTIVE){
tbSys.AddToInitiative(resHandle);
}
}

}

void LegacyCombatSystem::TurnProcessAi(objHndl obj)
{
//return addresses.TurnProcessing_100635E0(obj);
Expand Down Expand Up @@ -570,8 +624,8 @@ void LegacyCombatSystem::EndTurn()
tbSys.InitiativeListNextActor();

if (party.IsInParty(actor) && !actSeqSys.isPerforming(actor)){
static auto addToInitiativeWithinRect = temple::GetRef<void(__cdecl)(objHndl)>(0x10062AC0);
addToInitiativeWithinRect(actor);
//static auto addToInitiativeWithinRect = temple::GetRef<void(__cdecl)(objHndl)>(0x10062AC0);
AddToInitiativeWithinRect(actor);
}

// remove dead and OF_OFF from initiative
Expand Down
3 changes: 2 additions & 1 deletion TemplePlus/combat.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ struct LegacyCombatSystem : temple::AddressTable {
objHndl * GetHostileCombatantList(objHndl obj, int* count);
bool HasLineOfAttack(objHndl obj, objHndl target); // can shoot or attack target (i.e. target isn't behind a wall or sthg)



void AddToInitiativeWithinRect(objHndl objHndl) const; // adds critters to combat initiative around obj
void EndTurn();
void CombatSubturnEnd();
void Subturn();
Expand Down
13 changes: 12 additions & 1 deletion TemplePlus/objlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@

static struct ObjListAddresses : temple::AddressTable {
void(__cdecl *ObjListTile)(locXY loc, int flags, ObjListResult &result);
void(__cdecl *ObjListRect)(TileRect &trect, ObjectListFilter olcCritters, ObjListResult& result);
void(__cdecl *ObjListVicinity)(locXY loc, int flags, ObjListResult &result);
void(__cdecl *ObjListRadius)(LocAndOffsets loc, float radius, float unk1, float unk2, int flags, ObjListResult &result);
void(__cdecl *ObjListFollowers)(objHndl critter, ObjListResult &result);
void(__cdecl *ObjListFree)(ObjListResult &result);

ObjListAddresses() {
rebase(ObjListTile, 0x1001E970);
rebase(ObjListRect, 0x1001ECF0);
rebase(ObjListVicinity, 0x1001F1C0);
rebase(ObjListRadius, 0x10022E50);
rebase(ObjListFollowers, 0x1001F450);
rebase(ObjListFree, 0x1001F2C0);
}


} addresses;

ObjList::ObjList() {
Expand All @@ -34,6 +38,13 @@ void ObjList::ListTile(locXY loc, int flags) {
mHasToFree = true;
}

void ObjList::ListRect(TileRect& trect, ObjectListFilter olcCritters)
{
FreeResult();
addresses.ObjListRect(trect, olcCritters, mResult);
mHasToFree = true;
}

void ObjList::ListVicinity(locXY loc, int flags) {
FreeResult();
addresses.ObjListVicinity(loc, flags, mResult);
Expand Down
9 changes: 8 additions & 1 deletion TemplePlus/objlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ class ObjList {
*/
void ListTile(locXY loc, int flags);


/*
search within worldspace rect
*/
void ListRect(TileRect &trect, ObjectListFilter olcCritters);

/*
I believe this searches for all objects that would be visible if the screen was
centered on the given tile.
Expand Down Expand Up @@ -124,7 +130,7 @@ class ObjList {
int size();
objHndl get(int idx) {
auto item = mResult.objects;
for (int i = 1; i <= idx ; i++) { // tsk, tsk, tsk Pugmeister :D
for (int i = 1; i <= idx ; i++) {
item = item->next;
}
return item->handle;
Expand All @@ -133,6 +139,7 @@ class ObjList {
return get(idx);
}


private:
ObjListResult mResult;
bool mHasToFree = false;
Expand Down
23 changes: 17 additions & 6 deletions TemplePlus/turn_based.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,20 @@ int TurnBasedSys::InitiativeRefresh(int initiative, int initiativeNext)

void TurnBasedSys::InitiativeListNextActor()
{
logger->debug("InitiativeListNextActor: Actor {} ({}) finishing.", description.getDisplayName(*turnBasedCurrentActor), *turnBasedCurrentActor);
auto actorInitiative = objects.getInt32(*turnBasedCurrentActor, obj_f_initiative);
auto actorInitiativeIdx = party.ObjFindInGroupArray(groupInitiativeList, *turnBasedCurrentActor);
auto nextInitiativeIdx = actorInitiativeIdx + 1;
int initiativeListLen = GetInitiativeListLength();

if (nextInitiativeIdx >= initiativeListLen)
if (nextInitiativeIdx >= initiativeListLen) // time for next round
{
temple::GetRef<int>(0x10BCAD90) = 0; // surprise round
for (int i = 0; i < initiativeListLen; i++)
for (int i = 0; i < GetInitiativeListLength(); i++) // refreshing the init list length in case it changes
{
auto combatant = groupInitiativeList->GroupMembers[i];
auto dispatcher = objects.GetDispatcher(combatant);
if (dispatch.dispatcherValid(dispatcher))
{
if (dispatch.dispatcherValid(dispatcher)){
dispatch.DispatcherProcessor(dispatcher, dispTypeInitiative, 0, 0);
}
}
Expand All @@ -118,7 +118,7 @@ void TurnBasedSys::InitiativeListNextActor()
*turnBasedCurrentActor = actorNext;
if (actorNext)
{
logger->debug("Turn Based actor changed to {} ({})", description.getDisplayName(actorNext), actorNext);
logger->debug("InitiativeListNextActor: Turn Based actor changed to {} ({})", description.getDisplayName(actorNext), actorNext);
auto nextActorInitiative = objects.getInt32(actorNext, obj_f_initiative);
if (actorInitiative != nextActorInitiative)
{
Expand All @@ -131,11 +131,22 @@ void TurnBasedSys::InitiativeListNextActor()

}

int TurnBasedSys::GetInitiativeListIdx()
int TurnBasedSys::GetInitiativeListIdx() const
{
return party.ObjFindInGroupArray(groupInitiativeList, *turnBasedCurrentActor );
}

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

bool TurnBasedSys::IsInInitiativeList(objHndl handle) const // 0x100DEDD0
{
return party.ObjIsInGroupArray(groupInitiativeList, handle);
}

void _turnBasedSetCurrentActor(objHndl objHnd)
{
if (objHnd)
Expand Down
4 changes: 3 additions & 1 deletion TemplePlus/turn_based.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ struct TurnBasedSys : temple::AddressTable
unsigned GetInitiativeListLength();
int InitiativeRefresh(int initiative, int initiativeNext);
void InitiativeListNextActor(); // move initiative to next actor
int GetInitiativeListIdx();
int GetInitiativeListIdx() const;
void AddToInitiative(objHndl handle) const;
bool IsInInitiativeList(objHndl handle) const;
};

extern TurnBasedSys tbSys;
Expand Down

0 comments on commit f9d9955

Please sign in to comment.