
Post by Quintaxel on May 9, 2015 12:42:43 GMT 1
Interesting. I need to take a closer look at this code. Maybe I can code a funtion for calculating sin and cos. This would simplify things and opn some more possibilities I hope.



Post by Major Pain on May 9, 2015 14:07:47 GMT 1
Try the Sin and Cos functions and rule them out first. I'm certain I have seen them used before, perhaps the syntax needs something else. I'm still looking at this.
You can build a lookup table based on the angle to plot x2 and y 2, (it is actually an array).
You can lay all of it out in a spreadsheet like excel then transfer the data to the script.
The issue is going to be the number of arguments you would have. Consider 360 equations based on degrees not counting the decimals (at least steps of .25). I did not include the check for the <0 and > lower right corner, but you must have this to prevent 'division by zero' issues.



Post by Major Pain on May 9, 2015 15:31:26 GMT 1
I did some research on our formulas. We need to invoke the 'C' Code 'Math Library' to perform the math.
While Lua can usually perform the math, the C Math Library must be defined before the Lua Code is run the first time. In our case, the Math Library has not yet been declared. Lua by itself is unable to perform Higher Math Calculations, calculus/trigonometry. So we have to call the C Code into our Function.
So let's try this:
The Function:
Function RotateUnit()
x1, y1 = GetObjCoord(1000);
Angle1 = GetFrontDir(1000);
 We need to change the Degrees to Radians:  Radian1 = Angle1 (in Degrees) * (pi / 180) :::: Remember 2* Pi * Radius ::: 2^{,,}R
Radian1 = Angle1 * (pi /180)
Length = 2  This can be any value but should be high enough to obtain an accurate coordinate.
 In our Case, we will use [ math.cos(Radian1) ]
rx = Length * math.cos(Radian1);
ry = Length * math.sin(Radian1);
 We do not need to convert Radians back to degrees, or change the rx, ry.  In the event we get negative numbers, it represents one of the four quadrants of the grid.
Cmd(8, 1000, rx, ry);
suicide;
end;



Post by Quintaxel on May 10, 2015 8:32:03 GMT 1
...... So we have to call the C Code into our Function........ Major, you lost me here, sorry. Can we call the C code directly from the lua file Give me some time to check some things. "I'll be back"



Post by Major Pain on May 10, 2015 12:28:50 GMT 1
I've found several cases where the Script calls the C Code for Math Calculations.
So yes.
The syntax is: math.cos(R)
'math.' means it is calling the C mathematical tables.



Post by Quintaxel on May 10, 2015 14:41:29 GMT 1
I tried this script function CosCalc() DisplayTrace ("SinCalc = running");  This displays fine local c = math.cos(45); DisplayTrace ("The cosine of 90 = %g",c);  Does not work, value c is not displayed Suicide(); end; The funtion runs but no result for variable c ? These scripts you are refering to, are these BK Lua scripts ? Sorry if this sounds like a stupid question. I know the syntax is correct but I do not get it to work.



Post by Major Pain on May 10, 2015 20:47:02 GMT 1
Yes... the thing about LUA is you do not have to define the formulas and function for C. Since Lua is written in C, and BK was written in C and C+, the same Code be accessed by LUA by calling in Coroutines or Chunks.
The C and C+ languages already have the necessary math formulas defined. Unless the LUA programmer defines a Formula within Lua, which allows the C/C+ mathematical operations in LUA by defining an Expression, then you can still access the code within LUA to perform the operations.
An Expression might be defined in the front end of the LUA Code... and look as simple as a Letter or combination of letters and symbols: Common Expressions look like this: %A, #N. ^D.
Example: %A might be the Expression that defines the Function for Allowing a Random Number Generator. RandomInt(n)
So in our case... we must go to the base code to perform Expressions/Equations that have not been defined in the LUA table.
A Variable is a 'local' Variable, but you do not need to use local in the expression
function CosCalc()
DisplayTrace ("SinCalc = running");  This displays fine
A = 45 R = A * (math.pi /180)  We have to change the Angle Degrees to Radians
DisplayTrace ("The ANGLE (A) has been defined as: = %g",A);  You should see 45 DisplayTrace ("The RADIAN (R) has been defined as: = %g",R);  You should see .78540
C = math.cos(R);
DisplayTrace ("The COSINE of %g",A " Degrees = %g",C);  You should see .707 Suicide();
end;



Post by Quintaxel on May 10, 2015 22:14:32 GMT 1
I tried the script above but the values A and R still do not show.
The syntax math.cos(R); is recognized by the Lua editor and the script runs until the math funtion
R = A * (math.pi /180); is called.
It seems to me that all following funtions are ignored.
The function
Suicide();
should prevent the function CosCalc() to run more than once but it looks like CosCalc() runs everytime it's called by the Runscript. My conclusion is that the Suicide(); function did not run ?
function Init() looks like this.
function Init() RunScript ("CosCalc",3000); end;
Tomorrow I a have another look.



