Cleanup of inertial_thrust(), and some comments

git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@2522 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
avolkov
2006-11-23 16:25:31 +00:00
parent 9f33841318
commit 30e4761d07
2 changed files with 56 additions and 51 deletions
+50 -51
View File
@@ -42,9 +42,13 @@ animation_preprocess (PELEMENT ElementPtr)
} }
} }
UWORD UWORD
inertial_thrust (ELEMENTPTR ElementPtr) inertial_thrust (ELEMENTPTR ElementPtr)
{ {
#define MAX_ALLOWED_SPEED WORLD_TO_VELOCITY (DISPLAY_TO_WORLD (18))
#define MAX_ALLOWED_SPEED_SQR ((DWORD)MAX_ALLOWED_SPEED * MAX_ALLOWED_SPEED)
COUNT CurrentAngle, TravelAngle; COUNT CurrentAngle, TravelAngle;
COUNT max_thrust, thrust_increment; COUNT max_thrust, thrust_increment;
VELOCITYPTR VelocityPtr; VELOCITYPTR VelocityPtr;
@@ -54,84 +58,79 @@ inertial_thrust (ELEMENTPTR ElementPtr)
GetElementStarShip (ElementPtr, &StarShipPtr); GetElementStarShip (ElementPtr, &StarShipPtr);
CurrentAngle = FACING_TO_ANGLE (StarShipPtr->ShipFacing); CurrentAngle = FACING_TO_ANGLE (StarShipPtr->ShipFacing);
TravelAngle = GetVelocityTravelAngle (VelocityPtr);
thrust_increment = StarShipPtr->RaceDescPtr->characteristics.thrust_increment; thrust_increment = StarShipPtr->RaceDescPtr->characteristics.thrust_increment;
max_thrust = StarShipPtr->RaceDescPtr->characteristics.max_thrust; max_thrust = StarShipPtr->RaceDescPtr->characteristics.max_thrust;
if (thrust_increment == max_thrust) if (thrust_increment == max_thrust)
{ { // inertialess acceleration (Skiff)
SetVelocityVector (VelocityPtr, SetVelocityVector (VelocityPtr,
max_thrust, StarShipPtr->ShipFacing); max_thrust, StarShipPtr->ShipFacing);
return (SHIP_AT_MAX_SPEED); return (SHIP_AT_MAX_SPEED);
} }
else if ((TravelAngle = else if (TravelAngle == CurrentAngle
GetVelocityTravelAngle (VelocityPtr)) == CurrentAngle
&& (StarShipPtr->cur_status_flags && (StarShipPtr->cur_status_flags
& (SHIP_AT_MAX_SPEED | SHIP_BEYOND_MAX_SPEED)) & (SHIP_AT_MAX_SPEED | SHIP_BEYOND_MAX_SPEED))
&& !(StarShipPtr->cur_status_flags & SHIP_IN_GRAVITY_WELL)) && !(StarShipPtr->cur_status_flags & SHIP_IN_GRAVITY_WELL))
{ // already maxed-out acceleration
return (StarShipPtr->cur_status_flags return (StarShipPtr->cur_status_flags
& (SHIP_AT_MAX_SPEED | SHIP_BEYOND_MAX_SPEED)); & (SHIP_AT_MAX_SPEED | SHIP_BEYOND_MAX_SPEED));
}
else else
{ {
SIZE delta_x, delta_y; SIZE delta_x, delta_y;
SIZE cur_delta_x, cur_delta_y; SIZE cur_delta_x, cur_delta_y;
DWORD desired_speed, max_speed; DWORD desired_speed, max_speed;
DWORD current_speed;
thrust_increment = WORLD_TO_VELOCITY (thrust_increment); thrust_increment = WORLD_TO_VELOCITY (thrust_increment);
GetCurrentVelocityComponents (VelocityPtr, &cur_delta_x, &cur_delta_y); GetCurrentVelocityComponents (VelocityPtr, &cur_delta_x, &cur_delta_y);
delta_x = cur_delta_x current_speed = VelocitySquared (cur_delta_x, cur_delta_y);
+ COSINE (CurrentAngle, thrust_increment); delta_x = cur_delta_x + COSINE (CurrentAngle, thrust_increment);
delta_y = cur_delta_y delta_y = cur_delta_y + SINE (CurrentAngle, thrust_increment);
+ SINE (CurrentAngle, thrust_increment); desired_speed = VelocitySquared (delta_x, delta_y);
desired_speed = (DWORD)((long)delta_x * delta_x) max_speed = VelocitySquared (WORLD_TO_VELOCITY (max_thrust), 0);
+ (DWORD)((long)delta_y * delta_y);
max_speed = (DWORD)WORLD_TO_VELOCITY (max_thrust)
* WORLD_TO_VELOCITY (max_thrust);
if (desired_speed <= max_speed) if (desired_speed <= max_speed)
{ // normal acceleration
SetVelocityComponents (VelocityPtr, delta_x, delta_y); SetVelocityComponents (VelocityPtr, delta_x, delta_y);
}
else if (((StarShipPtr->cur_status_flags & SHIP_IN_GRAVITY_WELL)
&& desired_speed <= MAX_ALLOWED_SPEED_SQR)
|| desired_speed < current_speed)
{ // acceleration in a gravity well within max allowed
// deceleration after gravity whip
SetVelocityComponents (VelocityPtr, delta_x, delta_y);
return (SHIP_AT_MAX_SPEED | SHIP_BEYOND_MAX_SPEED);
}
else if (TravelAngle == CurrentAngle)
{ // normal max acceleration, same vector
if (current_speed <= max_speed)
SetVelocityVector (VelocityPtr, max_thrust,
StarShipPtr->ShipFacing);
return (SHIP_AT_MAX_SPEED);
}
else else
{ { // maxed-out acceleration at an angle to current travel vector
#define MAX_ALLOWED_SPEED WORLD_TO_VELOCITY (DISPLAY_TO_WORLD (18)) // thrusting at an angle while at max velocity only changes
DWORD current_speed; // the travel vector, but does not really change the velocity
if (((StarShipPtr->cur_status_flags & SHIP_IN_GRAVITY_WELL) VELOCITY_DESC v = *VelocityPtr;
&& desired_speed <=
(DWORD)MAX_ALLOWED_SPEED * (DWORD)MAX_ALLOWED_SPEED) DeltaVelocityComponents (&v,
|| (current_speed = COSINE (CurrentAngle, thrust_increment >> 1)
(DWORD)((long)cur_delta_x * (long)cur_delta_x) - COSINE (TravelAngle, thrust_increment),
+ (DWORD)((long)cur_delta_y * (long)cur_delta_y)) > desired_speed) SINE (CurrentAngle, thrust_increment >> 1)
- SINE (TravelAngle, thrust_increment));
GetCurrentVelocityComponents (&v, &cur_delta_x, &cur_delta_y);
desired_speed = VelocitySquared (cur_delta_x, cur_delta_y);
if (desired_speed > max_speed)
{ {
SetVelocityComponents (VelocityPtr, delta_x, delta_y); if (desired_speed < current_speed)
*VelocityPtr = v;
return (SHIP_AT_MAX_SPEED | SHIP_BEYOND_MAX_SPEED); return (SHIP_AT_MAX_SPEED | SHIP_BEYOND_MAX_SPEED);
} }
else if (TravelAngle == CurrentAngle)
{
if (current_speed <= max_speed)
SetVelocityVector (VelocityPtr,
max_thrust, StarShipPtr->ShipFacing);
return (SHIP_AT_MAX_SPEED);
}
else
{
VELOCITY_DESC v;
v = *VelocityPtr; *VelocityPtr = v;
DeltaVelocityComponents (&v,
COSINE (CurrentAngle, thrust_increment >> 1)
- COSINE (TravelAngle, thrust_increment),
SINE (CurrentAngle, thrust_increment >> 1)
- SINE (TravelAngle, thrust_increment));
GetCurrentVelocityComponents (&v, &cur_delta_x, &cur_delta_y);
desired_speed = (long)cur_delta_x * (long)cur_delta_x
+ (long)cur_delta_y * (long)cur_delta_y;
if (desired_speed > max_speed)
{
if (desired_speed < current_speed)
*VelocityPtr = v;
return (SHIP_AT_MAX_SPEED | SHIP_BEYOND_MAX_SPEED);
}
*VelocityPtr = v;
}
} }
return (0); return (0);
+6
View File
@@ -47,6 +47,12 @@ extern void SetVelocityComponents (VELOCITYPTR velocityptr, SIZE dx, SIZE
extern void DeltaVelocityComponents (VELOCITYPTR velocityptr, SIZE dx, extern void DeltaVelocityComponents (VELOCITYPTR velocityptr, SIZE dx,
SIZE dy); SIZE dy);
static inline DWORD
VelocitySquared (SIZE dx, SIZE dy)
{
return (DWORD)((long)dx * dx + (long)dy * dy);
}
#define VELOCITY_SHIFT 5 #define VELOCITY_SHIFT 5
#define VELOCITY_SCALE (1<<VELOCITY_SHIFT) #define VELOCITY_SCALE (1<<VELOCITY_SHIFT)