Skip to content

Commit

Permalink
add ai turn event as insertion point for ai stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
FynnTW committed Nov 25, 2023
1 parent 946bc05 commit 29bd9de
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 4 deletions.
57 changes: 57 additions & 0 deletions M2TWEOP Code/M2TWEOP library/Injects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3159,6 +3159,63 @@ void OnCreateMercUnitCheck::SetNewCode()



a->ret();
m_cheatBytes = (unsigned char*)a->make();

delete a;
}


onAiTurn::onAiTurn(MemWork* mem, LPVOID addr, int ver)
:AATemplate(mem), funcAddress(addr)
{
if (ver == 2)//steam
{
m_adress = 0x0052B074;
otherFunc = 0x005D2F00;
}
else if (ver == 1)//kingdoms
{
m_adress = 0x0052AA74;
otherFunc = 0x005D2A70;
}
}

onAiTurn::~onAiTurn()
{
}

void onAiTurn::SetOriginalCode()
{
Assembler* a = new Assembler();

a->cmp(eax, ecx);

a->ret();
m_originalBytes = (unsigned char*)a->make();
m_originalSize = m_memory->GetASMSize(m_originalBytes);

delete a;
}

void onAiTurn::SetNewCode()
{
Assembler* a = new Assembler();


a->pushad();
a->pushf();

a->mov(ecx, esi);
a->mov(eax, (DWORD)funcAddress);
a->call(eax);
a->popf();
a->popad();
a->mov(ecx, dword_ptr(esi, 0x24));
a->mov(eax, (DWORD)otherFunc);
a->call(eax);


a->ret();
m_cheatBytes = (unsigned char*)a->make();

Expand Down
14 changes: 14 additions & 0 deletions M2TWEOP Code/M2TWEOP library/Injects.h
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,20 @@ class onGameInitialized
LPVOID funcAddress;
};

class onAiTurn
:public AATemplate
{
public:
onAiTurn(MemWork* mem, LPVOID adr, int ver);
~onAiTurn();

void SetOriginalCode();
void SetNewCode();
private:
LPVOID funcAddress;
DWORD otherFunc;
};

class onGameConsoleCommandFromConsole
:public AATemplate
{
Expand Down
6 changes: 6 additions & 0 deletions M2TWEOP Code/M2TWEOP library/managerF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,12 @@ void managerF::doPachs()
toCreateMercUnit->Enable();
f1 << "Done" << endl;

f1 << "Start applying toCreateMercUnit patch" << endl;
onAiTurn* toAiTurn = new onAiTurn(mem, (LPVOID)patchesForGame::onAiTurn, globals::dataS.gamever);
toAiTurn->SetNewCode();
toAiTurn->Enable();
f1 << "Done" << endl;

f1 << "Start applying OnCreateUnitWrapper patch" << endl;
OnCreateUnitWrapper* toCreateUnitWrapper = new OnCreateUnitWrapper(mem, (LPVOID)patchesForGame::OnCreateUnitWrapper, globals::dataS.gamever);
toCreateUnitWrapper->SetNewCode();
Expand Down
5 changes: 5 additions & 0 deletions M2TWEOP Code/M2TWEOP library/patchesForGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,11 @@ void __stdcall patchesForGame::onGameInit()
plugins::onGameInit();
}

void __fastcall patchesForGame::onAiTurn(aiFaction* aifaction)
{
plugins::onAiTurn(aifaction);
}

void __stdcall patchesForGame::onChangeTurnNum()
{
#if defined TESTPATCHES
Expand Down
1 change: 1 addition & 0 deletions M2TWEOP Code/M2TWEOP library/patchesForGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class patchesForGame
//after reading EDU
static void WINAPI afterEDUread();
static void WINAPI onGameInit();
static void __fastcall onAiTurn(aiFaction* aifaction);


//before start of a first faction turn
Expand Down
15 changes: 13 additions & 2 deletions M2TWEOP Code/M2TWEOP library/plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ void plugins::onGameInit()
}
}

void plugins::onAiTurn(aiFaction* aifaction)
{
for (plugin* pl : pluginsCfg.plugins)
{
(*(*pl->onAiTurn))(aifaction);
}
}

void plugins::onWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
for (plugin* pl : pluginsCfg.plugins)
Expand Down Expand Up @@ -381,11 +389,14 @@ int plugin::init(string* nameP)
fName = "onfortificationlevelS";
onfortificationlevelS.Load(&plPath, &fName);


