File: gravpath.cpp

package info (click to toggle)
openmohaa 0.82.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 34,192 kB
  • sloc: cpp: 315,720; ansic: 275,789; sh: 312; xml: 246; asm: 141; makefile: 7
file content (679 lines) | stat: -rw-r--r-- 16,441 bytes parent folder | download
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
/*
===========================================================================
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
===========================================================================
*/

// gravpath.cpp: Gravity path - Used for underwater currents and wells.

#include "g_local.h"
#include "entity.h"
#include "gravpath.h"
#include "container.h"
#include "navigate.h"
#include "misc.h"
#include "player.h"
#include "g_phys.h"
#include "debuglines.h"

GravPathManager gravPathManager;

CLASS_DECLARATION(Class, GravPathManager, NULL) {
    {NULL, NULL}
};

GravPathManager::~GravPathManager()
{
    Reset();
}

void GravPathManager::Reset(void)
{
    while (pathList.NumObjects() > 0) {
        delete (GravPath *)pathList.ObjectAt(1);
    }

    pathList.FreeObjectList();
}

void GravPathManager::AddPath(GravPath *p)
{
    pathList.AddObject(p);
}

void GravPathManager::RemovePath(GravPath *p)
{
    pathList.RemoveObject(p);
}

void GravPathManager::DrawGravPaths(void)
{
    int i;
    int num = pathList.NumObjects();

    for (i = 1; i <= num; i++) {
        GravPath *p = (GravPath *)pathList.ObjectAt(i);

        p->DrawPath(1, 0, 0);
    }
}

Vector GravPathManager::CalculateGravityPull(Entity& ent, Vector pos, qboolean *force, float *max_speed)
{
    int           i, num;
    GravPath     *p;
    GravPathNode *node;
    Vector        point;
    Vector        newpoint;
    Vector        dir;
    float         bestdist = 99999;
    float         dist;
    float         speed;
    float         radius;
    Vector        velocity;
    int           bestpath = 0;
    int           entity_contents, grav_contents;

    num = pathList.NumObjects();
    if (!num) {
        return vec_zero;
    }

    entity_contents = gi.pointcontents(ent.origin, 0);

    for (i = 1; i <= num; i++) {
        p = (GravPath *)pathList.ObjectAt(i);

        if (!p) {
            continue;
        }

        // Check to see if path is active
        node = p->GetNode(1);
        if (!node || !node->active) {
            continue;
        }

        // Check to see if the contents are the same
        grav_contents = gi.pointcontents(node->origin, 0);

        // If grav node is in water, make sure ent is too.
        if ((grav_contents & CONTENTS_WATER) && !(entity_contents & CONTENTS_WATER)) {
            continue;
        }

        // Test to see if we are in this path's bounding box
        if ((pos.x < p->maxs.x) && (pos.y < p->maxs.y) && (pos.z < p->maxs.z) && (pos.x > p->mins.x)
            && (pos.y > p->mins.y) && (pos.z > p->mins.z)) {
            point = p->ClosestPointOnPath(pos, ent, &dist, &speed, &radius);

            // If the closest distance on the path is greater than the radius, then
            // do not consider this path.

            if (dist > radius) {
                continue;
            } else if (dist < bestdist) {
                bestpath = i;
                bestdist = dist;
            }
        }
    }

    if (!bestpath) {
        return vec_zero;
    }

    p = (GravPath *)pathList.ObjectAt(bestpath);
    if (!p) {
        return velocity;
    }
    *force   = p->force;
    dist     = p->DistanceAlongPath(pos, &speed);
    newpoint = p->PointAtDistance(pos, dist + speed, ent.isSubclassOf(Player), max_speed);
    dir      = newpoint - pos;
    dir.normalize();
    velocity = dir * speed;

    //velocity *= .75;
    return velocity;
}

/*****************************************************************************/
/*QUAKED info_grav_pathnode (0 0 .5) (-16 -16 0) (16 16 32) HEADNODE FORCE PULL_UPWARDS
 "radius" Radius of the effect of the pull (Default is 256)
 "speed"  Speed of the pull (Use negative for a repulsion) (Default is 100)

  Set HEADNODE to signify the head of the path.
  Set FORCE if you want un-fightable gravity ( i.e. can't go backwards )
  Set PULL_UPWARDS if you want the gravnodes to pull you upwards also
******************************************************************************/

