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