Buildings can be instantly constructed and destroyed with script. The commands are probably well known to scripters but some of their peculiarities might not be...
create_building
Syntax: console_command create_building {settlement_name} {building_level_name}
e.g. console_command create_building London town_watch
There are no rules here regarding what can be built. If the settlement already has such a building then this script still works: two of those buildings will now exist there. Clearly that is undesirable in typical situations. Also, it can be used to build "castle" class buildings in "city" class settlements and vice versa. Again, you would probably not wish to do that.
If your script is going to create a building then it would first need to check two things:
- That the building does not already exist in that settlement (unless your intention is to have two, of course).
- The class of the settlement - city or castle - to ensure that the correct tree is used. (Some building trees are for either/both - they have no "city" or "castle" qualifier - in which case the point is irrelevant.) And, of course, if the mod allows cities to convert to castles, and vice versa, then no assumption can be made by the script as to which class the settlement currently is. e.g. Want to build a barracks tree building in London? Is it currently a city ("barracks") or a castle ("castle_barracks") class? You'll need to find out with other script.
destroy_buildings
Syntax: destroy_buildings {faction_name} {building_tree_name} {true/false}
e.g. destroy_buildings england barracks true
This destroys all buildings of this building tree in the entire faction.
Sadly, there is no way to destroy a particular building/tree in just one settlement.
Actually, that's not quite true. One way would be to transfer ownership of the settlement to another faction (who does not have such a building/tree), use destroy_buildings on that other faction, then give the settlement back to the first faction. In the process it will kick out the occupant characters and units so in most cases it isn't a practical solution.
The above example will destroy the "barracks" tree (building levels: town_watch, town_guard, city_watch, militia_drill_square, militia_barracks, army_barracks & royal_armoury) in every settlement that england currently owns. It won't, of course, also destroy any buildings from the related castle tree - "castle_barracks" - so to destroy all barracks-type buildings in the faction destroy_buildings will need to be called twice: once with "barracks" and once with "castle_barracks".
The true/false indicates whether the faction should get money from the demolition(s), in the same way that the player gets money when using the "destroy building" button.
It is still safe to use this command when the faction does not currently have any of that tree's buildings constructed; no resulting CTD or log errors.
Hinterland buildings can be destroyed.
If a settlement has more than one building of that tree constructed then destroy_buildings will only destroy one of them. To destroy them all destroy_buildings must be called multiple times, i.e. twice if there are two buildings, three times if there are three, and so on. It is not possible for the player or AI to construct multiple buildings of the same tree in the same settlement; this situation could only arise via create_building or descr_strat.txt (I assume that descr_strat does not prevent you from giving a settlement multiple buildings of the same tree).
Recruitment
If the building being created or destroyed has any recruitment capabilities then there are some things to be aware of.
For example, say we have a building level named "my_building", in the building tree named "my_building_tree", and a recruitment capability of this:
Code:
recruit_pool "Peasants" 1 0.5 5 0
It has a starting unit count of 1; immediately upon normal construction it would make 1 unit of Peasants available for recruitment. This also applies to create_building. Every time "create_building my_building" is called it adds 1 Peasant to the current recruitment pool level in that settlement.
If the building is destroyed via destroy_buildings and then rebuilt using create_building then the pool resets - unless those two commands are used without any pause in between, i.e. in the same monitor without any wait, campaign_wait or while loop between them.
In this example the pool grows by 0.5 every turn. After 4 turns the pool will be 3: 1 from the initial build and 4 x 0.5 = 2 from the replenishment rate.
If at that time we did this...
Code:
monitor_event (whatever)
destroy_buildings england my_building_tree false
console_command create_building London my_building
end_monitor
... then in London that pool level of 3 will not be lost. (Although, it will now be 4 because create_building adds 1 as explained above. For this reason it might be best if such a building - one that we are destroying and rebuilding on a regular basis - did not have any starting pool level points.)
However, if we did this...
Code:
monitor_event (whatever)
destroy_buildings england my_building_tree false
campaign_wait 0.1
console_command create_building London my_building
end_monitor
...then the pool will be lost (reset back to 0, or 1 in this example again because of the create_building adding 1). Similarly if we did the destroy_buildings in one monitor and the create_building in another monitor (and therefore at a later time).
destroy_buildings also removes its recruit_pool units from the recruitment queue, even if create_building is used immediately after it. That is, unless some other building remains in that settlement that also provides recruitment of that unit.
In this example, if my_building was the only building in London that provided Peasants recruitment, and Peasants are currently in London's recruitment queue, then they will be automatically removed from the queue as soon as destroy_buildings is called. However, if Peasants are also provided by another building (e.g. barracks, which we are not destroying here) and that building exists in London then Peasants will not be removed from the queue.