#define PULL_UPWARDS (1 << 2)

Event EV_GravPath_Create
(
    "gravpath_create",
    EV_DEFAULT,
    NULL,
    NULL,
    "Create the grav path.",
    EV_NORMAL
);
Event EV_GravPath_Activate
(
    "activate",
    EV_DEFAULT,
    NULL,
    NULL,
    "Activate the grav path.",
    EV_NORMAL
);
Event EV_GravPath_Deactivate
(
    "deactivate",
    EV_DEFAULT,
    NULL,
    NULL,
    "Deactivate the grav path.",
    EV_NORMAL
);
Event EV_GravPath_SetSpeed
(
    "speed",
    EV_DEFAULT,
    "f",
    "speed",
    "Set the speed of the grav path.",
    EV_NORMAL
);
Event EV_GravPath_SetMaxSpeed
(
    "maxspeed",
    EV_DEFAULT,
    "f",
    "maxspeed",
    "Set the max speed of the grav path.",
    EV_NORMAL
);
Event EV_GravPath_SetRadius
(
    "radius",
    EV_DEFAULT,
    "f",
    "radius",
    "Set the radius of the grav path.",
    EV_NORMAL
);

CLASS_DECLARATION(Entity, GravPathNode, "info_grav_pathnode") {
    {&EV_GravPath_Create,      &GravPathNode::CreatePath },
    {&EV_GravPath_Activate,    &GravPathNode::Activate   },
    {&EV_GravPath_Deactivate,  &GravPathNode::Deactivate },
    {&EV_GravPath_SetSpeed,    &GravPathNode::SetSpeed   },
    {&EV_GravPath_SetMaxSpeed, &GravPathNode::SetMaxSpeed},
    {&EV_GravPath_SetRadius,   &GravPathNode::SetRadius  },
    {NULL,                     NULL                      }
};

GravPathNode::GravPathNode()
{
    if (LoadingSavegame) {
        // all data will be setup by the archive function
        return;
    }

    setMoveType(MOVETYPE_NONE);
    setSolidType(SOLID_NOT);
    hideModel();

    speed     = 100.0f;
    max_speed = 200.0f;
    radius    = 256.0f;
    headnode  = spawnflags & 1;
    active    = true;

    // This is the head of a new path, post an event to create the path
    if (headnode) {
        PostEvent(EV_GravPath_Create, 0);
    }
}

void GravPathNode::SetSpeed(Event *ev)
{
    speed = ev->GetFloat(1);
}

void GravPathNode::SetMaxSpeed(Event *ev)
{
    max_speed = ev->GetFloat(1);
}

void GravPathNode::SetRadius(Event *ev)
{
    radius = ev->GetFloat(1);
}

float GravPathNode::Speed(void)
{
    if (active) {
        return speed;
    } else {
        return 0;
    }
}

float GravPathNode::MaxSpeed(void)
{
    return max_speed;
}

void GravPathNode::Activate(Event *ev)
{
    GravPathNode *node;
    Entity       *ent;
    const char   *target;

    active = true;
    node   = this;
    // Go through the entire path and activate it
    target = node->Target();
    while (target[0]) {
        ent = (Entity *)G_FindTarget(NULL, target);
        if (ent) {
            node = (GravPathNode *)ent;
            assert(node);
            node->active = true;
        } else {
            gi.Error(ERR_DROP, "GravPathNode::CreatePath: target %s not found\n", target);
        }
        target = node->Target();
    }
}

void GravPathNode::Deactivate(Event *ev)
{
    GravPathNode *node;
    Entity       *ent;
    const char   *target;

    active = false;
    node   = this;
    // Go through the entire path and activate it
    target = node->Target();
    while (target[0]) {
        ent = (Entity *)G_FindTarget(NULL, target);
        if (ent) {
            node = (GravPathNode *)ent;
            assert(node);
            node->active = false;
        } else {
            gi.Error(ERR_DROP, "GravPathNode::CreatePath: target %s not found\n", target);
        }
        target = node->Target();
    }
}

