Gotta question, script gurus...

Gotta question, script gurus...

Discuss game development here, from a distinct game project to an accessible third-party mutator, down to the interaction and design of individual units if you like.

Moderator: Moderators

Post Reply
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Gotta question, script gurus...

Post by Argh »

Should be simple, but I'm stumped.

Is there any fast way to determine the distance to a target that a unit is currently aiming at?

If anybody could help me out on this, I would be much obliged :-)
User avatar
rattle
Damned Developer
Posts: 8278
Joined: 01 Jun 2006, 13:15

Post by rattle »

KDR's oil pump (or zwzsg's bonus giver) look nice for a start.

What you need would a fast reloading dummy weapon which is slaved to the weapon you'd like to know the target of. You could use the CheckForEmptier() function in the oil pump script but check if the unit is not allied.

You only need to find a way to compare the angle from the XZ distance (firepoint to the enemy unit) to the dummy weapon's heading. You'll need to get rid of the unit offset in the heading so the angle is global I think.
Comparing the distance value to the weapon range(*65536) the dummy weapon got could work too to get a little bit more accurate.

Perhaps with some small tolerance, I'm sure the angle values won't be the exactly the same.
User avatar
KDR_11k
Game Developer
Posts: 8293
Joined: 25 Jun 2006, 08:44

Post by KDR_11k »

I think you can use two guns and triangulate the position from the angle difference. Not sure how, though, since trigonometry is pretty much unsuppored by COB. I've heard that Zwzsg made a script that calculates the distance to the target.
User avatar
rattle
Damned Developer
Posts: 8278
Joined: 01 Jun 2006, 13:15

Post by rattle »

Hmm.. using or getting the angle from weapon range and heading to the north (or south) to compare against the angle of UNIT_XZ to the north (or south). Could work.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Post by zwzsg »

All the script know is the angle to target, not the distance to target. If you have two targets in the very same direction, one close, one far, the script is passed the same values.

But I made a script that calculate the point the unit is aiming at. Basically, I use a los weapon, I move the turret very up in the sky, then I follow the line that goes from the turret with the (heading,pitch) angles, and find where that line cross the ground surface, which gives me the target position. Since it would take too long to test all the points on the lines, I start by testing the distance 2,4,8,16,32,... etc.. up to around 2^31, until I find one point underground, then I use that (and the turret) as the first limits for a dicthomy to find the precise distance the ground is in few iterations.

It works very well in OTA, I used that script on units such as my sandworms, the M.W.H.G., the Stargate & Galactic Gate, the tic-tac-toe, the orbital bombardment, the weather control center, and probably some more. Some thanks go to Genghis Khan X who gave me the idea behind this (in this thread). With the time, I improved the script a bit, removing some bugs and adding some securities, so don't get a too early version. I think the very lastest version if the one on the Wormhole Prod Galactic gate, but there it's buried under a heavy mass of unrelated code, and it's not Spring-tested-and-fixed.

From the way it works, obviously it fails on plane: it would return the position of the place below the plane (actually not immediatly below but below in y and a tiny bit further in xz). Theorically if the map has very steep and high cliff it could fail, however the turret is moved so way up that the line of fire is so close to vertical that cliff are no issues. Also in Spring, since the turret center is used as the point of view for the first person mode, when you jump inside such a unit, you get a super bird view, more zoomed out than the max zoom out the camera allow.

