| 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 |
#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 |
|
| 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 |
|
| 96 |
if (!gCampaign.IsClient) |
| 97 |
{ |
| 98 |
|
| 99 |
|
| 100 |
|
| 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 |
|
| 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 |
|
| 138 |
if (gf.Sound && wc->u.Normal.Sound) |
| 139 |
{ |
| 140 |
SoundPlayAt(sd, wc->u.Normal.Sound, pos); |
| 141 |
|
| 142 |
|
| 143 |
AIWakeOnSoundAt(pos); |
| 144 |
} |
| 145 |
|
| 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 |
|
| 155 |
|
| 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 |
} |