Question about scripting and mathematical functions.

Corpse

Mildly Dipped
Is there anyway to obtain the square root of a number in a script and how would you go about it?

My problem is I want to work out the distance travelled between 2 points A and B; A is on co-ordinates X0, Y0 and B is on Co-ordinates X4, Y2

So Distance(squared) = AC2 (4x4) + CB2 (2x2):

Anyone?
 
I don't think there is any support for it in the language, but why not write the algorithm yourself? Google for square root algorithm.
 
de dood said:
I don't think there is any support for it in the language, but why not write the algorithm yourself? Google for square root algorithm.

Thanks for the suggestion de dood, should have thought of it myself but I was too wasted (and still am).

I found an algorithm which should work; I'm going to try it out later but still would appreciate any suggestions you can come up with.
 
Here is a brute force algorithm: (the result is the real sqr with fractions dropped):

Code:
procedure someProc begin
    variable n; // set n to be the number whose root you want to compute
    variable result; // result will contain the root when we are done
    variable min; // temp variables
    variable max;
    variable mindiff;
    variable maxdiff; 
    
    n := random(1, 400); // test value
    display_msg("num: " + n);
    result := (n / 2);
    while ((result * result) - n > 0) do begin
        min := result - 1;
        max := result + 1;
        
        mindiff := (min * min) - n;
        maxdiff := (max * max) - n;
        
        if (mindiff < maxdiff) then
            result := min;
        else
            result := max;
    end
    
    display_msg("root " + result);
    // result is now the root of n
end
 
I forgot one thing. If n = 1, the sqrt(1) = 1. You need to explicitly check for that case. For all other numbers that algorithm is ok.
 
I discovered that the code leads to integer overflows for larger values. This should work better, although the results will be rounded up instead of down. It also fixes the problem with sqrt(1)

Code:
    result := 0;
    while (n - (result * result) > 0) do begin
        result := result + 1;
    end

edit: Messed up the code first time. Also, this algorithm is really bad, consider implementing something faster.

edit: swap result := 0 for result := 1.
 
Cool, I got it working.

I had to remember back to the time when I was in High School, and that was a bloody long time ago.

The script obtains your current worldmap position, then prompts you to input the destination co-ordinates from a dialog screen; it then calculates distance, bearing and converts the distance to miles; there is always an error margin as the engine doesn't seem to work with decimals.

Here is a preview of what I did for anyone who can use it.

procedure start;
procedure use_obj_on_p_proc;
procedure Start_Dlg;
procedure Set_Coordinates;
procedure set_dest_x;
procedure DX1000A;
procedure DX1000B;
procedure DX0100A;
procedure DX0100B;
procedure DX0010A;
procedure DX0010B;
procedure DX0001A;
procedure DX0001B;
procedure set_dest_y;
procedure DY1000A;
procedure DY1000B;
procedure DY0100A;
procedure DY0100B;
procedure DY0010A;
procedure DY0010B;
procedure DY0001A;
procedure DY0001B;
procedure calc_dis_x;
procedure calc_dis_y;
procedure set_hor_dir;
procedure set_ver_dir;
procedure pre_check;
procedure dis_str;
procedure Triangulate;
procedure Approximate_Value_G;
procedure calc_00;
procedure calc_01;
procedure Recalculate;
procedure Display_distance;
procedure Node_X;

variable home_x := 0;
variable home_y := 0;
variable dest_x := 0;
variable dest_y := 0;
variable hor_dis := 0;
variable ver_dis := 0;
variable hor_dir := 0;
variable ver_dir := 0;
variable msg_dirh := 0;
variable msg_dirv := 0;
variable b_ratio := 0;
variable angle_x := 0;
variable bearing := 0;
variable dis2 := 0;
variable val_g := 0;
variable calc_root := 0;
variable diff := 0;
variable distance := 0;
variable dist_miles := 0;

procedure start
begin
if (script_action == 7) then
call use_obj_on_p_proc;
end

procedure use_obj_on_p_proc
begin
script_overrides;
if (not combat_is_initialized) then
call Start_Dlg;
end

procedure Start_Dlg
begin
start_gdialog(66, self_obj, 4, -1, -1);
gsay_start;
call Set_Coordinates;
gsay_end;
end_dialogue;
end

