1 /*
2 	C-Dogs SDL
3 	A port of the legendary (and fun) action/arcade cdogs.
4 	Copyright (C) 1995 Ronny Wester
5 	Copyright (C) 2003 Jeremy Chin
6 	Copyright (C) 2003-2007 Lucas Martin-King
7 
8 	This program is free software; you can redistribute it and/or modify
9 	it under the terms of the GNU General Public License as published by
10 	the Free Software Foundation; either version 2 of the License, or
11 	(at your option) any later version.
12 
13 	This program is distributed in the hope that it will be useful,
14 	but WITHOUT ANY WARRANTY; without even the implied warranty of
15 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 	GNU General Public License for more details.
17 
18 	You should have received a copy of the GNU General Public License
19 	along with this program; if not, write to the Free Software
20 	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 
22 	This file incorporates work covered by the following copyright and
23 	permission notice:
24 
25 	Copyright (c) 2013-2019, 2021 Cong Xu
26 	All rights reserved.
27 
28 	Redistribution and use in source and binary forms, with or without
29 	modification, are permitted provided that the following conditions are met:
30 
31 	Redistributions of source code must retain the above copyright notice, this
32 	list of conditions and the following disclaimer.
33 	Redistributions in binary form must reproduce the above copyright notice,
34 	this list of conditions and the following disclaimer in the documentation
35 	and/or other materials provided with the distribution.
36 
37 	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
41 	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47 	POSSIBILITY OF SUCH DAMAGE.
48 */
49 #include "weapon.h"
50 
WeaponCreate(const WeaponClass * wc)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 
WeaponUpdate(Weapon * w,const int ticks)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 					// do nothing
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 
WeaponGetUnlockedBarrel(const Weapon * w)114 int WeaponGetUnlockedBarrel(const Weapon *w)
115 {
116 	// To fire:
117 	// One barrel has lock == 0
118 	// No other barrel has lock > (gun lock - inter-gun lock)
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 
WeaponBarrelSetState(Weapon * w,const int barrel,const gunstate_e state)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 		// This is to make sure the gun stays recoiled as long as the gun is
146 		// "locked", i.e. cannot fire
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 
WeaponBarrelOnFire(Weapon * w,const int barrel)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 	// 2 seconds of overheating max
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 
WeaponBarrelIsOverheating(const Weapon * w,const int idx)172 bool WeaponBarrelIsOverheating(const Weapon *w, const int idx)
173 {
174 	if (!WeaponClassHasMuzzle(w->Gun))
175 	{
176 		return false;
177 	}
178 	// Overheat after 1 second of continuous firing
179 	return w->barrels[idx].heatCounter > FPS_FRAMELIMIT;
180 }
181