Ryan F. Mercer
Whys
The following XML and LUA files allow for buildings to set resource types and amounts produced per turn. The demonstration includes an Alchemist building that produces 1 gold resource and additional entries can be easily added to the XML.
Technically, everything works. Build an Alchemist and you get one gold resource. Problem is the necessary function is attached to the player turn start, not building complete or building destroyed. Thus the TopPanel is having trouble properly updating, misreporting empire happiness until either a unit is moved or another turn is taken. Similarly, when you sell the buildling, you have to take another turn before you actually lose the resource, and then another turn before TopPanel properly updates again.
I would like to release this mod, but these issues aren't acceptable. Does anyone know what can be done to improve this?
Technically, everything works. Build an Alchemist and you get one gold resource. Problem is the necessary function is attached to the player turn start, not building complete or building destroyed. Thus the TopPanel is having trouble properly updating, misreporting empire happiness until either a unit is moved or another turn is taken. Similarly, when you sell the buildling, you have to take another turn before you actually lose the resource, and then another turn before TopPanel properly updates again.
I would like to release this mod, but these issues aren't acceptable. Does anyone know what can be done to improve this?
Spoiler :
Code:
-- vymdt.01.2010.10.28.0000
-- Created by: Ryan F. Mercer -Open source
--===========================================================================
-- BuildingResources
--===========================================================================
--[[
Adds resources from buildings to player with corresponding buildings.
]]
include( "SaveUtils" ); MY_MOD_NAME = "Building Resources";
--===========================================================================
--[[
...
]]
function GetPlayer()
local iPlayerID = Game.GetActivePlayer();
if (iPlayerID < 0) then
print("Error - player index not correct");
return nil;
end
if (not Players[iPlayerID]:IsHuman()) then
return nil;
end;
return Players[iPlayerID];
end
--===========================================================================
--[[
...
]]
function addBuildingResources()
local pPlayer = GetPlayer();
local applied = load( pPlayer, "Building Resources" );
local listing = {};
for pCity in pPlayer:Cities() do
local cityID = pCity:GetID();
listing[cityID] = {};
applied[cityID] = applied[cityID] or {};
for row in GameInfo.Building_ResourceChange() do
local building = row.BuildingType;
local resource = row.ResourceType;
local change = row.ResourceChange;
local buildingID = GameInfo["Buildings"][building]["ID"];
if pCity:IsHasBuilding( buildingID ) then
listing[cityID][buildingID] = building;
if applied[cityID][buildingID] == nil then
pPlayer:ChangeNumResourceTotal( GameInfo["Resources"][resource]["ID"], change ); --cumulative.
end
applied[cityID][buildingID] = nil;
end
end
if len( applied[cityID] ) == 0 then applied[cityID] = nil; end
end
for cityID,buildings in pairs( applied ) do
for buildingID,building in pairs( buildings ) do
for row in GameInfo.Building_ResourceChange() do
local tBuilding = row.BuildingType;
local tResource = row.ResourceType;
local tChange = row.ResourceChange;
if tBuilding == building then
pPlayer:ChangeNumResourceTotal( GameInfo["Resources"][tResource]["ID"], -tChange ); --cumulative.
break;
end
end
end
end
save( pPlayer, "Building Resources", listing );
end
Events.ActivePlayerTurnStart.Add( addBuildingResources );
--===========================================================================
--END BuildingResources
--===========================================================================
-- Created by: Ryan F. Mercer -Open source
Code:
<?xml version="1.0" encoding="utf-8"?>
<!-- Created by ModBuddy on 10/18/2010 10:47:13 AM -->
<GameData>
<BuildingClasses>
<!-- Table data -->
<Row>
<Type>BUILDINGCLASS_ALCHEMIST</Type>
<DefaultBuilding>BUILDING_ALCHEMIST</DefaultBuilding>
<Description>TXT_KEY_BUILDING_ALCHEMIST</Description>
</Row>
</BuildingClasses>
</GameData>
Code:
<?xml version="1.0" encoding="utf-8"?>
<!-- Created by ModBuddy on 10/18/2010 10:36:36 AM -->
<GameData>
<!-- Table definition -->
<Table name="Building_ResourceChange">
<Column name="BuildingType" type="text" reference="Buildings(Type)"/>
<Column name="ResourceType" type="text" reference="Resources(Type)"/>
<Column name="ResourceChange" type="integer"/>
</Table>
<!-- Table data -->
<Buildings>
<Row>
<Type>BUILDING_ALCHEMIST</Type>
<BuildingClass>BUILDINGCLASS_ALCHEMIST</BuildingClass>
<Cost>1</Cost>
<GoldMaintenance>1</GoldMaintenance>
<Help>TXT_KEY_BUILDING_ALCHEMIST_HELP</Help>
<Description>TXT_KEY_BUILDING_ALCHEMIST</Description>
<Civilopedia>TXT_KEY_BUILDING_ALCHEMIST_PEDIA</Civilopedia>
<Strategy>TXT_KEY_BUILDING_ALCHEMIST_STRATEGY</Strategy> <!-- not used. -->
<ArtDefineTag>ART_DEF_BUILDING_FORGE</ArtDefineTag>
<MinAreaSize>-1</MinAreaSize>
<ConquestProb>0</ConquestProb>
<HurryCostModifier>-1</HurryCostModifier>
<IconAtlas>BW_ATLAS_1</IconAtlas>
<PortraitIndex>28</PortraitIndex>
</Row>
</Buildings>
<Building_ResourceChange>
<Row>
<BuildingType>BUILDING_ALCHEMIST</BuildingType>
<ResourceType>RESOURCE_GOLD</ResourceType>
<ResourceChange>1</ResourceChange>
</Row>
</Building_ResourceChange>
</GameData>