procedure Set_Coordinates
begin
home_x := metarule(44, 0);
home_y := metarule(45, 0);
dest_x := 0;
dest_y := 0;
call set_dest_x;
end

procedure set_dest_x
begin
gsay_reply(66, "Destination X: " + dest_x + " Current World Map Position : X" + home_x + " , Y" + home_y + " Set Destination X position.");
giq_option(1, 66, "100+", DX0100A, 50);
giq_option(1, 66, "100-", DX0100B, 50);
giq_option(1, 66, "10+", DX0010A, 50);
giq_option(1, 66, "10-", DX0010B, 50);
giq_option(1, 66, "1+", DX0001A, 50);
giq_option(1, 66, "1-", DX0001B, 50);
giq_option(1, 66, "[DONE]", set_dest_y, 50);
end

procedure DX1000A
begin
dest_x := dest_x + 1000;
call set_dest_x;
end

procedure DX1000B
begin
if (dest_x >= 1000) then
dest_x := dest_x - 1000;
else
dest_x := 0;
call set_dest_x;
end

procedure DX0100A
begin
dest_x := dest_x + 100;
call set_dest_x;
end

procedure DX0100B
begin
if (dest_x >= 100) then
dest_x := dest_x - 100;
else
dest_x := 0;
call set_dest_x;
end

procedure DX0010A
begin
dest_x := dest_x + 10;
call set_dest_x;
end

procedure DX0010B
begin
if (dest_x >= 10) then
dest_x := dest_x - 10;
else
dest_x := 0;
call set_dest_x;
end

procedure DX0001A
begin
dest_x := dest_x + 1;
call set_dest_x;
end

procedure DX0001B
begin
if (dest_x >= 1) then
dest_x := dest_x - 1;
else
dest_x := 0;
call set_dest_x;
end

procedure set_dest_y
begin
gsay_reply(66, "Destination Y: " + dest_y + " Current World Map Position : X" + home_x + " , Y" + home_y + " Destination Co-ordinates : X" + dest_x + " , Y" + dest_y + " Set Destination Y position.");
giq_option(1, 66, "100+", DY0100A, 50);
giq_option(1, 66, "100-", DY0100B, 50);
giq_option(1, 66, "10+", DY0010A, 50);
giq_option(1, 66, "10-", DY0010B, 50);
giq_option(1, 66, "1+", DY0001A, 50);
giq_option(1, 66, "1-", DY0001B, 50);
giq_option(1, 66, "[DONE]", calc_dis_x, 50);
end

procedure DY1000A
begin
dest_y := dest_y + 1000;
call set_dest_y;
end

procedure DY1000B
begin
if (dest_y >= 1000) then
dest_y := dest_y - 1000;
else
dest_y := 0;
call set_dest_y;
end

procedure DY0100A
begin
dest_y := dest_y + 100;
call set_dest_y;
end

procedure DY0100B
begin
if (dest_y >= 100) then
dest_y := dest_y - 100;
else
dest_y := 0;
call set_dest_y;
end

procedure DY0010A
begin
dest_y := dest_y + 10;
call set_dest_y;
end

procedure DY0010B
begin
if (dest_y >= 10) then
dest_y := dest_y - 10;
else
dest_y := 0;
call set_dest_y;
end

procedure DY0001A
begin
dest_y := dest_y + 1;
call set_dest_y;
end

procedure DY0001B
begin
if (dest_y >= 1) then
dest_y := dest_y - 1;
else
dest_y := 0;
call set_dest_y;
end

procedure calc_dis_x
begin
if (home_x == dest_x) then
begin
hor_dis := 0;
hor_dir := 0;
end
else if (home_x < dest_x) then
begin
hor_dis := dest_x - home_x;
hor_dir := 1;
end
else if (home_x > dest_x) then
begin
hor_dis := home_x - dest_x;
hor_dir := 2;
end
call calc_dis_y;
end

procedure calc_dis_y
begin
if (home_y == dest_y) then
begin
ver_dis := 0;
ver_dir := 0;
end
else if (home_y < dest_y) then
begin
ver_dis := dest_y - home_y;
ver_dir := 1;
end
else if (home_y > dest_y) then
begin
ver_dis := home_y - dest_y;
ver_dir := 2;
end
call set_hor_dir;
end

