I would like to share a script I made. It is a techdemo for KaM Remake version r6720 that demonstrates some possibilities of dynamic environmental changes, specifically:
1. Building walls.
3. Destroying and reconstructing a bridge.
I have attached the map here and you can also download the map from here: https://we.tl/t-h6M9BO3ySb
This map is meant to showcase some possibilities of scripting, not to provide a fun/challenging/balanced experience.
Note that if this map was properly balanced, in the phase after the bridge destruction, you would have to finish the bridge repairs before you run out of food because all food production buildings are on the other side of the river.
Other possible use of the ideas in this script Simulate high/low tide. This could be an interesting dynamic in multiplayer maps. Make the area of probable battlefield periodically flood, thus creating additional challenge for players to make sure they finish the battle quickly or they risk loosing troops caught up in a fight.
Note: to achieve nice transition between water and land during flooding, I created 6 versions of the map, each with different state of flooding. I then used a script to export tiles (type, rotation) from a rectangular block and hardcoded those tile parameters to final script. A nicer way would be to create an algorithm that creates transitions automatically, similar to how the brush tool works in map editor. During each stage of flooding, the entire block is redrawn. Possible optimization is to redraw only if new tile is different from current one.
For the bridge, I used a similar approach, but I have also exported the tile height. But I only used 2 maps: bridge intact and bridge completely destroyed. Because I dont need nice transitions between tiles, I didnt have to create 50+ maps for bridge destruction and 16 maps for bridge recontruction.
Limitations of the script
1. The code in script is not particulary robust (despite funcions being called "Robust..." ).
2. Wall building needs updates: You shouldnt be able to build a wall on a tile where a unit is standing. Handling of collisions with buildings is undefined at this point. Merging od adjacent walls doesnt work properly (placing walls in positions other than immediate left/right/top/bottom of existing wall tends to produce garbage). Placing the tiles with white bricks around the top of the wall needs work. These issues can be largely solved by improving the code in the script. For now, an easier solution is to only allow automatic building on predefined positions, similar to how reconstruction of the bridge is implemented. This makes it also easier to create ramps that allow soldiers to get on top of the walls. Also, currently it is possible to completely surround the village with walls, making it impossible for enemy to attack (another issue that can be easily solved by making it possible to build only on predefined places). This might be solvable within the script though.
3. It is impossible to destroy wine/fields. Thus, it is impossible to flood farms. This is a limitation of the current version of KaM Remake, not the script itself (at least I was unable to find functions to remove them). This is disappointing, because it makes it impossible to implement things like season changes: say you wanted to turn grass into snow (the kind on which you cannot build) and thus create challenge for the village to survive only on stored supplies for example. Same thing if you wanted to turn grass into desert.
4. During flooding and destroying the bridge, it is impossible to change the tile to a non-walkable (water) if a unit is currently occupying it. Therefore the unit must be killed first. However, the tile doesnt become "changable" untill the death animation finishes (setting the kill to silent doesnt seem to help). This is currently handled by trying to set the whole tile block again after around 2.5 seconds. A better solution would be to keep track of tiles that failed to change and keep tring to change only those tiles.
5. Setting the tiles should only destroy houses/kill units on that tile if that tile demands it (eg. the tile is water). This would require a list of all tiles which demand destruction. The script currently implements a simple workaround: it doesnt destroy only if the new tile is simple grass. This can be fixed by improving the script code.
6. Defeat conditions arent set and AI has only 1 basic attack specified.
If any of these limitations were removed in newer releases of KaM, please let me know.
Feel free to use this as a base for your own maps/scripts. If you'd let me know you used it though, that would be appreciated!