Skip to content

Commit

Permalink
feat: add utility function for adding info for new entities
Browse files Browse the repository at this point in the history
  • Loading branch information
LordMidas committed Sep 21, 2023
1 parent f6ca66d commit 3045639
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
11 changes: 11 additions & 0 deletions msu/hooks/config/global.nut
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local getDefaultFaction = ::Const.EntityType.getDefaultFaction;
::Const.EntityType.getDefaultFaction = function( _id )
{
local ret = getDefaultFaction(_id);
if (ret == ::Const.FactionType.Generic && (_id in ::MSU.Entities.EntityTypeToDefaultFactionMap))
{
return ::MSU.Entities.EntityTypeToDefaultFactionMap[_id];
}

return ret;
}
32 changes: 32 additions & 0 deletions msu/utils/entities.nut
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
::MSU.Entities <- {
EntityTypeToDefaultFactionMap = {},
HighestIDDiff = -99,
HighestID = -99,

// Utility function that puts all the required info of an entity in all the right places
function addEntity( _info )
{
// _info is a table that must contain the following keys:
// EntityType, EntityIcon, DefaultFaction, EntityName, EntityNamePlural, TroopDef, ActorDef

// If the Diff is now greater than before, that means some other mod added something to the ::Const.EntityType
// table, so we should update our HighestID again.
if (this.HighestID == -99 || ::Const.EntityType.len() - this.HighestID > this.HighestIDDiff)
{
foreach (key, value in ::Const.EntityType)
{
if (typeof value == "integer" && value > this.HighestID)
this.HighestID = value;
}
this.HighestIDDiff = ::Const.EntityType.len() - this.HighestID;
}

this.EntityTypeToDefaultFactionMap[_info.EntityType] <- _info.DefaultFaction;
::Const.EntityType[_info.EntityType] <- ++this.HighestID;
::Const.EntityIcon.push(_info.EntityIcon);
::Const.Strings.EntityName.push(_info.EntityName);
::Const.Strings.EntityNamePlural.push(_info.EntityNamePlural);
::Const.World.Spawn.Troops[_info.EntityType] <- _info.TroopDef;
::Const.Tactical.Actor[_info.EntityType] <- _info.ActorDef;
}
}

0 comments on commit 3045639

Please sign in to comment.