Skip to content

Commit

Permalink
add tick events
Browse files Browse the repository at this point in the history
  • Loading branch information
FynnTW committed Nov 5, 2024
1 parent afd3475 commit 2f6e9ae
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 31 deletions.
53 changes: 53 additions & 0 deletions M2TWEOP Code/M2TWEOP library/Injects.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "pch.h"
#include "Injects.h"

#include "dataOffsets.h"


Injects::Injects(MemWork* mem)
:AATemplate(mem)
Expand Down Expand Up @@ -5782,6 +5784,57 @@ void onEnemyClicked::SetNewCode()
delete a;
}

onCampaignTick::onCampaignTick(MemWork* mem, LPVOID addr, int ver)
:AATemplate(mem), funcAddress(addr)
{
if (ver == 2)//steam
m_adress = 0x47D5B6;

else if (ver == 1)//kingdoms
m_adress = 0x47D1D6;
}

void onCampaignTick::SetNewCode()
{
const auto a = new Assembler();
a->push(edx);
a->push(eax);
a->mov(eax, reinterpret_cast<DWORD>(funcAddress));
a->call(eax);
a->mov(ecx, eax);
a->pop(eax);
a->pop(edx);
a->ret();
m_cheatBytes = static_cast<unsigned char*>(a->make());
delete a;
}

onBattleTick::onBattleTick(MemWork* mem, LPVOID addr, int ver)
:AATemplate(mem), funcAddress(addr)
{
if (ver == 2)//steam
m_adress = 0x60FE6F;

else if (ver == 1)//kingdoms
m_adress = 0x60FABF;
}

void onBattleTick::SetNewCode()
{
const auto a = new Assembler();
a->push(ecx);
a->push(edx);
a->mov(eax, reinterpret_cast<DWORD>(funcAddress));
a->call(eax);
a->pop(edx);
a->pop(ecx);
a->mov(eax, dword_ptr(esi, 0x4));
a->cmp(eax, 5);
a->ret();
m_cheatBytes = static_cast<unsigned char*>(a->make());
delete a;
}

onCreateMarriageOption2::onCreateMarriageOption2(MemWork* mem, LPVOID addr, int ver)
:AATemplate(mem), funcAddress(addr)
{
Expand Down
24 changes: 24 additions & 0 deletions M2TWEOP Code/M2TWEOP library/Injects.h
Original file line number Diff line number Diff line change
Expand Up @@ -2117,6 +2117,30 @@ class onEnemyClicked
LPVOID funcAddress;
};

class onCampaignTick
:public AATemplate
{
public:
onCampaignTick(MemWork* mem, LPVOID addr, int ver);
~onCampaignTick() = default;

void SetNewCode();
private:
LPVOID funcAddress;
};

class onBattleTick
:public AATemplate
{
public:
onBattleTick(MemWork* mem, LPVOID addr, int ver);
~onBattleTick() = default;

void SetNewCode();
private:
LPVOID funcAddress;
};

class onCreateMarriageOption2
:public AATemplate
{
Expand Down
2 changes: 1 addition & 1 deletion M2TWEOP Code/M2TWEOP library/luaPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ void luaPlugin::fillHashMaps()
factions.insert_or_assign(std::string(faction->factionRecord->facName), faction->factionID);
}
const auto stratMap = stratMapHelpers::getStratMap();
if (!stratMap)
if (!stratMap || !stratMap->isOpen)
return;
for (int i = 0; i < stratMap->regionsNum; i++)
{
Expand Down
2 changes: 2 additions & 0 deletions M2TWEOP Code/M2TWEOP library/luaPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ class luaPlugin
sol::function* onChangeTurnNumFunc = nullptr;
sol::function* onClickAtTile = nullptr;
sol::function* onCharacterClicked = nullptr;
sol::function* onCampaignTick = nullptr;
sol::function* onBattleTick = nullptr;
sol::function* onNewGameStart = nullptr;
sol::function* onPluginLoad = nullptr;
sol::function* onCreateSaveFile = nullptr;
Expand Down
12 changes: 12 additions & 0 deletions M2TWEOP Code/M2TWEOP library/managerF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,18 @@ void managerF::execPatches()
toCreateMarriageOption2->Enable();
f1 << "Done" << '\n';

f1 << "Start applying onCampaignTick patch" << '\n';
onCampaignTick* toCampaignTick = new onCampaignTick(mem, (LPVOID)patchesForGame::onCampaignTick, globals::dataS.gameVersion);
toCampaignTick->SetNewCode();
toCampaignTick->Enable();
f1 << "Done" << '\n';

f1 << "Start applying onBattleTick patch" << '\n';
onBattleTick* toBattleTick = new onBattleTick(mem, (LPVOID)patchesForGame::onBattleTick, globals::dataS.gameVersion);
toBattleTick->SetNewCode();
toBattleTick->Enable();
f1 << "Done" << '\n';

//f1 << "Start applying onEnemyClicked patch" << '\n';
//onEnemyClicked* toEnemyClicked = new onEnemyClicked(mem, (LPVOID)patchesForGame::onEnemyClicked, globals::dataS.gameVersion);
//toEnemyClicked->SetNewCode();
Expand Down
12 changes: 12 additions & 0 deletions M2TWEOP Code/M2TWEOP library/patchesForGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,18 @@ void __fastcall patchesForGame::onCharacterClicked(character* enemy)
gameEvents::onCharacterClicked(enemy);
}

