Map Database  •  FAQ  •  RSS  •  Login

Killing a random citizen

<<

Aasfresser

Farmer

Posts: 20

Joined: 22 Mar 2014, 14:01

KaM Skill Level: Skilled

Post 26 Mar 2014, 10:56

Killing a random citizen

I just started with dynamic scripting in Knights and Merchants. I had a few problems here and there and I had to make some comprimises but all in all my code is working, except one thing I cant figure out. I need to kill 20 random citizens of Player 1 every 10 game Minutes. I dont want to kill the units only citizens and I want to kill RANDOM citizens. If you have any ideas/ know how to do this I would really appreciate your help.

Thank you,
Aasfresser
<<

sado1

User avatar

Council Member

Posts: 1430

Joined: 21 May 2012, 19:13

KaM Skill Level: Skilled

Post 26 Mar 2014, 12:13

Re: Killing a random citizen

This code is written off my mind, I'm 90% sure it won't work without some changes.
  Code:
procedure KillARandomCitizen; var P: Integer; // P - player var Units: array of Integer; Units := States.PlayerGetAllUnits(P); //or just "0" instead of "P", if you want to get all units of player 1 (game counts locations from 0 :P) UnitToBeKilled := Units[ KaMRandomI( Length(Units) ) ]; //we assign one of the units to a variale here - we randomly choose the number in "[ ]", which is the number of the unit from that player if ( States.UnitType(UnitToBeKilled) < 14 ) then //we're checking for unit types < 14 - all military types have IDs greater than or equal to 14 Actions.UnitKill(UnitToBeKilled, False) //so if the unit is not a soldier, we kill it. Notice lack of ";" because we're before "else" else KillARandomCitizen; // if the unit was a soldier, we just try to kill another guy, until we find a citizen. This thing - using the function itself - is called recursion, you may want to google on that if you're interested ;)
Then just use "KillARandomCitizen;" each time you want to kill somebody.
<<

Aasfresser

Farmer

Posts: 20

Joined: 22 Mar 2014, 14:01

KaM Skill Level: Skilled

Post 26 Mar 2014, 15:29

Re: Killing a random citizen

Thank you for the quick response will try it out in second! :lol:
<<

Duke Valennius

User avatar

Militia

Posts: 44

Joined: 10 Jul 2013, 11:01

KaM Skill Level: Average

Post 27 Mar 2014, 08:18

Re: Killing a random citizen

This code is written off my mind, I'm 90% sure it won't work without some changes.
  Code:
recursive function
Then just use "KillARandomCitizen;" each time you want to kill somebody.
If you use this function, you may want to check if there is someone to be killed, otherwise, you will get stack overflow. And even if there is one unit to be killed, there may be hundred soldiers and it may take a while to randomly hit that one ("take a while" means enough to create visible lag). What you can do to avoid it is construct array of units that you can kill, and pick random out of those.
<<

RandomLyrics

User avatar

Sword Fighter

Posts: 298

Joined: 21 Jul 2013, 02:15

KaM Skill Level: Fair

Post 27 Mar 2014, 13:28

Re: Killing a random citizen

Yep it will freeze game if on the map will be not enough citizen units. Just make global var CITIZEN_COUNT then before use RandomKill, use function to count citizen, then use If CITIZEN_COUNT >= 20 then RandomKill.

Return to “Dynamic Scripting”

Who is online

Users browsing this forum: No registered users and 9 guests