Ryoga
King
- Joined
- Oct 12, 2010
- Messages
- 993
This is my first attempt at modding using a LUA script. So there are many things that I'm not sure how they work.
As the title implies this mod is supposed to automatically give a special unit (a sort of mini Engineer, whose properties are yet to be defined) to any city that just saw its wonder production halted because someone else completed it first.
As far as I know there are no hooks for a Wonder race lost so I devised a workaround by modifying Hambil's "Wonder Race" mod.
Currently the code is like this:
As it stands now, the code seems to work as it should, but it is not quite easy to test every possible scenario.
I am particularly unsure about this part:
It is easy to see who will win if one is 1 turn from completion and the other is not. A lot more ambiguous when both are.
I know (or so I think) that the human player will always lose in this case, but I'm not sure how it is handled for everyone else.
I am assuming here that PlayerID is a number comprised between "0" (the human player) and the total number of CIVs at the start of the game and the same is true for iPlayer. And I'm assuming that whoever has the lower PlayerID will be winning the wonder race in case of a tie, but I can't be quite sure about that.
That apart at this point I would like to find a way to determine how much progress is lost at the time of the wonder race failure.
I know how to get the turns remaining, I could probably get the wonder base cost from Gameinfo, but how do I proceed from that?
I'm looking for suggestions.
As the title implies this mod is supposed to automatically give a special unit (a sort of mini Engineer, whose properties are yet to be defined) to any city that just saw its wonder production halted because someone else completed it first.
As far as I know there are no hooks for a Wonder race lost so I devised a workaround by modifying Hambil's "Wonder Race" mod.
Currently the code is like this:
Code:
-- "gDoomedToFail" stores the number of cities (of any CIV) that will lose their Wonder next turn.
-- Following arrays will store the city owner and city position for each instance of "gDoomedToFail"
local gDoomedToFail = 0
local gLoserPlayer = {}
local gLoserCityPlotX = {}
local gLoserCityPlotY = {}
function DoTurn( playerID )
local enemyTurnsLeft = 0
local myTurnsLeft = 0
local iUnitID
local player = Players[playerID]
local enemyPlayer
local buildingInfo
local buildingClassInfo
local enemyBuildingInfo
if not player then
return
end
-- if gDoomedToFail is greater than 0 process everything on human player turn and then do nothing until next turn.
if playerID == 0 then
while gDoomedToFail > 0 do
--Unit placeholder, will be changed later
iUnitID = GameInfoTypes["UNIT_MISSIONARY"]
gLoserPlayer[gDoomedToFail]:InitUnit (iUnitID, gLoserCityPlotX[gDoomedToFail], gLoserCityPlotY[gDoomedToFail])
gDoomedToFail = gDoomedToFail - 1
end
end
for city in player:Cities() do
buildingInfo = GameInfo.Buildings[city:GetProductionBuilding()]
if buildingInfo then
buildingClassInfo = GameInfo.BuildingClasses[buildingInfo.BuildingClass]
if buildingClassInfo.MaxGlobalInstances == 1 then
myTurnsLeft = city:GetProductionTurnsLeft()
for iPlayer = 0, GameDefines.MAX_MAJOR_CIVS - 1, 1 do
enemyPlayer = Players[iPlayer]
if enemyPlayer ~= nil and enemyPlayer ~= player and enemyPlayer:IsAlive() then
for enemyCity in enemyPlayer:Cities() do
enemyBuildingInfo = GameInfo.Buildings[enemyCity:GetProductionBuilding()]
if enemyBuildingInfo and enemyBuildingInfo == buildingInfo then
enemyTurnsLeft = enemyCity:GetProductionTurnsLeft()
--Will I lose the Race next turn? Only true if 1: Other Civ City is at 1 turn from completion.
--2a: I won't complete the wonder next turn OR 2b: I will complete the wonder next turn but I'm either
-- the human player or my turn will be processed after.
if enemyTurnsLeft == 1 and ( myTurnsLeft > 1 or ( myTurnsLeft == 1 and (PlayerID > iPlayer or PlayerID == 0))) then
gDoomedToFail = gDoomedToFail + 1
gLoserPlayer[gDoomedToFail] = player
gLoserCityPlotX[gDoomedToFail] = city:GetX()
gLoserCityPlotY[gDoomedToFail] = city:GetY()
end
-- I confirmed that this player is building the wonder, he can't possibly be building it in another city
-- Let's move to the next player
iPlayer = iPlayer + 1
end
end
end
end
end
end
end
end
function DoInit()
ContextPtr:SetHide(true)
end
GameEvents.PlayerDoTurn.Add( DoTurn );
Events.SequenceGameInitComplete.Add( DoInit );
As it stands now, the code seems to work as it should, but it is not quite easy to test every possible scenario.
I am particularly unsure about this part:
Code:
--Will I lose the Race next turn? Only true if 1: Other Civ City is at 1 turn from completion.
--2a: I won't complete the wonder next turn OR 2b: I will complete the wonder next turn but I'm either
-- the human player or my turn will be processed after.
if enemyTurnsLeft == 1 and ( myTurnsLeft > 1 or ( myTurnsLeft == 1 and (PlayerID > iPlayer or PlayerID == 0))) then
It is easy to see who will win if one is 1 turn from completion and the other is not. A lot more ambiguous when both are.
I know (or so I think) that the human player will always lose in this case, but I'm not sure how it is handled for everyone else.
I am assuming here that PlayerID is a number comprised between "0" (the human player) and the total number of CIVs at the start of the game and the same is true for iPlayer. And I'm assuming that whoever has the lower PlayerID will be winning the wonder race in case of a tie, but I can't be quite sure about that.
That apart at this point I would like to find a way to determine how much progress is lost at the time of the wonder race failure.
I know how to get the turns remaining, I could probably get the wonder base cost from Gameinfo, but how do I proceed from that?
I'm looking for suggestions.