Cobalt_Blue
Chieftain
Hi all, I just released my first Civ V mod, Strategic Availability (found here: http://forums.civfanatics.com/showthread.php?t=416215), which is designed to be a sort of middle ground between normal play and the "Strategic Balance" resource setting. It turned out to be a pretty small mod, just one lua file.
Now that I'm mostly done, I thought I might I'd dump the code for two reasons:
1) To ask you guys to glance at the code and make sure I didn't do anything monumentally stupid that is going to break other mods or something down the line.
2) I figure the code may help someone somewhere someday with their own modding.
Or any other suggestions, really. Are there any "best practices" established for this yet? Any "worst practices" that you see here?
Anyway, thanks in advance for looking.
Now that I'm mostly done, I thought I might I'd dump the code for two reasons:
1) To ask you guys to glance at the code and make sure I didn't do anything monumentally stupid that is going to break other mods or something down the line.
2) I figure the code may help someone somewhere someday with their own modding.
Or any other suggestions, really. Are there any "best practices" established for this yet? Any "worst practices" that you see here?
Anyway, thanks in advance for looking.
Spoiler :
Code:
-- Strategic_Availability
-- Author: Cobalt_Blue
-- DateCreated: 3/21/2011 8:26:41 PM
--------------------------------------------------------------
-------------
---GLOBALS---
-------------
--Flags
runOnceAlready = false;
--Techs
horsebackRidingID = -1;
TXT_HORSEBACK_RIDING = "TECH_HORSEBACK_RIDING";
steelID = -1;
TXT_STEEL = "TECH_STEEL";
steamPowerID = -1;
TXT_STEAM_POWER = "TECH_STEAM_POWER";
combustionID = -1;
TXT_COMBUSTION = "TECH_COMBUSTION";
electronicsID = -1;
TXT_ELECTRONICS = "TECH_ELECTRONICS";
nuclearFusionID = -1;
TXT_NULCEAR_FUSION = "TECH_NUCLEAR_FUSION";
--Resources
horsesID = -1;
TXT_HORSES = "RESOURCE_HORSE";
ironID = -1;
TXT_IRON = "RESOURCE_IRON";
coalID = -1;
TXT_COAL = "RESOURCE_COAL";
oilID = -1;
TXT_OIL = "RESOURCE_OIL";
aluminumID = -1;
TXT_ALUMINUM = "RESOURCE_ALUMINUM";
uraniumID = -1;
TXT_URANIUM = "RESOURCE_URANIUM";
-------------
--FUNCTIONS--
-------------
--Debugging tool, not used
function Listen(...)
print("LISTENING")
print(unpack({...}))
print("END LISTENING")
end
--Load all relevant ids into global vars
function StrategicAvailability_GetIds()
--Get Resource IDs
for i = 0, 63, 1 do
local pResourceInfo = GameInfo.Resources[i];
if (pResourceInfo ~= nil) then
szText = Locale.ConvertTextKey( pResourceInfo.Type);
if (szText == TXT_HORSES) then
horsesID = i;
elseif (szText == TXT_IRON) then
ironID = i;
elseif (szText == TXT_COAL) then
coalID = i;
elseif (szText == TXT_OIL) then
oilID = i;
elseif (szText == TXT_ALUMINUM) then
aluminumID = i;
elseif (szText == TXT_URANIUM) then
uraniumID = i;
end
end
end
--Get Tech IDs
for i = 0, 255, 1 do
local pTechInfo = GameInfo.Technologies[i];
if (pTechInfo ~= nil) then
szText = Locale.ConvertTextKey( pTechInfo.Type );
if (szText == TXT_HORSEBACK_RIDING) then
horsebackRidingID = i;
elseif (szText == TXT_STEEL) then
steelID = i;
elseif (szText == TXT_STEAM_POWER) then
steamPowerID = i;
elseif (szText ==TXT_COMBUSTION) then
combustionID = i;
elseif (szText == TXT_ELECTRONICS) then
electronicsID = i;
elseif (szText == TXT_NULCEAR_FUSION) then
nuclearFusionID = i;
end
end
end
end
--If the player acquired the right tech, increase their resource by one
function StrategicAvailability_TechAcquired(player_id, tech_id)
local player = Players[player_id];
local pTechInfo = GameInfo.Technologies[tech_id];
szText = Locale.ConvertTextKey( pTechInfo.Type );
--print( "player " .. player_id .. ": " .. player:GetName() .. "acquired tech " .. tech_id .. ": " .. szText );
if ((not player:IsMinorCiv()) and (not player:IsBarbarian())) then
--Steel/Iron
if ((tech_id == steelID) and (ironID ~= nil)) then
player:ChangeNumResourceTotal(ironID,1);
print("Giving 1 Iron to " .. player:GetName());
--HorsebackRiding/Horses
elseif ((tech_id == horsebackRidingID) and (horsesID ~= nil)) then
player:ChangeNumResourceTotal(horsesID,1);
print("Giving 1 Horse to " .. player:GetName());
--SteamPower/Coal
elseif ((tech_id == steamPowerID) and (coalID ~= nil)) then
player:ChangeNumResourceTotal(coalID,1);
print("Giving 1 Coal to " .. player:GetName());
--Combustion/Oil
elseif ((tech_id == combustionID) and (oilID ~= nil)) then
player:ChangeNumResourceTotal(oilID,1);
print("Giving 1 Oil to " .. player:GetName());
--Electronics/Aluminum
elseif ((tech_id == electronicsID) and (aluminumID ~= nil)) then
player:ChangeNumResourceTotal(aluminumID,1);
print("Giving 1 Aluminum to " .. player:GetName());
--NuclearFusion/Uranium
elseif ((tech_id == nuclearFusionID) and (uraniumID ~= nil)) then
player:ChangeNumResourceTotal(uraniumID,1);
print("Giving 1 Uranium to " .. player:GetName());
end
end
end
--check for techs once at the start of the game so that advanced starts get proper bonuses
function StrategicAvailability_StartGame(xyz, civID, ctr, a,b,c,d,e,f)
if (not runOnceAlready) then
print("STRATEGIC AVAILABILITY MOD: Starting up!");
for i = 0, 127, 1 do
local player = Players[i];
if (player ~= nil and player:IsAlive()) then
print(player:GetName().. " : " .. i);
if ((not player:IsMinorCiv()) and (not player:IsBarbarian())) then
--Grab techs player already has
teamID = player:GetTeam();
teamTechs = Teams[teamID]:GetTeamTechs()
--STEEL
if(teamTechs:HasTech(steelID)) then
print(player:GetName() .. " already has STEEL! Adding one (1) Iron.")
player:ChangeNumResourceTotal(ironID,1);
end
--HORSEBACK RIDING
if(teamTechs:HasTech(horsebackRidingID)) then
print(player:GetName() .. " already has HORSEBACK RIDING! Adding one (1) Horse.")
player:ChangeNumResourceTotal(horsesID,1);
end
--STEAM POWER
if(teamTechs:HasTech(steamPowerID)) then
print(player:GetName() .. " already has STEAM POWER! Adding one (1) Coal.")
player:ChangeNumResourceTotal(coalID,1);
end
--COMBUSTION
if(teamTechs:HasTech(combustionID)) then
print(player:GetName() .. " already has COMBUSTION! Adding one (1) Oil.")
player:ChangeNumResourceTotal(oilID,1);
end
--ELECTRONICS
if(teamTechs:HasTech(electronicsID)) then
print(player:GetName() .. " already has ELECTRONICS! Adding one (1) Aluminum.")
player:ChangeNumResourceTotal(aluminumID,1);
end
--NUCLEAR FUSION
if(teamTechs:HasTech(nuclearFusionID)) then
print(player:GetName() .. " already has NUCLEAR FUSION! Adding one (1) Uranium.")
player:ChangeNumResourceTotal(uraniumID,1);
end
end
end
end
runOnceAlready = true;
end
end
-------------
---STARTUP---
-------------
StrategicAvailability_GetIds();
Events.TechAcquired.Add(StrategicAvailability_TechAcquired);
Events.SerialEventCityCreated.Add(StrategicAvailability_StartGame);
--DEBUG MESSAGES, Making sure all the IDs were found
---
print("IRON:".. ironID);
print("HORSE:".. horsesID);
print("COAL:".. coalID);
print("OIL:".. oilID);
print("ALUMINUM:".. aluminumID);
print("URANIUM:".. uraniumID);
---
print("STEEL:".. steelID);
print("HORSEBACK_RIDING:".. horsebackRidingID);
print("STEAM_POWER:".. steamPowerID);
print("COMBUSTION:".. combustionID);
print("ELECTRONICS:".. electronicsID);
print("NUCLEAR_FUSION:".. nuclearFusionID);
---