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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
|
#include "physics_info.h"
#include "vecmath.h"
#include "math/vecmat.h"
#include "ship/shiphit.h"
namespace scripting {
namespace api {
physics_info_h::physics_info_h()
: objh(), pi(nullptr)
{}
physics_info_h::physics_info_h(object* objp)
: objh(objp), pi(nullptr)
{
if (objh.isValid())
pi = &objh.objp()->phys_info;
}
physics_info_h::physics_info_h(physics_info* in_pi)
: objh(), pi(in_pi)
{}
bool physics_info_h::isValid() const
{
return pi != nullptr && objh.isValid();
}
ADE_OBJ(l_Physics, physics_info_h, "physics", "Physics handle");
ADE_VIRTVAR(AfterburnerAccelerationTime, l_Physics, "number", "Afterburner acceleration time", "number", "Afterburner acceleration time, or 0 if handle is invalid")
{
physics_info_h *pih;
float f = 0.0f;
if(!ade_get_args(L, "o|f", l_Physics.GetPtr(&pih), &f))
return ade_set_error(L, "f", 0.0f);
if(!pih->isValid())
return ade_set_error(L, "f", 0.0f);
if(ADE_SETTING_VAR) {
pih->pi->afterburner_forward_accel_time_const = f;
}
return ade_set_args(L, "f", pih->pi->afterburner_forward_accel_time_const);
}
ADE_VIRTVAR(AfterburnerVelocityMax, l_Physics, "vector", "Afterburner max velocity (Local vector)", "vector", "Afterburner max velocity, or null vector if handle is invalid")
{
physics_info_h *pih;
vec3d *v3=NULL;
if(!ade_get_args(L, "o|o", l_Physics.GetPtr(&pih), l_Vector.GetPtr(&v3)))
return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
if(!pih->isValid())
return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
if(ADE_SETTING_VAR && v3 != NULL) {
pih->pi->afterburner_max_vel = *v3;
}
return ade_set_args(L, "o", l_Vector.Set(pih->pi->afterburner_max_vel));
}
ADE_VIRTVAR(BankingConstant, l_Physics, "number", "Banking constant", "number", "Banking constant, or 0 if handle is invalid")
{
physics_info_h *pih;
float f = 0.0f;
if(!ade_get_args(L, "o|f", l_Physics.GetPtr(&pih), &f))
return ade_set_error(L, "f", 0.0f);
if(!pih->isValid())
return ade_set_error(L, "f", 0.0f);
if(ADE_SETTING_VAR) {
pih->pi->delta_bank_const = f;
}
return ade_set_args(L, "f", pih->pi->delta_bank_const);
}
ADE_VIRTVAR(ForwardAccelerationTime, l_Physics, "number", "Forward acceleration time", "number", "Forward acceleration time, or 0 if handle is invalid")
{
physics_info_h *pih;
float f = 0.0f;
if(!ade_get_args(L, "o|f", l_Physics.GetPtr(&pih), &f))
return ade_set_error(L, "f", 0.0f);
if(!pih->isValid())
return ade_set_error(L, "f", 0.0f);
if(ADE_SETTING_VAR) {
pih->pi->forward_accel_time_const = f;
}
return ade_set_args(L, "f", pih->pi->forward_accel_time_const);
}
ADE_VIRTVAR(ForwardDecelerationTime, l_Physics, "number", "Forward deceleration time", "number", "Forward deceleration time, or 0 if handle is invalid")
{
physics_info_h *pih;
float f = 0.0f;
if(!ade_get_args(L, "o|f", l_Physics.GetPtr(&pih), &f))
return ade_set_error(L, "f", 0.0f);
if(!pih->isValid())
return ade_set_error(L, "f", 0.0f);
if(ADE_SETTING_VAR) {
pih->pi->forward_decel_time_const = f;
}
return ade_set_args(L, "f", pih->pi->forward_decel_time_const);
}
ADE_VIRTVAR(ForwardThrust, l_Physics, "number", "Forward thrust amount (-1 - 1), used primarily for thruster graphics and does not affect any physical behavior", "number", "Forward thrust, or 0 if handle is invalid")
{
physics_info_h *pih;
float f = 0.0f;
if(!ade_get_args(L, "o|f", l_Physics.GetPtr(&pih), &f))
return ade_set_error(L, "f", 0.0f);
if(!pih->isValid())
return ade_set_error(L, "f", 0.0f);
if(ADE_SETTING_VAR) {
pih->pi->linear_thrust.xyz.z = f;
}
return ade_set_args(L, "f", pih->pi->linear_thrust.xyz.z);
}
ADE_VIRTVAR(Mass, l_Physics, "number", "Object mass", "number", "Object mass, or 0 if handle is invalid")
{
physics_info_h *pih;
float f = 0.0f;
if(!ade_get_args(L, "o|f", l_Physics.GetPtr(&pih), &f))
return ade_set_error(L, "f", 0.0f);
if(!pih->isValid())
return ade_set_error(L, "f", 0.0f);
if(ADE_SETTING_VAR) {
pih->pi->mass = f;
}
return ade_set_args(L, "f", pih->pi->mass);
}
ADE_VIRTVAR(RotationalVelocity, l_Physics, "vector", "Rotational velocity (Local vector)", "vector", "Rotational velocity, or null vector if handle is invalid")
{
physics_info_h *pih;
vec3d *v3=NULL;
if(!ade_get_args(L, "o|o", l_Physics.GetPtr(&pih), l_Vector.GetPtr(&v3)))
return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
if(!pih->isValid())
return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
if(ADE_SETTING_VAR && v3 != NULL) {
pih->pi->rotvel = *v3;
}
return ade_set_args(L, "o", l_Vector.Set(pih->pi->rotvel));
}
ADE_VIRTVAR(RotationalVelocityDamping, l_Physics, "number", "Rotational damping, ie derivative of rotational speed", "number", "Rotational damping, or 0 if handle is invalid")
{
physics_info_h *pih;
float f = 0.0f;
if(!ade_get_args(L, "o|f", l_Physics.GetPtr(&pih), &f))
return ade_set_error(L, "f", 0.0f);
if(!pih->isValid())
return ade_set_error(L, "f", 0.0f);
if(ADE_SETTING_VAR) {
pih->pi->rotdamp = f;
}
return ade_set_args(L, "f", pih->pi->rotdamp);
}
ADE_VIRTVAR(RotationalVelocityDesired, l_Physics, "vector", "Desired rotational velocity", "vector", "Desired rotational velocity, or null vector if handle is invalid")
{
physics_info_h *pih;
vec3d *v3=NULL;
if(!ade_get_args(L, "o|o", l_Physics.GetPtr(&pih), l_Vector.GetPtr(&v3)))
return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
if(!pih->isValid())
return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
if(ADE_SETTING_VAR && v3 != NULL) {
pih->pi->desired_rotvel = *v3;
}
return ade_set_args(L, "o", l_Vector.Set(pih->pi->desired_rotvel));
}
ADE_VIRTVAR(RotationalVelocityMax, l_Physics, "vector", "Maximum rotational velocity (Local vector)", "vector", "Maximum rotational velocity, or null vector if handle is invalid")
{
physics_info_h *pih;
vec3d *v3=NULL;
if(!ade_get_args(L, "o|o", l_Physics.GetPtr(&pih), l_Vector.GetPtr(&v3)))
return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
if(!pih->isValid())
return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
if(ADE_SETTING_VAR && v3 != NULL) {
pih->pi->max_rotvel = *v3;
}
return ade_set_args(L, "o", l_Vector.Set(pih->pi->max_rotvel));
}
ADE_VIRTVAR(ShockwaveShakeAmplitude, l_Physics, "number", "How much shaking from shockwaves is applied to object", "number", "Shockwave shake amplitude, or 0 if handle is invalid")
{
physics_info_h *pih;
float f = 0.0f;
if(!ade_get_args(L, "o|f", l_Physics.GetPtr(&pih), &f))
return ade_set_error(L, "f", 0.0f);
if(!pih->isValid())
return ade_set_error(L, "f", 0.0f);
if(ADE_SETTING_VAR) {
pih->pi->shockwave_shake_amp = f;
}
return ade_set_args(L, "f", pih->pi->shockwave_shake_amp);
}
ADE_VIRTVAR(SideThrust, l_Physics, "number", "Side thrust amount (-1 - 1), used primarily for thruster graphics and does not affect any physical behavior", "number", "Side thrust amount, or 0 if handle is invalid")
{
physics_info_h *pih;
float f = 0.0f;
if(!ade_get_args(L, "o|f", l_Physics.GetPtr(&pih), &f))
return ade_set_error(L, "f", 0.0f);
if(!pih->isValid())
return ade_set_error(L, "f", 0.0f);
if(ADE_SETTING_VAR) {
pih->pi->linear_thrust.xyz.x = f;
}
return ade_set_args(L, "f", pih->pi->linear_thrust.xyz.x);
}
ADE_VIRTVAR(SlideAccelerationTime, l_Physics, "number", "Time to accelerate to maximum slide velocity", "number", "Sliding acceleration time, or 0 if handle is invalid")
{
physics_info_h *pih;
float f = 0.0f;
if(!ade_get_args(L, "o|f", l_Physics.GetPtr(&pih), &f))
return ade_set_error(L, "f", 0.0f);
if(!pih->isValid())
return ade_set_error(L, "f", 0.0f);
if(ADE_SETTING_VAR) {
pih->pi->slide_accel_time_const = f;
}
return ade_set_args(L, "f", pih->pi->slide_accel_time_const);
}
ADE_VIRTVAR(SlideDecelerationTime, l_Physics, "number", "Time to decelerate from maximum slide speed", "number", "Sliding deceleration time, or 0 if handle is invalid")
{
physics_info_h *pih;
float f = 0.0f;
if(!ade_get_args(L, "o|f", l_Physics.GetPtr(&pih), &f))
return ade_set_error(L, "f", 0.0f);
if(!pih->isValid())
return ade_set_error(L, "f", 0.0f);
if(ADE_SETTING_VAR) {
pih->pi->slide_decel_time_const = f;
}
return ade_set_args(L, "f", pih->pi->slide_decel_time_const);
}
ADE_VIRTVAR(Velocity, l_Physics, "vector", "Object world velocity (World vector). Setting this value may have minimal effect unless the $Fix scripted velocity game settings flag is used.", "vector", "Object velocity, or null vector if handle is invalid")
{
physics_info_h *pih;
vec3d *v3=NULL;
if(!ade_get_args(L, "o|o", l_Physics.GetPtr(&pih), l_Vector.GetPtr(&v3)))
return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
if(!pih->isValid())
return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
if(ADE_SETTING_VAR && v3 != NULL) {
pih->pi->vel = *v3;
pih->pi->speed = vm_vec_mag(&pih->pi->vel);
pih->pi->fspeed = vm_vec_dot(&pih->objh.objp()->orient.vec.fvec, &pih->pi->vel);
if (Fix_scripted_velocity)
pih->pi->flags |= PF_SCRIPTED_VELOCITY; // set flag to ensure physics respects this new value
}
return ade_set_args(L, "o", l_Vector.Set(pih->pi->vel));
}
ADE_VIRTVAR(VelocityDamping, l_Physics, "number", "Damping, the natural period (1 / omega) of the dampening effects on top of the acceleration model. Called 'side_slip_time_const' in code base. ", "number", "Damping, or 0 if handle is invalid")
{
physics_info_h *pih;
float f = 0.0f;
if(!ade_get_args(L, "o|f", l_Physics.GetPtr(&pih), &f))
return ade_set_error(L, "f", 0.0f);
if(!pih->isValid())
return ade_set_error(L, "f", 0.0f);
if(ADE_SETTING_VAR) {
pih->pi->side_slip_time_const = f;
}
return ade_set_args(L, "f", pih->pi->side_slip_time_const);
}
ADE_VIRTVAR(VelocityDesired, l_Physics, "vector", "Desired velocity (World vector)", "vector", "Desired velocity, or null vector if handle is invalid")
{
physics_info_h *pih;
vec3d *v3=NULL;
if(!ade_get_args(L, "o|o", l_Physics.GetPtr(&pih), l_Vector.GetPtr(&v3)))
return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
if(!pih->isValid())
return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
if(ADE_SETTING_VAR && v3 != NULL) {
pih->pi->desired_vel = *v3;
}
return ade_set_args(L, "o", l_Vector.Set(pih->pi->desired_vel));
}
ADE_VIRTVAR(VelocityMax, l_Physics, "vector", "Object max local velocity (Local vector)", "vector", "Maximum velocity, or null vector if handle is invalid")
{
physics_info_h *pih;
vec3d *v3=NULL;
if(!ade_get_args(L, "o|o", l_Physics.GetPtr(&pih), l_Vector.GetPtr(&v3)))
return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
if(!pih->isValid())
return ade_set_error(L, "o", l_Vector.Set(vmd_zero_vector));
if(ADE_SETTING_VAR && v3 != NULL) {
pih->pi->max_vel = *v3;
}
return ade_set_args(L, "o", l_Vector.Set(pih->pi->max_vel));
}
ADE_VIRTVAR(VerticalThrust, l_Physics, "number", "Vertical thrust amount (-1 - 1), used primarily for thruster graphics and does not affect any physical behavior", "number", "Vertical thrust amount, or 0 if handle is invalid")
{
physics_info_h *pih;
float f = 0.0f;
if(!ade_get_args(L, "o|f", l_Physics.GetPtr(&pih), &f))
return ade_set_error(L, "f", 0.0f);
if(!pih->isValid())
return ade_set_error(L, "f", 0.0f);
if(ADE_SETTING_VAR) {
pih->pi->linear_thrust.xyz.y = f;
}
return ade_set_args(L, "f", pih->pi->linear_thrust.xyz.y);
}
ADE_VIRTVAR(AfterburnerActive, l_Physics, "boolean", "Specifies if the afterburner is active or not", "boolean", "true if afterburner is active false otherwise")
{
physics_info_h *pih;
bool set = false;
if(!ade_get_args(L, "o|b", l_Physics.GetPtr(&pih), &set))
return ade_set_error(L, "b", false);
if(!pih->isValid())
return ade_set_error(L, "b", false);
if (ADE_SETTING_VAR)
{
if(set)
pih->pi->flags |= PF_AFTERBURNER_ON;
else
pih->pi->flags &= ~PF_AFTERBURNER_ON;
}
if (pih->pi->flags & PF_AFTERBURNER_ON)
return ade_set_args(L, "b", true);
else
return ade_set_args(L, "b", false);
}
ADE_VIRTVAR(GravityConst, l_Physics, "number", "Multiplier for the effect of gravity on this object", "number", "Multiplier, or 0 if handle is invalid")
{
physics_info_h* pih;
float grav_const = 0.0f;
if (!ade_get_args(L, "o|f", l_Physics.GetPtr(&pih), &grav_const))
return ade_set_error(L, "f", 0.0f);
if (!pih->isValid())
return ade_set_error(L, "f", 0.0f);
if (ADE_SETTING_VAR) {
pih->pi->gravity_const = grav_const;
}
return ade_set_args(L, "f", pih->pi->gravity_const);
}
ADE_FUNC(isValid, l_Physics, NULL, "True if valid, false or nil if not", "boolean", "Detects whether handle is valid")
{
physics_info_h *pih;
if(!ade_get_args(L, "o", l_Physics.GetPtr(&pih)))
return ADE_RETURN_NIL;
return ade_set_args(L, "b", pih->isValid());
}
ADE_FUNC(getSpeed, l_Physics, NULL, "Gets total speed as of last frame", "number", "Total speed, or 0 if handle is invalid")
{
physics_info_h *pih;
if(!ade_get_args(L, "o", l_Physics.GetPtr(&pih)))
return ade_set_error(L, "f", 0.0f);
if(!pih->isValid())
return ade_set_error(L, "f", 0.0f);
return ade_set_args(L, "f", pih->pi->speed);
}
ADE_FUNC(getForwardSpeed, l_Physics, NULL, "Gets total speed in the ship's 'forward' direction as of last frame", "number", "Total forward speed, or 0 if handle is invalid")
{
physics_info_h *pih;
if(!ade_get_args(L, "o", l_Physics.GetPtr(&pih)))
return ade_set_error(L, "f", 0.0f);
if(!pih->isValid())
return ade_set_error(L, "f", 0.0f);
return ade_set_args(L, "f", pih->pi->fspeed);
}
// Nuke's afterburner function
ADE_FUNC(isAfterburnerActive, l_Physics, NULL, "True if Afterburners are on, false or nil if not", "boolean", "Detects whether afterburner is active")
{
physics_info_h *pih;
if(!ade_get_args(L, "o", l_Physics.GetPtr(&pih)))
return ADE_RETURN_NIL;
if(!pih->isValid())
return ade_set_error(L, "b", false);
if (pih->pi->flags & PF_AFTERBURNER_ON)
return ade_set_args(L, "b", true);
else
return ade_set_args(L, "b", false);
}
//nukes glide function
ADE_FUNC(isGliding, l_Physics, NULL, "True if glide mode is on, false or nil if not", "boolean", "Detects if ship is gliding")
{
physics_info_h *pih;
if(!ade_get_args(L, "o", l_Physics.GetPtr(&pih)))
return ADE_RETURN_NIL;
if(!pih->isValid())
return ade_set_error(L, "b", false);
if (pih->pi->flags & (PF_GLIDING|PF_FORCE_GLIDE))
return ade_set_args(L, "b", true);
else
return ade_set_args(L, "b", false);
}
ADE_FUNC(applyWhack, l_Physics, "vector Impulse, [ vector Position]", "Applies a whack to an object based on an impulse vector, indicating the direction and strength of whack and optionally at a position relative to the ship in world orientation, the ship's center being default.", "boolean", "true if it succeeded, false otherwise")
{
object_h objh;
physics_info_h *pih;
vec3d *impulse;
vec3d *offset = nullptr;
if (!ade_get_args(L, "oo|o", l_Physics.GetPtr(&pih), l_Vector.GetPtr(&impulse), l_Vector.GetPtr(&offset)))
return ADE_RETURN_NIL;
objh = pih->objh;
if (offset == nullptr)
offset = &objh.objp()->pos;
else
vm_vec_add2(offset, &objh.objp()->pos);
ship_apply_whack(impulse, offset, objh.objp());
return ADE_RETURN_TRUE;
}
ADE_FUNC(applyWhackWorld, l_Physics, "vector Impulse, [ vector Position]", "Applies a whack to an object based on an impulse vector, indicating the direction and strength of whack and optionally at a world position, the ship's center being default.", "boolean", "true if it succeeded, false otherwise")
{
object_h objh;
physics_info_h* pih;
vec3d* impulse;
vec3d* world_pos = nullptr;
if (!ade_get_args(L, "oo|o", l_Physics.GetPtr(&pih), l_Vector.GetPtr(&impulse), l_Vector.GetPtr(&world_pos)))
return ADE_RETURN_NIL;
objh = pih->objh;
if (!world_pos) {
world_pos = &objh.objp()->pos;
}
ship_apply_whack(impulse, world_pos, objh.objp());
return ADE_RETURN_TRUE;
}
}
}
|