think you were trying to give me breadcrumbs when you posted the following, but I'm pretty sure a bird landed on my head, ate all the crumbs, and pecked half my brain out with it...
This is the code I was thinking of (not tested, so there may be some debugging to do). Note that you also need a function 'carrierOnTile' .
Code:
local text = require("text")
local function silentReArm(reArmUnit)
if alternateArmament[reArmUnit.type.id] and reArmUnit.moveSpent == 0 and carrierOnTile(reArmUnit.location) then
local newPlane = civ.createUnit(alternateArmament[reArmUnit.type.id],reArmUnit.owner,reArmUnit.location)
newPlane.homeCity = reArmUnit.homeCity
newPlane.moveSpent = newPlane.type.move
newPlane.attributes = reArmUnit.attributes -- this includes veteran status
civ.deleteUnit(reArmUnit)
end
end
local function textReArm(selectedUnit)
-- these are messages for if the unit can't re-arm
if not (alternateArmament[selectedUnit.type.id]) then
text.simple(selectedUnit.type.name.." units do not have an alternate munition.","Defense Minister")
return
elseif not carrierOnTile(selectedUnit.location) then
text.simple("Units can only be rearmed on a carrier.","Defense Minister")
return
elseif not (selectedUnit.moveSpent == 0 ) then
text.simple("Units can only be rearmed if they have full movement points.","Defense Minister")
return
end
-- this offers the choice of whether to re arm or not
local altUnitType = alternateArmament[selectedUnit.type.id]
local menuText = "Do you wish to change the armament of our "..selectedUnit.type.name.."? This will expend all movement points for the current turn."
local menuTable = { [1] = "Keep our "..selectedUnit.type.name.." as it is.",
[2] = "Change this "..selectedUnit.type.name.." unit to a "..altUnitType.name.." unit.",
[3] = "Change all "..selectedUnit.type.name.." units on this square to "..altUnitType.name.." units.",}
local choice = text.menu(menuTable,menuText,"Defense Minister")
-- execute the chosen outcome
if choice == 1 then
-- munition swap declined
return
elseif choice == 2 then
-- rearm the active unit only
silentReArm(selectedUnit)
return
elseif choice == 3 then
local convertType = selectedUnit.type
for plane in selectedUnit.location.units do
if plane.type == convertType then
-- if the plane can't be re-armed, the silentReArm check will catch it
silentReArm(plane)
end
end
return
end
end
If you want to use a different kind of table that's fine, but one of the advantages of using the key for information is that you don't have to use a loop to search the table for the entry you want. The table I suggested can also be expressed asalternateArmament={}
alternateArmament[unitAliases.kateBomb.id]=unitAliases.kateTorp
alternateArmament[unitAliases.kateTorp.id]=unitAliases.kateBomb
For whatever reason I just can't follow a table that looks like the above. I used a different style. This is as far as I've gotten tonight and I think my head is hurting too much to go further... At least it's a start.
Code:
alternateArmament = {
--[[KateBomb to KateTorp]] [unitAliases.kateBomb.id] = --[[unitSwappedTo]] unitAliases.kateTorp,
--[[KateTorp to KateBomb]] [unitAliases.kateTorp.id]= --[[unitSwappedTo]] unitAliases.kateBomb,
}