Goto https://www.stucuk.netGoto https://www.atlanticaonlinewiki.comGoto https://www.game-requirements.com


Menu


It is currently Sun Aug 19, 2018 1:37 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: To pick a random person from a list
PostPosted: Sat Jun 20, 2009 7:10 pm 
Offline
Moderator
Moderator
User avatar

Joined: Mon Mar 17, 2008 6:03 pm
Posts: 1129
Location: Denmark
Radzio: Viewer, please take a look down as discussion is still open.

Sometimes you just want a random person to say or do something instead of building up tons of 'if'-functions to ensure that only one person executes the command(s). A simple solution is creating a list of potential persons and let a function pick one randomly. This is what this function does.

Code:
Export Function PickRandomFromList(list);
     var temp_result;
     begin
          Randomize;
          if list <> 0 and UnitFilter(list,[f_alive]) > 0 then
               begin
                    repeat
                    temp_result = list[Rand(1,list)];
                    if temp_result = false then list = list diff temp_result;
                    until temp_result <> false;
                    result = temp_result;
               end
          else result = false;
     end;


The result is one person, which means that functions, which only accept 'integer', accept the result.


Last edited by McBenn on Tue Jul 21, 2009 11:41 pm, edited 3 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 17, 2009 2:09 pm 
Offline
Moderator
Moderator
User avatar

Joined: Mon Mar 17, 2008 6:03 pm
Posts: 1129
Location: Denmark
Updated - it's really simple now :D


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 17, 2009 3:34 pm 
Offline
Moderator
Moderator
User avatar

Joined: Fri Jan 13, 2006 6:20 pm
Posts: 2325
Location: Argentina
Excellent, we can learn from this now! :D

_________________
-Aleph.

THE Very active person

"You can only do well at something if you put the time in. Doesn't matter what subject it is. You can't make a master piece over night."
Wise man Stu-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 20, 2009 3:29 am 
Offline
Moderator
Moderator
User avatar

Joined: Mon Mar 17, 2008 6:03 pm
Posts: 1129
Location: Denmark
Updated again - discovered that editor doesn't like if the list is empty or if the function picks a dead character from the list.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 20, 2009 3:43 am 
Offline
Moderator
Moderator
User avatar

Joined: Fri Jan 13, 2006 6:20 pm
Posts: 2325
Location: Argentina
I can say its serious stuff now, really serious stuff. Good job.

_________________
-Aleph.

THE Very active person

"You can only do well at something if you put the time in. Doesn't matter what subject it is. You can't make a master piece over night."
Wise man Stu-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 20, 2009 12:06 pm 
Offline
Original-War.Net Owner
Original-War.Net Owner
User avatar