void GravPathNode::CreatePath(Event *ev)
{
    const char   *target;
    GravPath     *path = new GravPath;
    GravPathNode *node;
    Entity       *ent;

    ClearBounds(path->mins, path->maxs);

    // This node is the head of a path, create a new path in the path manager.
    // and add it in, then add all of it's children in the path.
    node = this;
    path->AddNode(node);
    path->force = spawnflags & 2;

    // Make the path from the targetlist.
    target = node->Target();
    while (target[0]) {
        ent = (Entity *)G_FindTarget(NULL, target);
        if (ent) {
            node = (GravPathNode *)ent;
            assert(node);
            path->AddNode(node);
        } else {
            gi.Error(ERR_DROP, "GravPathNode::CreatePath: target %s not found\n", target);
        }
        target = node->Target();
    }

    // Set the origin.
    path->origin = path->mins + path->maxs;
    path->origin *= 0.5f;
}

CLASS_DECLARATION(Listener, GravPath, NULL) {
    {NULL, NULL}
};

GravPath::GravPath()
{
    pathlength = 0;
    from       = NULL;
    to         = NULL;
    nextnode   = 1;

    if (!LoadingSavegame) {
        gravPathManager.AddPath(this);
    }
}

GravPath::~GravPath()
{
    pathlength = 0;
    from       = NULL;
    to         = NULL;
    nextnode   = 1;
    gravPathManager.RemovePath(this);
}

void GravPath::Clear(void)
{
    nextnode   = 1;
    pathlength = 0;
    from       = NULL;
    to         = NULL;
    pathlist.FreeObjectList();
}

void GravPath::Reset(void)
{
    nextnode = 1;
}

GravPathNode *GravPath::Start(void)
{
    return from;
}

GravPathNode *GravPath::End(void)
{
    return to;
}

void GravPath::AddNode(GravPathNode *node)
{
    int    num;
    Vector r, addp;

    if (!from) {
        from = node;
    }

    to = node;
    pathlist.AddObject(GravPathNodePtr(node));

    num = NumNodes();
    if (num > 1) {
        pathlength += (node->origin - GetNode(num)->origin).length();
    }

    r.setXYZ(node->Radius(), node->Radius(), node->Radius());
    addp = node->origin + r;
    AddPointToBounds(addp, mins, maxs);
    addp = node->origin - r;
    AddPointToBounds(addp, mins, maxs);
}

GravPathNode *GravPath::GetNode(int num)
{
    return pathlist.ObjectAt(num);
}

GravPathNode *GravPath::NextNode(void)
{
    if (nextnode <= NumNodes()) {
        return pathlist.ObjectAt(nextnode++);
    }
    return NULL;
}

Vector GravPath::ClosestPointOnPath(Vector pos, Entity& ent, float *ret_dist, float *speed, float *radius)
{
    GravPathNode *s;
    GravPathNode *e;
    int           num;
    int           i;
    float         bestdist;
    Vector        bestpoint;
    float         dist;
    float         segmentlength;
    Vector        delta;
    Vector        p1;
    Vector        p2;
    Vector        p3;
    float         t;
    trace_t       trace;

    num = NumNodes();
    s   = GetNode(1);
    trace =
        G_Trace(pos, ent.mins, ent.maxs, s->origin, &ent, MASK_PLAYERSOLID, false, "GravPath::ClosestPointOnPath 1");
    bestpoint = s->origin;
    delta     = bestpoint - pos;
    bestdist  = delta.length();
    *speed    = s->Speed();
    *radius   = s->Radius();

    for (i = 2; i <= num; i++) {
        e = GetNode(i);

        // check if we're closest to the endpoint
        delta = e->origin - pos;
        dist  = delta.length();

        if (dist < bestdist) {
            trace = G_Trace(
                pos, ent.mins, ent.maxs, e->origin, &ent, MASK_PLAYERSOLID, false, "GravPath::ClosestPointOnPath 2"
            );
            bestdist  = dist;
            bestpoint = e->origin;
            *speed    = e->Speed();
            *radius   = e->Radius();
        }

        // check if we're closest to the segment
        p1            = e->origin - s->origin;
        segmentlength = p1.length();
        p1 *= 1 / segmentlength;
        p2 = pos - s->origin;

        t = p1 * p2;
        if ((t > 0) && (t < segmentlength)) {
            p3 = (p1 * t) + s->origin;

            delta = p3 - pos;
            dist  = delta.length();
            if (dist < bestdist) {
                trace = G_Trace(
                    pos, ent.mins, ent.maxs, p3, &ent, MASK_PLAYERSOLID, false, "GravPath::ClosestPointOnPath 3"
                );
                bestdist  = dist;
                bestpoint = p3;
                *speed    = (e->Speed() * t) + (s->Speed() * (1.0f - t));
                *radius   = (e->Radius() * t) + (s->Radius() * (1.0f - t));
            }
        }

        s = e;
    }
    *ret_dist = bestdist;
    return bestpoint;
}

