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