procedure set_hor_dir
begin
if (hor_dir == 0) then
msg_dirh := "";
else if (hor_dir == 1) then
msg_dirh := "east";
else if (hor_dir == 2) then
msg_dirh := "west";
call set_ver_dir;
end

procedure set_ver_dir
begin
if (ver_dir == 0) then
msg_dirv := "";
else if (ver_dir == 1) then
msg_dirv := "south";
else if (ver_dir == 2) then
msg_dirv := "north";
call pre_check;
end

procedure pre_check
begin
if ((hor_dis == 0) or (ver_dis == 0)) then
call dis_str;
else
call Triangulate;
end

procedure dis_str
begin
distance := hor_dis + ver_dis;
dist_miles := (distance * 2222222) / 10000000;
gsay_reply(66, "Distance from current position: " + dist_miles + " miles " + msg_dirv + msg_dirh);
giq_option(1, 66, "[DONE]", Node_X, 50);
end

procedure Triangulate
begin
dis2 := (hor_dis * hor_dis) + (ver_dis * ver_dis);
b_ratio := 90000000 / (hor_dis + ver_dis);
if (ver_dis <= hor_dis) then
angle_x := b_ratio * ver_dis / 1000000;
else
angle_x := b_ratio * hor_dis / 1000000;
if ((hor_dir == 1) and (ver_dir == 2)) then
begin
if (ver_dis <= hor_dis) then
bearing := 90 - angle_x;
else
bearing := angle_x;
end
else if ((hor_dir == 1) and (ver_dir == 1)) then
begin
if (ver_dis <= hor_dis) then
bearing := 90 + angle_x;
else
bearing := 180 - angle_x;
end
else if ((hor_dir == 2) and (ver_dir == 1)) then
begin
if (ver_dis <= hor_dis) then
bearing := 270 - angle_x;
else
bearing := 180 + angle_x;
end
else if ((hor_dir == 2) and (ver_dir == 2)) then
begin
if (ver_dis <= hor_dis) then
bearing := 270 + angle_x;
else
bearing := 360 - angle_x;
end
call Approximate_Value_G;
end

procedure Approximate_Value_G
begin
if (dis2 < 4) then
val_g := 1;
else if ((dis2 >= 4) and (dis2 < 9)) then
val_g := 2;
else if ((dis2 >= 9) and (dis2 < 16)) then
val_g := 3;
else if ((dis2 >= 16) and (dis2 < 25)) then
val_g := 4;
else if ((dis2 >= 25) and (dis2 < 36)) then
val_g := 5;
else if ((dis2 >= 36) and (dis2 < 49)) then
val_g := 6;
else if ((dis2 >= 49) and (dis2 < 64)) then
val_g := 7;
else if ((dis2 >= 64) and (dis2 < 81)) then
val_g := 8;
else if ((dis2 >= 81) and (dis2 < 100)) then
val_g := 9;
else if ((dis2 >= 100) and (dis2 < 121)) then
val_g := 10;
else if ((dis2 >= 121) and (dis2 < 144)) then
val_g := 11;
else if ((dis2 >= 144) and (dis2 < 169)) then
val_g := 12;
else if ((dis2 >= 169) and (dis2 < 196)) then
val_g := 13;
else if ((dis2 >= 196) and (dis2 < 225)) then
val_g := 14;
else if ((dis2 >= 225) and (dis2 < 256)) then
val_g := 15;
else if ((dis2 >= 256) and (dis2 < 289)) then
val_g := 16;
else if ((dis2 >= 289) and (dis2 < 324)) then
val_g := 17;
else if ((dis2 >= 324) and (dis2 < 361)) then
val_g := 18;
else if ((dis2 >= 361) and (dis2 < 400)) then
val_g := 19;
else if (dis2 >= 400) then
val_g := 20;
call calc_00;
end

procedure calc_00
begin
calc_root := dis2 / val_g;
if (calc_root >= (val_g)) then
diff := calc_root - val_g;
if (calc_root < (val_g)) then
diff := val_g - calc_root;
call calc_01;
end

