Skip to content

Commit

Permalink
add onCharacterClicked event, reduce usages of new
Browse files Browse the repository at this point in the history
  • Loading branch information
FynnTW committed Nov 5, 2024
1 parent b8976ff commit afd3475
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 8 deletions.
28 changes: 28 additions & 0 deletions M2TWEOP Code/M2TWEOP library/Injects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5754,6 +5754,34 @@ void onCreateMarriageOption::SetNewCode()
delete a;
}

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

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

void onEnemyClicked::SetNewCode()
{
const auto a = new Assembler();
a->add(ecx, 0x88);
a->push(ecx);
a->push(edx);
a->push(eax);
a->mov(ecx, eax);
a->mov(eax, reinterpret_cast<DWORD>(funcAddress));
a->call(eax);
a->pop(eax);
a->pop(edx);
a->pop(ecx);
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
12 changes: 12 additions & 0 deletions M2TWEOP Code/M2TWEOP library/Injects.h
Original file line number Diff line number Diff line change
Expand Up @@ -2105,6 +2105,18 @@ class onCreateMarriageOption
LPVOID funcAddress;
};

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

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

class onCreateMarriageOption2
:public AATemplate
{
Expand Down
1 change: 1 addition & 0 deletions M2TWEOP Code/M2TWEOP library/luaPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class luaPlugin
sol::function* onCalculateUnitValue = nullptr;
sol::function* onChangeTurnNumFunc = nullptr;
sol::function* onClickAtTile = nullptr;
sol::function* onCharacterClicked = nullptr;
sol::function* onNewGameStart = nullptr;
sol::function* onPluginLoad = nullptr;
sol::function* onCreateSaveFile = nullptr;
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 @@ -922,6 +922,12 @@ void managerF::execPatches()
toCreateMarriageOption2->Enable();
f1 << "Done" << '\n';

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

f1 << "Start applying onCreateBirthOption patch" << '\n';
onCreateBirthOption* toCreateBirthOption = new onCreateBirthOption(mem, (LPVOID)patchesForGame::onMarriageOption, globals::dataS.gameVersion);
toCreateBirthOption->SetNewCode();
Expand Down
10 changes: 10 additions & 0 deletions M2TWEOP Code/M2TWEOP library/patchesForGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1675,10 +1675,20 @@ character* __fastcall patchesForGame::mercenaryMovePointsGetGeneral(armyStruct*

void __fastcall patchesForGame::clickAtTile(coordPair* xy)
{
const auto tile = stratMapHelpers::getTile(xy->xCoord, xy->yCoord);
if (!tile)
return;
if (const auto character = tile->getCharacter(); character)
onCharacterClicked(character);
gameEvents::onClickAtTile(xy->xCoord, xy->yCoord);
plannedRetreatRoute::onClickAtTile(xy->xCoord, xy->yCoord);
}

void __fastcall patchesForGame::onCharacterClicked(character* enemy)
{
gameEvents::onCharacterClicked(enemy);
}

void __stdcall patchesForGame::afterCampaignMapLoaded()
{
discordManager::onCampaignMapLoaded();
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 @@ -122,6 +122,7 @@ class patchesForGame
static unit** __fastcall onGetUnitByLabel(DWORD unitLabels, char* label);
static unitGroup** __fastcall onGetGroupByLabel(DWORD groupLabels, char* label);
static char* __fastcall onGetCultureEndTurnSound(int cultureID);
static void __fastcall onCharacterClicked(character* enemy);


static character* __fastcall mercenaryMovePointsGetGeneral(armyStruct* army);
Expand Down
6 changes: 3 additions & 3 deletions M2TWEOP Code/M2TWEOP library/types/eopBuildings.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,21 @@ struct buildingLevel { /* (name, tga's, models, etc) */
std::string getLocalizedName(const int factionID);
void setLocalizedName(const int factionID, const std::string& newName)
{
const auto nameMem = new UNICODE_STRING**;
const auto nameMem = techFuncs::createGameClass<UNICODE_STRING**>();
buildingName[factionID] = nameMem;
gameStringHelpers::createUniString(*buildingName[factionID], newName.c_str());
}
std::string getLocalizedDescr(const int factionID);
void setLocalizedDescr(const int factionID, const std::string& newName)
{
const auto nameMem = new UNICODE_STRING**;
const auto nameMem = techFuncs::createGameClass<UNICODE_STRING**>();
buildingDescr[factionID] = nameMem;
gameStringHelpers::createUniString(*buildingDescr[factionID], newName.c_str());
}
std::string getLocalizedDescrShort(const int factionID);
void setLocalizedDescrShort(const int factionID, const std::string& newName)
{
const auto nameMem = new UNICODE_STRING**;
const auto nameMem = techFuncs::createGameClass<UNICODE_STRING**>();
buildingDescrShort[factionID] = nameMem;
gameStringHelpers::createUniString(*buildingDescrShort[factionID], newName.c_str());
}
Expand Down
23 changes: 23 additions & 0 deletions M2TWEOP Code/M2TWEOP library/types/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,14 @@ void onClickAtTile(int x, int y)
}
}

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

std::string* onSelectWorldpkgdesc(const char* selectedRec, const char* selectedGroup)
{
std::string tmpS;
Expand Down Expand Up @@ -1338,6 +1346,7 @@ void luaPlugin::onPluginLoadF()
@tfield onUnloadCampaign onUnloadCampaign
@tfield onAiTurn onAiTurn
@tfield onClickAtTile onClickAtTile
@tfield onCharacterClicked onCharacterClicked
@tfield onCampaignMapLoaded onCampaignMapLoaded
@tfield onCreateSaveFile onCreateSaveFile
@tfield onLoadSaveFile onLoadSaveFile
Expand Down Expand Up @@ -4194,6 +4203,20 @@ void luaPlugin::onPluginLoadF()
onClickAtTile = new sol::function(luaState["onClickAtTile"]);
checkLuaFunc(&onClickAtTile);

/***
Called when clicking on a character.
@function onCharacterClicked
@tparam character clickedCharacter
@usage
function onCharacterClicked(clickedCharacter)
--something here
end
*/
onCharacterClicked = new sol::function(luaState["onCharacterClicked"]);
checkLuaFunc(&onCharacterClicked);

/***
Called after loading the campaign map
Expand Down
1 change: 1 addition & 0 deletions M2TWEOP Code/M2TWEOP library/types/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ namespace gameEvents


void onClickAtTile(int x, int y);
void onCharacterClicked(character* enemy);
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
10 changes: 5 additions & 5 deletions M2TWEOP Code/M2TWEOP library/types/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1298,22 +1298,22 @@ namespace unitHelpers

void setUnitName(eduEntry* entry, const std::string& name)
{
const auto nameMem = new UNICODE_STRING**;
const auto nameMem = techFuncs::createGameClass<UNICODE_STRING**>();
entry->localizedName = nameMem;
gameStringHelpers::createUniString(*entry->localizedName, name.c_str());
}

void setUnitDescr(eduEntry* entry, const std::string& descr)
{
const auto descrMem = new UNICODE_STRING * *[4];
entry->localizedDescr = descrMem;
const auto nameMem = techFuncs::createGameClass<UNICODE_STRING**>();
entry->localizedDescr = nameMem;
gameStringHelpers::createUniString(*entry->localizedDescr, descr.c_str());
}

void setUnitDescrShort(eduEntry* entry, const std::string& descr)
{
const auto shDescrMem = new UNICODE_STRING * *[4];
entry->localizedDescrShort = shDescrMem;
const auto nameMem = techFuncs::createGameClass<UNICODE_STRING**>();
entry->localizedDescrShort = nameMem;
gameStringHelpers::createUniString(*entry->localizedDescrShort, descr.c_str());
}

Expand Down

0 comments on commit afd3475

Please sign in to comment.