Skip to content

Commit

Permalink
Add getting faction record num
Browse files Browse the repository at this point in the history
  • Loading branch information
FynnTW committed Oct 14, 2024
1 parent 9391ffc commit 3fc685c
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 55 deletions.
29 changes: 18 additions & 11 deletions M2TWEOP Code/M2TWEOP library/gameStringHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,25 @@ namespace gameStringHelpers

std::string uniStringToStr(UNICODE_STRING**& uniString)
{
UNICODE_STRING* uniS = *uniString;
const wchar_t* wstr = reinterpret_cast<wchar_t*>(&uniS->Buffer);
std::string strTo;
const int wCharsNum = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, nullptr, 0, nullptr, nullptr);
if (wCharsNum <= 0)
try
{
UNICODE_STRING* uniS = *uniString;
const wchar_t* wstr = reinterpret_cast<wchar_t*>(&uniS->Buffer);
std::string strTo;
const int wCharsNum = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, nullptr, 0, nullptr, nullptr);
if (wCharsNum <= 0)
return strTo;
const auto szTo = new char[wCharsNum];
szTo[wCharsNum-1] = '\0';
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, szTo, wCharsNum, nullptr, nullptr);
strTo = szTo;
delete[] szTo;
return strTo;
const auto szTo = new char[wCharsNum];
szTo[wCharsNum-1] = '\0';
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, szTo, wCharsNum, nullptr, nullptr);
strTo = szTo;
delete[] szTo;
return strTo;
}
catch (...)
{
return "";
}
}
}

62 changes: 36 additions & 26 deletions M2TWEOP Code/M2TWEOP library/luaPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ sol::state* luaPlugin::init(std::string& luaFilePath, std::string& modPath)
@tfield getRebelFaction getRebelFaction
@tfield addBanner addBanner
@tfield getFactionRecord getFactionRecord
@tfield getFactionRecordNum getFactionRecordNum
@tfield hideUnknownUnitTooltips hideUnknownUnitTooltips
@tfield handleUnitCards handleUnitCards
@tfield setKhakiTextColor setKhakiTextColor
Expand Down Expand Up @@ -666,7 +667,7 @@ sol::state* luaPlugin::init(std::string& luaFilePath, std::string& modPath)
@function M2TWEOP.getOptions2
@treturn options2 options
@usage
local options = M2TWEOP.getOptions2();
local options = M2TWEOP.getOptions2();
*/
tables.M2TWEOP.set_function("getOptions2", &gameHelpers::getOptions2);

Expand All @@ -675,7 +676,7 @@ sol::state* luaPlugin::init(std::string& luaFilePath, std::string& modPath)
@function M2TWEOP.getCampaignDifficulty1
@treturn campaignDifficulty1 options
@usage
local modifiers = M2TWEOP.getCampaignDifficulty1();
local modifiers = M2TWEOP.getCampaignDifficulty1();
*/
tables.M2TWEOP.set_function("getCampaignDifficulty1", &campaignHelpers::getCampaignDifficulty1);

Expand All @@ -684,7 +685,7 @@ sol::state* luaPlugin::init(std::string& luaFilePath, std::string& modPath)
@function M2TWEOP.getCampaignDifficulty2
@treturn campaignDifficulty2 options
@usage
local modifiers = M2TWEOP.getCampaignDifficulty2();
local modifiers = M2TWEOP.getCampaignDifficulty2();
*/
tables.M2TWEOP.set_function("getCampaignDifficulty2", &campaignHelpers::getCampaignDifficulty2);

Expand All @@ -693,7 +694,7 @@ sol::state* luaPlugin::init(std::string& luaFilePath, std::string& modPath)
@function M2TWEOP.getCampaignDb
@treturn campaignDb options
@usage
local options = M2TWEOP.getCampaignDb();
local options = M2TWEOP.getCampaignDb();
*/
tables.M2TWEOP.set_function("getCampaignDb", &campaignHelpers::getCampaignDb);

Expand All @@ -702,7 +703,7 @@ sol::state* luaPlugin::init(std::string& luaFilePath, std::string& modPath)
@function M2TWEOP.getCampaignDbExtra
@treturn campaignDbExtra options
@usage
local options = M2TWEOP.getCampaignDbExtra();
local options = M2TWEOP.getCampaignDbExtra();
*/
tables.M2TWEOP.set_function("getCampaignDbExtra", &campaignHelpers::getCampaignDbExtra);

