File: viewthing.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 (845 lines) | stat: -rw-r--r-- 20,904 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
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
/*
===========================================================================
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
===========================================================================
*/

// viewthing.cpp: Actor code for the Viewthing.
//

#include "animate.h"
#include "viewthing.h"
#include "game.h"
#include "level.h"
#include "scriptexception.h"

Event EV_ViewThing_Think
(
    "viewthing_think",
    EV_DEFAULT,
    NULL,
    NULL,
    "Called every frame to process the view thing.",
    EV_NORMAL
);
Event EV_ViewThing_ToggleAnimate
(
    "viewanimate",
    EV_CHEAT,
    NULL,
    NULL,
    "Cycle through the animations modes of the current viewthing\n"
    "No Animation\n"
    "Animation with no motion\n"
    "Animation with looping motion\n"
    "Animation with motion\n",
    EV_NORMAL
);
Event EV_ViewThing_SetModel
(
    "viewmodel",
    EV_CHEAT,
    "s",
    "viewthingModel",
    "Set the model of the current viewthing",
    EV_NORMAL
);
Event EV_ViewThing_NextFrame
(
    "viewnext",
    EV_CHEAT,
    NULL,
    NULL,
    "Advance to the next frame of animation of the current viewthing",
    EV_NORMAL
);
Event EV_ViewThing_PrevFrame
(
    "viewprev",
    EV_CHEAT,
    NULL,
    NULL,
    "Advance to the previous frame of animation of the current viewthing",
    EV_NORMAL
);
Event EV_ViewThing_NextAnim
(
    "viewnextanim",
    EV_CHEAT,
    NULL,
    NULL,
    "Advance to the next animation of the current viewthing",
    EV_NORMAL
);
Event EV_ViewThing_PrevAnim
(
    "viewprevanim",
    EV_CHEAT,
    NULL,
    NULL,
    "Advance to the previous animation of the current viewthing",
    EV_NORMAL
);
Event EV_ViewThing_ScaleUp
(
    "viewscaleup",
    EV_CHEAT,
    NULL,
    NULL,
    "Increase the scale of the current viewthing",
    EV_NORMAL
);
Event EV_ViewThing_ScaleDown
(
    "viewscaledown",
    EV_CHEAT,
    NULL,
    NULL,
    "Decrease the scale of the current viewthing",
    EV_NORMAL
);
Event EV_ViewThing_SetScale
(
    "viewscale",
    EV_CHEAT,
    "f",
    "scale",
    "Set the scale of the current viewthing",
    EV_NORMAL
);
Event EV_ViewThing_SetYaw
(
    "viewyaw",
    EV_CHEAT,
    "f",
    "yaw",
    "Set the yaw of the current viewthing",
    EV_NORMAL
);
Event EV_ViewThing_SetPitch
(
    "viewpitch",
    EV_CHEAT,
    "f",
    "pitch",
    "Set the pitch of the current viewthing",
    EV_NORMAL
);
Event EV_ViewThing_SetRoll
(
    "viewroll",
    EV_CHEAT,
    "f",
    "roll",
    "Set the roll of the current viewthing",
    EV_NORMAL
);
Event EV_ViewThing_SetAngles
(
    "viewangles",
    EV_CHEAT,
    "f[0,360]f[0,360]f[0,360]",
    "pitch yaw roll",
    "Set the angles of the current viewthing",
    EV_NORMAL
);
Event EV_ViewThing_Spawn
(
    "viewspawn",
    EV_CHEAT,
    "s",
    "model",
    "Create a viewthing with the specified model",
    EV_NORMAL
);
Event EV_ViewThing_Next
(
    "viewthingnext",
    EV_CHEAT,
    NULL,
    NULL,
    "Change the active viewthing to the next viewthing",
    EV_NORMAL
);
Event EV_ViewThing_Prev
(
    "viewthingprev",
    EV_CHEAT,
    NULL,
    NULL,
    "Change the active viewthing to the previous viewthing",
    EV_NORMAL
);
Event EV_ViewThing_Attach
(
    "viewattach",
    EV_CHEAT,
    "ss",
    "tagname model",
    "Attach a model the the specified tagname",
    EV_NORMAL
);
Event EV_ViewThing_Detach
(
    "viewdetach",
    EV_CHEAT,
    NULL,
    NULL,
    "Detach the current viewthing from its parent",
    EV_NORMAL
);
Event EV_ViewThing_DetachAll
(
    "viewdetachall",
    EV_CHEAT,
    NULL,
    NULL,
    "Detach all the models attached to the current viewthing",
    EV_NORMAL
);
Event EV_ViewThing_Delete
(
    "viewdelete",
    EV_CHEAT,
    NULL,
    NULL,
    "Delete the current viewthing",
    EV_NORMAL
);
Event EV_ViewThing_SetOrigin
(
    "vieworigin",
    EV_CHEAT,
    "fff",
    "x y z",
    "Set the origin of the current viewthing",
    EV_NORMAL
);
Event EV_ViewThing_DeleteAll
(
    "viewdeleteall",
    EV_CHEAT,
    NULL,
    NULL,
    "Delete all viewthings",
    EV_NORMAL
);
Event EV_ViewThing_LastFrame
(
    "viewlastframe",
    EV_DEFAULT,
    NULL,
    NULL,
    "Called when the view things last animation frame is displayed.",
    EV_NORMAL
);
Event EV_ViewThing_SaveOffSurfaces
(
    "viewsavesurfaces",
    EV_DEFAULT,
    NULL,
    NULL,
    "Called after the model is spawned to save off the models original surfaces.",
    EV_NORMAL
);
Event EV_ViewThing_PrintTime
(
    "_viewthing_printtime",
    EV_DEFAULT,
    NULL,
    NULL,
    "Prints out the current level.time.",
    EV_NORMAL
);
Event EV_ViewThing_SetAnim
(
    "viewsetanim",
    EV_CHEAT,
    "f",
    "animNum",
    "Set the animation absolutely based off a floating point value",
    EV_NORMAL
);