procedure calc_01
begin
if ((calc_root == val_g) or (diff <= 1)) then
call Display_distance;
else
call Recalculate;
end

procedure Recalculate
begin
diff := diff / 2;
if (calc_root < val_g) then
val_g := val_g - diff;
else if (calc_root > val_g) then
val_g := val_g + diff;
call calc_00;
end

procedure Display_distance
begin
distance := calc_root;
dist_miles := (distance * 2222222) / 10000000;
if (diff == 1) then
Msg_Str := "Distance from current position: " + dist_miles + " miles (" + distance + "/" + val_g + ") -- Bearing : " + bearing + " degrees " + msg_dirv + msg_dirh;
else
Msg_Str := "Distance from current position: " + dist_miles + " miles -- Bearing : " + bearing + " degrees " + msg_dirv + msg_dirh;
gsay_reply(66, Msg_Str);
giq_option(1, 66, "[DONE]", Node_X, 50);
end

procedure Node_X
begin
end

Now all I have to do is make preset co-ordinates for all the cities in the Fallout 2 map so they can be selected from a menu. This script will be usefull for new vehicles as you can add procedures to plot the trayectory in the worldmap, and script random encounters to happen along the route; you can also use the distance to work out time and fuel consumption.
 
Cool, I got the script to plot trajectory co-ordinates using a simple linear equation; here is the whole script for anyone interested.


#include "C:\Program Files\Black Isle\Fallout 2 Mapper\scripts\headers\define.h"
//#include "C:\Program Files\Black Isle\Fallout 2 Mapper\scripts\headers\<TownMapName.h>"

#define NAME SCRIPT_UNN //Script name from scripts.h

#include "C:\Program Files\Black Isle\Fallout 2 Mapper\scripts\headers\command.h"
#include "C:\Program Files\Black Isle\Fallout 2 Mapper\scripts\headers\ModReact.h"
#include "C:\Program Files\Black Isle\Fallout 2 Mapper\scripts\headers\partybkg.h"

procedure start;
procedure use_p_proc;
procedure Start_Dlg;
procedure Set_Coordinates;
procedure set_dest_x;
procedure DX0100A;
procedure DX0100B;
procedure DX0010A;
procedure DX0010B;
procedure DX0001A;
procedure DX0001B;
procedure set_dest_y;
procedure DY0100A;
procedure DY0100B;
procedure DY0010A;
procedure DY0010B;
procedure DY0001A;
procedure DY0001B;
procedure calc_dis_x;
procedure calc_dis_y;
procedure set_hor_dir;
procedure set_ver_dir;
procedure pre_check;
procedure dis_str;
procedure Triangulate;
procedure Approximate_Value_G;
procedure calc_00;
procedure calc_01;
procedure Recalculate;
procedure Display_distance;
procedure Plot_Coord;
procedure Nx_co_str;
procedure generate_linear_equation;
procedure calc_nxy00;
procedure calc_nxy01;
procedure disp_xy_coord;
procedure disp_coord;
procedure Node_X;

variable home_x := 0;
variable home_y := 0;
variable dest_x := 0;
variable dest_y := 0;
variable hor_dis := 0;
variable ver_dis := 0;
variable hor_dir := 0;
variable ver_dir := 0;
variable msg_dirh := 0;
variable msg_dirv := 0;
variable b_ratio := 0.000;
variable angle_x := 0.000;
variable bearing := 0.000;
variable dis2 := 0.000;
variable val_g := 0.000;
variable calc_root := 0.000;
variable diff := 0.000;
variable distance := 0.000;
variable dist_miles := 0.000;
variable gen_next_X := 0;
variable gen_next_Y := 0;
variable X := 0;
variable Y := 0;
variable TX := 0;
variable TY := 0;
variable rise := 0.000;
variable run := 0.000;
variable slope := 0.000;
variable slx_var := 0.000;
variable Z := 0;
variable Msg_Str := 0;

procedure start
begin
if (script_action == 6) then
call use_p_proc;
end

procedure use_p_proc
begin
script_overrides;
if (not combat_is_initialized) then
call Start_Dlg;
end

