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
|
/*
===========================================================================
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
===========================================================================
*/
// bspline.cpp: Uniform non-rational bspline class.
//
#include "g_local.h"
#include "bspline.h"
#include "game.h"
#include "debuglines.h"
#include "g_phys.h"
#include "scriptexception.h"
void BSpline::Set(Vector *control_points_, int num_control_points_, splinetype_t type)
{
int i;
SetType(type);
has_orientation = false;
if (control_points) {
delete[] control_points;
control_points = NULL;
}
num_control_points = num_control_points_;
if (num_control_points) {
control_points = new BSplineControlPoint[num_control_points];
assert(control_points);
for (i = 0; i < num_control_points; i++) {
control_points[i].Set(control_points_[i]);
}
}
}
void BSpline::Set(
Vector *control_points_,
Vector *control_orients_,
float *control_speeds_,
int num_control_points_,
splinetype_t type
)
{
int i;
SetType(type);
has_orientation = true;
if (control_points) {
delete[] control_points;
control_points = NULL;
}
num_control_points = num_control_points_;
if (num_control_points) {
control_points = new BSplineControlPoint[num_control_points];
assert(control_points);
for (i = 0; i < num_control_points; i++) {
control_points[i].Set(control_points_[i], control_orients_[i], control_speeds_[i]);
}
}
}
void BSpline::Clear(void)
{
if (control_points) {
delete[] control_points;
control_points = NULL;
}
num_control_points = 0;
has_orientation = false;
}
inline float BSpline::EvalNormal(float u, Vector& pos, Vector& orient)
{
int segment_id;
float B[4];
float tmp;
float u_2;
float u_3;
Vector ang;
float roll;
float speed;
segment_id = (int)u;
if (segment_id < 0) {
segment_id = 0;
}
if (segment_id > num_control_points - 4) {
segment_id = num_control_points - 4;
}
u -= (float)segment_id;
u_2 = u * u;
u_3 = u * u_2;
tmp = 1 - u;
B[0] = (tmp * tmp * tmp) * (1.0f / 6.0f);
B[1] = (3.0f * u_3 - 6.0f * u_2 + 4.0f) * (1.0f / 6.0f);
B[2] = (-3.0f * u_3 + 3.0f * u_2 + 3.0f * u + 1) * (1.0f / 6.0f);
B[3] = u_3 * (1.0f / 6.0f);
pos = *control_points[0 + segment_id].GetPosition() * B[0] + *control_points[1 + segment_id].GetPosition() * B[1]
+ *control_points[2 + segment_id].GetPosition() * B[2] + *control_points[3 + segment_id].GetPosition() * B[3];
ang = *control_points[0 + segment_id].GetOrientation() * B[0]
+ *control_points[1 + segment_id].GetOrientation() * B[1]
+ *control_points[2 + segment_id].GetOrientation() * B[2]
+ *control_points[3 + segment_id].GetOrientation() * B[3];
roll = *control_points[0 + segment_id].GetRoll() * B[0] + *control_points[1 + segment_id].GetRoll() * B[1]
+ *control_points[2 + segment_id].GetRoll() * B[2] + *control_points[3 + segment_id].GetRoll() * B[3];
speed = *control_points[0 + segment_id].GetSpeed() * B[0] + *control_points[1 + segment_id].GetSpeed() * B[1]
+ *control_points[2 + segment_id].GetSpeed() * B[2] + *control_points[3 + segment_id].GetSpeed() * B[3];
orient = ang.toAngles();
orient[ROLL] = roll;
return speed;
}
inline float BSpline::EvalLoop(float t, Vector& pos, Vector& orient)
{
Vector retval;
Vector ang;
float speed;
float roll;
int segment_id;
int next_id;
float B[4];
float tmp;
float u;
float u_2;
float u_3;
int i;
int j;
segment_id = (int)floor(t);
u = t - floor(t);
segment_id %= num_control_points;
if (segment_id < 0) {
segment_id += num_control_points;
}
u_2 = u * u;
u_3 = u * u_2;
tmp = 1 - u;
B[0] = (tmp * tmp * tmp) * (1.0f / 6.0f);
B[1] = (3.0f * u_3 - 6.0f * u_2 + 4.0f) * (1.0f / 6.0f);
B[2] = (-3.0f * u_3 + 3.0f * u_2 + 3.0f * u + 1) * (1.0f / 6.0f);
B[3] = u_3 * (1.0f / 6.0f);
speed = 0;
roll = 0;
for (i = 0, j = segment_id; i < 4; i++, j++) {
if (j >= num_control_points) {
j -= (num_control_points - loop_control_point);
}
retval += *control_points[j].GetPosition() * B[i];
ang += *control_points[j].GetOrientation() * B[i];
speed += *control_points[j].GetSpeed() * B[i];
roll += *control_points[j].GetRoll() * B[i];
}
pos = retval;
next_id = segment_id + 1;
if (next_id >= num_control_points) {
next_id -= (num_control_points - loop_control_point);
}
orient = ang.toAngles();
orient[ROLL] = roll;
return speed;
}
inline float BSpline::EvalClamp(float t, Vector& pos, Vector& orient)
{
Vector retval;
Vector ang;
int segment_id;
int next_id;
float B[4];
float tmp;
float u;
float u_2;
float u_3;
int i;
int j;
float speed;
float roll;
segment_id = (int)floor(t);
u = t - floor(t);
u_2 = u * u;
u_3 = u * u_2;
tmp = 1 - u;
B[0] = (tmp * tmp * tmp) * (1.0f / 6.0f);
B[1] = (3.0f * u_3 - 6.0f * u_2 + 4.0f) * (1.0f / 6.0f);
B[2] = (-3.0f * u_3 + 3.0f * u_2 + 3.0f * u + 1) * (1.0f / 6.0f);
B[3] = u_3 * (1.0f / 6.0f);
speed = 0;
roll = 0;
for (i = 0; i < 4; i++, segment_id++) {
j = segment_id;
if (j < 0) {
j = 0;
} else if (j >= num_control_points) {
j = num_control_points - 1;
}
retval += *control_points[j].GetPosition() * B[i];
ang += *control_points[j].GetOrientation() * B[i];
speed += *control_points[j].GetSpeed() * B[i];
roll += *control_points[j].GetRoll() * B[i];
}
pos = retval;
next_id = segment_id + 1;
if (segment_id < 0) {
segment_id = 0;
}
if (segment_id >= num_control_points) {
segment_id = num_control_points - 1;
}
if (next_id < 0) {
next_id = 0;
}
if (next_id >= num_control_points) {
next_id = num_control_points - 1;
}
orient = ang.toAngles();
orient[ROLL] = roll;
return speed;
}
Vector BSpline::Eval(float u)
{
Vector pos;
Vector orient;
switch (curvetype) {
default:
case SPLINE_NORMAL:
EvalNormal(u, pos, orient);
break;
case SPLINE_CLAMP:
EvalClamp(u, pos, orient);
break;
case SPLINE_LOOP:
if (u < 0) {
EvalClamp(u, pos, orient);
} else {
EvalLoop(u, pos, orient);
}
break;
}
return pos;
}
float BSpline::Eval(float u, Vector& pos, Vector& orient)
{
switch (curvetype) {
default:
case SPLINE_NORMAL:
return EvalNormal(u, pos, orient);
break;
case SPLINE_CLAMP:
return EvalClamp(u, pos, orient);
break;
case SPLINE_LOOP:
if (u < 0) {
return EvalClamp(u, pos, orient);
} else {
return EvalLoop(u, pos, orient);
}
break;
}
}
void BSpline::DrawControlSegments(void)
{
int i;
G_BeginLine();
for (i = 0; i < num_control_points; i++) {
G_Vertex(*control_points[i].GetPosition());
}
G_EndLine();
}
void BSpline::DrawCurve(int num_subdivisions)
{
float u;
float du;
if (!num_control_points) {
return;
}
du = 1.0f / (float)num_subdivisions;
G_BeginLine();
for (u = -2.0f; u <= (float)num_control_points; u += du) {
G_Vertex((Vector)Eval(u));
}
G_EndLine();
}
void BSpline::DrawCurve(Vector offset, int num_subdivisions)
{
float u;
float du;
du = 1.0f / (float)num_subdivisions;
G_BeginLine();
for (u = -2.0f; u <= (float)num_control_points; u += du) {
G_Vertex(offset + (Vector)Eval(u));
}
G_EndLine();
}
void BSpline::AppendControlPoint(const Vector& new_control_point)
{
BSplineControlPoint *old_control_points;
int i;
old_control_points = control_points;
num_control_points++;
control_points = new BSplineControlPoint[num_control_points];
assert(control_points);
if (old_control_points) {
for (i = 0; i < num_control_points - 1; i++) {
control_points[i] = old_control_points[i];
}
delete[] old_control_points;
}
control_points[num_control_points - 1].Set(new_control_point);
}
void BSpline::AppendControlPoint(const Vector& new_control_point, const float& speed)
{
BSplineControlPoint *old_control_points;
int i;
old_control_points = control_points;
num_control_points++;
control_points = new BSplineControlPoint[num_control_points];
assert(control_points);
if (old_control_points) {
for (i = 0; i < num_control_points - 1; i++) {
control_points[i] = old_control_points[i];
}
delete[] old_control_points;
}
control_points[num_control_points - 1].Set(new_control_point, speed);
}
void BSpline::AppendControlPoint(
const Vector& new_control_point, const Vector& new_control_orient, const float& new_control_speed
)
{
BSplineControlPoint *old_control_points;
int i;
has_orientation = true;
old_control_points = control_points;
num_control_points++;
control_points = new BSplineControlPoint[num_control_points];
assert(control_points);
if (old_control_points) {
for (i = 0; i < num_control_points - 1; i++) {
control_points[i] = old_control_points[i];
}
delete[] old_control_points;
}
control_points[num_control_points - 1].Set(new_control_point, new_control_orient, new_control_speed);
}
void BSpline::SetLoopPoint(const Vector& pos)
{
int i;
for (i = 0; i < num_control_points; i++) {
if (pos == *control_points[i].GetPosition()) {
loop_control_point = i;
break;
}
}
}
int BSpline::PickControlPoint(const Vector& window_point, float pick_size)
{
int i;
float closest_dist_2;
int closest_index;
float dist_2;
Vector delta;
closest_index = -1;
closest_dist_2 = 1000000.0f;
for (i = 0; i < num_control_points; i++) {
delta = window_point - *control_points[i].GetPosition();
dist_2 = delta * delta;
if (dist_2 < closest_dist_2) {
closest_dist_2 = dist_2;
closest_index = i;
}
}
if (pick_size * pick_size >= closest_dist_2) {
return closest_index;
} else {
return -1;
}
}
Event EV_SplinePath_Create(
"SplinePath_create", EV_DEFAULT, NULL, NULL, "Creates the spline path from the target list.", EV_NORMAL
);
Event EV_SplinePath_Loop("loop", EV_CONSOLE, "s", "loop_name", "Sets the loop name.", EV_NORMAL);
Event EV_SplinePath_Speed("speed", EV_DEFAULT, "f", "speed", "Sets the path speed.", EV_NORMAL);
Event EV_SplinePath_SetTriggerTarget("triggertarget", EV_DEFAULT, "s", "target", "Sets the trigger target.", EV_NORMAL);
Event EV_SplinePath_SetWatch(
"watch", EV_CONSOLE, "s", "watchEntity", "Sets the entity to watch at this node.", EV_NORMAL
);
Event EV_SplinePath_SetFov("fov", EV_CONSOLE, "f", "cameraFOV", "Sets the fov at this node.", EV_NORMAL);
Event EV_SplinePath_SetFadeTime("fadetime", EV_DEFAULT, "f", "fadeTime", "Sets the fadetime at this node.", EV_NORMAL);
CLASS_DECLARATION(Entity, SplinePath, "info_splinepath") {
{&EV_SplinePath_Create, &SplinePath::CreatePath },
{&EV_SplinePath_Loop, &SplinePath::SetLoop },
{&EV_SplinePath_Speed, &SplinePath::SetSpeed },
{&EV_SplinePath_SetTriggerTarget, &SplinePath::SetTriggerTarget},
{&EV_SplinePath_SetWatch, &SplinePath::SetWatch },
{&EV_SplinePath_SetFov, &SplinePath::SetFov },
{&EV_SplinePath_SetFadeTime, &SplinePath::SetFadeTime },
{NULL, NULL }
};
SplinePath::SplinePath()
{
entflags |= ECF_SPLINEPATH;
AddWaitTill(STRING_REACH);
owner = this;
next = NULL;
loop = NULL;
speed = 1;
doWatch = false;
watchEnt = "";
fov = 0;
fadeTime = -1;
setMoveType(MOVETYPE_NONE);
setSolidType(SOLID_NOT);
hideModel();
if (!LoadingSavegame) {
PostEvent(EV_SplinePath_Create, FRAMETIME);
}
}
SplinePath::~SplinePath()
{
// disconnect from the chain
if (owner != this) {
owner->SetNext(next);
} else if (next) {
next->SetPrev(NULL);
next = NULL;
}
assert(owner == this);
assert(next == NULL);
entflags &= ~ECF_SPLINEPATH;
}
void SplinePath::SetLoop(Event *ev)
{
loop_name = ev->GetString(1);
}
void SplinePath::SetSpeed(Event *ev)
{
speed = ev->GetFloat(1);
}
void SplinePath::SetTriggerTarget(Event *ev)
{
SetTriggerTarget(ev->GetString(1));
}
void SplinePath::CreatePath(Event *ev)
{
const char *target;
Entity *ent;
// Make the path from the targetlist.
target = Target();
if (target[0]) {
ent = (Entity *)G_FindTarget(NULL, target);
if (ent && ent->IsSubclassOfSplinePath()) {
next = (SplinePath *)ent;
if (next->owner != next) {
// Fixed in OPM
// If the target already has an owner, make sure to properly remove the owner
next->owner->SetNext(NULL);
}
next->owner = this;
} else if (ent) {
ScriptError(
"SplinePath::CreatePath: target '%s' for '%s' not found (cannot connect to class '%s')\n",
target,
targetname.c_str(),
ent->getClassname()
);
} else {
ScriptError("SplinePath::CreatePath: target %s not found\n", target);
}
}
if (loop_name.length()) {
ent = (Entity *)G_FindTarget(NULL, loop_name.c_str());
if (ent) {
loop = (SplinePath *)ent;
}
}
}
SplinePath *SplinePath::GetNext(void)
{
return next;
}
SplinePath *SplinePath::GetPrev(void)
{
if (owner == this) {
return NULL;
}
return owner;
}
void SplinePath::SetNext(SplinePath *node)
{
if (next) {
// remove ourselves from the chain
next->owner = next;
}
next = node;
if (next) {
// disconnect next from it's previous node
if (next->owner != next) {
next->owner->next = NULL;
}
next->owner = this;
}
}
void SplinePath::SetPrev(SplinePath *node)
{
if (owner != this) {
owner->next = NULL;
}
if (node && (node != this)) {
// safely remove the node from its chain
if (node->next) {
node->next->owner = node->next;
}
node->next = this;
owner = node;
} else {
owner = this;
}
}
SplinePath *SplinePath::GetLoop(void)
{
return loop;
}
void SplinePath::SetWatch(const char *name)
{
if (watchEnt != name) {
watchEnt = name;
if (watchEnt.length()) {
doWatch = true;
} else {
doWatch = false;
}
}
}
void SplinePath::SetWatch(Event *ev)
{
SetWatch(ev->GetString(1));
}
void SplinePath::NoWatch(void)
{
doWatch = true;
watchEnt = "none";
}
str SplinePath::GetWatch(void)
{
return watchEnt;
}
void SplinePath::SetFov(float newFov)
{
fov = newFov;
}
void SplinePath::SetFov(Event *ev)
{
fov = ev->GetFloat(1);
}
float SplinePath::GetFov(void)
{
return fov;
}
void SplinePath::SetFadeTime(float newFadeTime)
{
fadeTime = newFadeTime;
}
void SplinePath::SetFadeTime(Event *ev)
{
fadeTime = ev->GetFloat(1);
}
float SplinePath::GetFadeTime(void)
{
return fadeTime;
}
|