CLASS_DECLARATION(Animate, ViewMaster, NULL) {
    {&EV_ViewThing_Spawn,         &ViewMaster::Spawn        },
    {&EV_ViewThing_Next,          &ViewMaster::Next         },
    {&EV_ViewThing_Prev,          &ViewMaster::Prev         },
    {&EV_ViewThing_SetModel,      &ViewMaster::SetModelEvent},
    {&EV_ViewThing_DeleteAll,     &ViewMaster::DeleteAll    },
    {&EV_ViewThing_ToggleAnimate, &ViewMaster::PassEvent    },
    {&EV_ViewThing_NextFrame,     &ViewMaster::PassEvent    },
    {&EV_ViewThing_PrevFrame,     &ViewMaster::PassEvent    },
    {&EV_ViewThing_NextAnim,      &ViewMaster::PassEvent    },
    {&EV_ViewThing_PrevAnim,      &ViewMaster::PassEvent    },
    {&EV_ViewThing_ScaleUp,       &ViewMaster::PassEvent    },
    {&EV_ViewThing_ScaleDown,     &ViewMaster::PassEvent    },
    {&EV_ViewThing_SetScale,      &ViewMaster::PassEvent    },
    {&EV_ViewThing_SetYaw,        &ViewMaster::PassEvent    },
    {&EV_ViewThing_SetPitch,      &ViewMaster::PassEvent    },
    {&EV_ViewThing_SetRoll,       &ViewMaster::PassEvent    },
    {&EV_ViewThing_SetAngles,     &ViewMaster::PassEvent    },
    {&EV_ViewThing_Attach,        &ViewMaster::PassEvent    },
    {&EV_ViewThing_Detach,        &ViewMaster::PassEvent    },
    {&EV_ViewThing_DetachAll,     &ViewMaster::PassEvent    },
    {&EV_ViewThing_Delete,        &ViewMaster::PassEvent    },
    {&EV_ViewThing_SetOrigin,     &ViewMaster::PassEvent    },
    {&EV_ViewThing_SetAnim,       &ViewMaster::PassEvent    },
    {NULL,                        NULL                      }
};

ViewMaster Viewmodel;

ViewMaster::ViewMaster()
{
    current_viewthing = NULL;
}