DWORD patchesForGame::onCampaignTick()
{
gameEvents::onCampaignTick();
const auto stratMap = stratMapHelpers::getStratMap();
return reinterpret_cast<DWORD>(stratMap);
}

void patchesForGame::onBattleTick()
{
gameEvents::onBattleTick();
}

void __stdcall patchesForGame::afterCampaignMapLoaded()
{
discordManager::onCampaignMapLoaded();
Expand Down
2 changes: 2 additions & 0 deletions M2TWEOP Code/M2TWEOP library/patchesForGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ class patchesForGame
static unitGroup** __fastcall onGetGroupByLabel(DWORD groupLabels, char* label);
static char* __fastcall onGetCultureEndTurnSound(int cultureID);
static void __fastcall onCharacterClicked(character* enemy);
static DWORD __fastcall onCampaignTick();
static void __fastcall onBattleTick();


static character* __fastcall mercenaryMovePointsGetGeneral(armyStruct* army);
Expand Down
6 changes: 4 additions & 2 deletions M2TWEOP Code/M2TWEOP library/types/battle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,8 @@ namespace battleHelpers
@tfield int finishTimer
@tfield float battleSpeed
@tfield float secondsPassed
@tfield int secondsSinceBattleLoaded
@tfield int tickCount
@tfield int ticksSinceBattleStart
@tfield int hidingEnabledSet
@tfield fortBattleInfo fortInfo
@tfield float mapWidth
Expand Down Expand Up @@ -424,7 +425,8 @@ namespace battleHelpers
typeAll.battleTable.set("inBattle", &battleDataS::inBattle);
typeAll.battleTable.set("battleSpeed", &battleDataS::speed);
typeAll.battleTable.set("secondsPassed", &battleDataS::secondsPassed);
typeAll.battleTable.set("secondsSinceBattleLoaded", &battleDataS::secondsSinceBattleLoaded);
typeAll.battleTable.set("tickCount", &battleDataS::tickCount);
typeAll.battleTable.set("ticksSinceBattleStart", &battleDataS::ticksSinceBattleStart);
typeAll.battleTable.set("hidingEnabledSet", &battleDataS::hidingEnabledSet);
typeAll.battleTable.set("mapWidthDoubled", &battleDataS::mapWidth);
typeAll.battleTable.set("mapHeightDoubled", &battleDataS::mapHeight);
Expand Down
4 changes: 2 additions & 2 deletions M2TWEOP Code/M2TWEOP library/types/battle.h
Original file line number Diff line number Diff line change
Expand Up @@ -1445,10 +1445,10 @@ struct battleDataS {
bool paused; //0x0054
char pad_0055[3]; //0x0055
float speed; //0x0058
int ticks;
int ticksSinceBattleStart;
int ticksMilliSec;
float secondsPassed; //0x0064
int32_t secondsSinceBattleLoaded; //0x0068
int32_t tickCount; //0x0068
int finishTimer;
int32_t autoResolveMode;
float centreX;
Expand Down
45 changes: 45 additions & 0 deletions M2TWEOP Code/M2TWEOP library/types/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,23 @@ void onCharacterClicked(character* enemy)
}
}

void onCampaignTick()
{
if (plugData::data.luaAll.onCampaignTick != nullptr)
{
tryLua((*plugData::data.luaAll.onCampaignTick)());
}
}


void onBattleTick()
{
if (plugData::data.luaAll.onBattleTick != nullptr)
{
tryLua((*plugData::data.luaAll.onBattleTick)());
}
}

std::string* onSelectWorldpkgdesc(const char* selectedRec, const char* selectedGroup)
{
std::string tmpS;
Expand Down Expand Up @@ -1347,6 +1364,8 @@ void luaPlugin::onPluginLoadF()
@tfield onAiTurn onAiTurn
@tfield onClickAtTile onClickAtTile
@tfield onCharacterClicked onCharacterClicked
@tfield onCampaignTick onCampaignTick
@tfield onBattleTick onBattleTick
@tfield onCampaignMapLoaded onCampaignMapLoaded
@tfield onCreateSaveFile onCreateSaveFile
@tfield onLoadSaveFile onLoadSaveFile
Expand Down Expand Up @@ -4217,6 +4236,32 @@ void luaPlugin::onPluginLoadF()
onCharacterClicked = new sol::function(luaState["onCharacterClicked"]);
checkLuaFunc(&onCharacterClicked);

/***
Called on campaign tick.
@function onCampaignTick
@usage
function onCampaignTick()
--something here
end
*/
onCampaignTick = new sol::function(luaState["onCampaignTick"]);
checkLuaFunc(&onCampaignTick);

/***
Called on battle tick.
@function onBattleTick
@usage
function onBattleTick()
--something here
end
*/
onBattleTick = new sol::function(luaState["onBattleTick"]);
checkLuaFunc(&onBattleTick);

/***
Called after loading the campaign map
Expand Down
2 changes: 2 additions & 0 deletions M2TWEOP Code/M2TWEOP library/types/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ namespace gameEvents

void onClickAtTile(int x, int y);
void onCharacterClicked(character* enemy);
void onCampaignTick();
void onBattleTick();
std::string * onSelectWorldpkgdesc(const char* selectedRec, const char* selectedGroup);
int onFortificationLevelS(settlementStruct * settlement, bool* isCastle, bool* isChanged);
float onCalculateUnitValue(eduEntry* entry, float value);
Expand Down
Loading

0 comments on commit 2f6e9ae

Please sign in to comment.