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 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
|
/*
===========================================================================
Copyright (C) 2015 the OpenMoHAA team
This file is part of OpenMoHAA source code.
OpenMoHAA source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// beam.cpp:
//
#include "beam.h"
#include "../qcommon/qfiles.h"
#include "game.h"
#include <utility>
/*****************************************************************************/
/*QUAKED func_beam (0 0.25 .5) (-8 -8 -8) (8 8 8) START_ON PERSIST WAVE NOISE
This creates a beam effect from the origin to the target's origin. If no
target is specified, uses angles and projects beam out from there.
"model" Specifies the model to use as the beam
"overlap" Specifies the amount of overlap each beam link should have. Use this to fill
in the cracks when using electric on beams. (Default is 0)
"minoffset" Minimum amount of electrical variation (Default is 0)
"maxoffset" Maximum amount of electrical variation (Default is 5)
"color" Vector specifiying the red,green, and blue components. (Default is "1 1 1")
"alpha" Alpha of the beam (Default is 1.0)
"damage" Amount of damage the beam inflicts if beam hits someone (Default is 0)
"angles" Sets the angle of the beam if no target is specified.
"life" Sets the life of the beam for use with the persist spawnflag. This is how long a beam will
be displayed.
"numsegments" Number of segments in a beam (Default is 4)
"delay" Delay between beam updates. (i.e. slows the effect of the beam down)
"shader" Set the shader of the beam
"scale" Set the width of the beam
START_ON - Starts the beam on
PERSIST - Keeps the last few beams around and fades them out over the life of the beam
WAVE - Make the beam follow a sin wave pattern
NOISE - Use a more computationally expensive random effect, but the results are smoother
If the model field is not set, then a renderer generated beam will be created
using the color, minoffset, maxoffset, scale, and subdivisions fields
If the targetname is set, it will use the target specified as the endpoint of the beam
/*****************************************************************************/
Event EV_FuncBeam_Activate
(
"activate",
EV_DEFAULT,
NULL,
NULL,
"Activate the beam",
EV_NORMAL
);
Event EV_FuncBeam_Deactivate
(
"deactivate",
EV_DEFAULT,
NULL,
NULL,
"Deactivate the beam",
EV_NORMAL
);
Event EV_FuncBeam_Diameter
(
"diameter",
EV_DEFAULT,
"f",
"diameter",
"Set the diameter of the beam",
EV_NORMAL
);
Event EV_FuncBeam_Maxoffset
(
"maxoffset",
EV_DEFAULT,
"f",
"max_offset",
"Set the maximum offset the beam can travel above, below, forward or back of it's endpoints",
EV_NORMAL
);
Event EV_FuncBeam_Minoffset
(
"minoffset",
EV_DEFAULT,
"f",
"min_offset",
"Set the minimum offset the beam can travel above, below, forward or back of it's endpoints",
EV_NORMAL
);
Event EV_FuncBeam_Overlap
(
"overlap",
EV_DEFAULT,
"f",
"beam_overlap",
"Set the amount of overlap the beams have when they are being strung together",
EV_NORMAL
);
Event EV_FuncBeam_Color
(
"color",
EV_DEFAULT,
"v[0,1][0,1][0,1]",
"beam_color",
"Set the color of the beam",
EV_NORMAL
);
Event EV_FuncBeam_SetTarget
(
"target",
EV_DEFAULT,
"s",
"beam_target",
"Set the target of the beam. The beam will be drawn from the origin\n"
"to the origin of the target entity",
EV_NORMAL
);
Event EV_FuncBeam_SetEndPoint
(
"endpoint",
EV_DEFAULT,
"v",
"beam_end_point",
"Set the end point of the beam. The beam will be draw from the origin to\n"
"the end point.",
EV_NORMAL
);
Event EV_FuncBeam_SetLife
(
"life",
EV_DEFAULT,
"f",
"beam_life",
"Set the amount of time the beam stays on when activated",
EV_NORMAL
);
Event EV_FuncBeam_Shader
(
"shader",
EV_DEFAULT,
"s",
"beam_shader",
"Set the shader that the beam will use",
EV_NORMAL
);
Event EV_FuncBeam_TileShader
(
"tileshader",
EV_DEFAULT,
"s",
"beam_shader",
"Set the shader that the beam will use. This shader will be tiled.",
EV_NORMAL
);
Event EV_FuncBeam_Segments
(
"numsegments",
EV_DEFAULT,
"i",
"numsegments",
"Set the number of segments for the beam",
EV_NORMAL
);
Event EV_FuncBeam_Delay
(
"delay",
EV_DEFAULT,
"f",
"delay",
"Set the amount of delay on the beam updater",
EV_NORMAL
);
Event EV_FuncBeam_NumSphereBeams
(
"numspherebeams",
EV_DEFAULT,
"i",
"num",
"Set the number of beams that will be shot out in a sphere like formation",
EV_NORMAL
);
Event EV_FuncBeam_SphereRadius
(
"radius",
EV_DEFAULT,
"f",
"radius",
"Set the starting radius of the beams if this is a beamsphere",
EV_NORMAL
);
Event EV_FuncBeam_ToggleDelay
(
"toggledelay",
EV_DEFAULT,
"SF",
"[random] time",
"Causes a beam toggling effect. Sets the time between toggling. If random is specified, The time will be between 0 and time",
EV_NORMAL
);
Event EV_FuncBeam_FindEndpoint
(
"findendpoint",
EV_DEFAULT,
NULL,
NULL,
"Find the endpoint of a beam",
EV_NORMAL
);
Event EV_FuncBeam_UpdateEndpoint
(
"updateendpoint",
EV_DEFAULT,
NULL,
NULL,
"Update the endpoint of a beam",
EV_NORMAL
);
Event EV_FuncBeam_UpdateOrigin
(
"updateorigin",
EV_DEFAULT,
NULL,
NULL,
"Update the origin of a beam",
EV_NORMAL
);
Event EV_FuncBeam_EndAlpha
(
"endalpha",
EV_DEFAULT,
"f",
"alpha",
"Set the endpoint alpha value of the beam",
EV_NORMAL
);
Event EV_FuncBeam_Shoot
(
"shoot",
EV_DEFAULT,
NULL,
NULL,
"Make the beam cause damage to entities that get in the way",
EV_NORMAL
);
Event EV_FuncBeam_ShootRadius
(
"shootradius",
EV_DEFAULT,
"f",
"radius",
"Set the radius of the damage area between beam endpoints",
EV_NORMAL
);
Event EV_FuncBeam_Persist
(
"persist",
EV_DEFAULT,
"b",
"bool",
"Set the persist property of the beam",
EV_NORMAL
);
Event EV_FuncBeam_IgnoreWalls
(
"ignorewalls",
EV_DEFAULT,
"B",
"bool",
"Set if the beam can traverse walls",
EV_NORMAL
);
CLASS_DECLARATION(ScriptSlave, FuncBeam, "func_beam") {
{&EV_Activate, &FuncBeam::Activate },
{&EV_FuncBeam_Activate, &FuncBeam::Activate },
{&EV_FuncBeam_Deactivate, &FuncBeam::Deactivate },
{&EV_FuncBeam_Maxoffset, &FuncBeam::SetMaxoffset },
{&EV_FuncBeam_Minoffset, &FuncBeam::SetMinoffset },
{&EV_FuncBeam_Overlap, &FuncBeam::SetOverlap },
{&EV_FuncBeam_Color, &FuncBeam::SetColor },
{&EV_SetAngle, &FuncBeam::SetAngle },
{&EV_FuncBeam_Segments, &FuncBeam::SetSegments },
{&EV_SetAngles, &FuncBeam::SetAngles },
{&EV_FuncBeam_SetEndPoint, &FuncBeam::SetEndPoint },
{&EV_Model, &FuncBeam::SetModel },
{&EV_Damage, &FuncBeam::SetDamage },
{&EV_FuncBeam_SetLife, &FuncBeam::SetLife },
{&EV_FuncBeam_Shader, &FuncBeam::SetBeamShader },
{&EV_FuncBeam_TileShader, &FuncBeam::SetBeamTileShader},
{&EV_FuncBeam_Delay, &FuncBeam::SetDelay },
{&EV_FuncBeam_NumSphereBeams, &FuncBeam::SetNumSphereBeams},
{&EV_FuncBeam_SphereRadius, &FuncBeam::SetSphereRadius },
{&EV_FuncBeam_ToggleDelay, &FuncBeam::SetToggleDelay },
{&EV_FuncBeam_ShootRadius, &FuncBeam::SetShootRadius },
{&EV_FuncBeam_EndAlpha, &FuncBeam::SetEndAlpha },
{&EV_FuncBeam_Persist, &FuncBeam::SetPersist },
{&EV_FuncBeam_FindEndpoint, &FuncBeam::FindEndpoint },
{&EV_FuncBeam_UpdateEndpoint, &FuncBeam::UpdateEndpoint },
{&EV_FuncBeam_UpdateOrigin, &FuncBeam::UpdateOrigin },
{&EV_FuncBeam_Shoot, &FuncBeam::Shoot },
{&EV_FuncBeam_IgnoreWalls, &FuncBeam::SetIgnoreWalls },
{NULL, NULL }
};
FuncBeam::FuncBeam()
{
setSolidType(SOLID_NOT);
setOrigin();
damage = 0;
life = 0;
shootradius = 0;
use_angles = false;
m_bIgnoreWalls = false;
edict->s.renderfx |= RF_BEAM;
edict->s.eType = ET_BEAM; // Entity type beam
edict->s.modelindex = 1; // must be non-zero
SetBeamShader("beamshader");
if (!LoadingSavegame) {
edict->s.alpha = 1; // alpha
edict->s.surfaces[4] = 4; // num segments
edict->s.surfaces[0] = 16; // life
edict->s.surfaces[9] = 16; // endalpha
edict->s.beam_entnum = ENTITYNUM_NONE;
edict->s.bone_angles[0][1] = 5; // Max offset
if (spawnflags & 0x0001) { // Start On
PostEvent(EV_Activate, EV_POSTSPAWN);
} else {
hideModel();
}
edict->s.skinNum = 0;
if (spawnflags & 0x0002) {
edict->s.skinNum |= BEAM_PERSIST_EFFECT;
}
if (spawnflags & 0x0004) {
edict->s.skinNum |= BEAM_WAVE_EFFECT;
}
if (spawnflags & 0x0008) {
edict->s.skinNum |= BEAM_USE_NOISE;
}
// Try to find the endpoint of this beam after everything has been spawned
PostEvent(EV_FuncBeam_FindEndpoint, EV_LINKBEAMS);
}
}
void FuncBeam::SetEndAlpha(Event *ev)
{
BEAM_PARM_TO_PKT(ev->GetFloat(1), edict->s.surfaces[9]);
}
void FuncBeam::SetToggleDelay(Event *ev)
{
edict->s.skinNum |= BEAM_TOGGLE;
if (ev->NumArgs() > 2) {
str arg = ev->GetString(1);
if (!arg.icmp("random")) {
edict->s.skinNum |= BEAM_RANDOM_TOGGLEDELAY;
}
BEAM_PARM_TO_PKT(ev->GetFloat(2), edict->s.surfaces[8]);
} else {
BEAM_PARM_TO_PKT(ev->GetFloat(1), edict->s.surfaces[8]);
}
}
void FuncBeam::SetSphereRadius(Event *ev)
{
edict->s.skinNum |= BEAM_SPHERE_EFFECT;
BEAM_PARM_TO_PKT(ev->GetFloat(1), edict->s.surfaces[7]);
}
void FuncBeam::SetNumSphereBeams(Event *ev)
{
edict->s.skinNum |= BEAM_SPHERE_EFFECT;
edict->s.surfaces[6] = ev->GetInteger(1);
}
void FuncBeam::SetAngle(Event *ev)
{
Vector movedir;
movedir = G_GetMovedir(ev->GetFloat(1));
setAngles(movedir.toAngles());
}
void FuncBeam::SetAngles(Event *ev)
{
setAngles(ev->GetVector(1));
}
// Override setAngles to update the endpoint of the beam if it's rotated
void FuncBeam::setAngles(Vector angles)
{
trace_t trace;
Vector endpoint;
ScriptSlave::setAngles(angles);
// If there is no target, then use the angles to determine where to put the
// endpoint
if (!end) {
endpoint = origin + Vector(orientation[0]) * MAP_SIZE;
trace = G_Trace(origin, vec_zero, vec_zero, endpoint, this, MASK_LINE, false, "FuncBeam");
VectorCopy(trace.endpos, edict->s.origin2);
use_angles = true;
}
}
void FuncBeam::SetEndPoint(Event *ev)
{
trace_t trace;
end_point = ev->GetVector(1);
trace = G_Trace(origin, vec_zero, vec_zero, end_point, this, MASK_LINE, false, "FuncBeam");
VectorCopy(trace.endpos, edict->s.origin2);
use_angles = false;
}
void FuncBeam::SetModel(Event *ev)
{
setModel(ev->GetString(1));
edict->s.renderfx &= ~RF_BEAM;
edict->s.eType = ET_BEAM;
edict->s.skinNum |= BEAM_USEMODEL;
}
void FuncBeam::SetDamage(Event *ev)
{
damage = ev->GetFloat(2);
}
void FuncBeam::SetLife(Event *ev)
{
BEAM_PARM_TO_PKT(ev->GetFloat(1), edict->s.surfaces[0]);
}
void FuncBeam::SetColor(Event *ev)
{
Vector color = ev->GetVector(1);
G_SetConstantLight(&edict->s.constantLight, &color[0], &color[1], &color[2], NULL);
}
void FuncBeam::SetSegments(Event *ev)
{
edict->s.surfaces[4] = ev->GetInteger(1);
}
void FuncBeam::SetBeamShader(str beam_shader)
{
str temp_shader;
shader = beam_shader;
edict->s.tag_num = gi.imageindex(shader);
temp_shader = shader + ".spr";
CacheResource(temp_shader);
}
void FuncBeam::SetBeamShader(Event *ev)
{
SetBeamShader(ev->GetString(1));
}
void FuncBeam::SetBeamTileShader(Event *ev)
{
SetBeamShader(ev->GetString(1));
edict->s.skinNum |= BEAM_TILESHADER;
}
void FuncBeam::SetMaxoffset(Event *ev)
{
edict->s.bone_angles[0][1] = ev->GetFloat(1);
}
void FuncBeam::SetMinoffset(Event *ev)
{
edict->s.bone_angles[0][0] = ev->GetFloat(1);
}
void FuncBeam::SetOverlap(Event *ev)
{
BEAM_PARM_TO_PKT(ev->GetFloat(1), edict->s.surfaces[3]);
}
void FuncBeam::SetDelay(Event *ev)
{
if (ev->NumArgs() > 2) {
str arg = ev->GetString(1);
if (!arg.icmp("random")) {
edict->s.skinNum |= BEAM_RANDOM_DELAY;
}
BEAM_PARM_TO_PKT(ev->GetFloat(2), edict->s.surfaces[5]);
} else {
BEAM_PARM_TO_PKT(ev->GetFloat(1), edict->s.surfaces[5]);
}
}
void FuncBeam::Deactivate(Event *ev)
{
hideModel();
// Cancel all of the events to activate
CancelEventsOfType(EV_FuncBeam_Activate);
CancelEventsOfType(EV_Activate);
CancelEventsOfType(EV_FuncBeam_UpdateEndpoint);
CancelEventsOfType(EV_FuncBeam_UpdateOrigin);
}
void FuncBeam::SetShootRadius(Event *ev)
{
shootradius = ev->GetFloat(1);
}
void FuncBeam::SetPersist(Event *ev)
{
qboolean persist = ev->GetBoolean(1);
if (persist) {
edict->s.skinNum |= BEAM_PERSIST_EFFECT;
} else {
edict->s.skinNum &= ~BEAM_PERSIST_EFFECT;
}
}
void FuncBeam::Shoot(Event *ev)
{
trace_t trace;
Vector start, end;
start = edict->s.origin;
end = edict->s.origin2;
Vector dir(end - start);
Vector b1(-shootradius, -shootradius, -shootradius);
Vector b2(shootradius, shootradius, shootradius);
trace = G_Trace(start, b1, b2, end, this, MASK_SHOT, false, "FuncBeam::Activate");
if (trace.ent && trace.ent->entity && trace.ent->entity->takedamage) {
// damage the ent
trace.ent->entity->Damage(this, this, damage, trace.endpos, dir, trace.plane.normal, 0, 0, MOD_BEAM);
}
PostEvent(EV_FuncBeam_Shoot, 0.1f);
}
void FuncBeam::SetIgnoreWalls(Event *ev)
{
if (ev->NumArgs() > 0) {
m_bIgnoreWalls = ev->GetBoolean(1);
} else {
m_bIgnoreWalls = true;
}
}
void FuncBeam::Activate(Event *ev)
{
Vector forward;
trace_t trace;
showModel();
// An entity is targeted
if (end) {
VectorCopy(end->origin, edict->s.origin2);
// Post an event so that the beam will update itself every frame
PostEvent(EV_FuncBeam_UpdateEndpoint, level.frametime);
} else if (use_angles) {
angles.AngleVectorsLeft(&forward, NULL, NULL);
Vector endpoint(orientation[0]);
endpoint *= MAP_SIZE;
if (m_bIgnoreWalls) {
VectorCopy(endpoint, trace.endpos);
} else {
trace = G_Trace(origin, vec_zero, vec_zero, endpoint, this, MASK_LINE, false, "FuncBeam::Activate");
}
VectorCopy(trace.endpos, edict->s.origin2);
} else {
if (m_bIgnoreWalls) {
VectorCopy(end_point, trace.endpos);
} else {
trace = G_Trace(origin, vec_zero, vec_zero, end_point, this, MASK_LINE, false, "FuncBeam::Activate");
}
VectorCopy(trace.endpos, edict->s.origin2);
}
if (origin_target) {
PostEvent(EV_FuncBeam_UpdateOrigin, level.frametime);
}
if (damage) {
// Shoot beam and cause damage every frame
ProcessEvent(EV_FuncBeam_Shoot);
}
// If life is set, then post a deactivate message
if (life > 0 && !EventPending(EV_FuncBeam_Deactivate)) {
PostEvent(EV_FuncBeam_Deactivate, life);
return;
}
}
void FuncBeam::UpdateEndpoint(Event *ev)
{
if (end) {
Event *ev1 = new Event(std::move(*ev));
VectorCopy(end->origin, edict->s.origin2);
PostEvent(ev1, level.frametime);
}
}
void FuncBeam::UpdateOrigin(Event *ev)
{
if (origin_target) {
Event *ev1 = new Event(std::move(*ev));
setOrigin(origin_target->centroid);
PostEvent(ev1, level.frametime);
}
}
void FuncBeam::FindEndpoint(Event *ev)
{
if (target && strlen(target)) {
end = (Entity *)G_FindTarget(NULL, target);
if (end) {
VectorCopy(end->origin, edict->s.origin2);
}
}
}
FuncBeam *CreateBeam(
const char *model,
const char *shader,
Vector start,
Vector end,
int numsegments,
float scale,
float life,
float damage,
Entity *origin_target
)
{
FuncBeam *beam;
trace_t trace;
// set start point
beam = new FuncBeam;
beam->setOrigin(start);
if (origin_target) {
beam->origin_target = origin_target;
}
// set endpoint
beam->end_point = end;
trace = G_Trace(start, vec_zero, vec_zero, end, beam, MASK_LINE, false, "CreateBeam");
VectorCopy(trace.endpos, beam->edict->s.origin2);
beam->use_angles = false;
if (model) {
// set the model if we have one
beam->setModel(model);
beam->edict->s.renderfx &= ~RF_BEAM;
beam->edict->s.eType = ET_BEAM;
beam->edict->s.skinNum |= BEAM_USEMODEL;
}
if (shader) {
// Set the shader as an image configstring
beam->SetBeamShader(shader);
}
// set num segments
beam->edict->s.surfaces[4] = numsegments;
// set scale
beam->setScale(scale);
// set life
BEAM_PARM_TO_PKT(life, beam->edict->s.surfaces[0]);
beam->life = life;
// set damage
beam->damage = damage;
// activate it
beam->ProcessEvent(EV_Activate);
beam->PostEvent(EV_Remove, life);
return beam;
}
|