Hook: merge offhand and weapon behaviour
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / weapon / porto.qc
1 #ifndef IMPLEMENTATION
2 CLASS(PortoLaunch, Weapon)
3 /* ammotype  */ ATTRIB(PortoLaunch, ammo_field, .int, ammo_none)
4 /* impulse   */ ATTRIB(PortoLaunch, impulse, int, 0)
5 /* flags     */ ATTRIB(PortoLaunch, spawnflags, int, WEP_TYPE_OTHER | WEP_FLAG_SUPERWEAPON);
6 /* rating    */ ATTRIB(PortoLaunch, bot_pickupbasevalue, float, 0);
7 /* color     */ ATTRIB(PortoLaunch, wpcolor, vector, '0.5 0.5 0.5');
8 /* modelname */ ATTRIB(PortoLaunch, mdl, string, "porto");
9 #ifndef MENUQC
10 /* model     */ ATTRIB(PortoLaunch, m_model, Model, MDL_PORTO_ITEM);
11 #endif
12 /* crosshair */ ATTRIB(PortoLaunch, w_crosshair, string, "gfx/crosshairporto");
13 /* crosshair */ ATTRIB(PortoLaunch, w_crosshair_size, float, 0.6);
14 /* wepimg    */ ATTRIB(PortoLaunch, model2, string, "weaponporto");
15 /* refname   */ ATTRIB(PortoLaunch, netname, string, "porto");
16 /* wepname   */ ATTRIB(PortoLaunch, message, string, _("Port-O-Launch"));
17 ENDCLASS(PortoLaunch)
18 REGISTER_WEAPON(PORTO, NEW(PortoLaunch));
19
20 #define PORTO_SETTINGS(w_cvar,w_prop) PORTO_SETTINGS_LIST(w_cvar, w_prop, PORTO, porto)
21 #define PORTO_SETTINGS_LIST(w_cvar,w_prop,id,sn) \
22         w_cvar(id, sn, BOTH, animtime) \
23         w_cvar(id, sn, BOTH, lifetime) \
24         w_cvar(id, sn, BOTH, refire) \
25         w_cvar(id, sn, BOTH, speed) \
26         w_cvar(id, sn, NONE, secondary) \
27         w_prop(id, sn, float,  switchdelay_raise, switchdelay_raise) \
28         w_prop(id, sn, float,  switchdelay_drop, switchdelay_drop) \
29         w_prop(id, sn, string, weaponreplace, weaponreplace) \
30         w_prop(id, sn, float,  weaponstart, weaponstart) \
31         w_prop(id, sn, float,  weaponstartoverride, weaponstartoverride) \
32         w_prop(id, sn, float,  weaponthrowable, weaponthrowable)
33
34 #ifdef SVQC
35 PORTO_SETTINGS(WEP_ADD_CVAR, WEP_ADD_PROP)
36 .entity porto_current;
37 .vector porto_v_angle; // holds "held" view angles
38 .float porto_v_angle_held;
39 .vector right_vector;
40 #endif
41 #endif
42 #ifdef IMPLEMENTATION
43 #ifdef SVQC
44 #include "../../triggers/trigger/jumppads.qh"
45
46 spawnfunc(weapon_porto) { weapon_defaultspawnfunc(WEP_PORTO.m_id); }
47
48 void W_Porto_Success(void)
49 {SELFPARAM();
50         if(self.realowner == world)
51         {
52                 objerror("Cannot succeed successfully: no owner\n");
53                 return;
54         }
55
56         self.realowner.porto_current = world;
57         remove(self);
58 }
59
60 string W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo);
61 void W_Porto_Fail(float failhard)
62 {SELFPARAM();
63         if(self.realowner == world)
64         {
65                 objerror("Cannot fail successfully: no owner\n");
66                 return;
67         }
68
69         // no portals here!
70         if(self.cnt < 0)
71         {
72                 Portal_ClearWithID(self.realowner, self.portal_id);
73         }
74
75         self.realowner.porto_current = world;
76
77         if(self.cnt < 0 && !failhard && self.realowner.playerid == self.playerid && self.realowner.deadflag == DEAD_NO && !(self.realowner.weapons & WEPSET(PORTO)))
78         {
79                 setsize(self, '-16 -16 0', '16 16 32');
80                 setorigin(self, self.origin + trace_plane_normal);
81                 if(move_out_of_solid(self))
82                 {
83                         self.flags = FL_ITEM;
84                         self.velocity = trigger_push_calculatevelocity(self.origin, self.realowner, 128);
85                         tracetoss(self, self);
86                         if(vlen(trace_endpos - self.realowner.origin) < 128)
87                         {
88                                 W_ThrowNewWeapon(self.realowner, WEP_PORTO.m_id, 0, self.origin, self.velocity);
89                                 Send_Notification(NOTIF_ONE, self.realowner, MSG_CENTER, CENTER_PORTO_FAILED);
90                         }
91                 }
92         }
93         remove(self);
94 }
95
96 void W_Porto_Remove(entity p)
97 {SELFPARAM();
98         if(p.porto_current.realowner == p && p.porto_current.classname == "porto")
99         {
100                 WITH(entity, self, p.porto_current, W_Porto_Fail(1));
101         }
102 }
103
104 void W_Porto_Think(void)
105 {SELFPARAM();
106         trace_plane_normal = '0 0 0';
107         if(self.realowner.playerid != self.playerid)
108                 remove(self);
109         else
110                 W_Porto_Fail(0);
111 }
112
113 void W_Porto_Touch(void)
114 {SELFPARAM();
115         vector norm;
116
117         // do not use PROJECTILE_TOUCH here
118         // FIXME but DO handle warpzones!
119
120         if(other.classname == "portal")
121                 return; // handled by the portal
122
123         norm = trace_plane_normal;
124         if(trace_ent.iscreature)
125         {
126                 traceline(trace_ent.origin, trace_ent.origin + '0 0 2' * PL_MIN.z, MOVE_WORLDONLY, self);
127                 if(trace_fraction >= 1)
128                         return;
129                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK || trace_dphitcontents & DPCONTENTS_PLAYERCLIP)
130                         return;
131                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
132                         return;
133         }
134
135         if(self.realowner.playerid != self.playerid)
136         {
137                 sound(self, CH_SHOTS, SND_PORTO_UNSUPPORTED, VOL_BASE, ATTEN_NORM);
138                 remove(self);
139         }
140         else if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK || trace_dphitcontents & DPCONTENTS_PLAYERCLIP)
141         {
142                 spamsound(self, CH_SHOTS, SND(PORTO_BOUNCE), VOL_BASE, ATTEN_NORM);
143                 // just reflect
144                 self.right_vector = self.right_vector - 2 * trace_plane_normal * (self.right_vector * trace_plane_normal);
145                 self.angles = vectoangles(self.velocity - 2 * trace_plane_normal * (self.velocity * trace_plane_normal));
146         }
147         else if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
148         {
149                 sound(self, CH_SHOTS, SND_PORTO_UNSUPPORTED, VOL_BASE, ATTEN_NORM);
150                 W_Porto_Fail(0);
151                 if(self.cnt < 0)
152                         Portal_ClearAll_PortalsOnly(self.realowner);
153         }
154         else if(self.cnt == 0)
155         {
156                 // in-portal only
157                 if(Portal_SpawnInPortalAtTrace(self.realowner, self.right_vector, self.portal_id))
158                 {
159                         sound(self, CH_SHOTS, SND_PORTO_CREATE, VOL_BASE, ATTEN_NORM);
160                         trace_plane_normal = norm;
161                         Send_Notification(NOTIF_ONE, self.realowner, MSG_CENTER, CENTER_PORTO_CREATED_IN);
162                         W_Porto_Success();
163                 }
164                 else
165                 {
166                         sound(self, CH_SHOTS, SND_PORTO_UNSUPPORTED, VOL_BASE, ATTEN_NORM);
167                         trace_plane_normal = norm;
168                         W_Porto_Fail(0);
169                 }
170         }
171         else if(self.cnt == 1)
172         {
173                 // out-portal only
174                 if(Portal_SpawnOutPortalAtTrace(self.realowner, self.right_vector, self.portal_id))
175                 {
176                         sound(self, CH_SHOTS, SND_PORTO_CREATE, VOL_BASE, ATTEN_NORM);
177                         trace_plane_normal = norm;
178                         Send_Notification(NOTIF_ONE, self.realowner, MSG_CENTER, CENTER_PORTO_CREATED_OUT);
179                         W_Porto_Success();
180                 }
181                 else
182                 {
183                         sound(self, CH_SHOTS, SND_PORTO_UNSUPPORTED, VOL_BASE, ATTEN_NORM);
184                         trace_plane_normal = norm;
185                         W_Porto_Fail(0);
186                 }
187         }
188         else if(self.effects & EF_RED)
189         {
190                 self.effects += EF_BLUE - EF_RED;
191                 if(Portal_SpawnInPortalAtTrace(self.realowner, self.right_vector, self.portal_id))
192                 {
193                         sound(self, CH_SHOTS, SND_PORTO_CREATE, VOL_BASE, ATTEN_NORM);
194                         trace_plane_normal = norm;
195                         Send_Notification(NOTIF_ONE, self.realowner, MSG_CENTER, CENTER_PORTO_CREATED_IN);
196                         self.right_vector = self.right_vector - 2 * trace_plane_normal * (self.right_vector * norm);
197                         self.angles = vectoangles(self.velocity - 2 * trace_plane_normal * (self.velocity * norm));
198                         CSQCProjectile(self, true, PROJECTILE_PORTO_BLUE, true); // change type
199                 }
200                 else
201                 {
202                         sound(self, CH_SHOTS, SND_PORTO_UNSUPPORTED, VOL_BASE, ATTEN_NORM);
203                         trace_plane_normal = norm;
204                         Portal_ClearAll_PortalsOnly(self.realowner);
205                         W_Porto_Fail(0);
206                 }
207         }
208         else
209         {
210                 if(self.realowner.portal_in.portal_id == self.portal_id)
211                 {
212                         if(Portal_SpawnOutPortalAtTrace(self.realowner, self.right_vector, self.portal_id))
213                         {
214                                 sound(self, CH_SHOTS, SND_PORTO_CREATE, VOL_BASE, ATTEN_NORM);
215                                 trace_plane_normal = norm;
216                                 Send_Notification(NOTIF_ONE, self.realowner, MSG_CENTER, CENTER_PORTO_CREATED_OUT);
217                                 W_Porto_Success();
218                         }
219                         else
220                         {
221                                 sound(self, CH_SHOTS, SND_PORTO_UNSUPPORTED, VOL_BASE, ATTEN_NORM);
222                                 Portal_ClearAll_PortalsOnly(self.realowner);
223                                 W_Porto_Fail(0);
224                         }
225                 }
226                 else
227                 {
228                         sound(self, CH_SHOTS, SND_PORTO_UNSUPPORTED, VOL_BASE, ATTEN_NORM);
229                         Portal_ClearAll_PortalsOnly(self.realowner);
230                         W_Porto_Fail(0);
231                 }
232         }
233 }
234
235 void W_Porto_Attack(float type)
236 {SELFPARAM();
237         entity gren;
238
239         W_SetupShot(self, false, 4, SND(PORTO_FIRE), CH_WEAPON_A, 0);
240         // always shoot from the eye
241         w_shotdir = v_forward;
242         w_shotorg = self.origin + self.view_ofs + ((w_shotorg - self.origin - self.view_ofs) * v_forward) * v_forward;
243
244         //Send_Effect(EFFECT_GRENADE_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
245
246         gren = spawn();
247         gren.cnt = type;
248         gren.owner = gren.realowner = self;
249         gren.playerid = self.playerid;
250         gren.classname = "porto";
251         gren.bot_dodge = true;
252         gren.bot_dodgerating = 200;
253         gren.movetype = MOVETYPE_BOUNCEMISSILE;
254         PROJECTILE_MAKETRIGGER(gren);
255         gren.effects = EF_RED;
256         gren.scale = 4;
257         setorigin(gren, w_shotorg);
258         setsize(gren, '0 0 0', '0 0 0');
259
260         gren.nextthink = time + WEP_CVAR_BOTH(porto, (type <= 0), lifetime);
261         gren.think = W_Porto_Think;
262         gren.touch = W_Porto_Touch;
263
264         if(self.items & ITEM_Strength.m_itemid)
265                 W_SetupProjVelocity_Basic(gren, WEP_CVAR_BOTH(porto, (type <= 0), speed) * autocvar_g_balance_powerup_strength_force, 0);
266         else
267                 W_SetupProjVelocity_Basic(gren, WEP_CVAR_BOTH(porto, (type <= 0), speed), 0);
268
269         gren.angles = vectoangles(gren.velocity);
270         gren.flags = FL_PROJECTILE;
271
272         gren.portal_id = time;
273         self.porto_current = gren;
274         gren.playerid = self.playerid;
275         fixedmakevectors(fixedvectoangles(gren.velocity));
276         gren.right_vector = v_right;
277
278         gren.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
279
280         if(type > 0)
281                 CSQCProjectile(gren, true, PROJECTILE_PORTO_BLUE, true);
282         else
283                 CSQCProjectile(gren, true, PROJECTILE_PORTO_RED, true);
284
285         MUTATOR_CALLHOOK(EditProjectile, self, gren);
286 }
287
288                 METHOD(PortoLaunch, wr_aim, void(entity thiswep))
289                 {
290                         SELFPARAM();
291                         self.BUTTON_ATCK = false;
292                         self.BUTTON_ATCK2 = false;
293                         if(!WEP_CVAR(porto, secondary))
294                                 if(bot_aim(WEP_CVAR_PRI(porto, speed), 0, WEP_CVAR_PRI(porto, lifetime), false))
295                                         self.BUTTON_ATCK = true;
296                 }
297                 METHOD(PortoLaunch, wr_config, void(entity this))
298                 {
299                         PORTO_SETTINGS(WEP_CONFIG_WRITE_CVARS, WEP_CONFIG_WRITE_PROPS);
300                 }
301                 METHOD(PortoLaunch, wr_think, void(entity thiswep, entity actor, bool fire1, bool fire2))
302                 {
303                         if(WEP_CVAR(porto, secondary))
304                         {
305                                 if(fire1)
306                                 if(!actor.porto_current)
307                                 if(!actor.porto_forbidden)
308                                 if(weapon_prepareattack(thiswep, actor, false, WEP_CVAR_PRI(porto, refire)))
309                                 {
310                                         W_Porto_Attack(0);
311                                         weapon_thinkf(actor, WFRAME_FIRE1, WEP_CVAR_PRI(porto, animtime), w_ready);
312                                 }
313
314                                 if(fire2)
315                                 if(!actor.porto_current)
316                                 if(!actor.porto_forbidden)
317                                 if(weapon_prepareattack(thiswep, actor, true, WEP_CVAR_SEC(porto, refire)))
318                                 {
319                                         W_Porto_Attack(1);
320                                         weapon_thinkf(actor, WFRAME_FIRE2, WEP_CVAR_SEC(porto, animtime), w_ready);
321                                 }
322                         }
323                         else
324                         {
325                                 if(actor.porto_v_angle_held)
326                                 {
327                                         if(!fire2)
328                                         {
329                                                 actor.porto_v_angle_held = 0;
330
331                                                 ClientData_Touch(actor);
332                                         }
333                                 }
334                                 else
335                                 {
336                                         if(fire2)
337                                         {
338                                                 actor.porto_v_angle = actor.v_angle;
339                                                 actor.porto_v_angle_held = 1;
340
341                                                 ClientData_Touch(actor);
342                                         }
343                                 }
344                                 if(actor.porto_v_angle_held)
345                                         makevectors(actor.porto_v_angle); // override the previously set angles
346
347                                 if(fire1)
348                                 if(!actor.porto_current)
349                                 if(!actor.porto_forbidden)
350                                 if(weapon_prepareattack(thiswep, actor, false, WEP_CVAR_PRI(porto, refire)))
351                                 {
352                                         W_Porto_Attack(-1);
353                                         weapon_thinkf(actor, WFRAME_FIRE1, WEP_CVAR_PRI(porto, animtime), w_ready);
354                                 }
355                         }
356                 }
357                 METHOD(PortoLaunch, wr_checkammo1, bool(entity this))
358                 {
359                         // always allow infinite ammo
360                         return true;
361                 }
362                 METHOD(PortoLaunch, wr_checkammo2, bool(entity this))
363                 {
364                         // always allow infinite ammo
365                         return true;
366                 }
367                 METHOD(PortoLaunch, wr_init, void(entity this))
368                 {
369                         PORTO_SETTINGS(WEP_SKIP_CVAR, WEP_SET_PROP);
370                 }
371                 METHOD(PortoLaunch, wr_setup, void(entity thiswep))
372                 {
373                         SELFPARAM();
374                         self.ammo_field = ammo_none;
375                 }
376                 METHOD(PortoLaunch, wr_resetplayer, void(entity thiswep))
377                 {
378                         SELFPARAM();
379                         self.porto_current = world;
380                 }
381 #endif
382 #ifdef CSQC
383                 METHOD(PortoLaunch, wr_impacteffect, void(entity this)) {
384                         LOG_WARNING("Since when does Porto send DamageInfo?\n");
385                 }
386 #endif
387 #endif