|
Post by Quintaxel on Aug 13, 2015 15:21:13 GMT 1
It's possible to calculate the distance between 2 vehicles without the Lua libraries using the pythagorean theorem. To do this you need a function to calculate the square root. As we cannot access the math library we will have to work arround it. Below you can find a function to calculate the square root.
You can use the function
local x,y = GetObjCoord (UnitScriptId); to get the x,y coordinates of each units. I guess you can figure out the rest, but if not let me know.
function sqrt(x)
--function sqrt() --Calculates the square of the x value --Input parameters: x integer value --Output parameters: returns the square of x --Calling protocol: sqrt(x);
-- Function sqrt() -- Calculate the square of the x value -- from the officialmap Secure_area.lua -- Input parameters : x integer value -- Output parameters: -- returns the square of x -- Callingprotocol : sqrt(x);
local ITNUM = 4; local sp =0 local i = ITNUM; local inv = 0; local a, b; if ( x<= 0) then return 0; end; if ( x< 1) then x = 1 /x; inv = 1; end; while ( x >16) do sp= sp+ 1; x = x /16; end; a= 2; -- Newtonian algorithm while (i > 0) do b = x /a; a = a +b; a = a *0.5; i = i -1; end; -- while ( sp > 0) do sp= sp- 1; a = a *4; end; if ( inv ==1) then a = 1 /a; end;
return a; end;
|
|
|
Post by Quintaxel on Aug 13, 2015 16:05:13 GMT 1
Here's a small script to calculate the distance between 2 units on the map. I did not take the effort to optimize the code. To test the script, just copy and paste the code below in a lua file linked to a map. Do not forget to copy the function sqrt(x) Put 2 units on the map with ScriptId 100 and 200. ------------------------- Startup Message ----------------------------------- DisplayTrace ("Calculation of distance between 2 units on the map"); DisplayTrace ("Map design & scripting by Quintaxel"); -----------------------------------------------------------------------------
function DistBetwUnits(unit1, unit2) local x1,y1 = GetObjCoord (unit1); local x2,y2 = GetObjCoord (unit2); local x= x1-x2; local y = y1-y2; local dist = sqrt ((x*x) + (y*y)); DisplayTrace (" x=%g anf y = %g", x,y) DisplayTrace ("Distance between Unit %g and Unit %g is %g",unit1,unit2,dist); Suicide (); end;
function Distance() DistBetwUnits (100,200); Suicide(); end;
function Init() RunScript ("Distance", 4000); end; ----Meta functions ----
-- add function sqrt( x) here. Hope this helps. Have fun
|
|
bb
General
Blitzkrieg junkie (tried to quit several times).
Posts: 1,362
|
Post by bb on Aug 14, 2015 1:31:39 GMT 1
Hi Che! How are you, buddy?
Well, a question to you scripters. This should be easy:
I want to give the same reinforcement several times in a random time (lets say, 15, 30, 40 minutes), how can I make it?
|
|
|
Post by Quintaxel on Aug 14, 2015 10:31:20 GMT 1
bb, do you want specific intervals (15,30,40) minutes or is any random time between a minimum time and maximum time ok ? If specific intervals is ok then the code below should do the trick. To try the code just create a map and put a Reinfocement group with ID 100 on it. Maybe there are other, better methods but this is the best solution I could come up with for now. Let me know if the code is useful. ---------------------------------- Startup Message --------------------------------------------- DisplayTrace ("Sending in reinforcements @ random intervals"); DisplayTrace ("Scripting by Quintaxel"); ------------------------------------------------------------------------------------------------
function RandTimeReinf() delay = {5, 10, 15, 20, 25}; -- Delay between reinforcements that will be randomly chosen in sec local r = RandomInt (5) + 1; -- Position of delay in the table scrpt{} local t = delay[r]*1000; -- Multiply by 60000 if the delay is in minutes. RunScript ("BB_Reinforce",t); -- Run the script Suicide(); end;
function BB_Reinforce() LandReinforcement (100); QCmd (0,100, 1300,1300); -- Just added this line so the reinf area does not get overcrowded ;-) RunScript ("RandomScript",3000); Suicide(); end;
function Init() RunScript ("RandTimeReinf",3000,1); -- Script only needs to run once end;
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Aug 14, 2015 14:10:53 GMT 1
This thread is very didactic.
I recommend it for all map makers.
A greeting.
|
|
bb
General
Blitzkrieg junkie (tried to quit several times).
Posts: 1,362
|
Post by bb on Aug 14, 2015 19:19:02 GMT 1
I'll try, thanks!
|
|
Che Guevara
Stariji vodnik
Scripting for fun.
Posts: 58
|
Post by Che Guevara on Aug 18, 2015 12:31:20 GMT 1
Quintaxel: For some maps I scripted some years ago I tried to use the pythagorean theorem to calculate the distance. The problem was that I didn't find an accurate way tot calculate the Squareroot of a number. I tried to use a metafunction from, similar to the one you posted, to calculate these. (Think I got it from the Mission Kursk addon) I found that they didn't calculate it very accuratly because they rounded up the numbers. Perhaps this meta function works better? bb: Nice to see you again. I'm good. How are you?
|
|
|
Post by Quintaxel on Aug 18, 2015 13:08:15 GMT 1
Che Guevara: The metafuntion for calcualting the hypotenuse can be made more accurate but in my experience it is accurate enough as it is, at least for BK. I do not know how well Lua can handle more complex calculation and better accuracy equals more iterations equals more time to calculate. So far I did not experience that Lua would slows down the game but then I have not seen complex calculations in the Lua scripts that are used in BK.
|
|
|
Post by Quintaxel on Aug 21, 2015 15:09:59 GMT 1
I'm looking for a method to queue custom made functions in Lua ? Let me explain what I mean. BK functions make it possible to put functions in a queue so these functions are carried out one after the other.
eg.
Cmd (0,100,500,600); --Move unit with ScriptId 100 to position 500,600 QCmd (0,100,1000,1000);-- Move unit with ScriptId 100 to position 1000,1000 QCmd (3,100,2000,2500);-- Move in assault mode the unit with ScriptId 100 to position 1000,1000
The BK engine will carry out the function Cmd (0,100,500,600) first. When this function is executed so when the unit with ScriptId 100 has reached position 500,600 on the map the next function in the queue being QCmd (0,100,1000,1000) will run to move the unit to the position 1000,1000.
So these functions must be queued somewhere. This is Probably doen in C+ but I need to do the same thing in BK Lua.
If we are to create new BK unctions then I think it will be necessary that these functions can be queued.
The BK Lua is a crippled version as it does not seem to include the functions for manipulation tables so this does not make thing easier. (eg.table.insert and table.remove do not work)
Another challenge is to invoke functions that are stored in a table ? Any hints or ideas are welcomed!
|
|
bb
General
Blitzkrieg junkie (tried to quit several times).
Posts: 1,362
|
Post by bb on Aug 23, 2015 19:50:01 GMT 1
hm... I'm not sure if this waht you want, but i learnt a very cool script in GZM campaign. Lets say, you want to send a group of units to attack some area first and after attack that area they should attack another area. But once you dont know how many units survived and also you cant to sincronize the time the units will spend to get in the are, you can write this script:
function ataques1() RunScript( "grupo204", 80000); RunScript( "grupo204a", 80000); Suicide(); end;
function grupo204() Cmd(3, 204, GetScriptAreaParams("204")); ---- the script group 204 you will attack the area 204 Suicide(); end; function grupo204a() --once all units get the area 204, it will be called a new script. The script calls the right number units surviving in the group. if(GetNUnitsInScriptGroup(204)==GetNScriptUnitsInArea(204,"204"))then ChangeFormation(204, 3); RunScript("area", 1000); Suicide(); end; end; function area() ------------------------------------ noe the group will attack the area 202a Cmd(3, 204, GetScriptAreaParams("202a")); Suicide(); end;
|
|
bb
General
Blitzkrieg junkie (tried to quit several times).
Posts: 1,362
|
Post by bb on Aug 23, 2015 20:33:11 GMT 1
BTW, if I want to produce an action when to scriptgroups are linking: when group 101 encounter group 102, linking two separate army groups, I want to give a reinforcement to the player, how can I do this?
|
|
|
Post by Quintaxel on Aug 23, 2015 21:32:10 GMT 1
bb Thanks for the reply. This is not what I'm looking for but it's an interesting script. I do not exaxtly understand your request. So if group 101 comes close to group 102 then you want reinforcements? Is this correct ? This should not be to difficult. I see what I can do this week.
|
|
bb
General
Blitzkrieg junkie (tried to quit several times).
Posts: 1,362
|
Post by bb on Aug 23, 2015 22:08:10 GMT 1
Yes, that's it. The idea is to produce an action (reinforcemnt) when two groups are linked (group 101 is close of group 102).
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Aug 23, 2015 23:17:05 GMT 1
This begins to like a lot.
I think I'm going to deepen the Lua code.
A greeting.
|
|
|
Post by Quintaxel on Aug 24, 2015 16:19:48 GMT 1
bb This is what you want, right ? I'll post the script later today or tomorrow.
|
|