Destroy a city by a script in 3.0?

Do you want to help out with Freeciv development? Then check out this forum.
Post Reply
Ignatus
Elite
Posts: 644
Joined: Mon Nov 06, 2017 12:05 pm
Location: St.Petersburg, Russia
Contact:

Destroy a city by a script in 3.0?

Post by Ignatus »

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.
Molo_Parko
Hardened
Posts: 158
Joined: Fri Jul 02, 2021 4:00 pm

Re: Destroy a city by a script in 3.0?

Post by Molo_Parko »

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
Works in Freeciv 2.6.4. Requires that the [scenario] setting "barbarians" be set to at least "HUTS_ONLY". Complications ensue if Barbarians already exist within the game -- they and all their cities are completely destroyed, but they are still able to return. Alternately the Barbarian tribe could be created manually (which doesn't require even the "HUTS_ONLY" setting, I think), and one Barbarian military unit created next to the town, move unit into town to capture, then create the Barbarian gameloss unit in the town, and destroy the gameloss unit to destroy all Barbarian tribe units and cities while also eliminating the tribe from the game. They can be brought-back repeatedly as needed for each DestroyCity use.
Ignatus
Elite
Posts: 644
Joined: Mon Nov 06, 2017 12:05 pm
Location: St.Petersburg, Russia
Contact:

Re: Destroy a city by a script in 3.0?

Post by Ignatus »

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.
cazfi
Elite
Posts: 3069
Joined: Tue Jan 29, 2013 6:54 pm

Re: Destroy a city by a script in 3.0?

Post by cazfi »

You likely are not concerned about it, but such creation and destruction of temporary units likely mess player's statistics, maybe also score.
Molo_Parko
Hardened
Posts: 158
Joined: Fri Jul 02, 2021 4:00 pm

Re: Destroy a city by a script in 3.0?

Post by Molo_Parko »

^ 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.
Molo_Parko
Hardened
Posts: 158
Joined: Fri Jul 02, 2021 4:00 pm

Re: Destroy a city by a script in 3.0?

Post by Molo_Parko »

Ignatus wrote: Mon Nov 27, 2023 8:29 pmThe only consideration is that probably barbarians won't be properly unleashed before their onset turn.
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.
Post Reply