From 53fe96018428af94e854a6fdc4de073e11f8d876 Mon Sep 17 00:00:00 2001 From: FynnTW Date: Wed, 2 Oct 2024 14:55:37 +0300 Subject: [PATCH] fix fog of war settlements crash --- .../M2TWEOP library/stratModelsHelper.cpp | 43 +++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/M2TWEOP Code/M2TWEOP library/stratModelsHelper.cpp b/M2TWEOP Code/M2TWEOP library/stratModelsHelper.cpp index dcb68fcb..6d2fbfbc 100644 --- a/M2TWEOP Code/M2TWEOP library/stratModelsHelper.cpp +++ b/M2TWEOP Code/M2TWEOP library/stratModelsHelper.cpp @@ -45,10 +45,15 @@ namespace stratModelsChange && settlement->model->cityModel == modelP )return; - const auto oldMod = cultures::getCultureSettlement(settlement->level, settlement->fac_creatorModNum); - const auto newMod = techFuncs::createGameClass(); - GAME_FUNC(void(__thiscall*)(cultureSettlement*), createCultureSettlement)(newMod); - *newMod = *oldMod; + cultureSettlement* newMod = settlement->model; + if (settlement->model && settlement->model->index != 9999) + { + const auto oldMod = cultures::getCultureSettlement(settlement->level, settlement->fac_creatorModNum); + newMod = techFuncs::createGameClass(); + GAME_FUNC(void(__thiscall*)(cultureSettlement*), createCultureSettlement)(newMod); + *newMod = *oldMod; + newMod->index = 9999; + } newMod->cityModel = modelP; newMod->castleModel = modelP; for (int i = 0; i < 31; i++) @@ -58,17 +63,37 @@ namespace stratModelsChange } settlement->model = newMod; } + + bool isVanillaCultureFort(const DWORD addr) + { + const auto cultureDb = cultures::getCultureDb(); + const auto cultureCount = cultureDb->culturesCount; + for (int i = 0; i < cultureCount; i++) + { + const auto culture = &cultureDb->cultures[i]; + if (reinterpret_cast(&culture->fort) == addr) + return true; + } + return false; + } void changeFortStratModel(fortStruct* fort, model_Rigid* modelP, model_Rigid* modelP2) { if (fort->stratModel->model == modelP && fort->stratModel->wallsModel == modelP2) return; - const auto oldMod = fort->stratModel; - const auto newMod = techFuncs::createGameClass(); - GAME_FUNC(void(__thiscall*)(cultureFort*), createCultureFort)(newMod); - if (oldMod) - *newMod = *oldMod; + + cultureFort* newMod = fort->stratModel; + + if (const DWORD addr = reinterpret_cast(fort->stratModel); + fort->stratModel && isVanillaCultureFort(addr)) + { + const auto oldMod = fort->stratModel; + newMod = techFuncs::createGameClass(); + GAME_FUNC(void(__thiscall*)(cultureFort*), createCultureFort)(newMod); + if (oldMod) + *newMod = *oldMod; + } newMod->model = modelP; newMod->wallsModel = modelP2; for (int i = 0; i < 31; i++)