//onReadGameDbsAtStart
//onGameInit
fName = "onGameInit";
onGameInit.Load(&plPath, &fName);

//onAiTurn
fName = "onAiTurn";
onAiTurn.Load(&plPath, &fName);

//onReadGameDbsAtStart
fName = "onReadGameDbsAtStart";
onReadGameDbsAtStart.Load(&plPath, &fName);
Expand Down
2 changes: 2 additions & 0 deletions M2TWEOP Code/M2TWEOP library/plugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class plugin
ProcLoader<LRESULT(__cdecl*)(HWND, UINT, WPARAM, LPARAM)> onWndProc;
ProcLoader<void(__cdecl*)()> onReadGameDbsAtStart;
ProcLoader<void(__cdecl*)()> onGameInit;
ProcLoader<void(__cdecl*)(aiFaction*)> onAiTurn;

ProcLoader<void(__cdecl*)(int, int)> onEndSiege;
ProcLoader<void(__cdecl*)(int, int)> onStartSiege;
Expand Down Expand Up @@ -138,6 +139,7 @@ class plugins

static void onReadGameDbsAtStart();
static void onGameInit();
static void onAiTurn(aiFaction* aifaction);

static void onWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static void onEndScene(LPDIRECT3DDEVICE9 pDevice);
Expand Down
2 changes: 1 addition & 1 deletion M2TWEOP Code/M2TWEOP library/stratModelsChange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ namespace stratModelsChange
funcAddr = codes::offsets.assignShadowCasToFlexi;
texturepath = nullptr;
}
float scale = 0.7;
float scale = 0.7f;
_asm
{
push 1
Expand Down
8 changes: 8 additions & 0 deletions M2TWEOP-luaPlugin/luaPlugin/basicEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,14 @@ void onGameInit()
}
}

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

void onEndSiege(int x, int y)
{
if (plugData::data.luaAll.onEndSiege != nullptr)
Expand Down
1 change: 1 addition & 0 deletions M2TWEOP-luaPlugin/luaPlugin/basicEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using namespace std;

extern "C" PLUGINM2TWEOP_API void onReadGameDbsAtStart();
extern "C" PLUGINM2TWEOP_API void onGameInit();
extern "C" PLUGINM2TWEOP_API void onAiTurn(aiFaction* aifaction);
extern "C" PLUGINM2TWEOP_API void onEndSiege(int x, int y);
extern "C" PLUGINM2TWEOP_API void onStartSiege(int x, int y);
std::unordered_map<int, const char*> religionNames = {
Expand Down
16 changes: 16 additions & 0 deletions M2TWEOP-luaPlugin/luaPlugin/luaEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ void luaP::onPluginLoadF()
@tfield onNewGameStart onNewGameStart
@tfield onReadGameDbsAtStart onReadGameDbsAtStart
@tfield onGameInit onGameInit
@tfield onAiTurn onAiTurn
@tfield onClickAtTile onClickAtTile
@tfield onCampaignMapLoaded onCampaignMapLoaded
@tfield onCreateSaveFile onCreateSaveFile
Expand Down Expand Up @@ -2997,6 +2998,21 @@ void luaP::onPluginLoadF()
onGameInit = new sol::function(luaState["onGameInit"]);
checkLuaFunc(&onGameInit);

/***
Called on ai initialized on turn start.
@function onAiTurn
@tparam aiFaction aiFaction
@usage
function onAiTurn(aiFaction)
--something here
end
*/

onAiTurn = new sol::function(luaState["onAiTurn"]);
checkLuaFunc(&onAiTurn);

/***
Called on clicking the stratmap.
Expand Down
3 changes: 2 additions & 1 deletion M2TWEOP-luaPlugin/luaPlugin/luaP.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class luaP

sol::function* onReadGameDbsAtStart = nullptr;
sol::function* onGameInit = nullptr;
sol::function* onAiTurn = nullptr;
sol::function* onEndSiege = nullptr;
sol::function* onStartSiege = nullptr;
sol::function* onSelectWorldpkgdesc = nullptr;
Expand All @@ -167,7 +168,7 @@ class luaP
sol::function* onLoadingFonts = nullptr;
sol::function* initDXFunc = nullptr;
void checkLuaFunc(sol::function** lRef);



private:
Expand Down

0 comments on commit 29bd9de

Please sign in to comment.