void ViewMaster::Init(void)
{
    gi.AddCommand("viewanimate", NULL);
    gi.AddCommand("viewmodel", NULL);
    gi.AddCommand("viewnext", NULL);
    gi.AddCommand("viewprev", NULL);
    gi.AddCommand("viewnextanim", NULL);
    gi.AddCommand("viewprevanim", NULL);
    gi.AddCommand("viewscaleup", NULL);
    gi.AddCommand("viewscaledown", NULL);
    gi.AddCommand("viewscale", NULL);
    gi.AddCommand("viewyaw", NULL);
    gi.AddCommand("viewpitch", NULL);
    gi.AddCommand("viewroll", NULL);
    gi.AddCommand("viewangles", NULL);
    gi.AddCommand("viewspawn", NULL);
    gi.AddCommand("viewthingnext", NULL);
    gi.AddCommand("viewthingprev", NULL);
    gi.AddCommand("viewattach", NULL);
    gi.AddCommand("viewdetach", NULL);
    gi.AddCommand("viewdetachall", NULL);
    gi.AddCommand("viewdelete", NULL);
    gi.AddCommand("vieworigin", NULL);
    gi.AddCommand("viewdeleteall", NULL);
    gi.AddCommand("viewsetanim", NULL);
}

void ViewMaster::Next(Event *ev)
{
    Viewthing *viewthing;
    Entity    *ent;

    ent = (Entity *)G_FindClass(current_viewthing, "viewthing");
    if (ent) {
        current_viewthing = ent;

        viewthing = (Viewthing *)((Entity *)current_viewthing);
        gi.Printf("current viewthing model %s.\n", viewthing->model.c_str());
        viewthing->UpdateCvars();
    } else {
        gi.Printf("no more viewthings on map.\n");
    }
}

void ViewMaster::Prev(Event *ev)
{
    Viewthing *viewthing;
    Entity    *prev;
    Entity    *next;

    next = NULL;
    do {
        prev = next;
        next = (Entity *)G_FindClass(prev, "viewthing");
    } while (next != current_viewthing);

    if (prev) {
        current_viewthing = prev;

        viewthing = (Viewthing *)((Entity *)current_viewthing);
        gi.Printf("current viewthing model %s.\n", viewthing->model.c_str());
        viewthing->UpdateCvars();
    } else {
        gi.Printf("no more viewthings on map.\n");
    }
}

void ViewMaster::DeleteAll(Event *ev)
{
    Entity *next;

    for (next = (Entity *)G_FindClass(NULL, "viewthing"); next != NULL;
         next = (Entity *)G_FindClass(next, "viewthing")) {
        next->PostEvent(EV_Remove, 0);
    }

    current_viewthing = NULL;
}

void ViewMaster::Spawn(Event *ev)
{
    Viewthing *viewthing;
    str        mdl;
    Vector     forward;
    Vector     up;
    Vector     delta;
    Event     *event;
    Entity    *ent;

    mdl = ev->GetString(1);
    if (!mdl || !mdl[0]) {
        ScriptError("Must specify a model name");
        return;
    }

    // Check if we have a client
    ent = g_entities[0].entity;
    assert(ent);
    if (!ent) {
        return;
    }

    // create a new viewthing
    viewthing = new Viewthing;

    // set the current_viewthing
    current_viewthing = viewthing;

    //FIXME FIXME
    ent->angles.AngleVectors(&forward, NULL, &up);

    viewthing->baseorigin = ent->origin;
    viewthing->baseorigin += forward * 48;
    viewthing->baseorigin += up * 48;

    viewthing->setOrigin(viewthing->baseorigin);
    viewthing->droptofloor(256);

    viewthing->baseorigin = viewthing->origin;

    delta = ent->origin - viewthing->origin;
    viewthing->setAngles(delta.toAngles());

    event = new Event(EV_ViewThing_SetModel);
    event->AddString(mdl);
    viewthing->ProcessEvent(event);

    if (!gi.modeltiki(viewthing->model)) {
        ScriptError("model %s not found, viewmodel not spawned.", mdl.c_str());
        delete viewthing;
        current_viewthing = NULL;
        return;
    }
}