Post by Major Pain on May 11, 2015 20:37:52 GMT 1
It should be showing up in the display trace...
Display trace is there only for debugging purposes... but tells us if the Function is working.
So I'm looking for another syntax error after the first first display trace.
Add another display trace without any variables... at the end to see if the function is triggered through and not dumping.
One Error I found...
R = A * (math.pi /180)  < I left out the () on pi. pi() returns 3.1415926535898 / 3.14 could be used.
** We could replace this as:
R = A * (3.14/180) returns 0.78500
R = A * (3.1415/180) returns 0.78538... which is closer
R = A * (math.pi()/180) returns 0.78540
.0004 is not going to change the direction much.
But the Equation should be:
R = A * (math.pi()/180)



Post by Quintaxel on May 12, 2015 8:36:46 GMT 1
I’m still not convinced that we call functions from the math library in Lua although it sounds very logical because these libraries are also used to create the BK Lua functions. So far I’ve never seen a function being used from the Lua libraries in any of the map scripts I’ve seen and this make me a bit suspicious. I’ll do some more test and get back with more feedback. Just imagine that we could use the Lua libraries…..



Post by Quintaxel on Jun 12, 2015 13:06:17 GMT 1
I have been working on new functions for BK. The idea is to give scripters some more functions to use in their scripts. So far I did not succeed in calling a Lua file from the Lua map script. (I'm afraid this cannot be done without altering the game.exe but I'm not an authority on this) But,.... the functions could be added as meta functions to the lua file of a map script. The only concern I have is that Lua maybe not fast enough to do what I want, but we'll see. So far I have managed to create the following functions. SinCos (angle)Function returns sine and cosine of angle. These trigonometric functions are necessary to get better control of units on the map. RotateUnitToAngle (UnitScriptId,angle)Order unit with scripId to turn its front to angle (in degrees) WayPointTrail (Direction, IAction, ScriptId, WayPoint, TotWayPoints)Function to make a unit follow a trial or path on the map. More explanation can be found on my webpage, kindly hosted by my friend Boltz. I'm now thinking about/working on the following functions. Find out if a unit with ScripId is positioned within a certain circle with a given radius. This is simple to program and could be handy to find out the relative position from a unit to another. Make units swap positions. After they swapped positions the units will also look in the same direction of the unit they replace. Maybe I will add the option to dig in or not after the units swapped positions. A function to make 2 squads attack an armoured vehicle form the sides or the back. If anyone is interested, has any other ideas or thought then please let me know. You can post in this thread. I work on these function when I have time, which is not a lot, so things take their time.



Post by Quintaxel on Jun 16, 2015 11:33:21 GMT 1
Just finished another new Blitzkrieg Lua function
IsUnitInCircle (iScriptID, xc,yc,Radius)
This function checks if the ScriptId unit is within a circle with xc,yc coordinates and radius, Radius Input parameters: UnitId, x,y coordinates and radius of circle. Output parameters: 1 if unit is within circle, 0 if unit is outside circle 1 if the unit does not exist.
Is there anyone who has a list of global variables that are available in BK ?



Post by Quintaxel on Aug 13, 2015 12:39:21 GMT 1
Here's just another silly idea. I made this simple script to randomly run a function. Maybe it could be used to add some variation to a map. function RandomScript() scrpt = {"Alpha", "Beta", "Gamma", "Delta", "Upsilon"};  functions that will be randomly chosen local r = RandomInt (5);  Position of script in the table scrpt{} local t = RandomInt (4000) + 2000;  Delay before chosen script starts in 1/1000 second RunScript (scrpt[r],t);  Running the chosens script Suicide(); end;
function Alpha() DisplayTrace ("Script of function Alpha is running now"); Suicide(); end;
function Beta() DisplayTrace ("Script of function Beta is running now"); Suicide(); end;
function Gamma() DisplayTrace ("Script of function Gamma is running now"); Suicide(); end;
function Delta() DisplayTrace ("Script of function Delta is running now"); Suicide(); end;
function Upsilon() DisplayTrace ("Script of function Upsilon is running now"); Suicide(); end;



Che Guevara
Stariji vodnik
Scripting for fun.
Posts: 54

Post by Che Guevara on Aug 13, 2015 14:41:57 GMT 1
@major Pain: You mean there is a way to use higher math in the lua scripting for blitzkrieg? If so, that would open up many new features. One of the most I can think of is that you could actually calculate the distance between two units.

