Need help in LUA(giving units to enemy after city capture)

magzhi

Warlord
Joined
May 23, 2010
Messages
197
I need help for my mod. I want to make units to appear after someone captures city. For example: civ X captures city A which belong to civ Y. then civ Y receives some militia units in their closest city B. I already created militia units so only thing i need is to make them appear on map
 
You want the Players:InitUnit function, I think. This allows you to place a unit at a given X and Y value, with ownership belonging to a specified player. I don't have the syntax in front of me, but I use it in my own mod (Crazy Spatz, over in the Modpacks forum) to allow five kinds of later-era Barbarian units to spawn inside controlled territory. Give me six hours or so and I can post the code used. (I'm at work. Lunch hour.)
Since you'd already have the X and Y value of the captured city, it shouldn't be hard to do; all you'd need are the IDs of the unit types you want to spawn.

The hard part would be the trigger itself; you'd need an event that triggers whenever a city is captured, and not just the events that trigger only when a player can see it happening. The easiest way to get around this is a start-of-turn event:
Loop over all players.
Loop over all cities owned by that player.
If the city is in resistance/anarchy, generate a militia unit and assign it to the Barbarian faction.
(end loops)
This'd allow for the creation of these units, without the headache of trying to ensure an OnCityCaptured event fires correctly. The biggest downside would be that this'd trigger if you switched from Piety to Rationalism (one turn of anarchy for all cities!). So you'd probably want an N>1 check; a city only spawns militia if it has more than 1 turn of anarchy remaining.
 
check out the code of http://forums.civfanatics.com/showthread.php?t=396471. the improvements/techs/diplo can be deleted and then the civ stuff can be edited for exactly what you want. its been a while but iirc the only tricky part was to catch the event so it fires when a civ properly captures a city, as the API would fire on other triggers, not just captures, but the conditionals there handle all that ... last time i tried it anyway ;)

good luck
 
The hard part would be the trigger itself; you'd need an event that triggers whenever a city is captured, and not just the events that trigger only when a player can see it happening. The easiest way to get around this is a start-of-turn event:
Loop over all players.
Loop over all cities owned by that player.
If the city is in resistance/anarchy, generate a militia unit and assign it to the Barbarian faction.
(end loops)
This'd allow for the creation of these units, without the headache of trying to ensure an OnCityCaptured event fires correctly. The biggest downside would be that this'd trigger if you switched from Piety to Rationalism (one turn of anarchy for all cities!). So you'd probably want an N>1 check; a city only spawns militia if it has more than 1 turn of anarchy remaining.
There's a GameEvent for that... CityCaptureComplete. Not tried it myself, but the GameEvents in general fire whether or not the player can see the relevant tiles, and it is used by DLC scenarios.
 
True, but you'll notice that my suggested fix would work a bit differently. You wouldn't just get X units popping up when a city is captured, you'd get a unit popping up every turn the city was resisting, which at least forces you to keep part of your military near the newly-conquered city instead of just moving on to the next target in line. Using the CityCaptured type of events couldn't do that sort of thing without the save functions, but using a start-of-turn event instead would open up these other possibilities.

Sorry about not posting the code last night, I got stuck at work until really late. I'll try again tonight.
 
thx for replies. I will give it a try tonight, however my skills in LUA aren't that good
 
Back
Top Bottom