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


Menu


It is currently Sat Sep 22, 2018 2:45 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Assigning ID to a vehicle automatically
PostPosted: Sat Aug 22, 2009 1:26 am 
Offline
Soldier level 0
Soldier level 0
User avatar

Joined: Thu Apr 02, 2009 4:29 pm
Posts: 29
Finally!
After days of researches we (Mc Benn[Thx a lot ;)] and me) have developed a function that automatically assigns an id to a vehicle ;D
The code isn't very complex, but what matters is that it works. The code:
Code:
Export vehicle1;
Export Function ComConstructWithID(fact, cha, eng, con, wea);
var temp_vehicle;
     Begin
         ComConstruct(fact, cha, eng, con, wea);

         repeat
         wait(0$1);
         until not IsOK(fact) or GetWorkingProgress(fact) = 0 and IsIdle(fact);
         while temp_vehicle=false do
              Begin
                   Wait(0$0.5);
                   temp_Vehicle=FilterAllUnits([[f_side,GetSide(fact)],[f_type,unit_vehicle],[f_dist,fact,4],[f_chassis,cha],[f_engine,eng],[f_control,con],[f_weapon,wea]])[1];
                   end;
          Result=temp_vehicle;
          temp_vehicle=0;
     end;

//in the code ->

vehicle1=ComConstructWithID(am_factory, us_light_wheeled, engine_combustion, control_manual, us_machine_gun);

Voila ;D You can use it when it's hard or maybe even impossible to find a specific vehicle using an event. Later I'll think about similiar AddCom function. Post ur suggestions and opinions ;)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 22, 2009 8:33 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
1) It can easily mistake itself. U think only this one veh will be 4 hexes from factory?
2) It halts execution of the rest of code. IMO that's clear for you. It waits until blah blah, so the next command won't be executed too fast.
3) It will hurt even more if the exit of factory is blocked. U'll get index out of bounds. (ofc if there is no other vehs around)

So at the end this function is a crap. Event was created for this purpose and should be used instead. There is always a way to reach the goal using it.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 22, 2009 9:55 am 
Offline
Soldier level 0
Soldier level 0
User avatar

Joined: Thu Apr 02, 2009 4:29 pm
Posts: 29
Mhm, good point. Later I'll post fixed code. You can easily calculate coordinates where the veh should stand, so u can check it too. I'm sure there's also a way u can check if the exit is blocked. We've made it that way because we couldn't export values from function, like:
Code:
export temp_id, factory, a, b ,c, function_running;
Export function xyz(vehid, factory, d, e, f);
begin
ComConstruct(factory, d, e, f);
function_running=true;
a=d;
b=e;
c=f;
temp_id=vehid;
end;

on vehicleconstructed(fac,veh) do
begin
if function_running=true and GetEngine(veh)=a and GetChassis(veh)=b ...
then temp_id=veh;
end;

otherwise it would be much simplier, safer and more certain; also it wouldn't cause contradiction with point(2). But in such a case exported parameters were just names of function parameters


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 22, 2009 10:05 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
Shouldn't you check only factory?
Well, I think u can build any vehicle in computer controlled base only by using SAIL so the rest is unnecessary or maybe I don't get what u rly need.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 22, 2009 1:01 pm 
Offline
Moderator
Moderator
User avatar

Joined: Mon Mar 17, 2008 6:03 pm
Posts: 1129
Location: Denmark
Corrected code in agreement with Radzio's 3 errors.

- Only checks the hex in the front of the factory for a vehicle of the specified construction. A brand new vehicle rolling out of the factory is apparently on this hex.
- (the halt issue) Just use it in an every-block and use a marked to activate the function.
- The function checks if the entrance is blocked. If it's a vehicle which blocks the entrance, the nearest idle unit will go inside that vehicle and thereby move it away.
- Checks if there is one and only one vehicle which fulfills the criteria before returning that vehicle.