float GravPath::DistanceAlongPath(Vector pos, float *speed)
{
    GravPathNode *s;
    GravPathNode *e;
    int           num;
    int           i;
    float         bestdist;
    float         dist;
    float         segmentlength;
    Vector        delta;
    Vector        segment;
    Vector        p1;
    Vector        p2;
    Vector        p3;
    float         t;
    float         pathdist;
    float         bestdistalongpath;
    float         oosl;
    pathdist = 0;

    num               = NumNodes();
    s                 = GetNode(1);
    delta             = s->origin - pos;
    bestdist          = delta.length();
    bestdistalongpath = 0;
    *speed            = s->Speed();

    for (i = 2; i <= num; i++) {
        e = GetNode(i);

        segment       = e->origin - s->origin;
        segmentlength = segment.length();

        // check if we're closest to the endpoint
        delta = e->origin - pos;
        dist  = delta.length();

        if (dist < bestdist) {
            bestdist          = dist;
            bestdistalongpath = pathdist + segmentlength;
            *speed            = e->Speed();
        }

        // check if we're closest to the segment
        oosl = (1 / segmentlength);
        p1   = segment * oosl;
        p1.normalize();
        p2 = pos - s->origin;

        t = p1 * p2;
        if ((t > 0) && (t < segmentlength)) {
            p3 = (p1 * t) + s->origin;

            delta = p3 - pos;
            dist  = delta.length();
            if (dist < bestdist) {
                bestdist          = dist;
                bestdistalongpath = pathdist + t;

                t *= oosl;
                *speed = (e->Speed() * t) + (s->Speed() * (1.0f - t));
            }
        }

        s = e;
        pathdist += segmentlength;
    }

    return bestdistalongpath;
}

Vector GravPath::PointAtDistance(Vector pos, float dist, qboolean is_player, float *max_speed)
{
    GravPathNode *s;
    GravPathNode *e;
    int           num;
    int           i;
    Vector        delta;
    Vector        p1;
    float         t;
    float         pathdist;
    float         segmentlength;

    num      = NumNodes();
    s        = GetNode(1);
    pathdist = 0;

    for (i = 2; i <= num; i++) {
        e = GetNode(i);

        delta         = e->origin - s->origin;
        segmentlength = delta.length();

        if ((pathdist + segmentlength) > dist) {
            t = dist - pathdist;

            p1 = delta * (t / segmentlength);
            //         return p1 + s->origin;

            if (e->spawnflags & PULL_UPWARDS && is_player) {
                p1.z = p1.length() / 2;
            }

            *max_speed = e->MaxSpeed();

            return p1 + pos;
        }

        s = e;
        pathdist += segmentlength;
    }

    *max_speed = s->MaxSpeed();

    // cap it off at start or end of path
    return s->origin;
}

void GravPath::DrawPath(float r, float g, float b)
{
    Vector        s;
    Vector        e;
    Vector        offset;
    GravPathNode *node;
    int           num;
    int           i;

    num  = NumNodes();
    node = GetNode(1);
    s    = node->origin;
    G_DebugBBox(s, Vector(8, 8, 8), Vector(-8, -8, -8), 0, 1, 0, 1);
    offset = Vector(r, g, b) * 4 + Vector(0, 0, 0);
    offset = Vector(0, 0, 0);

    for (i = 2; i <= num; i++) {
        node = GetNode(i);
        e    = node->origin;

        G_DebugLine(s + offset, e + offset, r, g, b, 1);
        G_DebugBBox(e, Vector(8, 8, 8), Vector(-8, -8, -8), 0, 1, 0, 1);
        s = e;
    }

    G_DebugBBox(origin, mins - origin, maxs - origin, 1, 0, 0, 1);
}

int GravPath::NumNodes(void)
{
    return pathlist.NumObjects();
}

float GravPath::Length(void)
{
    return pathlength;
}