void ViewMaster::SetModelEvent(Event *ev)
{
    str        mdl;
    char       str[128];
    Event     *event;
    Viewthing *viewthing;

    mdl = ev->GetString(1);
    if (!mdl || !mdl[0]) {
        ScriptError("Must specify a model name");
        return;
    }

    if (!current_viewthing) {
        // try to find one on the map
        current_viewthing = (Entity *)G_FindClass(NULL, "viewthing");
        if (!current_viewthing) {
            ScriptError("No viewmodel");
            return;
        }
    }

    viewthing = (Viewthing *)((Entity *)current_viewthing);

    // Prepend 'models/' to make things easier
    str[0] = 0;
    if ((mdl[1] != ':') && Q_stricmpn(mdl, "models", 6)) {
        strcpy(str, "models/");
    }
    strcat(str, mdl);

    event = new Event(EV_ViewThing_SetModel);
    event->AddString(str);
    viewthing->ProcessEvent(event);
    viewthing->UpdateCvars();
}

void ViewMaster::PassEvent(Event *ev)
{
    Viewthing *viewthing;
    Event     *event;

    if (!current_viewthing) {
        ScriptError("No viewmodel");
        return;
    }

    viewthing = (Viewthing *)((Entity *)current_viewthing);
    if (viewthing) {
        viewthing->ProcessEvent(*ev);
    }
}

CLASS_DECLARATION(Animate, Viewthing, "viewthing") {
    {&EV_ViewThing_Think,           &Viewthing::ThinkEvent        },
    {&EV_ViewThing_LastFrame,       &Viewthing::LastFrameEvent    },
    {&EV_ViewThing_ToggleAnimate,   &Viewthing::ToggleAnimateEvent},
    {&EV_ViewThing_SetModel,        &Viewthing::SetModelEvent     },
    {&EV_ViewThing_NextFrame,       &Viewthing::NextFrameEvent    },
    {&EV_ViewThing_PrevFrame,       &Viewthing::PrevFrameEvent    },
    {&EV_ViewThing_NextAnim,        &Viewthing::NextAnimEvent     },
    {&EV_ViewThing_PrevAnim,        &Viewthing::PrevAnimEvent     },
    {&EV_ViewThing_ScaleUp,         &Viewthing::ScaleUpEvent      },
    {&EV_ViewThing_ScaleDown,       &Viewthing::ScaleDownEvent    },
    {&EV_ViewThing_SetScale,        &Viewthing::SetScaleEvent     },
    {&EV_ViewThing_SetYaw,          &Viewthing::SetYawEvent       },
    {&EV_ViewThing_SetPitch,        &Viewthing::SetPitchEvent     },
    {&EV_ViewThing_SetRoll,         &Viewthing::SetRollEvent      },
    {&EV_ViewThing_SetAngles,       &Viewthing::SetAnglesEvent    },
    {&EV_ViewThing_Attach,          &Viewthing::AttachModel       },
    {&EV_ViewThing_Detach,          &Viewthing::Delete            },
    {&EV_ViewThing_DetachAll,       &Viewthing::DetachAll         },
    {&EV_ViewThing_Delete,          &Viewthing::Delete            },
    {&EV_ViewThing_SetOrigin,       &Viewthing::ChangeOrigin      },
    {&EV_ViewThing_SaveOffSurfaces, &Viewthing::SaveSurfaces      },
    {&EV_ViewThing_PrintTime,       &Viewthing::PrintTime         },
    {&EV_ViewThing_SetAnim,         &Viewthing::SetAnim           },
    {NULL,                          NULL                          }
};

Viewthing::Viewthing(void)
{
    frame     = 0;
    animstate = 0;
    setSolidType(SOLID_NOT);
    baseorigin                  = origin;
    Viewmodel.current_viewthing = this;
    edict->s.eType              = ET_MODELANIM;
    edict->s.renderfx |= RF_SHADOW;
    edict->s.renderfx |= RF_SHADOW_PRECISE;
    edict->s.renderfx |= RF_EXTRALIGHT;

    // save off surfaces once the model is spawned
    PostEvent(EV_ViewThing_SaveOffSurfaces, FRAMETIME);

    PostEvent(EV_ViewThing_Think, FRAMETIME);
}

