Give W_SetupShot a deathtype parameter, fixes some ugly hacks
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / weapons / weapon / porto.qc
1 #include "porto.qh"
2
3 #ifdef SVQC
4 #include <common/triggers/trigger/jumppads.qh>
5
6 REGISTER_MUTATOR(porto_ticker, true);
7 MUTATOR_HOOKFUNCTION(porto_ticker, SV_StartFrame) {
8         FOREACH_CLIENT(IS_PLAYER(it), it.porto_forbidden = max(0, it.porto_forbidden - 1));
9 }
10
11 void W_Porto_Success(entity this)
12 {
13         if(this.realowner == NULL)
14         {
15                 objerror(this, "Cannot succeed successfully: no owner\n");
16                 return;
17         }
18
19         this.realowner.porto_current = NULL;
20         delete(this);
21 }
22
23 string W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo, .entity weaponentity);
24 void W_Porto_Fail(entity this, float failhard)
25 {
26         if(this.realowner == NULL)
27         {
28                 objerror(this, "Cannot fail successfully: no owner\n");
29                 return;
30         }
31
32         // no portals here!
33         if(this.cnt < 0)
34         {
35                 Portal_ClearWithID(this.realowner, this.portal_id);
36         }
37
38         this.realowner.porto_current = NULL;
39
40         if(this.cnt < 0 && !failhard && this.realowner.playerid == this.playerid && !IS_DEAD(this.realowner) && !(this.realowner.weapons & WEPSET(PORTO)))
41         {
42                 setsize(this, '-16 -16 0', '16 16 32');
43                 setorigin(this, this.origin + trace_plane_normal);
44                 if(move_out_of_solid(this))
45                 {
46                         this.flags = FL_ITEM;
47                         IL_PUSH(g_items, this);
48                         this.velocity = trigger_push_calculatevelocity(this.origin, this.realowner, 128, this);
49                         tracetoss(this, this);
50                         if(vdist(trace_endpos - this.realowner.origin, <, 128))
51                         {
52                                 .entity weaponentity = this.weaponentity_fld;
53                                 W_ThrowNewWeapon(this.realowner, WEP_PORTO.m_id, 0, this.origin, this.velocity, weaponentity);
54                                 Send_Notification(NOTIF_ONE, this.realowner, MSG_CENTER, CENTER_PORTO_FAILED);
55                         }
56                 }
57         }
58         delete(this);
59 }
60
61 void W_Porto_Remove(entity p)
62 {
63         if(p.porto_current.realowner == p && p.porto_current.classname == "porto")
64         {
65                 W_Porto_Fail(p.porto_current, 1);
66         }
67 }
68
69 void W_Porto_Think(entity this)
70 {
71         trace_plane_normal = '0 0 0';
72         if(this.realowner.playerid != this.playerid)
73                 delete(this);
74         else
75                 W_Porto_Fail(this, 0);
76 }
77
78 void W_Porto_Touch(entity this, entity toucher)
79 {
80         vector norm;
81
82         // do not use PROJECTILE_TOUCH here
83         // FIXME but DO handle warpzones!
84
85         if(toucher.classname == "portal")
86                 return; // handled by the portal
87
88         norm = trace_plane_normal;
89         if(trace_ent.iscreature)
90         {
91                 // TODO: why not use entity size?
92                 traceline(trace_ent.origin, trace_ent.origin + '0 0 2' * PL_MIN_CONST.z, MOVE_WORLDONLY, this);
93                 if(trace_fraction >= 1)
94                         return;
95                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK || trace_dphitcontents & DPCONTENTS_PLAYERCLIP)
96                         return;
97                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
98                         return;
99         }
100
101         if(this.realowner.playerid != this.playerid)
102         {
103                 sound(this, CH_SHOTS, SND_PORTO_UNSUPPORTED, VOL_BASE, ATTEN_NORM);
104                 delete(this);
105         }
106         else if((trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK) || (trace_dphitcontents & DPCONTENTS_PLAYERCLIP))
107         {
108                 spamsound(this, CH_SHOTS, SND_PORTO_BOUNCE, VOL_BASE, ATTEN_NORM);
109                 // just reflect
110                 this.right_vector = this.right_vector - 2 * trace_plane_normal * (this.right_vector * trace_plane_normal);
111                 this.angles = vectoangles(this.velocity - 2 * trace_plane_normal * (this.velocity * trace_plane_normal));
112         }
113         else if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
114         {
115                 sound(this, CH_SHOTS, SND_PORTO_UNSUPPORTED, VOL_BASE, ATTEN_NORM);
116                 W_Porto_Fail(this, 0);
117                 if(this.cnt < 0)
118                         Portal_ClearAll_PortalsOnly(this.realowner);
119         }
120         else if(this.cnt == 0)
121         {
122                 // in-portal only
123                 if(Portal_SpawnInPortalAtTrace(this.realowner, this.right_vector, this.portal_id))
124                 {
125                         sound(this, CH_SHOTS, SND_PORTO_CREATE, VOL_BASE, ATTEN_NORM);
126                         trace_plane_normal = norm;
127                         Send_Notification(NOTIF_ONE, this.realowner, MSG_CENTER, CENTER_PORTO_CREATED_IN);
128                         W_Porto_Success(this);
129                 }
130                 else
131                 {
132                         sound(this, CH_SHOTS, SND_PORTO_UNSUPPORTED, VOL_BASE, ATTEN_NORM);
133                         trace_plane_normal = norm;
134                         W_Porto_Fail(this, 0);
135                 }
136         }
137         else if(this.cnt == 1)
138         {
139                 // out-portal only
140                 if(Portal_SpawnOutPortalAtTrace(this.realowner, this.right_vector, this.portal_id))
141                 {
142                         sound(this, CH_SHOTS, SND_PORTO_CREATE, VOL_BASE, ATTEN_NORM);
143                         trace_plane_normal = norm;
144                         Send_Notification(NOTIF_ONE, this.realowner, MSG_CENTER, CENTER_PORTO_CREATED_OUT);
145                         W_Porto_Success(this);
146                 }
147                 else
148                 {
149                         sound(this, CH_SHOTS, SND_PORTO_UNSUPPORTED, VOL_BASE, ATTEN_NORM);
150                         trace_plane_normal = norm;
151                         W_Porto_Fail(this, 0);
152                 }
153         }
154         else if(this.effects & EF_RED)
155         {
156                 this.effects += EF_BLUE - EF_RED;
157                 if(Portal_SpawnInPortalAtTrace(this.realowner, this.right_vector, this.portal_id))
158                 {
159                         sound(this, CH_SHOTS, SND_PORTO_CREATE, VOL_BASE, ATTEN_NORM);
160                         trace_plane_normal = norm;
161                         Send_Notification(NOTIF_ONE, this.realowner, MSG_CENTER, CENTER_PORTO_CREATED_IN);
162                         this.right_vector = this.right_vector - 2 * trace_plane_normal * (this.right_vector * norm);
163                         this.angles = vectoangles(this.velocity - 2 * trace_plane_normal * (this.velocity * norm));
164                         CSQCProjectile(this, true, PROJECTILE_PORTO_BLUE, true); // change type
165                 }
166                 else
167                 {
168                         sound(this, CH_SHOTS, SND_PORTO_UNSUPPORTED, VOL_BASE, ATTEN_NORM);
169                         trace_plane_normal = norm;
170                         Portal_ClearAll_PortalsOnly(this.realowner);
171                         W_Porto_Fail(this, 0);
172                 }
173         }
174         else
175         {
176                 if(this.realowner.portal_in.portal_id == this.portal_id)
177                 {
178                         if(Portal_SpawnOutPortalAtTrace(this.realowner, this.right_vector, this.portal_id))
179                         {
180                                 sound(this, CH_SHOTS, SND_PORTO_CREATE, VOL_BASE, ATTEN_NORM);
181                                 trace_plane_normal = norm;
182                                 Send_Notification(NOTIF_ONE, this.realowner, MSG_CENTER, CENTER_PORTO_CREATED_OUT);
183                                 W_Porto_Success(this);
184                         }
185                         else
186                         {
187                                 sound(this, CH_SHOTS, SND_PORTO_UNSUPPORTED, VOL_BASE, ATTEN_NORM);
188                                 Portal_ClearAll_PortalsOnly(this.realowner);
189                                 W_Porto_Fail(this, 0);
190                         }
191                 }
192                 else
193                 {
194                         sound(this, CH_SHOTS, SND_PORTO_UNSUPPORTED, VOL_BASE, ATTEN_NORM);
195                         Portal_ClearAll_PortalsOnly(this.realowner);
196                         W_Porto_Fail(this, 0);
197                 }
198         }
199 }
200
201 void W_Porto_Attack(entity actor, .entity weaponentity, float type)
202 {
203         entity gren;
204
205         W_SetupShot(actor, weaponentity, false, 4, SND_PORTO_FIRE, CH_WEAPON_A, 0, WEP_PORTO.m_id); // TODO: does the deathtype even need to be set here? porto can't hurt people
206         // always shoot from the eye
207         w_shotdir = v_forward;
208         w_shotorg = actor.origin + actor.view_ofs + ((w_shotorg - actor.origin - actor.view_ofs) * v_forward) * v_forward;
209
210         //Send_Effect(EFFECT_GRENADE_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
211
212         gren = new(porto);
213         gren.weaponentity_fld = weaponentity;
214         gren.cnt = type;
215         gren.owner = gren.realowner = actor;
216         gren.playerid = actor.playerid;
217         gren.bot_dodge = true;
218         gren.bot_dodgerating = 200;
219         set_movetype(gren, MOVETYPE_BOUNCEMISSILE);
220         PROJECTILE_MAKETRIGGER(gren);
221         gren.effects = EF_RED;
222         gren.scale = 4;
223         setorigin(gren, w_shotorg);
224         setsize(gren, '0 0 0', '0 0 0');
225
226         gren.nextthink = time + WEP_CVAR_BOTH(porto, (type <= 0), lifetime);
227         setthink(gren, W_Porto_Think);
228         settouch(gren, W_Porto_Touch);
229
230         if(actor.items & ITEM_Strength.m_itemid)
231                 W_SetupProjVelocity_Basic(gren, WEP_CVAR_BOTH(porto, (type <= 0), speed) * autocvar_g_balance_powerup_strength_force, 0);
232         else
233                 W_SetupProjVelocity_Basic(gren, WEP_CVAR_BOTH(porto, (type <= 0), speed), 0);
234
235         gren.angles = vectoangles(gren.velocity);
236         gren.flags = FL_PROJECTILE;
237         IL_PUSH(g_projectiles, gren);
238         IL_PUSH(g_bot_dodge, gren);
239
240         gren.portal_id = time;
241         actor.porto_current = gren;
242         gren.playerid = actor.playerid;
243         fixedmakevectors(fixedvectoangles(gren.velocity));
244         gren.right_vector = v_right;
245
246         gren.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
247
248         if(type > 0)
249                 CSQCProjectile(gren, true, PROJECTILE_PORTO_BLUE, true);
250         else
251                 CSQCProjectile(gren, true, PROJECTILE_PORTO_RED, true);
252
253         MUTATOR_CALLHOOK(EditProjectile, actor, gren);
254 }
255
256 METHOD(PortoLaunch, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
257 {
258     PHYS_INPUT_BUTTON_ATCK(actor) = false;
259     PHYS_INPUT_BUTTON_ATCK2(actor) = false;
260     if(!WEP_CVAR(porto, secondary))
261         if(bot_aim(actor, weaponentity, WEP_CVAR_PRI(porto, speed), 0, WEP_CVAR_PRI(porto, lifetime), false))
262             PHYS_INPUT_BUTTON_ATCK(actor) = true;
263 }
264 METHOD(PortoLaunch, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
265 {
266     if(WEP_CVAR(porto, secondary))
267     {
268         if(fire & 1)
269         if(!actor.porto_current)
270         if(!actor.porto_forbidden)
271         if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR_PRI(porto, refire)))
272         {
273             W_Porto_Attack(actor, weaponentity, 0);
274             weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR_PRI(porto, animtime), w_ready);
275         }
276
277         if(fire & 2)
278         if(!actor.porto_current)
279         if(!actor.porto_forbidden)
280         if(weapon_prepareattack(thiswep, actor, weaponentity, true, WEP_CVAR_SEC(porto, refire)))
281         {
282             W_Porto_Attack(actor, weaponentity, 1);
283             weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR_SEC(porto, animtime), w_ready);
284         }
285     }
286     else
287     {
288         if(actor.(weaponentity).porto_v_angle_held)
289         {
290             if(!(fire & 2))
291                 actor.(weaponentity).porto_v_angle_held = 0;
292         }
293         else
294         {
295             if(fire & 2)
296             {
297                 actor.(weaponentity).porto_v_angle = actor.v_angle;
298                 actor.(weaponentity).porto_v_angle_held = 1;
299             }
300         }
301         if(actor.(weaponentity).porto_v_angle_held)
302             makevectors(actor.(weaponentity).porto_v_angle); // override the previously set angles
303
304         if(fire & 1)
305         if(!actor.porto_current)
306         if(!actor.porto_forbidden)
307         if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR_PRI(porto, refire)))
308         {
309             W_Porto_Attack(actor, weaponentity, -1);
310             weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR_PRI(porto, animtime), w_ready);
311         }
312     }
313 }
314 METHOD(PortoLaunch, wr_checkammo1, bool(entity thiswep, entity this, .entity weaponentity))
315 {
316     // always allow infinite ammo
317     return true;
318 }
319 METHOD(PortoLaunch, wr_checkammo2, bool(entity thiswep, entity this, .entity weaponentity))
320 {
321     // always allow infinite ammo
322     return true;
323 }
324 METHOD(PortoLaunch, wr_resetplayer, void(entity thiswep, entity actor))
325 {
326     actor.porto_current = NULL;
327 }
328 #endif
329 #ifdef CSQC
330 METHOD(PortoLaunch, wr_impacteffect, void(entity this, entity actor)) {
331     LOG_WARN("Since when does Porto send DamageInfo?");
332 }
333 #endif