Joined: Wed Jan 25, 2006 10:21 am
Posts: 2786
Location: Poland, Białystok
Here is the much simpler version which will select any variable that is not empty (because the function's name states PickRandomFromList, not alive from list like u do):
Code:
export function PickRandomFromList(list);
var e_list,i;
  begin
    e_list=[];
    for i in list do
      if i <> 0 then e_list=e_list^i;
    if e_list > 0 then
        begin
          Randomize;
          result = e_list[Rand(1,e_list)];
        end
    else result = false;
  end;


PS: Why do u repeat until the value is different than false? It makes no sense..


Last edited by Radzio on Wed Jul 22, 2009 8:24 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 20, 2009 12:52 pm 
Offline
Moderator
Moderator
User avatar

Joined: Mon Mar 17, 2008 6:03 pm
Posts: 1129
Location: Denmark
Hmm..that's much better. I was only thinking of characters, not numbers and what else you can put in a list.

Radzio wrote:
PS: Why do u repeat until the value is different than false? It makes no sense..


Because I'm a noob :P No, apparently a dead or non-existing character has a value of false/0/nill (whatever you like) in a list, and we want the function to pick an existing character.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 20, 2009 5:36 pm 
Offline
Original-War.Net Owner
Original-War.Net Owner
User avatar

Joined: Wed Jan 25, 2006 10:21 am
Posts: 2786
Location: Poland, Białystok
Sorry, I was thinking about do..while xD Then it would make no sense but this's ok.
Anyway, if u want so u can check out this code. It has just one line changed (comparing to my previous) and therefore picks only characters (human, if u want some other then it's really easy to modify):
Code:
export function PickRandomCharacterFromList(list);
var e_list,i;
  begin
    e_list=[];
    for i in list do
      if GetType(i) = 1 then e_list=e_list^i; //HERE!
    if e_list > 0 then
        begin
          Randomize;
          result = e_list[Rand(1,e_list)];
        end
    else result = false;
  end;


If variable is not a proper unit then GetType will return 0 so if u want to pick any unit (human/vehicle/building; however I don't see why xD) simply modify the line to check if type is different than 0.


Last edited by Radzio on Wed Jul 22, 2009 8:24 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 21, 2009 1:29 pm 
Offline
Moderator
Moderator
User avatar

Joined: Mon Mar 17, 2008 6:03 pm
Posts: 1129
Location: Denmark
Interesting little function that GetType one :P but I think PickRandomFromList is better in your original version (where it simply picks a random value from a list). I presume that if people don't want to risk a vehicle to be picked, then they wouldn't put it into the list :wink: . So for the sake of simplicity: Let's stick to the function that actually does what the name states.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 21, 2009 11:55 pm 
Offline
Moderator
Moderator
User avatar

Joined: Mon Mar 17, 2008 6:03 pm
Posts: 1129
Location: Denmark
MAJOR PROBLEM - AND SOLUTION
Apparently I was wrong when I stated that a non-existing or dead character has a value of false/0/nill in a list. It has a value > 0 (depending on when the character was created I suppose). This really troubles the function (since you can't do anything with a dead or non-existing character) :!: But I have a solution :D A dead unit or a number makes the function GetType return false/0, and a number makes the function IsDead return true (a number is apparently dead :lol:) and the function IsPlaced return false (logically). And since predefined constants are perceived as numbers by functions, we have covered what you can possibly put into a list.
We want the function to pick a random value. If the value is a unit we want that unit to be alive and placed on the map. Here is the function:

Code:
Export Function PickRandomFromList(list);
     var e_list, i;
     begin
          Randomize;
          e_list = [];
          for i in list do
               if GetType(i) in [1,2,3] then e_list = e_list^i;
               if GetType(i) = 0 then
                    begin
                         if not IsDead(i) and IsPlaced(i) then e_list = e_list^i;
                         if IsDead(i) and not IsPlaced(i) then e_list = e_list^i;
                    end;
          if e_list > 0 then
               begin
                    result = e_list[Rand(1,e_list)];
               end
          else result = false;
     end;


Credit for the structure of the functions goes to Radzio, ofc :wink:
Focus on these two lines:

Code:
if not IsDead(i) and IsPlaced(i) then e_list = e_list^i;
if IsDead(i) and not IsPlaced(i) then e_list = e_list^i;


The first line makes sure that (living) units on the map are put into e_list.
The second line makes sure that numbers are put into e_list (since a number always is dead and never placed on the map).


And btw Radzio; you have forgotten to change the "list" to "e_list" in one place in your function. At "result = list[Rand(1,e_list)];".


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 22, 2009 8:45 am 
Offline
Original-War.Net Owner
Original-War.Net Owner
User avatar

Joined: Wed Jan 25, 2006 10:21 am
Posts: 2786
Location: Poland, Białystok
Ok :P
FIXED SOLUTION
This time I'm sure ur last code makes no sense.
Why do u check if it's dead/placed whatever if the first "if" is about GetType(xyz)=0? (despite even that this part is not put into the "for" loop block as there is no begin/end)
If GetType(un) returns 0 then un is no unit so both functions (IsDead/IsPlaced) will be simply confused as they work only with units.
BTW, there is 4th type of unit - materials. They also are treated as units.
I think that the best solution for this function is to allow only Placed and Alive units except materials. It would go like this (I also moved Randomize; because there is no point in randomizing values if rand will not be used):
Code:
Export Function PickRandomUnitFromList(list);
     var e_list, i;
     begin
          e_list = [];
          for i in list do
               if GetType(i) in [1,2,3] and IsOK(i) then e_list = e_list^i;
          if e_list > 0 then
               begin
                    Randomize;
                    result = e_list[Rand(1,e_list)];
               end
          else result = false;
     end;

And more flexible edition that allows to choose which types are allowed:
Code:
Export Function PickRandomUnitTypeFromList(list,types);
     var e_list, i;
     begin
          e_list = [];
          for i in list do
               if GetType(i) in types and IsOK(i) then e_list = e_list^i;
          if e_list > 0 then
               begin
                    Randomize;
                    result = e_list[Rand(1,e_list)];
               end
          else result = false;
     end;


EDIT: Fixed. Uses IsOK to check the unit.
EDIT2: Cosmetic, name changes.


Last edited by Radzio on Wed Jul 22, 2009 3:05 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 22, 2009 1:05 pm 
Offline
Moderator
Moderator
User avatar

Joined: Mon Mar 17, 2008 6:03 pm
Posts: 1129
Location: Denmark
Radzio wrote:
This time I'm sure ur last code makes no sense.


and this time you are completely right - I made that code 5 am in the morning and it makes no sense :?

Radzio wrote:
Why do u check if it's dead/placed whatever if the first "if" is about GetType(xyz)=0?


Because apparently dead units have type 0 (as well as numbers).

Radzio wrote:
despite even that this part is not put into the "for" loop block as there is no begin/end


That's my bad, I admit it, I'm guilty :cry:

Radzio wrote:
I also moved Randomize; because there is no point in randomizing values if rand will not be used


Good idea. I can see I have done that in some editions (I have many editions of that function by now :wink: ).

Radzio wrote:
I think that the best solution for this function is to allow only Placed and Alive units except materials.


Agreed, agreed indeed. I have really tried but there is NO WAY to differ a dead unit from a number :( . So I think we should split the function in two functions: One for units and one for numbers. The one for units is the one you present in your post (should be called PickRandomUnitFromList). The one for numbers should be like this:

Code:
Export Function PickRandomValueFromList(list);
     begin
          if list > 0 then
               begin
                    Randomize;
                    result = list[Rand(1,list)];
               end
          else result = false;
     end;


Your extended function with custom types should be named "PickRandomTypesFromList".

And by the way: It's not necessary to check both if the unit is alive and is placed. If the unit isn't placed, IsLive returns false.


And THANK YOU VERY MUCH RADZIO for helping with this :D :D :D


Radzio's edit: Sorry for sticking my nose into ur post but I changed function's name to PickRandomValueFromList - less confusing as it could pick any value (not only a number).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 22, 2009 3:11 pm 
Offline
Original-War.Net Owner
Original-War.Net Owner
User avatar

Joined: Wed Jan 25, 2006 10:21 am
Posts: 2786
Location: Poland, Białystok
Thanks, changed that to IsOK because I wanted to eliminate dying ppl. Anyway thanks for the advice.


You are welcome and.. thanks to you too. I like this kind of activity. Kind of brainstorming. Active brainstorming. Makes me more creative :D


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 22, 2009 3:14 pm 
Offline
Moderator
Moderator
User avatar

Joined: Fri Jan 13, 2006 6:20 pm
Posts: 2325
Location: Argentina
Brainstorming is a way of planning. PAWNED! :D

Very supportive, I must admit, Radzio. And good idea, splitted functions, since units are complex than numbers, and numbers aren't units. Logical. The best would have been saying: "If X is not an unit, then ignore".

_________________
-Aleph.

THE Very active person

"You can only do well at something if you put the time in. Doesn't matter what subject it is. You can't make a master piece over night."
Wise man Stu-


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 22, 2009 3:18 pm 
Offline
Original-War.Net Owner
Original-War.Net Owner
User avatar

Joined: Wed Jan 25, 2006 10:21 am
Posts: 2786
Location: Poland, Białystok
I called it active brainstorming because new ideas came immediately with their results. There was no real planning. If u state that this is planning then all my life would be planning :P


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

Original War is an RTS / RPG game by Altar Games
Original War Support is not part of Altar Games but is maintaining Original War for Altar Games

Privacy Policy