void Viewthing::PrintTime(Event *ev)
{
    gi.Printf("ev current frame %d leveltime %.2f\n", ev->GetInteger(1), level.time);
}

void Viewthing::UpdateCvars(qboolean quiet)
{
    gi.cvar_set("viewmodelentity", va("%d", edict->s.number));
}

void Viewthing::ThinkEvent(Event *ev)
{
    int f;
    if (animstate >= 2) {
        Vector forward;
        Vector left;
        Vector up;
        Vector realmove;

        angles.AngleVectors(&forward, &left, &up);
        realmove = left * accel[1] + up * accel[2] + forward * accel[0];
        setOrigin(baseorigin + realmove);
        gi.cvar_set(
            "viewthingorigin", va("%0.2f,%0.2f,%0.2f", edict->s.origin[0], edict->s.origin[1], edict->s.origin[2])
        );
    }
    PostEvent(EV_ViewThing_Think, FRAMETIME);
    if ((animstate > 0) && (Viewmodel.current_viewthing == this)) {
        f = GetTime() / gi.Anim_Frametime(edict->tiki, CurrentAnim());
        if (f != lastframe) {
            float time;
            lastframe = f;
            time      = f * AnimTime() / NumFrames();
            gi.Printf("current frame %d time %.2f\n", f, time);
            gi.cvar_set("viewmodeltime", va("%.2f", time));
            gi.cvar_set("viewmodelframe", va("%d", f));
            gi.cvar_set("viewmodelanim", AnimName());
        }
    }
}

void Viewthing::LastFrameEvent(Event *ev) {}

void Viewthing::ToggleAnimateEvent(Event *ev)
{
    animstate = (animstate + 1) % 4;
    setOrigin(baseorigin);
    // reset to a known state
    switch (animstate) {
    case 0:
        SetFrame();
        gi.Printf("Animation stopped.\n");
        gi.cvar_set("viewmodelanimmode", "Stopped");
        break;
    case 1:
        NewAnim(CurrentAnim());
        gi.Printf("Animation no motion.\n");
        gi.cvar_set("viewmodelanimmode", "No Motion");
        break;
    case 2:
        NewAnim(CurrentAnim(), EV_ViewThing_LastFrame);
        gi.Printf("Animation with motion and looping.\n");
        gi.cvar_set("viewmodelanimmode", "Motion and Looping");
        break;
    case 3:
        NewAnim(CurrentAnim(), EV_ViewThing_LastFrame);
        gi.Printf("Animation with motion no looping.\n");
        gi.cvar_set("viewmodelanimmode", "Motion and No Looping");
        break;
    }
    UpdateCvars(qtrue);
}

void Viewthing::SetModelEvent(Event *ev)
{
    str modelname;

    modelname = ev->GetString(1);

    setModel(modelname);

    if (gi.modeltiki(model)) {
        NewAnim(0);
        SetFrame();
    }
    UpdateCvars();
}

void Viewthing::NextFrameEvent(Event *ev)
{
    int numframes;

    numframes = NumFrames();
    if (numframes) {
        frame = (frame + 1) % numframes;
        SetFrame();
        animstate = 0;
        UpdateCvars();
    }
}

void Viewthing::PrevFrameEvent(Event *ev)
{
    int numframes;

    numframes = NumFrames();
    if (numframes) {
        frame = (frame - 1) % numframes;
        SetFrame();
        animstate = 0;
        UpdateCvars();
    }
}

void Viewthing::SetAnim(Event *ev)
{
    int numanims, anim;

    numanims = NumAnims();
    if (numanims) {
        // restore original surfaces
        memcpy(edict->s.surfaces, origSurfaces, sizeof(origSurfaces));

        anim = ev->GetFloat(1) * numanims;
        if (anim >= numanims) {
            anim = numanims - 1;
        }
        NewAnim(anim % numanims);
        frame = 0;
        SetFrame();
        animstate = 0;
        UpdateCvars();
    }
}