procedure Start_Dlg
begin
start_gdialog(66, target_obj, 4, -1, -1);
gsay_start;
call Set_Coordinates;
gsay_end;
end_dialogue;
end

procedure Set_Coordinates
begin
home_x := metarule(44, 0);
home_y := metarule(45, 0);
dest_x := 0;
dest_y := 0;
call set_dest_x;
end

procedure set_dest_x
begin
gsay_reply(961, "Destination X: " + dest_x + " Current World Map Position : X" + home_x + " , Y" + home_y + " Set Destination X position.");
giq_option(1, 961, "100+", DX0100A, 50);
giq_option(1, 961, "100-", DX0100B, 50);
giq_option(1, 961, "10+", DX0010A, 50);
giq_option(1, 961, "10-", DX0010B, 50);
giq_option(1, 961, "1+", DX0001A, 50);
giq_option(1, 961, "1-", DX0001B, 50);
giq_option(1, 961, "[DONE]", set_dest_y, 50);
end

procedure DX0100A
begin
dest_x := dest_x + 100;
call set_dest_x;
end

procedure DX0100B
begin
if (dest_x >= 100) then
dest_x := dest_x - 100;
else
dest_x := 0;
call set_dest_x;
end

procedure DX0010A
begin
dest_x := dest_x + 10;
call set_dest_x;
end

procedure DX0010B
begin
if (dest_x >= 10) then
dest_x := dest_x - 10;
else
dest_x := 0;
call set_dest_x;
end

procedure DX0001A
begin
dest_x := dest_x + 1;
call set_dest_x;
end

procedure DX0001B
begin
if (dest_x >= 1) then
dest_x := dest_x - 1;
else
dest_x := 0;
call set_dest_x;
end

procedure set_dest_y
begin
gsay_reply(961, "Destination Y: " + dest_y + " Current World Map Position : X" + home_x + " , Y" + home_y + " Destination Co-ordinates : X" + dest_x + " , Y" + dest_y + " Set Destination Y position.");
giq_option(1, 961, "100+", DY0100A, 50);
giq_option(1, 961, "100-", DY0100B, 50);
giq_option(1, 961, "10+", DY0010A, 50);
giq_option(1, 961, "10-", DY0010B, 50);
giq_option(1, 961, "1+", DY0001A, 50);
giq_option(1, 961, "1-", DY0001B, 50);
giq_option(1, 961, "[DONE]", calc_dis_x, 50);
end

procedure DY0100A
begin
dest_y := dest_y + 100;
call set_dest_y;
end

procedure DY0100B
begin
if (dest_y >= 100) then
dest_y := dest_y - 100;
else
dest_y := 0;
call set_dest_y;
end

procedure DY0010A
begin
dest_y := dest_y + 10;
call set_dest_y;
end

procedure DY0010B
begin
if (dest_y >= 10) then
dest_y := dest_y - 10;
else
dest_y := 0;
call set_dest_y;
end

procedure DY0001A
begin
dest_y := dest_y + 1;
call set_dest_y;
end

procedure DY0001B
begin
if (dest_y >= 1) then
dest_y := dest_y - 1;
else
dest_y := 0;
call set_dest_y;
end

procedure calc_dis_x
begin
if (home_x == dest_x) then
begin
hor_dis := 0;
hor_dir := 0;
end
else if (home_x < dest_x) then
begin
hor_dis := dest_x - home_x;
hor_dir := 1;
end
else if (home_x > dest_x) then
begin
hor_dis := home_x - dest_x;
hor_dir := 2;
end
call calc_dis_y;
end

procedure calc_dis_y
begin
if (home_y == dest_y) then
begin
ver_dis := 0;
ver_dir := 0;
end
else if (home_y < dest_y) then
begin
ver_dis := dest_y - home_y;
ver_dir := 1;
end
else if (home_y > dest_y) then
begin
ver_dis := home_y - dest_y;
ver_dir := 2;
end
call set_hor_dir;
end

procedure set_hor_dir
begin
if (hor_dir == 0) then
msg_dirh := "";
else if (hor_dir == 1) then
msg_dirh := "east";
else if (hor_dir == 2) then
msg_dirh := "west";
call set_ver_dir;
end

