| 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 |
#include "weapon.h" |
| 50 |
|
| 51 |
Weapon WeaponCreate(const WeaponClass *wc) |
| 52 |
{ |
| 53 |
Weapon w; |
| 54 |
memset(&w, 0, sizeof w); |
| 55 |
w.Gun = wc; |
| 56 |
if (wc != NULL) |
| 57 |
{ |
| 58 |
for (int i = 0; i < WeaponClassNumBarrels(wc); i++) |
| 59 |
{ |
| 60 |
w.barrels[i].state = GUNSTATE_READY; |
| 61 |
w.barrels[i].stateCounter = -1; |
| 62 |
} |
| 63 |
} |
| 64 |
return w; |
| 65 |
} |
| 66 |
|
| 67 |
void WeaponUpdate(Weapon *w, const int ticks) |
| 68 |
{ |
| 69 |
w->clickLock -= ticks; |
| 70 |
if (w->clickLock < 0) |
| 71 |
{ |
| 72 |
w->clickLock = 0; |
| 73 |
} |
| 74 |
for (int i = 0; i < WeaponClassNumBarrels(w->Gun); i++) |
| 75 |
{ |
| 76 |
w->barrels[i].lock -= ticks; |
| 77 |
if (w->barrels[i].lock < 0) |
| 78 |
{ |
| 79 |
w->barrels[i].lock = 0; |
| 80 |
} |
| 81 |
w->barrels[i].soundLock -= ticks; |
| 82 |
if (w->barrels[i].soundLock < 0) |
| 83 |
{ |
| 84 |
w->barrels[i].soundLock = 0; |
| 85 |
} |
| 86 |
if (w->barrels[i].stateCounter >= 0) |
| 87 |
{ |
| 88 |
w->barrels[i].stateCounter = |
| 89 |
MAX(0, w->barrels[i].stateCounter - ticks); |
| 90 |
if (w->barrels[i].stateCounter == 0) |
| 91 |
{ |
| 92 |
switch (w->barrels[i].state) |
| 93 |
{ |
| 94 |
case GUNSTATE_FIRING: |
| 95 |
WeaponBarrelSetState(w, i, GUNSTATE_RECOIL); |
| 96 |
break; |
| 97 |
case GUNSTATE_RECOIL: |
| 98 |
WeaponBarrelSetState(w, i, GUNSTATE_FIRING); |
| 99 |
break; |
| 100 |
default: |
| 101 |
|
| 102 |
break; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
w->barrels[i].heatCounter -= ticks; |
| 107 |
if (w->barrels[i].heatCounter < 0) |
| 108 |
{ |
| 109 |
w->barrels[i].heatCounter = 0; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
int WeaponGetUnlockedBarrel(const Weapon *w) |
| 115 |
{ |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
int unlockedBarrel = -1; |
| 120 |
for (int i = 0; i < WeaponClassNumBarrels(w->Gun); i++) |
| 121 |
{ |
| 122 |
if (w->barrels[i].lock == 0 && unlockedBarrel < 0) |
| 123 |
{ |
| 124 |
unlockedBarrel = i; |
| 125 |
} |
| 126 |
else if ( |
| 127 |
w->barrels[i].lock > |
| 128 |
(WeaponClassGetBarrel(w->Gun, i)->Lock - w->Gun->Lock)) |
| 129 |
{ |
| 130 |
return -1; |
| 131 |
} |
| 132 |
} |
| 133 |
return unlockedBarrel; |
| 134 |
} |
| 135 |
|
| 136 |
void WeaponBarrelSetState(Weapon *w, const int barrel, const gunstate_e state) |
| 137 |
{ |
| 138 |
w->barrels[barrel].state = state; |
| 139 |
switch (state) |
| 140 |
{ |
| 141 |
case GUNSTATE_FIRING: |
| 142 |
w->barrels[barrel].stateCounter = 4; |
| 143 |
break; |
| 144 |
case GUNSTATE_RECOIL: |
| 145 |
|
| 146 |
|
| 147 |
w->barrels[barrel].stateCounter = MAX(1, w->barrels[barrel].lock - 3); |
| 148 |
break; |
| 149 |
default: |
| 150 |
w->barrels[barrel].stateCounter = -1; |
| 151 |
break; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
void WeaponBarrelOnFire(Weapon *w, const int barrel) |
| 156 |
{ |
| 157 |
const WeaponClass *wc = WeaponClassGetBarrel(w->Gun, barrel); |
| 158 |
if (w->barrels[barrel].soundLock <= 0) |
| 159 |
{ |
| 160 |
w->barrels[barrel].soundLock = wc->u.Normal.SoundLockLength; |
| 161 |
} |
| 162 |
|
| 163 |
w->barrels[barrel].heatCounter += wc->Lock * 2; |
| 164 |
|
| 165 |
if (w->barrels[barrel].heatCounter > FPS_FRAMELIMIT * 3) |
| 166 |
{ |
| 167 |
w->barrels[barrel].heatCounter = FPS_FRAMELIMIT * 3; |
| 168 |
} |
| 169 |
w->barrels[barrel].lock = wc->Lock; |
| 170 |
} |
| 171 |
|
| 172 |
bool WeaponBarrelIsOverheating(const Weapon *w, const int idx) |
| 173 |
{ |
| 174 |
if (!WeaponClassHasMuzzle(w->Gun) || w->Gun->OverheatTicks < 0) |
| 175 |
{ |
| 176 |
return false; |
| 177 |
} |
| 178 |
|
| 179 |
return w->barrels[idx].heatCounter > w->Gun->OverheatTicks; |
| 180 |
} |