1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
/*
* Copyright (C) Volition, Inc. 1999. All rights reserved.
*
* All source code herein is the property of Volition, Inc. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*/
#include "math/staticrand.h"
#include "network/multi.h"
#include "object/object.h"
#include "object/objectshield.h"
#include "ship/ship.h"
#include "ship/subsysdamage.h"
#include <limits.h>
float shield_get_strength(object *objp)
{
// no shield system, no strength!
if (objp->flags & OF_NO_SHIELDS)
return 0.0f;
int i;
float strength = 0.0f;
for (i = 0; i < MAX_SHIELD_SECTIONS; i++)
strength += shield_get_quad(objp, i);
return strength;
}
void shield_set_strength(object *objp, float strength)
{
int i;
for (i = 0; i < MAX_SHIELD_SECTIONS; i++)
shield_set_quad(objp, i, strength / MAX_SHIELD_SECTIONS);
}
// Recharge whole shield.
// Apply delta/MAX_SHIELD_SECTIONS to each shield section.
void shield_add_strength(object *objp, float delta)
{
// if we aren't going to change anything anyway then just bail
if (delta == 0.0f)
return;
if (!(Ai_info[Ships[objp->instance].ai_index].ai_profile_flags & AIPF_SMART_SHIELD_MANAGEMENT)
|| delta <= 0.0f) //SUSHI: We don't want smart shield management for negative delta
{
for (int i = 0; i < MAX_SHIELD_SECTIONS; i++)
shield_add_quad(objp, i, delta / MAX_SHIELD_SECTIONS);
}
else
{
float section_max = shield_get_max_quad(objp);
// smart shield repair
while (delta > 0.0f)
{
//WMC - Set to INT_MAX so that this is set to something
float weakest = i2fl(INT_MAX);
int weakest_idx = -1;
// find weakest shield quadrant
for (int i = 0; i < MAX_SHIELD_SECTIONS; i++)
{
float quad = shield_get_quad(objp, i);
if (weakest_idx < 0 || quad < weakest)
{
weakest = quad;
weakest_idx = i;
}
}
// all quads are at full strength
if (weakest >= section_max)
break;
// throw all possible shield power at this quadrant
// if there's any left over then apply it to the next weakest on the next pass
float xfer_amount;
if (weakest + delta > section_max)
xfer_amount = section_max - weakest;
else
xfer_amount = delta;
shield_add_quad(objp, weakest_idx, xfer_amount);
delta -= xfer_amount;
}
}
}
static double factor = 1.0 / (log(50.0) - log(1.0));
// Goober5000
float scale_quad(float generator_fraction, float quad_strength)
{
// the following formula makes a nice logarithmic curve between 1 and 50,
// when x goes from 0 to 100:
//
// ln(x) * (100 - 0)
// -----------------
// ln(50) - ln(1)
//
float effective_strength = quad_strength * ((float)log(generator_fraction) * (float)factor);
// ensure not negative, which may happen if the shield gets below 1 percent
// (since we're dealing with logs)
if (effective_strength < 0.0f)
return 0.0f;
else
return effective_strength;
}
// Goober5000
float shield_get_quad(object *objp, int quadrant_num)
{
// no shield system, no strength!
if (objp->flags & OF_NO_SHIELDS)
return 0.0f;
// check array bounds
Assert(quadrant_num >= 0 && quadrant_num < MAX_SHIELD_SECTIONS);
if (quadrant_num < 0 || quadrant_num >= MAX_SHIELD_SECTIONS)
return 0.0f;
if (objp->type != OBJ_SHIP && objp->type != OBJ_START)
return 0.0f;
//WMC - I removed SUBSYSTEM_SHIELD_GENERATOR to prevent pilot file
// corruption, so comment all this out...
/*
// yarr!
ship_subsys_info *ssip = &Ships[objp->instance].subsys_info[SUBSYSTEM_SHIELD_GENERATOR];
// do we have a shield generator?
if (ssip->num > 0 && !Fred_running)
{
// rules for shield generator affecting coverage:
// 1. if generator above 50%, effective strength = actual strength
// 2. if generator below 50%, effective strength uses the scale_quad formula
// 3. if generator below 30%, shields only have a sqrt(generator strength)
// chance of working, in addition to #2
float generator_fraction = ssip->current_hits / ssip->total_hits;
if (generator_fraction > MIN_SHIELDS_FOR_FULL_STRENGTH)
{
return objp->shield_quadrant[quadrant_num];
}
else if (generator_fraction > MIN_SHIELDS_FOR_FULL_COVERAGE)
{
return scale_quad(generator_fraction, objp->shield_quadrant[quadrant_num]);
}
else
{
// randomize according to this object and the current time
// (Missiontime >> 13 is eighths of a second)
float rand_num = static_randf(OBJ_INDEX(objp) ^ (Missiontime >> 13));
// maybe flicker the shield
if (rand_num < sqrt(generator_fraction))
return scale_quad(generator_fraction, objp->shield_quadrant[quadrant_num]);
else
return 0.0f;
}
}
// no shield generator, so behave as normal
else
*/
return objp->shield_quadrant[quadrant_num];
}
// Goober5000
void shield_set_quad(object *objp, int quadrant_num, float strength)
{
// check array bounds
Assert(quadrant_num >= 0 && quadrant_num < MAX_SHIELD_SECTIONS);
if (quadrant_num < 0 || quadrant_num >= MAX_SHIELD_SECTIONS)
return;
// check range
if (strength < 0.0f)
strength = 0.0f;
float max_quad = shield_get_max_quad(objp);
if (strength > max_quad)
strength = max_quad;
objp->shield_quadrant[quadrant_num] = strength;
}
// Goober5000
void shield_add_quad(object *objp, int quadrant_num, float delta)
{
// if we aren't going to change anything anyway then just bail
if (delta == 0.0f)
return;
// check array bounds
Assert(quadrant_num >= 0 && quadrant_num < MAX_SHIELD_SECTIONS);
if (quadrant_num < 0 || quadrant_num >= MAX_SHIELD_SECTIONS)
return;
// important: don't use shield_get_quad here
float strength = objp->shield_quadrant[quadrant_num] + delta;
// check range
if (strength < 0.0f)
strength = 0.0f;
float max_quad = shield_get_max_quad(objp);
if (strength > max_quad)
strength = max_quad;
objp->shield_quadrant[quadrant_num] = strength;
}
// Goober5000
float shield_get_max_strength(object *objp)
{
if (objp->type != OBJ_SHIP && objp->type != OBJ_START)
return 0.0f;
return Ships[objp->instance].ship_max_shield_strength;
}
void shield_set_max_strength(object *objp, float newmax)
{
if(objp->type != OBJ_SHIP)
return;
Ships[objp->instance].ship_max_shield_strength = newmax;
}
// Goober5000
float shield_get_max_quad(object *objp)
{
return shield_get_max_strength(objp) / MAX_SHIELD_SECTIONS;
}
// ***** This is the version that works on a quadrant basis.
// Return absolute amount of damage not applied.
float shield_apply_damage(object *objp, int quadrant_num, float damage)
{
float remaining_damage;
// multiplayer clients bail here if nodamage
// if(MULTIPLAYER_CLIENT && (Netgame.debug_flags & NETD_FLAG_CLIENT_NODAMAGE)){
if (MULTIPLAYER_CLIENT)
return damage;
// check array bounds
Assert(quadrant_num >= 0 && quadrant_num < MAX_SHIELD_SECTIONS);
if ((quadrant_num < 0) || (quadrant_num >= MAX_SHIELD_SECTIONS))
return damage;
if (objp->type != OBJ_SHIP && objp->type != OBJ_START)
return damage;
Ai_info[Ships[objp->instance].ai_index].last_hit_quadrant = quadrant_num;
remaining_damage = damage - shield_get_quad(objp, quadrant_num);
if (remaining_damage > 0.0f)
{
shield_set_quad(objp, quadrant_num, 0.0f);
return remaining_damage;
}
else
{
shield_add_quad(objp, quadrant_num, -damage);
return 0.0f;
}
}
// Returns true if the shield presents any opposition to something
// trying to force through it.
// If quadrant is -1, looks at entire shield, otherwise
// just one quadrant
int shield_is_up(object *objp, int quadrant_num)
{
if ((quadrant_num >= 0) && (quadrant_num < MAX_SHIELD_SECTIONS))
{
// Just check one quadrant
float quad = shield_get_quad(objp, quadrant_num);
if (quad > MAX(2.0f, 0.1f * shield_get_max_quad(objp)))
return 1;
}
else
{
// Check all quadrants
float strength = shield_get_strength(objp);
if (strength > MAX(2.0f * MAX_SHIELD_SECTIONS, 0.1f * shield_get_max_strength(objp)))
return 1;
}
return 0; // no shield strength
}
// return quadrant containing hit_pnt.
// \ 1 /.
// 3 \ / 0
// / \.
// / 2 \.
// Note: This is in the object's local reference frame. Do _not_ pass a vector in the world frame.
int shield_get_quadrant(vec3d *hit_pnt)
{
int result = 0;
if (hit_pnt->xyz.x < hit_pnt->xyz.z)
result |= 1;
if (hit_pnt->xyz.x < -hit_pnt->xyz.z)
result |= 2;
return result;
}
// Given a global point and an object, get the quadrant number the point belongs to.
int shield_get_quadrant_global(object *objp, vec3d *global_pos)
{
vec3d tpos;
vec3d rotpos;
vm_vec_sub(&tpos, global_pos, &objp->pos);
vm_vec_rotate(&rotpos, &tpos, &objp->orient);
return shield_get_quadrant(&rotpos);
}
|