Debian Codegraph @

src/cdogs/actor_fire.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2013-2017, 2019, 2021 Cong Xu
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "actor_fire.h"
29
30 #include "ai.h"
31 #include "net_util.h"
32 #include "objs.h"
33
34 void ActorFireBarrel(Weapon *w, const TActor *a, const int barrel)
35 {
36 if (w->barrels[barrel].state != GUNSTATE_FIRING &&
37 w->barrels[barrel].state != GUNSTATE_RECOIL)
38 {
39 GameEvent e = GameEventNew(GAME_EVENT_GUN_STATE);
40 e.u.GunState.ActorUID = a->uid;
41 e.u.GunState.Barrel = barrel;
42 e.u.GunState.State = GUNSTATE_FIRING;
43 GameEventsEnqueue(&gGameEvents, e);
44 }
45 if (!WeaponClassCanShoot(w->Gun))
46 {
47 return;
48 }
49
50 const double radians = dir2radians[a->direction];
51 const struct vec2 muzzleOffset = ActorGetMuzzleOffset(a, w, barrel);
52 const struct vec2 muzzlePosition = svec2_add(a->Pos, muzzleOffset);
53 const bool playSound = w->barrels[barrel].soundLock <= 0;
54 const TActor *firingActor = ActorGetByUID(a->pilotUID);
55 WeaponClassFire(
56 WeaponClassGetBarrel(w->Gun, barrel), muzzlePosition,
57 WeaponClassGetMuzzleHeight(w->Gun, GUNSTATE_FIRING, barrel), radians,
58 firingActor->flags, firingActor->uid, playSound, true);
59 WeaponBarrelOnFire(w, barrel);
60 }
61
62 void ActorFireUpdate(Weapon *w, const TActor *a, const int ticks)
63 {
64 // Reload sound
65 if (ConfigGetBool(&gConfig, "Sound.Reloads"))
66 {
67 for (int i = 0; i < WeaponClassNumBarrels(w->Gun); i++)
68 {
69 const int reloadLead = WC_BARREL_ATTR(*(w->Gun), ReloadLead, i);
70 if (w->barrels[i].lock > reloadLead &&
71 w->barrels[i].lock - ticks <= reloadLead &&
72 w->barrels[i].lock > 0)
73 {
74 GameEvent e = GameEventNew(GAME_EVENT_GUN_RELOAD);
75 e.u.GunReload.PlayerUID = a->PlayerUID;
76 strcpy(
77 e.u.GunReload.Gun, WeaponClassGetBarrel(w->Gun, i)->name);
78 const struct vec2 muzzleOffset = ActorGetMuzzleOffset(a, w, i);
79 const struct vec2 muzzlePosition =
80 svec2_add(a->Pos, muzzleOffset);
81 e.u.GunReload.Pos = Vec2ToNet(muzzlePosition);
82 e.u.GunReload.Direction = a->direction;
83 GameEventsEnqueue(&gGameEvents, e);
84 }
85 }
86 }
87 }
88
89 void OnGunFire(const NGunFire gf, SoundDevice *sd)
90 {
91 const WeaponClass *wc = StrWeaponClass(gf.Gun);
92 CASSERT(wc->Type != GUNTYPE_MULTI, "unexpected gun type");
93 const struct vec2 pos = NetToVec2(gf.MuzzlePos);
94
95 // Add bullets
96 if (!gCampaign.IsClient)
97 {
98 // Find the starting angle of the spread (clockwise)
99 // Keep in mind the fencepost problem, i.e. spread of 3 means a
100 // total spread angle of 2x width
101 const float spreadStartAngle =
102 wc->u.Normal.AngleOffset -
103 (wc->u.Normal.Spread.Count - 1) * wc->u.Normal.Spread.Width / 2;
104 for (int i = 0; i < wc->u.Normal.Spread.Count; i++)
105 {
106 const float recoil = RAND_FLOAT(-0.5f, 0.5f) * wc->u.Normal.Recoil;
107 const float finalAngle = gf.Angle + spreadStartAngle +
108 i * wc->u.Normal.Spread.Width + recoil;
109 GameEvent ab = GameEventNew(GAME_EVENT_ADD_BULLET);
110 ab.u.AddBullet.MuzzlePos = Vec2ToNet(pos);
111 ab.u.AddBullet.MuzzleHeight = gf.Z;
112 ab.u.AddBullet.Angle = finalAngle;
113 ab.u.AddBullet.Elevation = RAND_INT(
114 wc->u.Normal.ElevationLow, wc->u.Normal.ElevationHigh);
115 ab.u.AddBullet.Flags = gf.Flags;
116 ab.u.AddBullet.ActorUID = gf.ActorUID;
117 strcpy(ab.u.AddBullet.Gun, wc->name);
118
119 CA_FOREACH(const BulletClass *, bc, wc->u.Normal.Bullets)
120 ab.u.AddBullet.UID = MobObjsObjsGetNextUID();
121 strcpy(ab.u.AddBullet.BulletClass, (*bc)->Name);
122 GameEventsEnqueue(&gGameEvents, ab);
123 CA_FOREACH_END()
124 }
125 }
126
127 // Add muzzle flash
128 if (WeaponClassHasMuzzle(wc) && wc->u.Normal.MuzzleFlash != NULL)
129 {
130 GameEvent ap = GameEventNew(GAME_EVENT_ADD_PARTICLE);
131 ap.u.AddParticle.Class = wc->u.Normal.MuzzleFlash;
132 ap.u.AddParticle.Pos = pos;
133 ap.u.AddParticle.Z = (float)gf.Z;
134 ap.u.AddParticle.Angle = gf.Angle;
135 GameEventsEnqueue(&gGameEvents, ap);
136 }
137 // Sound
138 if (gf.Sound && wc->u.Normal.Sound)
139 {
140 SoundPlayAt(sd, wc->u.Normal.Sound, pos);
141
142 // Alert sleeping AI
143 AIWakeOnSoundAt(pos);
144 }
145 // Screen shake
146 if (wc->u.Normal.Shake.Amount > 0)
147 {
148 GameEvent s = GameEventNew(GAME_EVENT_SCREEN_SHAKE);
149 s.u.Shake.Amount = wc->u.Normal.Shake.Amount;
150 s.u.Shake.CameraSubjectOnly = wc->u.Normal.Shake.CameraSubjectOnly;
151 s.u.Shake.ActorUID = gf.ActorUID;
152 GameEventsEnqueue(&gGameEvents, s);
153 }
154 // Brass shells
155 // If we have a reload lead, defer the creation of shells until then
156 if (wc->u.Normal.Brass && wc->u.Normal.ReloadLead == 0)
157 {
158 const direction_e d = RadiansToDirection(gf.Angle);
159 WeaponClassAddBrass(wc, d, pos);
160 }
161 }