/
actors.c
1493 lines (1394 loc) · 38.8 KB
/
actors.c
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
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (C) 1995 Ronny Wester
Copyright (C) 2003 Jeremy Chin
Copyright (C) 2003-2007 Lucas Martin-King
This program 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.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
This file incorporates work covered by the following copyright and
permission notice:
Copyright (c) 2013-2016, Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "actors.h"
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "actor_placement.h"
#include "ai_coop.h"
#include "ai_utils.h"
#include "ammo.h"
#include "character.h"
#include "collision.h"
#include "config.h"
#include "drawtools.h"
#include "events.h"
#include "game_events.h"
#include "log.h"
#include "pic_manager.h"
#include "sounds.h"
#include "defs.h"
#include "objs.h"
#include "pickup.h"
#include "gamedata.h"
#include "triggers.h"
#include "hiscores.h"
#include "mission.h"
#include "game.h"
#include "utils.h"
#define FOOTSTEP_DISTANCE_PLUS 380
#define REPEL_STRENGTH 14
#define SLIDE_LOCK 50
#define SLIDE_X (TILE_WIDTH / 3)
#define SLIDE_Y (TILE_HEIGHT / 3)
#define VEL_DECAY_X (TILE_WIDTH * 2)
#define VEL_DECAY_Y (TILE_WIDTH * 2) // Note: deliberately tile width
#define SOUND_LOCK_WEAPON_CLICK 20
#define DROP_GUN_CHANCE 0.2
#define DRAW_RADIAN_SPEED (PI/16)
#define BLEED_PERCENTAGE 25 // start bleeding if below this % of health
CArray gPlayerIds;
CArray gActors;
static unsigned int sActorUIDs = 0;
void ActorSetState(TActor *actor, const ActorAnimation state)
{
actor->anim = AnimationGetActorAnimation(state);
}
static void CheckPickups(TActor *actor);
void UpdateActorState(TActor * actor, int ticks)
{
Weapon *gun = ActorGetGun(actor);
WeaponUpdate(
gun, ticks, actor->Pos, actor->direction, actor->PlayerUID);
// If we're ready to pick up, always check the pickups
if (actor->PickupAll && !gCampaign.IsClient)
{
CheckPickups(actor);
}
if (actor->health > 0)
{
if (actor->lastHealth != actor->health)
{
actor->lastHealth > actor->health ? -- actor->lastHealth:
++ actor->lastHealth;
}
actor->flamed = MAX(0, actor->flamed - ticks);
if (actor->poisoned)
{
if ((actor->poisoned & 7) == 0)
{
InjureActor(actor, 1);
}
actor->poisoned = MAX(0, actor->poisoned - ticks);
}
actor->petrified = MAX(0, actor->petrified - ticks);
actor->confused = MAX(0, actor->confused - ticks);
}
actor->slideLock = MAX(0, actor->slideLock - ticks);
TileItemUpdate(&actor->tileItem, ticks);
actor->stateCounter = MAX(0, actor->stateCounter - ticks);
if (actor->stateCounter > 0)
{
return;
}
if (actor->health <= 0) {
actor->dead++;
actor->MoveVel = Vec2iZero();
actor->stateCounter = 4;
actor->tileItem.flags = 0;
return;
}
// Draw rotation interpolation
const float targetRadians = (float)dir2radians[actor->direction];
if (actor->DrawRadians - targetRadians > PI)
{
actor->DrawRadians -= 2 * (float)PI;
}
if (actor->DrawRadians - targetRadians < -PI)
{
actor->DrawRadians += 2 * (float)PI;
}
const float dr = actor->DrawRadians - targetRadians;
if (dr < 0)
{
actor->DrawRadians += (float)MIN(DRAW_RADIAN_SPEED*ticks, -dr);
}
else if (dr > 0)
{
actor->DrawRadians -= (float)MIN(DRAW_RADIAN_SPEED*ticks, dr);
}
// Footstep sounds
// Step on 1
// TODO: custom animation and footstep frames
if (ConfigGetBool(&gConfig, "Sound.Footsteps") &&
AnimationGetFrame(&actor->anim) == STATE_WALKING_1 &&
actor->anim.newFrame)
{
SoundPlayAtPlusDistance(
&gSoundDevice,
SoundGetRandomFootstep(&gSoundDevice),
Vec2iNew(actor->tileItem.x, actor->tileItem.y),
FOOTSTEP_DISTANCE_PLUS);
}
// Animation
AnimationUpdate(&actor->anim, ticks);
// Chatting
actor->ChatterCounter = MAX(0, actor->ChatterCounter - ticks);
if (actor->ChatterCounter == 0)
{
// Stop chatting
strcpy(actor->Chatter, "");
}
}
static Vec2i GetConstrainedFullPos(
const Map *map, const Vec2i fromFull, const Vec2i toFull,
const Vec2i size);
static void OnMove(TActor *a);
bool TryMoveActor(TActor *actor, Vec2i pos)
{
CASSERT(!Vec2iEqual(actor->Pos, pos), "trying to move to same position");
actor->hasCollided = true;
actor->CanPickupSpecial = false;
const Vec2i oldPos = actor->Pos;
pos = GetConstrainedFullPos(&gMap, actor->Pos, pos, actor->tileItem.size);
if (Vec2iEqual(oldPos, pos))
{
return false;
}
// Check for object collisions
// Only do this if we are the owner of the actor, since this may lead to
// melee damage
if ((!gCampaign.IsClient && actor->PlayerUID < 0) ||
ActorIsLocalPlayer(actor->uid))
{
Vec2i realPos = Vec2iFull2Real(pos);
TTileItem *target = CollideGetFirstItem(
&actor->tileItem, realPos, TILEITEM_IMPASSABLE,
CalcCollisionTeam(1, actor),
IsPVP(gCampaign.Entry.Mode));
if (target)
{
Weapon *gun = ActorGetGun(actor);
const TObject *object = target->kind == KIND_OBJECT ?
CArrayGet(&gObjs, target->id) : NULL;
if (!gun->Gun->CanShoot && actor->health > 0 &&
(!object || !ObjIsDangerous(object)))
{
if (CanHit(actor->flags, actor->uid, target))
{
// Tell the server that we want to melee something
GameEvent e = GameEventNew(GAME_EVENT_ACTOR_MELEE);
e.u.Melee.UID = actor->uid;
strcpy(e.u.Melee.BulletClass, gun->Gun->Bullet->Name);
e.u.Melee.TargetKind = target->kind;
switch (target->kind)
{
case KIND_CHARACTER:
e.u.Melee.TargetUID =
((const TActor *)CArrayGet(&gActors, target->id))->uid;
e.u.Melee.HitType = HIT_FLESH;
break;
case KIND_OBJECT:
e.u.Melee.TargetUID =
((const TObject *)CArrayGet(&gObjs, target->id))->uid;
e.u.Melee.HitType = HIT_OBJECT;
break;
default:
CASSERT(false, "cannot damage target kind");
break;
}
if (gun->soundLock <= 0)
{
gun->soundLock += gun->Gun->SoundLockLength;
}
else
{
e.u.Melee.HitType = (int)HIT_NONE;
}
GameEventsEnqueue(&gGameEvents, e);
}
return false;
}
Vec2i realYPos = Vec2iFull2Real(Vec2iNew(actor->Pos.x, pos.y));
if (CollideGetFirstItem(
&actor->tileItem, realYPos, TILEITEM_IMPASSABLE,
CalcCollisionTeam(1, actor),
IsPVP(gCampaign.Entry.Mode)))
{
pos.y = actor->Pos.y;
}
Vec2i realXPos = Vec2iFull2Real(Vec2iNew(pos.x, actor->Pos.y));
if (CollideGetFirstItem(
&actor->tileItem, realXPos, TILEITEM_IMPASSABLE,
CalcCollisionTeam(1, actor),
IsPVP(gCampaign.Entry.Mode)))
{
pos.x = actor->Pos.x;
}
if (pos.x != actor->Pos.x && pos.y != actor->Pos.y)
{
// Both x-only or y-only movement are viable,
// i.e. we are colliding corner vs corner
// Arbitrarily choose x-only movement
pos.y = actor->Pos.y;
}
realPos = Vec2iFull2Real(pos);
if ((pos.x == actor->Pos.x && pos.y == actor->Pos.y) ||
IsCollisionWithWall(realPos, actor->tileItem.size))
{
return false;
}
}
}
actor->Pos = pos;
OnMove(actor);
actor->hasCollided = false;
return true;
}
// Get a movement position that is constrained by collisions
// May return a position that is the same as the 'from', that is, we cannot
// move in the direction specified.
// Note: must use full coordinates to do collisions, despite collisions using
// real coordinates, because fractional movement will be blocked otherwise
// since real coordinates are the same.
static Vec2i GetConstrainedFullPos(
const Map *map, const Vec2i fromFull, const Vec2i toFull,
const Vec2i size)
{
// Check collision with wall
if (!IsCollisionWithWall(Vec2iFull2Real(toFull), size))
{
// Not in collision; just return where we wanted to go
return toFull;
}
CASSERT(size.x >= size.y, "tall collision not supported");
const Vec2i dv = Vec2iMinus(toFull, fromFull);
// If moving diagonally, use rectangular bounds and
// try to move in only x or y directions
if (dv.x != 0 && dv.y != 0)
{
// X-only movement
const Vec2i xVec = Vec2iNew(toFull.x, fromFull.y);
if (!IsCollisionWithWall(Vec2iFull2Real(xVec), size))
{
return xVec;
}
// Y-only movement
const Vec2i yVec = Vec2iNew(fromFull.x, toFull.y);
if (!IsCollisionWithWall(Vec2iFull2Real(yVec), size))
{
return yVec;
}
// If we're still stuck, we're possibly stuck on a corner which is not
// in collision with a diamond but is colliding with the box.
// If so try x- or y-only movement, but with the benefit of diamond
// slipping.
const Vec2i xPos = GetConstrainedFullPos(map, fromFull, xVec, size);
if (!Vec2iEqual(xPos, fromFull))
{
return xPos;
}
const Vec2i yPos = GetConstrainedFullPos(map, fromFull, yVec, size);
if (!Vec2iEqual(yPos, fromFull))
{
return yPos;
}
}
// Now check diagonal movement, if we were moving in an x- or y-
// only direction
// Note: we're moving at extra speed because dx/dy are only magnitude 1;
// if we divide then we get 0 which ruins the logic
if (dv.x == 0)
{
// Moving up or down; try moving to the left or right diagonally
// Scale X movement because our diamond is wider than tall, so we
// may need to scale the diamond wider.
const int xScale =
size.x > size.y ? (int)ceil((double)size.x / size.y) : 1;
const Vec2i diag1Vec =
Vec2iAdd(fromFull, Vec2iNew(-dv.y * xScale, dv.y));
if (!IsCollisionDiamond(map, Vec2iFull2Real(diag1Vec), size))
{
return diag1Vec;
}
const Vec2i diag2Vec =
Vec2iAdd(fromFull, Vec2iNew(dv.y * xScale, dv.y));
if (!IsCollisionDiamond(map, Vec2iFull2Real(diag2Vec), size))
{
return diag2Vec;
}
}
else if (dv.y == 0)
{
// Moving left or right; try moving up or down diagonally
const Vec2i diag1Vec =
Vec2iAdd(fromFull, Vec2iNew(dv.x, -dv.x));
if (!IsCollisionDiamond(map, Vec2iFull2Real(diag1Vec), size))
{
return diag1Vec;
}
const Vec2i diag2Vec =
Vec2iAdd(fromFull, Vec2iNew(dv.x, dv.x));
if (!IsCollisionDiamond(map, Vec2iFull2Real(diag2Vec), size))
{
return diag2Vec;
}
}
// All alternative movements are in collision; don't move
return fromFull;
}
void ActorMove(const NActorMove am)
{
TActor *a = ActorGetByUID(am.UID);
if (a == NULL || !a->isInUse) return;
a->Pos = Net2Vec2i(am.Pos);
a->MoveVel = Net2Vec2i(am.MoveVel);
OnMove(a);
}
static void CheckTrigger(const Vec2i tilePos);
static void CheckRescue(const TActor *a);
static void OnMove(TActor *a)
{
const Vec2i realPos = Vec2iFull2Real(a->Pos);
MapTryMoveTileItem(&gMap, &a->tileItem, realPos);
if (MapIsTileInExit(&gMap, &a->tileItem))
{
a->action = ACTORACTION_EXITING;
}
else
{
a->action = ACTORACTION_MOVING;
}
if (!gCampaign.IsClient)
{
CheckTrigger(Vec2iToTile(realPos));
CheckPickups(a);
CheckRescue(a);
}
}
static void CheckTrigger(const Vec2i tilePos)
{
const Tile *t = MapGetTile(&gMap, tilePos);
CA_FOREACH(Trigger *, tp, t->triggers)
if (TriggerCanActivate(*tp, gMission.KeyFlags))
{
GameEvent e = GameEventNew(GAME_EVENT_TRIGGER);
e.u.TriggerEvent.ID = (*tp)->id;
e.u.TriggerEvent.Tile = Vec2i2Net(tilePos);
GameEventsEnqueue(&gGameEvents, e);
}
CA_FOREACH_END()
}
// Check if the player can pickup any item
static bool CheckPickupFunc(TTileItem *ti, void *data);
static void CheckPickups(TActor *actor)
{
// NPCs can't pickup
if (actor->PlayerUID < 0)
{
return;
}
CollideTileItems(
&actor->tileItem, Vec2iFull2Real(actor->Pos), 0,
CalcCollisionTeam(true, actor),
IsPVP(gCampaign.Entry.Mode), CheckPickupFunc, actor);
}
static bool CheckPickupFunc(TTileItem *ti, void *data)
{
// Always return true, as we can pickup multiple items in one go
if (ti->kind != KIND_PICKUP) return true;
TActor *a = data;
PickupPickup(a, CArrayGet(&gPickups, ti->id), a->PickupAll);
return true;
}
static void CheckRescue(const TActor *a)
{
// NPCs can't rescue
if (a->PlayerUID < 0) return;
// Check an area slightly bigger than the actor's size for rescue
// objectives
#define RESCUE_CHECK_PAD 2
const TTileItem *target = OverlapGetFirstItem(
&a->tileItem, Vec2iFull2Real(a->Pos),
Vec2iAdd(a->tileItem.size, Vec2iNew(RESCUE_CHECK_PAD, RESCUE_CHECK_PAD)),
TILEITEM_IMPASSABLE,
CalcCollisionTeam(true, a),
IsPVP(gCampaign.Entry.Mode));
if (target != NULL && target->kind == KIND_CHARACTER)
{
TActor *other = CArrayGet(&gActors, target->id);
CASSERT(other->isInUse, "Cannot find nonexistent player");
if (other->flags & FLAGS_PRISONER)
{
other->flags &= ~FLAGS_PRISONER;
GameEvent e = GameEventNew(GAME_EVENT_RESCUE_CHARACTER);
e.u.Rescue.UID = other->uid;
GameEventsEnqueue(&gGameEvents, e);
UpdateMissionObjective(
&gMission, other->tileItem.flags, OBJECTIVE_RESCUE, 1);
}
}
}
void ActorHeal(TActor *actor, int health)
{
actor->lastHealth = actor->health;
actor->health += health;
actor->health = MIN(actor->health, ActorGetCharacter(actor)->maxHealth);
}
void InjureActor(TActor * actor, int injury)
{
const int lastHealth = actor->health;
actor->health -= injury;
if (lastHealth > 0 && actor->health <= 0)
{
actor->stateCounter = 0;
const Vec2i pos = Vec2iNew(actor->tileItem.x, actor->tileItem.y);
SoundPlayAt(&gSoundDevice, SoundGetRandomScream(&gSoundDevice), pos);
if (actor->PlayerUID >= 0)
{
SoundPlayAt(
&gSoundDevice,
StrSound("hahaha"),
pos);
}
if (actor->tileItem.flags & TILEITEM_OBJECTIVE)
{
UpdateMissionObjective(
&gMission, actor->tileItem.flags, OBJECTIVE_KILL, 1);
// If we've killed someone we have rescued, deduct from the
// rescue objective
if (!(actor->flags & FLAGS_PRISONER))
{
UpdateMissionObjective(
&gMission, actor->tileItem.flags, OBJECTIVE_RESCUE, -1);
}
}
}
}
void ActorAddAmmo(TActor *actor, const int ammoId, const int amount)
{
int *ammo = CArrayGet(&actor->ammo, ammoId);
*ammo += amount;
const int ammoMax = AmmoGetById(&gAmmo, ammoId)->Max;
*ammo = CLAMP(*ammo, 0, ammoMax);
}
bool ActorUsesAmmo(const TActor *actor, const int ammoId)
{
CA_FOREACH(const Weapon, w, actor->guns)
if (w->Gun->AmmoId == ammoId)
{
return true;
}
CA_FOREACH_END()
return false;
}
static bool ActorHasGun(const TActor *a, const GunDescription *gun);
void ActorReplaceGun(const NActorReplaceGun rg)
{
TActor *a = ActorGetByUID(rg.UID);
if (a == NULL || !a->isInUse) return;
const GunDescription *gun = StrGunDescription(rg.Gun);
CASSERT(gun != NULL, "cannot find gun");
// If player already has gun, don't do anything
if (ActorHasGun(a, gun))
{
return;
}
Weapon w = WeaponCreate(gun);
if (a->guns.size <= rg.GunIdx)
{
CASSERT(rg.GunIdx < a->guns.size + 1, "gun idx would leave gap");
CArrayPushBack(&a->guns, &w);
// Switch immediately to picked up gun
a->gunIndex = (int)a->guns.size - 1;
}
else
{
memcpy(CArrayGet(&a->guns, rg.GunIdx), &w, a->guns.elemSize);
}
SoundPlayAt(&gSoundDevice, gun->SwitchSound, Vec2iFull2Real(a->Pos));
}
static bool ActorHasGun(const TActor *a, const GunDescription *gun)
{
CA_FOREACH(const Weapon, w, a->guns)
if (w->Gun == gun)
{
return true;
}
CA_FOREACH_END()
return false;
}
// Set AI state and possibly say something based on the state
void ActorSetAIState(TActor *actor, const AIState s)
{
if (AIContextSetState(actor->aiContext, s) &&
AIContextShowChatter(
actor->aiContext, ConfigGetEnum(&gConfig, "Interface.AIChatter")))
{
// Say something for a while
strcpy(actor->Chatter, AIStateGetChatterText(actor->aiContext->State));
actor->ChatterCounter = 2;
}
}
void Shoot(TActor *actor)
{
Weapon *gun = ActorGetGun(actor);
if (!ActorCanFire(actor))
{
if (!WeaponIsLocked(gun) && ConfigGetBool(&gConfig, "Game.Ammo"))
{
CASSERT(ActorGunGetAmmo(actor, gun) == 0, "should be out of ammo");
// Play a clicking sound if this gun is out of ammo
if (gun->clickLock <= 0)
{
SoundPlayAt(
&gSoundDevice,
gSoundDevice.clickSound, Vec2iFull2Real(actor->Pos));
gun->clickLock = SOUND_LOCK_WEAPON_CLICK;
}
}
return;
}
WeaponFire(
gun,
actor->direction,
actor->Pos,
actor->flags,
actor->PlayerUID,
actor->uid);
if (actor->PlayerUID >= 0)
{
if (ConfigGetBool(&gConfig, "Game.Ammo") && gun->Gun->AmmoId >= 0)
{
GameEvent e = GameEventNew(GAME_EVENT_ACTOR_USE_AMMO);
e.u.UseAmmo.UID = actor->uid;
e.u.UseAmmo.PlayerUID = actor->PlayerUID;
e.u.UseAmmo.AmmoId = gun->Gun->AmmoId;
e.u.UseAmmo.Amount = 1;
GameEventsEnqueue(&gGameEvents, e);
}
else if (gun->Gun->Cost != 0)
{
// Classic C-Dogs score consumption
GameEvent e = GameEventNew(GAME_EVENT_SCORE);
e.u.Score.PlayerUID = actor->PlayerUID;
e.u.Score.Score = -gun->Gun->Cost;
GameEventsEnqueue(&gGameEvents, e);
}
}
}
static bool ActorTryChangeDirection(
TActor *actor, const int cmd, const int prevCmd)
{
const bool willChangeDirecton =
!actor->petrified &&
CMD_HAS_DIRECTION(cmd) &&
(!(cmd & CMD_BUTTON2) || ConfigGetEnum(&gConfig, "Game.SwitchMoveStyle") != SWITCHMOVE_STRAFE) &&
(!(prevCmd & CMD_BUTTON1) || ConfigGetEnum(&gConfig, "Game.FireMoveStyle") != FIREMOVE_STRAFE);
const direction_e dir = CmdToDirection(cmd);
if (willChangeDirecton && dir != actor->direction)
{
GameEvent e = GameEventNew(GAME_EVENT_ACTOR_DIR);
e.u.ActorDir.UID = actor->uid;
e.u.ActorDir.Dir = (int32_t)dir;
GameEventsEnqueue(&gGameEvents, e);
// Change direction immediately because this affects shooting
actor->direction = dir;
}
return willChangeDirecton;
}
int ActorTryShoot(TActor *actor, int cmd)
{
int willShoot = !actor->petrified && (cmd & CMD_BUTTON1);
if (willShoot)
{
Shoot(actor);
}
else if (ActorGetGun(actor)->state != GUNSTATE_READY)
{
GameEvent e = GameEventNew(GAME_EVENT_GUN_STATE);
e.u.GunState.ActorUID = actor->uid;
e.u.GunState.State = GUNSTATE_READY;
GameEventsEnqueue(&gGameEvents, e);
}
return willShoot;
}
static bool ActorTryMove(TActor *actor, int cmd, int hasShot, int ticks);
void CommandActor(TActor * actor, int cmd, int ticks)
{
if (actor->confused)
{
cmd = CmdGetReverse(cmd);
}
if (actor->health > 0)
{
int hasChangedDirection, hasShot, hasMoved;
hasChangedDirection = ActorTryChangeDirection(actor, cmd, actor->lastCmd);
hasShot = ActorTryShoot(actor, cmd);
hasMoved = ActorTryMove(actor, cmd, hasShot, ticks);
if (!hasChangedDirection && !hasShot && !hasMoved)
{
// Idle if player hasn't done anything
if (actor->anim.Type != ACTORANIMATION_IDLE)
{
GameEvent e = GameEventNew(GAME_EVENT_ACTOR_STATE);
e.u.ActorState.UID = actor->uid;
e.u.ActorState.State = (int32_t)ACTORANIMATION_IDLE;
GameEventsEnqueue(&gGameEvents, e);
}
}
}
actor->lastCmd = cmd;
if (cmd & CMD_BUTTON2)
{
if (CMD_HAS_DIRECTION(cmd))
{
actor->specialCmdDir = true;
}
else
{
// Special: pick up things that can only be picked up on demand
if (!actor->PickupAll)
{
GameEvent e = GameEventNew(GAME_EVENT_ACTOR_PICKUP_ALL);
e.u.ActorPickupAll.UID = actor->uid;
e.u.ActorPickupAll.PickupAll = true;
GameEventsEnqueue(&gGameEvents, e);
}
actor->PickupAll = true;
}
}
else
{
actor->specialCmdDir = false;
if (actor->PickupAll)
{
GameEvent e = GameEventNew(GAME_EVENT_ACTOR_PICKUP_ALL);
e.u.ActorPickupAll.UID = actor->uid;
e.u.ActorPickupAll.PickupAll = false;
GameEventsEnqueue(&gGameEvents, e);
}
actor->PickupAll = false;
}
}
static bool ActorTryMove(TActor *actor, int cmd, int hasShot, int ticks)
{
const bool canMoveWhenShooting =
ConfigGetEnum(&gConfig, "Game.FireMoveStyle") != FIREMOVE_STOP ||
!hasShot ||
(ConfigGetEnum(&gConfig, "Game.SwitchMoveStyle") == SWITCHMOVE_STRAFE &&
(cmd & CMD_BUTTON2));
const bool willMove =
!actor->petrified && CMD_HAS_DIRECTION(cmd) && canMoveWhenShooting;
actor->MoveVel = Vec2iZero();
if (willMove)
{
const int moveAmount = ActorGetCharacter(actor)->speed * ticks;
if (cmd & CMD_LEFT)
{
actor->MoveVel.x -= moveAmount;
}
else if (cmd & CMD_RIGHT)
{
actor->MoveVel.x += moveAmount;
}
if (cmd & CMD_UP)
{
actor->MoveVel.y -= moveAmount;
}
else if (cmd & CMD_DOWN)
{
actor->MoveVel.y += moveAmount;
}
if (actor->anim.Type != ACTORANIMATION_WALKING)
{
GameEvent e = GameEventNew(GAME_EVENT_ACTOR_STATE);
e.u.ActorState.UID = actor->uid;
e.u.ActorState.State = (int32_t)ACTORANIMATION_WALKING;
GameEventsEnqueue(&gGameEvents, e);
}
}
else
{
if (actor->anim.Type != ACTORANIMATION_IDLE)
{
GameEvent e = GameEventNew(GAME_EVENT_ACTOR_STATE);
e.u.ActorState.UID = actor->uid;
e.u.ActorState.State = (int32_t)ACTORANIMATION_IDLE;
GameEventsEnqueue(&gGameEvents, e);
}
}
// If we have changed our move commands, send the move event
if (cmd != actor->lastCmd || actor->hasCollided)
{
GameEvent e = GameEventNew(GAME_EVENT_ACTOR_MOVE);
e.u.ActorMove.UID = actor->uid;
e.u.ActorMove.Pos = Vec2i2Net(actor->Pos);
e.u.ActorMove.MoveVel = Vec2i2Net(actor->MoveVel);
GameEventsEnqueue(&gGameEvents, e);
}
return willMove;
}
void SlideActor(TActor *actor, int cmd)
{
// Check that actor can slide
if (actor->slideLock > 0)
{
return;
}
if (actor->petrified)
return;
if (actor->confused)
{
cmd = CmdGetReverse(cmd);
}
GameEvent e = GameEventNew(GAME_EVENT_ACTOR_SLIDE);
e.u.ActorSlide.UID = actor->uid;
Vec2i vel = Vec2iZero();
if (cmd & CMD_LEFT) vel.x = -SLIDE_X * 256;
else if (cmd & CMD_RIGHT) vel.x = SLIDE_X * 256;
if (cmd & CMD_UP) vel.y = -SLIDE_Y * 256;
else if (cmd & CMD_DOWN) vel.y = SLIDE_Y * 256;
e.u.ActorSlide.Vel = Vec2i2Net(vel);
GameEventsEnqueue(&gGameEvents, e);
actor->slideLock = SLIDE_LOCK;
}
static void ActorUpdatePosition(TActor *actor, int ticks);
static void ActorDie(TActor *actor);
void UpdateAllActors(int ticks)
{
CA_FOREACH(TActor, actor, gActors)
if (!actor->isInUse)
{
continue;
}
ActorUpdatePosition(actor, ticks);
UpdateActorState(actor, ticks);
if (actor->dead > DEATH_MAX)
{
if (!gCampaign.IsClient)
{
ActorDie(actor);
}
continue;
}
// Find actors that are on the same team and colliding,
// and repel them
if (!gCampaign.IsClient &&
gCollisionSystem.allyCollision == ALLYCOLLISION_REPEL)
{
Vec2i realPos = Vec2iFull2Real(actor->Pos);
TTileItem *collidingItem = CollideGetFirstItem(
&actor->tileItem, realPos, TILEITEM_IMPASSABLE,
COLLISIONTEAM_NONE,
IsPVP(gCampaign.Entry.Mode));
if (collidingItem && collidingItem->kind == KIND_CHARACTER)
{
TActor *collidingActor = CArrayGet(
&gActors, collidingItem->id);
if (CalcCollisionTeam(1, collidingActor) ==
CalcCollisionTeam(1, actor))
{
Vec2i v = Vec2iMinus(actor->Pos, collidingActor->Pos);
if (Vec2iIsZero(v))
{
v = Vec2iNew(1, 0);
}
v = Vec2iScale(Vec2iNorm(v), REPEL_STRENGTH);
GameEvent e = GameEventNew(GAME_EVENT_ACTOR_IMPULSE);
e.u.ActorImpulse.UID = actor->uid;
e.u.ActorImpulse.Vel = Vec2i2Net(v);
e.u.ActorImpulse.Pos = Vec2i2Net(actor->Pos);
GameEventsEnqueue(&gGameEvents, e);
e.u.ActorImpulse.UID = collidingActor->uid;
e.u.ActorImpulse.Vel = Vec2i2Net(Vec2iScale(v, -1));
e.u.ActorImpulse.Pos = Vec2i2Net(collidingActor->Pos);
GameEventsEnqueue(&gGameEvents, e);
}
}
}
// If low on health, bleed
const int maxHealth = ActorGetCharacter(actor)->maxHealth;
const int healthPct = actor->health * 100 / maxHealth;
if (healthPct < BLEED_PERCENTAGE)
{
actor->bleedCounter--;
if (actor->bleedCounter <= 0)
{
ActorAddBloodSplatters(actor, 1, Vec2iZero());
actor->bleedCounter += healthPct;
}
}
CA_FOREACH_END()
}
static void CheckManualPickups(TActor *a);
static void ActorUpdatePosition(TActor *actor, int ticks)
{
Vec2i newPos = Vec2iAdd(actor->Pos, actor->MoveVel);
if (!Vec2iIsZero(actor->Vel))
{
newPos = Vec2iAdd(newPos, Vec2iScale(actor->Vel, ticks));
for (int i = 0; i < ticks; i++)
{
if (actor->Vel.x > 0)
{
actor->Vel.x = MAX(0, actor->Vel.x - VEL_DECAY_X);
}
else
{
actor->Vel.x = MIN(0, actor->Vel.x + VEL_DECAY_X);
}
if (actor->Vel.y > 0)
{
actor->Vel.y = MAX(0, actor->Vel.y - VEL_DECAY_Y);
}
else
{
actor->Vel.y = MIN(0, actor->Vel.y + VEL_DECAY_Y);
}
}
}
if (!Vec2iEqual(actor->Pos, newPos))
{
TryMoveActor(actor, newPos);
}
// Check if we're standing over any manual pickups
CheckManualPickups(actor);
}
// Check if the actor is over any manual pickups
static bool CheckManualPickupFunc(TTileItem *ti, void *data);
static void CheckManualPickups(TActor *a)
{
// NPCs can't pickup
if (a->PlayerUID < 0) return;
CollideTileItems(
&a->tileItem, Vec2iFull2Real(a->Pos), 0,
CalcCollisionTeam(true, a),
IsPVP(gCampaign.Entry.Mode), CheckManualPickupFunc, a);
}
static bool CheckManualPickupFunc(TTileItem *ti, void *data)
{
TActor *a = data;
if (ti->kind != KIND_PICKUP) return true;
const Pickup *p = CArrayGet(&gPickups, ti->id);
if (!PickupIsManual(p)) return true;
// "Say" that the weapon must be picked up using a command
const PlayerData *pData = PlayerDataGetByUID(a->PlayerUID);
if (pData->IsLocal && IsPlayerHuman(pData))
{
char buf[256];
strcpy(buf, "");
InputGetButtonName(
pData->inputDevice, pData->deviceIndex, CMD_BUTTON2, buf);
sprintf(a->Chatter, "%s to pick up\n%s",
buf, IdGunDescription(p->class->u.GunId)->name);
a->ChatterCounter = 2;
}
// If co-op AI, alert it so it can try to pick the gun up
if (a->aiContext != NULL)
{
AICoopOnPickupGun(a, p->class->u.GunId);
}
a->CanPickupSpecial = true;
return false;
}
static void ActorAddAmmoPickup(const TActor *actor);
static void ActorAddGunPickup(const TActor *actor);
static void ActorDie(TActor *actor)
{
// Add an ammo pickup of the actor's gun
if (ConfigGetBool(&gConfig, "Game.Ammo"))
{
ActorAddAmmoPickup(actor);
}