Code:
Export Function ComConstructWithID(fact, cha, eng, con, wea);
     var x_shift, y_shift, info, idle_units, the_unit, temp_vehicle;
     Begin
         ComConstruct(fact, cha, eng, con, wea);

         repeat
         wait(0$1);
         until not IsOK(fact) or GetWorkingProgress(fact) = 0 and IsIdle(fact) or BuildingStatus(fact) = bs_waiting;

         Wait(0$4);
         if not IsOK(fact) then exit;

         x_shift = ShiftX(GetX(fact),GetDir(fact),4);
         y_shift = ShiftY(GetY(fact),GetDir(fact),4);

         if BuildingStatus(fact) = bs_waiting then
              begin
                   info = HexInfo(x_shift,y_shift);

                   if not (info > 0) then exit;
                   if GetType(info) = unit_building then exit;

                   idle_units = FilterAllUnits([[f_side,GetSide(fact)],[f_type,unit_human],[f_not,[f_hastask]]]);
                   if idle_units = 0 then exit;
                   the_unit = NearestUnitToUnit(idle_units,info);
                   ComEnterUnit(the_unit, info);

                   repeat
                   wait(0$1);
                   until not IsOK(info) or BuildingStatus(fact) <> bs_waiting;

                   if not IsOK(info) then exit;

                   Wait(0$2);
                   ComExitVehicle(the_unit);
              end;

         temp_Vehicle = FilterAllUnits([[f_side,GetSide(fact)],[f_type,unit_vehicle],[f_distxy,x_shift,y_shift,0],[f_chassis,cha],[f_engine,eng],[f_control,con],[f_weapon,wea]]);
         if temp_vehicle = 1 then Result=temp_vehicle[1]
         else result = 0;
     end;

USAGE
Code:
Every [time] [trigger / marked] do
     begin
          {the ID} = ComConstructWithID(...);
     end;



EDIT1: Minor correction - see Radio's post below


Last edited by McBenn on Sat Aug 22, 2009 3:36 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 22, 2009 2:59 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
Code:
         temp_Vehicle = FilterAllUnits([[f_side,GetSide(fact)],[f_type,unit_vehicle],[f_distxy,x_shift,y_shift,0],[f_chassis,cha],[f_engine,eng],[f_control,con],[f_weapon,wea]])[1];
         if temp_vehicle = 1 then Result=temp_vehicle

IMO this part doesn't check if there is "one and only one (blah, blah)" vehicle but if there is at least one. Well, if there isn't player will still get index out of bounds error.
Code:
                   idle_units = FilterAllUnits([[f_side,GetSide(fact)],[f_type,unit_human],[f_not,[f_hastask]]]);
                   if idle_units = 0 then exit;

So u think if there are no free units the code shall fail and only produce a vehicles without assigning him any ID? Ok, better stay away from this piece of code.

Still I don't get WHY event isn't enough...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 22, 2009 3:34 pm 
Offline
Moderator
Moderator
User avatar

Joined: Mon Mar 17, 2008 6:03 pm
Posts: 1129
Location: Denmark
Radzio wrote:
Code:
         temp_Vehicle = FilterAllUnits([[f_side,GetSide(fact)],[f_type,unit_vehicle],[f_distxy,x_shift,y_shift,0],[f_chassis,cha],[f_engine,eng],[f_control,con],[f_weapon,wea]])[1];
         if temp_vehicle = 1 then Result=temp_vehicle


IMO this part doesn't check if there is "one and only one (blah, blah)" vehicle but if there is at least one. Well, if there isn't player will still get index out of bounds error.


Ach damit forgot to correct that part. Thx :)

Radzio wrote:
So u think if there are no free units the code shall fail and only produce a vehicles without assigning him any ID?


I dunno what should happen if there are no free units - it's up to the user of the code to find out what is best in the situation.

radzio wrote:
Still I don't get WHY event isn't enough...


It is - I was just trying to correct the code so it looked most like the original one. If you want to use events would it be something like this?

Code:
Export vehicle1;
Export assign_id_please;
Export factory, chassis, engine, control, weapon;

//somewhere in Starting block
assign_id_please = false;

//somewhere in code
factory = {a factory};
chassis = {a chassis};
engine = {an engine};
control = {a control};
weapon = {a weapon};
ComConstruct(factory, chassis, engine, control, weapon);
assign_id_please = true;

On VehicleConstructed(veh, fact) do
     begin
          if GetChassis(veh) = chassis and GetEngine(veh) = engine and GetControl(veh) = control and GetWeapon(veh) = weapon and fact = factory and assign_id_please then
               begin
                    vehicle1 = veh;
                    assign_id_please = false;
               end;
     end;


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 22, 2009 3:43 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
Yup, it could be but I don't get why u check each part. U treat it as if CPU could build different car without ur permission xD


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 22, 2009 3:56 pm 
Offline
Moderator
Moderator
User avatar