procedure set_ver_dir
begin
if (ver_dir == 0) then
msg_dirv := "";
else if (ver_dir == 1) then
msg_dirv := "south";
else if (ver_dir == 2) then
msg_dirv := "north";
call pre_check;
end

procedure pre_check
begin
speed := random(115, 149);
if ((hor_dis == 0) or (ver_dis == 0)) then
call dis_str;
else
call Triangulate;
end

procedure dis_str
begin
distance := hor_dis + ver_dis;
dist_miles := distance * 0.222;
gsay_reply(961, "Distance from current position: " + dist_miles + " miles " + msg_dirv + msg_dirh);
giq_option(1, 961, "Plot Trajectory", Plot_Coord, 50);
giq_option(1, 961, "[DONE]", Node_X, 50);
end

procedure Triangulate
begin
dis2 := 0.00 + (hor_dis * hor_dis) + (ver_dis * ver_dis);
b_ratio := 90.00 / (hor_dis + ver_dis);
if (ver_dis <= hor_dis) then
angle_x := 0.00 + b_ratio * ver_dis;
else
angle_x := 0.00 + b_ratio * hor_dis;
if ((hor_dir == 1) and (ver_dir == 2)) then
begin
if (ver_dis <= hor_dis) then
bearing := 90.00 - angle_x;
else
bearing := angle_x;
end
else if ((hor_dir == 1) and (ver_dir == 1)) then
begin
if (ver_dis <= hor_dis) then
bearing := 90.00 + angle_x;
else
bearing := 180.00 - angle_x;
end
else if ((hor_dir == 2) and (ver_dir == 1)) then
begin
if (ver_dis <= hor_dis) then
bearing := 270.00 - angle_x;
else
bearing := 180.00 + angle_x;
end
else if ((hor_dir == 2) and (ver_dir == 2)) then
begin
if (ver_dis <= hor_dis) then
bearing := 270.00 + angle_x;
else
bearing := 360.00 - angle_x;
end
call Approximate_Value_G;
end

procedure Approximate_Value_G
begin
if (dis2 < 4) then
val_g := 1.0;
else if ((dis2 >= 4) and (dis2 < 9)) then
val_g := 2.0;
else if ((dis2 >= 9) and (dis2 < 16)) then
val_g := 3.0;
else if ((dis2 >= 16) and (dis2 < 25)) then
val_g := 4.0;
else if ((dis2 >= 25) and (dis2 < 36)) then
val_g := 5.0;
else if ((dis2 >= 36) and (dis2 < 49)) then
val_g := 6.0;
else if ((dis2 >= 49) and (dis2 < 64)) then
val_g := 7.0;
else if ((dis2 >= 64) and (dis2 < 81)) then
val_g := 8.0;
else if ((dis2 >= 81) and (dis2 < 100)) then
val_g := 9.0;
else if ((dis2 >= 100) and (dis2 < 121)) then
val_g := 10.0;
else if ((dis2 >= 121) and (dis2 < 144)) then
val_g := 11.0;
else if ((dis2 >= 144) and (dis2 < 169)) then
val_g := 12.0;
else if ((dis2 >= 169) and (dis2 < 196)) then
val_g := 13.0;
else if ((dis2 >= 196) and (dis2 < 225)) then
val_g := 14.0;
else if ((dis2 >= 225) and (dis2 < 256)) then
val_g := 15.0;
else if ((dis2 >= 256) and (dis2 < 289)) then
val_g := 16.0;
else if ((dis2 >= 289) and (dis2 < 324)) then
val_g := 17.0;
else if ((dis2 >= 324) and (dis2 < 361)) then
val_g := 18.0;
else if ((dis2 >= 361) and (dis2 < 400)) then
val_g := 19.0;
else if (dis2 >= 400) then
val_g := 20.0;
call calc_00;
end

procedure calc_00
begin
calc_root := 0.000 + dis2 / val_g;
if (calc_root >= (val_g)) then
diff := calc_root - val_g;
if (calc_root < (val_g)) then
diff := val_g - calc_root;
call calc_01;
end

procedure calc_01
begin
if ((calc_root == val_g) or (diff <= 0.0001)) then
call Display_distance;
else
call Recalculate;
end

