]> icculus.org git repositories - divverent/nexuiz.git/blob - qcsrc/gamec/cl_weaponsystem.c
fixed a large number of weapon switch issues (switchweapon is no longer cleared on...
[divverent/nexuiz.git] / qcsrc / gamec / cl_weaponsystem.c
1 /*
2 ===========================================================================
3
4   CLIENT WEAPONSYSTEM CODE
5   Bring back W_Weaponframe
6
7 ===========================================================================
8 */
9
10 void() CL_Weaponentity_Think =
11 {
12         self.nextthink = time;
13         if (self.owner.weaponentity != self)
14         {
15                 remove(self);
16                 return;
17         }
18 };
19
20 // spawning weaponentity for client
21 void() CL_SpawnWeaponentity =
22 {
23         if (self.weaponentity)
24         {
25                 w_clear();
26                 return;
27         }
28         self.weaponentity = spawn();
29         self.weaponentity.solid = SOLID_NOT;
30         self.weaponentity.owner = self;
31         self.weaponentity.weaponentity = self.weaponentity;
32         setmodel(self.weaponentity, "");
33         self.weaponentity.origin = '0 0 0';
34         self.weaponentity.angles = '0 0 0';
35         self.weaponentity.viewmodelforclient = self;
36         self.weaponentity.flags = 0;
37         self.weaponentity.think = CL_Weaponentity_Think;
38         self.weaponentity.nextthink = time;
39 };
40
41 // convertion from index (= impulse) to flag in .items
42 float(float index) weapon_translateindextoflag =
43 {
44         if (index == WEP_LASER)
45                 return IT_LASER;
46         else if (index == WEP_SHOTGUN)
47                 return IT_SHOTGUN;
48         else if (index == WEP_UZI)
49                 return IT_UZI;
50         else if (index == WEP_GRENADE_LAUNCHER)
51                 return IT_GRENADE_LAUNCHER;
52         else if (index == WEP_ELECTRO)
53                 return IT_ELECTRO;
54         else if (index == WEP_CRYLINK)
55                 return IT_CRYLINK;
56         else if (index == WEP_NEX)
57                 return IT_NEX;
58         else if (index == WEP_HAGAR)
59                 return IT_HAGAR;
60         else if (index == WEP_ROCKET_LAUNCHER)
61                 return IT_ROCKET_LAUNCHER;
62         return IT_LASER;
63 };
64
65 float(entity cl, float wpn, float andammo) client_hasweapon =
66 {
67         local float itemcode;
68         local entity oldself;
69
70         weapon_hasammo = TRUE;
71         if (wpn < WEP_FIRST || wpn > WEP_LAST)
72                 return FALSE;
73         itemcode = weapon_translateindextoflag(wpn);
74         if (cl.items & itemcode)
75         {
76                 if (andammo)
77                 {
78                         oldself = self;
79                         self = cl;
80                         weapon_action(wpn, WR_CHECKAMMO);
81                         self = oldself;
82                         if (weapon_hasammo)
83                                 return TRUE;
84                         return FALSE;
85                 }
86                 return TRUE;
87         }
88         return FALSE;
89 };
90
91 // Weapon subs
92 void() w_clear =
93 {
94         weapon_action(self.weapon, WR_CLEAR);
95         if (self.weapon != -1)
96                 self.weapon = 0;
97         self.weaponentity.state = WS_CLEAR;
98         setmodel(self.weaponentity, "");
99         self.weaponentity.effects = 0;
100 };
101
102 void() w_ready =
103 {
104         self.weaponentity.state = WS_READY;
105         weapon_action(self.weapon, WR_IDLE);
106 };
107
108 // FIXME: add qw-style client-custom weaponrating (cl_weaponrating)?
109 float(entity e) w_getbestweapon
110 {// add new weapons here
111         if (client_hasweapon(e, WEP_ROCKET_LAUNCHER, TRUE))
112                 return WEP_ROCKET_LAUNCHER;
113         else if (client_hasweapon(e, WEP_NEX, TRUE))
114                 return WEP_NEX;
115         else if (client_hasweapon(e, WEP_HAGAR, TRUE))
116                 return WEP_HAGAR;
117         else if (client_hasweapon(e, WEP_GRENADE_LAUNCHER, TRUE))
118                 return WEP_GRENADE_LAUNCHER;
119         else if (client_hasweapon(e, WEP_ELECTRO, TRUE))
120                 return WEP_ELECTRO;
121         else if (client_hasweapon(e, WEP_CRYLINK, TRUE))
122                 return WEP_CRYLINK;
123         else if (client_hasweapon(e, WEP_UZI, TRUE))
124                 return WEP_UZI;
125         else if (client_hasweapon(e, WEP_SHOTGUN, TRUE))
126                 return WEP_SHOTGUN;
127         else
128         {
129                 weapon_hasammo = TRUE;
130                 return WEP_LASER;
131         }
132 };
133
134 // Setup weapon for client (after this raise frame will be launched)
135 void(float windex, string wmodel, float hudammo) weapon_setup =
136 {
137         local string wdir, weaponmdl;
138
139         self.items = self.items - (self.items & (IT_SHELLS | IT_NAILS | IT_ROCKETS | IT_CELLS));
140         self.items = self.items | hudammo;
141
142         self.weapon = windex;
143
144         if (wmodel != "")
145         {
146                 weaponmdl = strzone(strcat("models/weapons/", wmodel));
147                 setmodel(self.weaponentity, weaponmdl);
148         }
149         // VorteX: update visible weapon
150         // CL_ViswepUpdate();
151 };
152
153 // shot direction
154 float WEAPON_MAXRELX = 14; // if more, shot can be spawned after wall surface (in empty worldspace) or inside other entity if client stands close to it
155 void(float x, float y, float z) weapon_shotdir =
156 {
157         makevectors(self.v_angle);
158         self.shotorg  = self.origin + self.view_ofs + v_forward*bound(0, x, WEAPON_MAXRELX) + v_right*(y + self.weaponentity.view_ofs_y) + v_up*z;
159         self.shotdir = aim(self, 1000);
160 };
161
162 // perform weapon to attack (weaponstate and attack_finished check is here)
163 void(float() checkfunc1, float() checkfunc2, void() firefunc, float atktime) weapon_prepareattack =
164 {
165         // Change to best weapon if failed
166         if (!(game & GAME_INSTAGIB) && !(game & GAME_ROCKET_ARENA))
167         {
168                 if (!checkfunc1())
169                 {
170                         if (!checkfunc2())
171                                 self.switchweapon = w_getbestweapon(self);
172                         return;
173                 }
174         }
175         // Don't do shot if previos attack  not finished
176         if (!(game & GAME_INSANE))
177                 if (time < self.attack_finished)
178                         return;
179         // Can't do shot if changing weapon
180         if (!(game & GAME_INSANE))
181                 if (self.weaponentity.state != WS_READY)
182                         return;
183
184         self.attack_finished = time + atktime;
185         firefunc();
186 };
187
188 // perform weapon attack
189 void(float() checkfunc1, float() checkfunc2, void() firefunc) weapon_doattack
190 {
191         // Change to best weapon if failed
192         if (!(game & GAME_INSTAGIB) && !(game & GAME_ROCKET_ARENA))
193         {
194                 if (!checkfunc1())
195                 {
196                         if (!checkfunc2())
197                                 self.switchweapon = w_getbestweapon(self);
198                         return;
199                 }
200         }
201         self.weaponentity.state = WS_INUSE;
202         firefunc();
203         weapon_action(self.weapon, WR_UPDATECOUNTS); // update ammo now
204 };
205
206 void(entity ent, float recoil) weapon_recoil =
207 {
208         ent.punchangle = (randomvec() + '-1 0 0')*recoil;
209         ent.punchangle_z = 0; // don't want roll
210         if (recoil > 3) // push back if large recoil
211                 ent.velocity = ent.velocity - normalize(ent.shotdir)*recoil*25;
212 };
213
214 void(float fr, float t, void() func) weapon_thinkf =
215 {
216         if (fr >= 0)
217                 if (self.weaponentity != world)
218                         self.weaponentity.frame = fr;
219         // VorteX: haste can be added here
220         self.weapon_nextthink = time + t;
221         self.weapon_think = func;
222 };
223
224 void(float spd, vector org) weapon_boblayer1 =
225 {
226         // VorteX: haste can be added here
227         self.weaponentity.pos1 =org;
228         self.weaponentity.lip = spd;
229 };