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


Menu


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

All times are UTC [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Get away from that hex!
PostPosted: Mon Jul 27, 2009 2:02 pm 
Offline
Moderator
Moderator
User avatar

Joined: Mon Mar 17, 2008 6:03 pm
Posts: 1129
Location: Denmark
If your factory has constructed a vehicle, you want the vehicle to get away from the entrance of the factory. If a siberite bomb explodes in the middle of your base, people have to get out of there fast. And you certainly wanna keep your scientists, engineers and mechanics in safe distance of enemy soldiers/vehicles. All these things can be done with a simple ComMoveXY command, but it is often difficult to predict, where the units should go to get away as fast as possible. This function is the solution (I apologize if I sound like I have invented a remedy for all illnesses :wink: ).
Simply write which unit should get away from what hex and how far he/she/it should get away. The function then makes the unit pick the shortest way avoiding walking into environmental objects, other units or out of the map.

Code:
Export Function GetAwayFromXY(unit1, x, y, dist);
     var list_coords, a, min_dist, b, c, list_nearest, d, rand_num, rand_near_hex, e;
     begin
          if not IsOk(unit1) or dist = 1 or not ValidHex(x, y) then Exit;

          list_nearest = [];

          list_coords = [[x,y-dist],[x+dist,y],[x+dist,y+dist],[x,y+dist],[x-dist,y],[x-dist,y-dist]];
          a = 1;
          while a <= dist - 1 do
               begin
                    list_coords = Insert(list_coords, 1, [x+a,y-dist+a]);
                    list_coords = Insert(list_coords, 1, [x+dist, y+a]);
                    list_coords = Insert(list_coords, 1, [x+dist-a, y+dist]);
                    list_coords = Insert(list_coords, 1, [x-a, y+dist-a]);
                    list_coords = Insert(list_coords, 1, [x-dist, y-a]);
                    list_coords = Insert(list_coords, 1, [x-dist+a, y-dist]);
                    a = a + 1
               end;

          repeat
          if list_coords = 0 then exit;

          min_dist = 5000;
          for b in list_coords do
               if GetDistUnitXY(unit1, b[1], b[2]) < min_dist then min_dist = GetDistUnitXY(unit1, b[1], b[2]);

          c = 0;
          repeat
          c = c + 1;
          if GetDistUnitXY(unit1, list_coords[c][1], list_coords[c][2]) = min_dist then
               begin
                    list_nearest = Insert(list_nearest, 1, [list_coords[c][1], list_coords[c][2]]);
                    list_coords = Delete(list_coords, c);     
                    c = c - 1;
               end;
          until c = list_coords;

          Randomize;
          d = 0;
          repeat
          rand_num = Rand(1,list_nearest);
          rand_near_hex = list_nearest[rand_num];

          if ValidHex(rand_near_hex[1], rand_near_hex[2]) and HexInfo(rand_near_hex[1], rand_near_hex[2]) = 0 then
               begin
                    d = 1;
               end
          else list_nearest = Delete(list_nearest, rand_num);
          until d = 1 or list_nearest = 0 or not IsOK(unit1);

          until d = 1 or not IsOK(unit1);

          ComMoveXY(unit1, rand_near_hex[1], rand_near_hex[2]);
     end;



EDIT1: Fixed the initial part so the correct hexes will be put in list_coords


Last edited by McBenn on Thu Aug 13, 2009 10:33 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Get away from that hex!
PostPosted: Mon Jul 27, 2009 3:16 pm 
Offline
Moderator
Moderator
User avatar

Joined: Fri Jan 13, 2006 6:20 pm
Posts: 2325
Location: Argentina
Very nice, very nice. :D

A bit too much of code, by the way. I was like "my God!".

McBenn wrote:
The function then makes the unit pick the shortest way avoiding walking into environmental objects, other units or out of the map.


If I remember correctly, every Real Time Strategy game has pathfinding, unless you mean something special.

Good work!!

_________________
-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 27, 2009 3:50 pm 
Offline
Moderator
Moderator
User avatar

Joined: Mon Mar 17, 2008 6:03 pm
Posts: 1129
Location: Denmark
gwren1 wrote:
A bit too much of code, by the way. I was like "my God!".


Yeah ok ... but can't think of any better ways of doing it right now. And the function isn't slowing my machine (meaning it wouldn't slow any machine! :wink:).

gwren1 wrote:
McBenn wrote:
The function then makes the unit pick the shortest way avoiding walking into environmental objects, other units or out of the map.


If I remember correctly, every Real Time Strategy game has pathfinding, unless you mean something special.


Sorry if I didn't formulate it clear enough. I meant, that the unit will not pick a hex occupied by anything else, not even buildings (HexInfo makes sure of that).

And thanks for the appreciation :D


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

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