void Viewthing::NextAnimEvent(Event *ev)
{
    int numanims;

    numanims = NumAnims();
    if (numanims) {
        // restore original surfaces
        memcpy(edict->s.surfaces, origSurfaces, sizeof(origSurfaces));

        NewAnim((CurrentAnim() + 1) % numanims);
        frame = 0;
        SetFrame();
        animstate = 0;
        UpdateCvars();
    }
}

void Viewthing::PrevAnimEvent(Event *ev)
{
    int anim;
    int numanims;

    numanims = NumAnims();
    if (numanims) {
        // restore original surfaces
        memcpy(edict->s.surfaces, origSurfaces, sizeof(origSurfaces));

        anim = CurrentAnim() - 1;
        while (anim < 0) {
            anim += numanims;
        }
        NewAnim(anim);
        frame = 0;
        SetFrame();
        animstate = 0;
        UpdateCvars();
    }
}

void Viewthing::ScaleUpEvent(Event *ev)
{
    edict->s.scale += 0.01f;
    UpdateCvars();
}

void Viewthing::ScaleDownEvent(Event *ev)
{
    edict->s.scale -= 0.01f;
    UpdateCvars();
}

void Viewthing::SetScaleEvent(Event *ev)
{
    float s;

    if (ev->NumArgs()) {
        s              = ev->GetFloat(1);
        edict->s.scale = s;
        UpdateCvars();
    } else {
        gi.Printf("viewscale = %f\n", edict->s.scale);
    }
}

void Viewthing::SetYawEvent(Event *ev)
{
    if (ev->NumArgs() > 0) {
        angles.setYaw(ev->GetFloat(1));
        setAngles(angles);
    }
    gi.Printf("yaw = %f\n", angles.yaw());
}

void Viewthing::SetPitchEvent(Event *ev)
{
    if (ev->NumArgs() > 0) {
        angles.setPitch(ev->GetFloat(1));
        setAngles(angles);
    }
    gi.Printf("pitch = %f\n", angles.pitch());
}

void Viewthing::SetRollEvent(Event *ev)
{
    if (ev->NumArgs() > 0) {
        angles.setRoll(ev->GetFloat(1));
        setAngles(angles);
    }
    gi.Printf("roll = %f\n", angles.roll());
}

void Viewthing::SetAnglesEvent(Event *ev)
{
    if (ev->NumArgs() > 2) {
        angles.x = ev->GetFloat(1);
        angles.y = ev->GetFloat(2);
        angles.z = ev->GetFloat(3);
        setAngles(angles);
    }

    gi.Printf("angles = %f, %f, %f\n", angles.x, angles.y, angles.z);
}

void Viewthing::AttachModel(Event *ev)
{
    Event     *event;
    Viewthing *child;

    child = new Viewthing;
    child->setModel(ev->GetString(2));

    //
    // attach the child
    //
    event = new Event(EV_Attach);
    event->AddEntity(this);
    event->AddString(ev->GetString(1));
    child->ProcessEvent(event);
}

void Viewthing::Delete(Event *ev)
{
    Viewmodel.current_viewthing = NULL;
    PostEvent(EV_Remove, 0);
    Viewmodel.PostEvent(EV_ViewThing_Next, 0);
}

void Viewthing::DetachAll(Event *ev)
{
    int i;
    int num;

    num = numchildren;
    for (i = 0; i < MAX_MODEL_CHILDREN; i++) {
        Entity *ent;
        if (!children[i]) {
            continue;
        }
        ent = (Entity *)G_GetEntity(children[i]);
        ent->PostEvent(EV_Remove, 0);
        num--;
        if (!num) {
            break;
        }
    }
}

void Viewthing::ChangeOrigin(Event *ev)
{
    if (ev->NumArgs()) {
        origin.x = ev->GetFloat(1);
        origin.y = ev->GetFloat(2);
        origin.z = ev->GetFloat(3);
        setOrigin(origin);
        baseorigin = origin;
        UpdateCvars();
    }
    gi.Printf("vieworigin = x%f y%f z%f\n", origin.x, origin.y, origin.z);
}

void Viewthing::SaveSurfaces(Event *ev)
{
    memcpy(origSurfaces, edict->s.surfaces, sizeof(origSurfaces));
}