Hard Light Productions Forums

Modding, Mission Design, and Coding => FS2 Open Coding - The Source Code Project (SCP) => Topic started by: TheTempest on August 22, 2007, 10:04:43 pm

Title: Update to the missile-lock targeting computer
Post by: TheTempest on August 22, 2007, 10:04:43 pm
Hello,

I felt that on the "hard" difficulty, that the missile-lock targeting computer was a little too slow to actually lock on so I made a variable threshold on how close the targeting lock has to be to the target (I found a 5 pixel threashold was pretty good) in order to lock.

Here's the code:
Code: [Select]
//HudLock.h

const float hud_lock_threashold_in_pixels = 5.0f;
bool hud_is_lock_within_threashold();
float distance_f(float x1, float y1, float x2, float y2);

//HudLock.cpp
bool hud_is_lock_within_threashold()
{
float dist;

dist = distance_f(Players[Player_num].lock_indicator_x, Players[Player_num].lock_indicator_y, Player->current_target_sx, Player->current_target_sy);

if(dist < hud_lock_threashold_in_pixels)
return true;
else
return false;
}

float distance_f(float x1, float y1, float x2, float y2)
{
float dist;

dist = sqrt(pow((x2 - x1), 2.0f) + pow((y2 - y1),2.0f));

return dist;
}

//replace this line:
if(Players[Player_num].lock_indicator_x == Player->current_target_sx) && (Players[Player_num].lock_indicator_y == Player->current_target_sy)  {

//with this one:
if ( hud_is_lock_within_threashold()) {


This makes it lock if the lock-target
Title: Re: New code if anyone wants it
Post by: jr2 on August 23, 2007, 04:02:57 am
Umm... :welcomeblue:
Title: Re: New code if anyone wants it
Post by: karajorma on August 23, 2007, 08:03:45 am
Nice to see you've wandered over here Tempest.

Have you thought about making the pixel width a value you can set in the ai_profiles table? The current change would probably change gameplay on FS2 a little (or a lot) but if you make it an ai_profiles option you can have the game use the old method by default and switch to the amount given by the modder in newer mods.
Title: Re: New code if anyone wants it
Post by: Inquisitor on August 23, 2007, 12:14:27 pm
You know, there is alot of code snippets that get posted here, but may not need to be in the main branch, it might be darn handy to have a place to keep those where they could be searched easily...
Title: Re: New code if anyone wants it
Post by: TheTempest on August 23, 2007, 03:20:11 pm
You know, there is alot of code snippets that get posted here, but may not need to be in the main branch, it might be darn handy to have a place to keep those where they could be searched easily...
Here here  :D

Nice to see you've wandered over here Tempest.

Have you thought about making the pixel width a value you can set in the ai_profiles table? The current change would probably change gameplay on FS2 a little (or a lot) but if you make it an ai_profiles option you can have the game use the old method by default and switch to the amount given by the modder in newer mods.

sure, that sounds like a really good idea.  I'll start looking into the ai_profile.

Thanks
Title: Re: New code if anyone wants it
Post by: TheTempest on August 23, 2007, 05:07:09 pm
Ok, I made the correct changes by adding a new float array into the ai_profiles structure and then put the code to load that variable from the ai_profiles table into memory.  As well as implemented the threshold by referencing this variable in the missile-lock code.  Note: Variable defaults to zero (ie. pixels must match identically (the original version))

//Hudlock.h
Code: [Select]
//append this code to the file just before the "#endif"

//08/20/07 - TheTempest -
bool hud_lock_is_within_threshold();

//helper fucntions
float distance_f(float x1, float y1, float x2, float y2);


//Hudlock.cpp
Code: [Select]
#include "freespace2/freespace.h"

//find this line:
if (Players[Player_num].lock_indicator_x == Player->current_target_sx) && (Players[Player_num].lock_indicator_y == Player->current_target_sy) {
//and replace it with this
if ( hud_lock_is_within_threshold()) {


//append this code to the end of the file
//==================================
//08/20/07 - TheTempest - is if the distance between the target and the lock indictor is within the given threshold, then return true.  Return false otherwise.
bool hud_lock_is_within_threshold()
{
float dist;

dist = distance_f(Players[Player_num].lock_indicator_x, Players[Player_num].lock_indicator_y, Player->current_target_sx, Player->current_target_sy);

//08/23/07 - TheTempest - Use the new ap_profile option and check the distance between the coordinates
if(dist < The_mission.ai_profile->missile_lock_threshold_in_pixels[Game_skill_level])
return true;
else
return false;
}

//08/20/07 - TheTempest - simple distance formula
float distance_f(float x1, float y1, float x2, float y2)
{
float dist;

dist = sqrt(pow((x2 - x1), 2.0f) + pow((y2 - y1),2.0f));

return dist;
}
//==================================

//ai_profiles.cpp
Code: [Select]
//find this line:
parse_float_list(profile->shield_manage_delay, NUM_SKILL_LEVELS);
//and add this just after that line

//08/23/07 - TheTempest - Add - Read pixel width threshold setup for missle lock from the ai_profile table
if (optional_string("$Missile Lock Threshold In Pixels:"))
parse_float_list(profile->missile_lock_threshold_in_pixels, NUM_SKILL_LEVELS);

//ai_profiles.h
Code: [Select]
//find this line
float shield_manage_delay[NUM_SKILL_LEVELS]; // how long before AI manages shields (note that the player's team always uses the average skill's
//and add this after it
float missile_lock_threshold_in_pixels[NUM_SKILL_LEVELS]; // max distance between target indicator and target position that the computer can still lock on (in pixels) - 08/23/07 - TheTempest