Page 1 of 1

Killing a random citizen

PostPosted: 26 Mar 2014, 10:56
by Aasfresser
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

Re: Killing a random citizen

PostPosted: 26 Mar 2014, 12:13
by sado1
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.

Re: Killing a random citizen

PostPosted: 26 Mar 2014, 15:29
by Aasfresser
Thank you for the quick response will try it out in second! :lol:

Re: Killing a random citizen

PostPosted: 27 Mar 2014, 08:18
by Duke Valennius
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.

Re: Killing a random citizen

PostPosted: 27 Mar 2014, 13:28
by RandomLyrics
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.