procedure Recalculate
begin
diff := diff / 2.000;
if (calc_root < val_g) then
val_g := val_g - diff;
else if (calc_root > val_g) then
val_g := val_g + diff;
call calc_00;
end

procedure Display_distance
begin
distance := 0.000 + calc_root;
dist_miles := distance * 0.222;
if (diff == 1) then
Msg_Str := "Distance from current position: " + dist_miles + " miles " + msg_dirv + msg_dirh + "(Hexes : " + distance + "/" + val_g + ") -- Bearing : " + bearing + " degrees";
else
Msg_Str := "Distance from current position: " + dist_miles + " miles, " + msg_dirv + msg_dirh + " -- (Hexes : " + distance + ") -- Bearing : " + bearing + " degrees";
gsay_reply(961, Msg_Str);
giq_option(1, 961, "Plot Trajectory", Plot_Coord, 50);
giq_option(1, 961, "[DONE]", Node_X, 50);
end

procedure Plot_Coord
begin
if ((home_x == dest_x) or (home_y == dest_y)) then
call Nx_co_str;
else
call generate_linear_equation;
end

procedure Nx_co_str
begin
if (home_x == dest_x) then
begin
gen_next_Y := random(0, ver_dis);
if (home_y < dest_y) then
Y := home_y + gen_next_Y;
else
Y := home_y - gen_next_Y;
display_msg("Next Co-ordinates : X" + home_x + ", Y" + Y);
end
else if (home_y == dest_y) then
begin
gen_next_X := random(0, hor_dis);
if (home_x < dest_x) then
X := home_x + gen_next_X;
else
X := home_x - gen_next_X;
display_msg("Next Co-ordinates : X" + X + ", Y" + home_y);
end
end

procedure generate_linear_equation
begin
gen_next_X := hor_dis;
rise := home_y - dest_y;
run := home_x - dest_x;
slope := (ver_dis * 10.00) / (hor_dis * 10.00);
X := home_x;
if (home_x > dest_x) then
slope := slope - (slope * 2);
slx_var := slope * X;
if (home_y < dest_y) then
Z := home_y - slx_var;
else if (home_y > dest_y) then
Z := home_y + slx_var;
gsay_reply(961, " Rise : " + rise + " Run : " + run + " Slope : " + slope + " Equation : Y = " + slope + "X (" + slx_var + ") + " + Z);
giq_option(1, 961, "Continue", calc_nxy00, 50);
giq_option(1, 961, "[DONE]", Node_X, 50);
end

procedure calc_nxy00
begin
if (gen_next_X > 0) then
call calc_nxy01;
else
call disp_coord;
end

procedure calc_nxy01
begin
if (home_x < dest_x) then
X := X + 1;
else if (home_x > dest_x) then
X := X - 1;
if (home_y < dest_y) then
begin
TY := slope * X;
Y := TY + Z;
end
else if (home_y > dest_y) then
begin
TX := slope * X;
TY := TX - Z;
Y := TY - (TY * 2);
end
call disp_xy_coord;
end

procedure disp_xy_coord
begin
gen_next_X := gen_next_X - 1;
gsay_reply(961, "X" + X + ", Y" + Y);
giq_option(1, 961, "Next co-ordinate", calc_nxy00, 50);
giq_option(1, 961, "[DONE]", Node_X, 50);
end

procedure disp_coord
begin
gsay_reply(961, "X" + X + ", Y" + Y);
giq_option(1, 961, "[DONE]", Node_X, 50);
end

procedure Node_X
begin
end
 
Ok, I have run into some problems: I added an option to go to the X, Y position displayed; the procedure loads a random encouter map, then uses the 'wm_area_set_pos" command to set the x, y position.

If I go to the final destination it works ok, because it uses the variable I set (dest_y) as final destination; if I try to go to any other co-ordinate in between however the game locks up. This is because variable 'Y' returns in decimal places and the 'wm_area_set_pos" command does not seem to like this.

Is there anyway of making this variable convert back to a whole number?

** Just thought of something that might work, going to test it but keep the suggestions coming anyway just in case it fails.

***It's working like a dream, it now loads a random desert encounter map and sets the wm xy position of that area to the desired co-ordinates.
 
Back
Top