Joined: Mon Mar 17, 2008 6:03 pm
Posts: 1129
Location: Denmark
Yeah ok, just thought other factories could be assigned to build other vehicles in the meantime. But even if it was so, you could restrict youself to "if assign_id_please and fact = factory then" :P

BTW: Does such events keep themselves alive without and "enable"? Do they just run every time the event is triggered? 'Cause then you have to be careful if you use the event more than once in your code :P


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 22, 2009 4:08 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
Let's try to read the main form:
Code:
on event do
// blah

So it will be executed ON (each) EVENT and will DO...(blah)
Basicaly it's like event handling by WinAPI. Each time u click on the button it does something. U aren't limited to one click per button xD


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 22, 2009 4:55 pm 
Offline
Moderator
Moderator
User avatar

Joined: Mon Mar 17, 2008 6:03 pm
Posts: 1129
Location: Denmark
Fine so it works like I thought - just had to be sure :wink:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 24, 2009 1:05 am 
Offline
Soldier level 0
Soldier level 0
User avatar

Joined: Thu Apr 02, 2009 4:29 pm
Posts: 29
McBenn: U can use only one specified event. If u use it more than once (e. g. 2x VehicleConstructed) u'll get 'event redeclared' error. But that one event can have infinite number of ifs and checks.

Radzio: The main purpose of this function is use in making campaign mods. It would really make modding easier for newbies. And I'm trying so stubbornly to make it work because I've met people who had problem with it.
Earlier we've written much safer code which refused to cooperate xP Just look at it:
Code:
Export veh_id, veh_fact, veh_chas, veh_engi, veh_cont, veh_weap, ComConstructWithID_is_running;

Export Function ComConstructWithID(id, fact, cha, eng, con, wea);
     Begin
         ComConstruct(fact, cha, eng, con, wea);
         veh_id = id;
         veh_fact = fact;
         veh_chas = cha;
         veh_engi = eng;
         veh_cont = con;
         veh_weap = wea;
         ComConstructWithID_is_running = true;
     end;

On VehicleConstructed(vehicle, factory) do
     begin
          if GetChassis(vehicle) = veh_chas and GetEngine(vehicle) = veh_engi and GetControl(vehicle) = veh_cont and GetWeapon(vehicle) = veh_weap and factory = veh_fact and ComConstructWithID_is_running then
               begin
                    veh_id = vehicle;
                    ComConstructWithID_is_running = false;
               end;
     end;

Export Function AddComConstructWithID(id, fact, cha, eng, con, wea);
Begin
     Repeat Wait(0$1); until ComConstructWithID_is_running = false;
          Begin
              ComConstruct(fact, cha, eng, con, wea);
              veh_id := id;
              veh_fact := fact;
              veh_chas := cha;
              veh_engi := eng;
              veh_cont := con;
              veh_weap := wea;
              ComConstructWithID_is_running = true;
          end;
end;

McBenn said that SAIL assigns 'veh_' variable text values of names of variables used in function so the code doesn't work. So we made diferent function (in the 1st post). At least it works and is in some way useful for beginning modders.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 24, 2009 3:46 am 
Offline
Moderator
Moderator
User avatar

Joined: Fri Jan 13, 2006 6:20 pm
Posts: 2325
Location: Argentina
An important acclaration: You CAN'T use an event TWICE!!!

Adding an event checker into a function is plain ridiculus for the past reason...

_________________
-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 Aug 24, 2009 9:05 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
IMO beginners don't need that. >.>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 24, 2009 1:30 pm 
Offline
OW Support Owner
OW Support Owner
User avatar

Joined: Wed Dec 28, 2005 11:13 pm
Posts: 5001
Location: UK, Scotland
People should use the best method from the start, they shouldn't be using Newb friendly code which isn't using SAIL properly. People should learn from the begining how to do things right. So that they don't need to re-learn how to do things when they want to do things right (And have less errors).

_________________
Image
Free Map Editor - Game Requirements - Stucuk.Net
-Stu


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 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:  
cron
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