Expand All @@ -713,7 +714,7 @@ sol::state* luaPlugin::init(std::string& luaFilePath, std::string& modPath)
@tparam int modelId Unique ID to use the model later.
@tparam bool isSettlement optional
@usage
M2TWEOP.addModelToGame("data/models_strat/residences/invisible.CAS",1);
M2TWEOP.addModelToGame("data/models_strat/residences/invisible.CAS",1);
*/
tables.M2TWEOP.set_function("addModelToGame", sol::overload(
sol::resolve<void(const std::string&, uint32_t)>(stratModelsChange::addModelToGameNoBool),
Expand All @@ -727,9 +728,9 @@ sol::state* luaPlugin::init(std::string& luaFilePath, std::string& modPath)
@tparam eventTrigger|nil eventData
@treturn bool isTrue
@usage
if M2TWEOP.condition("InEnemyLands", eventData) then
--do stuff
end
if M2TWEOP.condition("InEnemyLands", eventData) then
--do stuff
end
*/
tables.M2TWEOP.set_function("condition", &gameHelpers::conditionLua);

Expand All @@ -742,7 +743,7 @@ sol::state* luaPlugin::init(std::string& luaFilePath, std::string& modPath)
@tparam int targetY only adjacent tiles! Does not calculate paths just the cost of moving from one tile to another.
@treturn float moveCost
@usage
local moveCost = M2TWEOP.getTileMoveCost(153, 245, 154, 245);
local moveCost = M2TWEOP.getTileMoveCost(153, 245, 154, 245);
*/
tables.M2TWEOP.set_function("getTileMoveCost", &stratMapHelpers::getTileMoveCost);

Expand All @@ -754,8 +755,8 @@ sol::state* luaPlugin::init(std::string& luaFilePath, std::string& modPath)
@tparam int modelId used for: watchtower, resource, settlement, fort, port
@tparam int modelId2 used for: fort wall (use fort coords), dock (use port coords)
@usage
M2TWEOP.addModelToGame("data/models_strat/residences/invisible.CAS",1)
M2TWEOP.setModel(288,257,1,1)
M2TWEOP.addModelToGame("data/models_strat/residences/invisible.CAS",1)
M2TWEOP.setModel(288,257,1,1)
*/
tables.M2TWEOP.set_function("setModel", sol::overload(&stratModelsChange::setModel,&stratModelsChange::setModelOneVar));

Expand All @@ -767,13 +768,13 @@ sol::state* luaPlugin::init(std::string& luaFilePath, std::string& modPath)
@treturn string error Note: string can be empty but not nil
@usage
-- Creating units, adding money
function onCharacterSelected(eventData)
local selectedChar = eventData.character
local err = M2TWEOP.callConsole("add_money", "2321")
local err2 = M2TWEOP.callConsole("create_unit", "testcharacter 'Cool Unit' 4 1 1 1")
print(err)
print(err2)
end
function onCharacterSelected(eventData)
local selectedChar = eventData.character
local err = M2TWEOP.callConsole("add_money", "2321")
local err2 = M2TWEOP.callConsole("create_unit", "testcharacter 'Cool Unit' 4 1 1 1")
print(err)
print(err2)
end
*/
tables.M2TWEOP.set_function("callConsole", &gameHelpers::callConsole);

Expand All @@ -783,7 +784,7 @@ sol::state* luaPlugin::init(std::string& luaFilePath, std::string& modPath)
@tparam string counterName
@tparam int value
@usage
M2TWEOP.setScriptCounter("SomeCounter", 25)
M2TWEOP.setScriptCounter("SomeCounter", 25)
*/
tables.M2TWEOP.set_function("setScriptCounter", &gameHelpers::setScriptCounter);

Expand All @@ -794,12 +795,12 @@ sol::state* luaPlugin::init(std::string& luaFilePath, std::string& modPath)
@treturn int counterValue Returns the value of the counter
@usage
---@param eventData eventTrigger
function onEventCounter(eventData)
-- get the name and value of the triggered counter
local counterName = eventData.eventCounter
local counterValue = M2TWEOP.getScriptCounter(counterName)
print('counter : '..counterName..' has been set to : '..counterValue)
end
function onEventCounter(eventData)
-- get the name and value of the triggered counter
local counterName = eventData.eventCounter
local counterValue = M2TWEOP.getScriptCounter(counterName)
print('counter : '..counterName..' has been set to : '..counterValue)
end
*/
tables.M2TWEOP.set_function("getScriptCounter", &gameHelpers::getScriptCounterNoBool);

Expand Down Expand Up @@ -832,6 +833,15 @@ sol::state* luaPlugin::init(std::string& luaFilePath, std::string& modPath)
*/
tables.M2TWEOP.set_function("getFactionRecord", &factionHelpers::getFactionRecord);

/***
Get amount of factions in descr_sm_factions.
@function M2TWEOP.getFactionRecordNum
@treturn int facNum
@usage
local facNum = M2TWEOP.getFactionRecordNum()
*/
tables.M2TWEOP.set_function("getFactionRecordNum", &factionHelpers::getFactionRecordNum);

/***
Hides tooltips for unknown units, only use if you use empty card instead of question mark as the UI.
@function M2TWEOP.hideUnknownUnitTooltips
Expand Down
31 changes: 31 additions & 0 deletions M2TWEOP Code/M2TWEOP library/types/eopBuildings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//@author Fynn
//@license GPL-3.0
#include "pch.h"
#include "faction.h"
#include "eopBuildings.h"
#include "settlement.h"
#include "dataOffsets.h"
Expand Down Expand Up @@ -172,6 +173,36 @@ void eopHiddenResources::initialize()
}


std::string buildingLevel::getLocalizedName(const int factionID)
{
if (const int facNum = factionHelpers::getFactionRecordNum(); factionID >= facNum)
return "";
if (buildingName[factionID] != nullptr && *buildingName[factionID] != nullptr)
return gameStringHelpers::uniStringToStr(*buildingName[factionID]);
return "";
}


std::string buildingLevel::getLocalizedDescr(const int factionID)
{
if (const int facNum = factionHelpers::getFactionRecordNum(); factionID >= facNum)
return "";
if (buildingDescr[factionID] != nullptr && *buildingDescr[factionID] != nullptr)
return gameStringHelpers::uniStringToStr(*buildingDescr[factionID]);
return "";
}


std::string buildingLevel::getLocalizedDescrShort(const int factionID)
{
if (const int facNum = factionHelpers::getFactionRecordNum(); factionID >= facNum)
return "";
if (buildingDescrShort[factionID] != nullptr)
return gameStringHelpers::uniStringToStr(*buildingDescrShort[factionID]);
return "";
}


//add new building capability, bonus refers to bonus keyboard in edb
void buildingLevel::addCapability(int capability, int16_t value, bool bonus, const std::string& condition)
{
Expand Down
22 changes: 4 additions & 18 deletions M2TWEOP Code/M2TWEOP library/types/eopBuildings.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <string>

#include "dataOffsets.h"
#include "gameStringHelpers.h"
#include "realGameTypes.h"
#include "techFuncs.h"
Expand Down Expand Up @@ -156,36 +157,21 @@ struct buildingLevel { /* (name, tga's, models, etc) */
{
gameStringHelpers::setHashedString(&name, newName.c_str());
}
std::string getLocalizedName(const int factionID)
{
if (buildingName[factionID] != nullptr)
return gameStringHelpers::uniStringToStr(*buildingName[factionID]);
return "";
}
std::string getLocalizedName(const int factionID);
void setLocalizedName(const int factionID, const std::string& newName)
{
const auto nameMem = new UNICODE_STRING**;
buildingName[factionID] = nameMem;
gameStringHelpers::createUniString(*buildingName[factionID], newName.c_str());
}
std::string getLocalizedDescr(const int factionID)
{
if (buildingDescr[factionID] != nullptr)
return gameStringHelpers::uniStringToStr(*buildingDescr[factionID]);
return "";
}
std::string getLocalizedDescr(const int factionID);
void setLocalizedDescr(const int factionID, const std::string& newName)
{
const auto nameMem = new UNICODE_STRING**;
buildingDescr[factionID] = nameMem;
gameStringHelpers::createUniString(*buildingDescr[factionID], newName.c_str());
}
std::string getLocalizedDescrShort(const int factionID)
{
if (buildingDescrShort[factionID] != nullptr)
return gameStringHelpers::uniStringToStr(*buildingDescrShort[factionID]);
return "";
}
std::string getLocalizedDescrShort(const int factionID);
void setLocalizedDescrShort(const int factionID, const std::string& newName)
{
const auto nameMem = new UNICODE_STRING**;
Expand Down
5 changes: 5 additions & 0 deletions M2TWEOP Code/M2TWEOP library/types/faction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ namespace factionHelpers
return nullptr;
}

int getFactionRecordNum()
{
return dataOffsets::offsets.descr_sm_factionslist->size;
}

template <char fieldIndex>
std::string getStringProperty(const factionStruct* fac)
{
Expand Down
1 change: 1 addition & 0 deletions M2TWEOP Code/M2TWEOP library/types/faction.h
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ class eopFactionData
namespace factionHelpers
{
factionRecord* getFactionRecord(int id);
int getFactionRecordNum();
std::string getFactionName(const factionStruct* fac);
void changeFactionName(factionStruct* fac, const char* newName);
UNICODE_STRING** getFactionNameLocal(factionStruct* fac);
Expand Down

0 comments on commit 3fc685c

Please sign in to comment.