Destroy a city by a script in 3.0?
Destroy a city by a script in 3.0?
Does anybody know a code to destroy a city by Lua prior to we have the city:remove() method? My own idea is, after deleting all units, we create an enemy military unit near the city (we don't know dipl rels, so finding an enemy is trial and error) and move it towards the city; if it succeeds and the city size shrinks, we delete the unit and create a similar unit of the previous owner, then cycle the conquest until the city is finally destroyed. I tried to do it in a script here but something goes wrong.
-
- Hardened
- Posts: 158
- Joined: Fri Jul 02, 2021 4:00 pm
Re: Destroy a city by a script in 3.0?
Code: Select all
function DestroyCity(x,y)
local owner, player, city, tile, nextTile, unit, unitType, BarbarianPlayer
tile=find.tile(x,y)
city=(tile):city()
if ( city == nil ) then
log.error("%s","DestroyCity: City not found on tile")
return
end
owner=(city).owner
for unit in (tile):units_iterate() do
edit.unit_kill(unit,"disbanded",owner)
end
edit.unleash_barbarians(tile)
for player in ( players_iterate() ) do
if ( ( (player).nation ) == ( find.nation_type("Barbarian") ) ) then
BarbarianPlayer=player
end
end
for h=-1,1,1 do
for v=-1,1,1 do
nextTile=find.tile((x+h),(y+v))
if ( (nextTile).terrain:class_name() == "Land" )
and ( nextTile ~= tile )
and ( (nextTile):city() == nil )
and ( (nextTile):num_units() >= 1 ) then
for unit in (nextTile):units_iterate() do
edit.unit_move(unit,tile,99)
end
end
end
end
-- If city is a 1x1 island, would require creating a boat with a Marine
unitType=find.unit_type("Leader")
edit.create_unit(BarbarianPlayer, tile, unitType, 3, nil, 3)
for unit in (tile):units_iterate() do
edit.unit_kill(unit,"disbanded",BarbarianPlayer)
end
end
Re: Destroy a city by a script in 3.0?
An excellent finding and masterpiece of hacking! Reallly, I have tried something like this when I needed but something was broken. The only consideration is that probably barbarians won't be properly unleashed before their onset turn.
Re: Destroy a city by a script in 3.0?
You likely are not concerned about it, but such creation and destruction of temporary units likely mess player's statistics, maybe also score.
-
- Hardened
- Posts: 158
- Joined: Fri Jul 02, 2021 4:00 pm
Re: Destroy a city by a script in 3.0?
^ I don't know what types of stats other players track, but according to the Freeciv Lua Reference manual at https://freeciv.fandom.com/wiki/Lua_ref ... ss_reasons , if the unit is killed with reason "disbanded" then neither the unit's player nor the killer's player scores are affected. And disbanded units don't appear as unit losses due to combat, at least they don't in Freeciv 2.6.4.
-
- Hardened
- Posts: 158
- Joined: Fri Jul 02, 2021 4:00 pm
Re: Destroy a city by a script in 3.0?
The onsetbarbs turn not yet having occurred doesn't prevent unleash barbarians via script, it''s just that they won't unleash automatically until that turn or after. But Lua can unleash them any time as long as the scenario file has barbarians set to "HUTS_ONLY" or better. In other words, the normal barbarian unleash / appearance will work as expected and the Lua script can still unleash before the onset turn.