Now for some script exerpt, the aiming of the space slug. That one was tested and fixed to work well in Spring AFAIK:
AimPrimary(heading, pitch)
{
if (biting_in_progress)
{
if(heading!=last_heading || ((!spring) && (pitch!=last_pitch)))//because in spring the pitch can change if terrain is dug
{
if ((can_barf) && (attack_mode==0))
{
attack_mode=1;

//var heading_minus_last_heading; // Spring don't like that being there, so this variable was made global
heading_minus_last_heading=heading - last_heading;
while (heading_minus_last_heading<0)
{heading_minus_last_heading=heading_minus_last_heading + 65536;}
while(heading_minus_last_heading>32767)
{heading_minus_last_heading=heading_minus_last_heading - 65536;}

if (heading_minus_last_heading < 0)
{
turn seg01 to y-axis last_heading - <10> speed barf_turn_speed;
}
if (heading_minus_last_heading > 0)
{
turn seg01 to y-axis last_heading + <10> speed barf_turn_speed;
}
last_heading=heading;
last_pitch=pitch;
}
}
return(FALSE);
}
if (already_aiming)
{
attack_mode=5;
return(FALSE);
}

if(heading==last_heading && (spring || pitch==last_pitch))//because in spring the pitch can change if terrain is dug
{return(FALSE);}

signal SIG_AIM1;
set-signal-mask SIG_AIM1;
already_aiming=TRUE;

// Script to determine the absolute xz position of target_finder_extremity point
// Works only for LOS weapon, and within range
// The idea is:
// - Put the turret center on a very up position
// - Wait till an aiming is done
// - Retrieve the heading and pitch
// - Turn the turret accordingly
// - Move a piece further and further folling the weapon LOS, until it goes underground
// - Use dichotomia inside the interval [turret, point underground] to find the exact limit where the piece moving along weapon LOS goes through ground
// - Use that position as xz

//don't forget that in the create there is a //move target_finder_center to y-axis [2000] now; // 200000 makes very weird things

var arm_length, arm_length_inc;


turn target_finder_center to y-axis heading now;
turn target_finder_center to x-axis (0 - pitch) now;
move target_finder_extremity to z-axis 0 now;
if(spring){sleep 1;}//override spring smoothing

arm_length=2;
while (get GROUND_HEIGHT(get PIECE_XZ(target_finder_extremity))<get PIECE_Y(target_finder_extremity))//while target_finder_extremity_point is over the ground
{
arm_length=arm_length*2;

// Lock Prevention
if (arm_length==1073741824)
{arm_length=1073741823;}
if (arm_length==2147483646)
{
while(1)
{
already_aiming=FALSE;
sleep 99999;
}
}
// Lock Prevention

move target_finder_extremity to z-axis arm_length now;move target_finder_extremity to z-axis arm_length speed 1;
if(spring){sleep 1;}//override spring smoothing
//wait-for-move target_finder_extremity along z-axis;
//explode target_finder_extremity type BITMAPONLY | BITMAP2;
}

arm_length_inc=arm_length;
while (arm_length_inc>0)
{
arm_length_inc=arm_length_inc/2;
move target_finder_extremity to z-axis (arm_length - arm_length_inc) now;
if(spring){sleep 1;}//override spring smoothing
//wait-for-move target_finder_extremity along z-axis;
//explode target_finder_extremity type BITMAPONLY | BITMAPNUKE;
if (get GROUND_HEIGHT(get PIECE_XZ(target_finder_extremity))>get PIECE_Y(target_finder_extremity))//if target_finder_extremity_point is underground
{
arm_length=arm_length - arm_length_inc;
}
}

chosen_point_xz=get PIECE_XZ(target_finder_extremity);

//explode target_finder_extremity type BITMAPONLY | BITMAP2;

letfirego=FALSE;
if (!biting_in_progress)
{
biting_in_progress=TRUE;
attack_mode=0;
last_heading=heading;
last_pitch=pitch;
call-script Bite(chosen_point_xz,get GROUND_HEIGHT(chosen_point_xz));
}//end if not already biting

while(!letfirego)
{
already_aiming=FALSE;
sleep 200;
}

return(TRUE);
}
Off course some part of it needs the rest of the bos, but pasting the whole 1300 lines here would only add confusion, and I don't remember if and where I have uploaded the lastest version of the space slug. But hopefully you're now a good enough scriptor to understand how to retrieve and adapt the part you need while leaving the rest.

It was impossible in TA, but in Spring there is the weapon slaving, which indeed maybe could be harnessed to find the target position by finding where two slaved weapons line of fire meet.
Post Reply

Return to “Game Development”