c586e907fa948de4d400f9a34ad965299b09f616
[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 "../../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), LAMBDA(it.porto_forbidden = max(0, it.porto_forbidden - 1)));
54 }
55
56 void W_Porto_Success()
57 {SELFPARAM();
58         if(self.realowner == world)
59         {
60                 objerror("Cannot succeed successfully: no owner\n");
61                 return;
62         }
63
64         self.realowner.porto_current = world;
65         remove(self);
66 }
67
68 string W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo);
69 void W_Porto_Fail(float failhard)
70 {SELFPARAM();
71         if(self.realowner == world)
72         {
73                 objerror("Cannot fail successfully: no owner\n");
74                 return;
75         }
76
77         // no portals here!
78         if(self.cnt < 0)
79         {
80                 Portal_ClearWithID(self.realowner, self.portal_id);
81         }
82
83         self.realowner.porto_current = world;
84
85         if(self.cnt < 0 && !failhard && self.realowner.playerid == self.playerid && self.realowner.deadflag == DEAD_NO && !(self.realowner.weapons & WEPSET(PORTO)))
86         {
87                 setsize(self, '-16 -16 0', '16 16 32');
88                 setorigin(self, self.origin + trace_plane_normal);
89                 if(move_out_of_solid(self))
90                 {
91                         self.flags = FL_ITEM;
92                         self.velocity = trigger_push_calculatevelocity(self.origin, self.realowner, 128);
93                         tracetoss(self, self);
94                         if(vlen(trace_endpos - self.realowner.origin) < 128)
95                         {
96                                 W_ThrowNewWeapon(self.realowner, WEP_PORTO.m_id, 0, self.origin, self.velocity);
97                                 Send_Notification(NOTIF_ONE, self.realowner, MSG_CENTER, CENTER_PORTO_FAILED);
98                         }
99                 }
100         }
101         remove(self);
102 }
103
104 void W_Porto_Remove(entity p)
105 {SELFPARAM();
106         if(p.porto_current.realowner == p && p.porto_current.classname == "porto")
107         {
108                 WITH(entity, self, p.porto_current, W_Porto_Fail(1));
109         }
110 }
111
112 void W_Porto_Think()
113 {SELFPARAM();
114         trace_plane_normal = '0 0 0';
115         if(self.realowner.playerid != self.playerid)
116                 remove(self);
117         else
118                 W_Porto_Fail(0);
119 }
120
121 void W_Porto_Touch()
122 {SELFPARAM();
123         vector norm;
124
125         // do not use PROJECTILE_TOUCH here
126         // FIXME but DO handle warpzones!
127
128         if(other.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, self);
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(self.realowner.playerid != self.playerid)
144         {
145                 sound(self, CH_SHOTS, SND_PORTO_UNSUPPORTED, VOL_BASE, ATTEN_NORM);
146                 remove(self);
147         }
148         else if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK || trace_dphitcontents & DPCONTENTS_PLAYERCLIP)
149         {
150                 spamsound(self, CH_SHOTS, SND(PORTO_BOUNCE), VOL_BASE, ATTEN_NORM);
151                 // just reflect
152                 self.right_vector = self.right_vector - 2 * trace_plane_normal * (self.right_vector * trace_plane_normal);
153                 self.angles = vectoangles(self.velocity - 2 * trace_plane_normal * (self.velocity * trace_plane_normal));
154         }
155         else if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
156         {
157                 sound(self, CH_SHOTS, SND_PORTO_UNSUPPORTED, VOL_BASE, ATTEN_NORM);
158                 W_Porto_Fail(0);
159                 if(self.cnt < 0)
160                         Portal_ClearAll_PortalsOnly(self.realowner);
161         }
162         else if(self.cnt == 0)
163         {
164                 // in-portal only
165                 if(Portal_SpawnInPortalAtTrace(self.realowner, self.right_vector, self.portal_id))
166                 {
167                         sound(self, CH_SHOTS, SND_PORTO_CREATE, VOL_BASE, ATTEN_NORM);
168                         trace_plane_normal = norm;
169                         Send_Notification(NOTIF_ONE, self.realowner, MSG_CENTER, CENTER_PORTO_CREATED_IN);
170                         W_Porto_Success();
171                 }
172                 else
173                 {
174                         sound(self, CH_SHOTS, SND_PORTO_UNSUPPORTED, VOL_BASE, ATTEN_NORM);
175                         trace_plane_normal = norm;
176                         W_Porto_Fail(0);
177                 }
178         }
179         else if(self.cnt == 1)
180         {
181                 // out-portal only
182                 if(Portal_SpawnOutPortalAtTrace(self.realowner, self.right_vector, self.portal_id))
183                 {
184                         sound(self, CH_SHOTS, SND_PORTO_CREATE, VOL_BASE, ATTEN_NORM);
185                         trace_plane_normal = norm;
186                         Send_Notification(NOTIF_ONE, self.realowner, MSG_CENTER, CENTER_PORTO_CREATED_OUT);
187                         W_Porto_Success();
188                 }
189                 else
190                 {
191                         sound(self, CH_SHOTS, SND_PORTO_UNSUPPORTED, VOL_BASE, ATTEN_NORM);
192                         trace_plane_normal = norm;
193                         W_Porto_Fail(0);
194                 }
195         }
196         else if(self.effects & EF_RED)
197         {
198                 self.effects += EF_BLUE - EF_RED;
199                 if(Portal_SpawnInPortalAtTrace(self.realowner, self.right_vector, self.portal_id))
200                 {
201                         sound(self, CH_SHOTS, SND_PORTO_CREATE, VOL_BASE, ATTEN_NORM);
202                         trace_plane_normal = norm;
203                         Send_Notification(NOTIF_ONE, self.realowner, MSG_CENTER, CENTER_PORTO_CREATED_IN);
204                         self.right_vector = self.right_vector - 2 * trace_plane_normal * (self.right_vector * norm);
205                         self.angles = vectoangles(self.velocity - 2 * trace_plane_normal * (self.velocity * norm));
206                         CSQCProjectile(self, true, PROJECTILE_PORTO_BLUE, true); // change type
207                 }
208                 else
209                 {
210                         sound(self, CH_SHOTS, SND_PORTO_UNSUPPORTED, VOL_BASE, ATTEN_NORM);
211                         trace_plane_normal = norm;
212                         Portal_ClearAll_PortalsOnly(self.realowner);
213                         W_Porto_Fail(0);
214                 }
215         }
216         else
217         {
218                 if(self.realowner.portal_in.portal_id == self.portal_id)
219                 {
220                         if(Portal_SpawnOutPortalAtTrace(self.realowner, self.right_vector, self.portal_id))
221                         {
222                                 sound(self, CH_SHOTS, SND_PORTO_CREATE, VOL_BASE, ATTEN_NORM);
223                                 trace_plane_normal = norm;
224                                 Send_Notification(NOTIF_ONE, self.realowner, MSG_CENTER, CENTER_PORTO_CREATED_OUT);
225                                 W_Porto_Success();
226                         }
227                         else
228                         {
229                                 sound(self, CH_SHOTS, SND_PORTO_UNSUPPORTED, VOL_BASE, ATTEN_NORM);
230                                 Portal_ClearAll_PortalsOnly(self.realowner);
231                                 W_Porto_Fail(0);
232                         }
233                 }
234                 else
235                 {
236                         sound(self, CH_SHOTS, SND_PORTO_UNSUPPORTED, VOL_BASE, ATTEN_NORM);
237                         Portal_ClearAll_PortalsOnly(self.realowner);
238                         W_Porto_Fail(0);
239                 }
240         }
241 }
242
243 void W_Porto_Attack(float type)
244 {SELFPARAM();
245         entity gren;
246
247         W_SetupShot(self, false, 4, SND(PORTO_FIRE), CH_WEAPON_A, 0);
248         // always shoot from the eye
249         w_shotdir = v_forward;
250         w_shotorg = self.origin + self.view_ofs + ((w_shotorg - self.origin - self.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 = self;
257         gren.playerid = self.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         gren.think = W_Porto_Think;
269         gren.touch = W_Porto_Touch;
270
271         if(self.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         self.porto_current = gren;
281         gren.playerid = self.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, self, gren);
293 }
294
295                 METHOD(PortoLaunch, wr_aim, void(entity thiswep))
296                 {
297                         SELFPARAM();
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                 METHOD(PortoLaunch, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
305                 {
306                         if(WEP_CVAR(porto, secondary))
307                         {
308                                 if(fire & 1)
309                                 if(!actor.porto_current)
310                                 if(!actor.porto_forbidden)
311                                 if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR_PRI(porto, refire)))
312                                 {
313                                         W_Porto_Attack(0);
314                                         weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR_PRI(porto, animtime), w_ready);
315                                 }
316
317                                 if(fire & 2)
318                                 if(!actor.porto_current)
319                                 if(!actor.porto_forbidden)
320                                 if(weapon_prepareattack(thiswep, actor, weaponentity, true, WEP_CVAR_SEC(porto, refire)))
321                                 {
322                                         W_Porto_Attack(1);
323                                         weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, WEP_CVAR_SEC(porto, animtime), w_ready);
324                                 }
325                         }
326                         else
327                         {
328                                 if(actor.porto_v_angle_held)
329                                 {
330                                         if(!(fire & 2))
331                                         {
332                                                 actor.porto_v_angle_held = 0;
333
334                                                 ClientData_Touch(actor);
335                                         }
336                                 }
337                                 else
338                                 {
339                                         if(fire & 2)
340                                         {
341                                                 actor.porto_v_angle = actor.v_angle;
342                                                 actor.porto_v_angle_held = 1;
343
344                                                 ClientData_Touch(actor);
345                                         }
346                                 }
347                                 if(actor.porto_v_angle_held)
348                                         makevectors(actor.porto_v_angle); // override the previously set angles
349
350                                 if(fire & 1)
351                                 if(!actor.porto_current)
352                                 if(!actor.porto_forbidden)
353                                 if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR_PRI(porto, refire)))
354                                 {
355                                         W_Porto_Attack(-1);
356                                         weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR_PRI(porto, animtime), w_ready);
357                                 }
358                         }
359                 }
360                 METHOD(PortoLaunch, wr_checkammo1, bool(entity this))
361                 {
362                         // always allow infinite ammo
363                         return true;
364                 }
365                 METHOD(PortoLaunch, wr_checkammo2, bool(entity this))
366                 {
367                         // always allow infinite ammo
368                         return true;
369                 }
370                 METHOD(PortoLaunch, wr_setup, void(entity thiswep))
371                 {
372                         SELFPARAM();
373                         self.ammo_field = ammo_none;
374                 }
375                 METHOD(PortoLaunch, wr_resetplayer, void(entity thiswep))
376                 {
377                         SELFPARAM();
378                         self.porto_current = world;
379                 }
380 #endif
381 #ifdef CSQC
382                 METHOD(PortoLaunch, wr_impacteffect, void(entity this)) {
383                         LOG_WARNING("Since when does Porto send DamageInfo?\n");
384                 }
385 #endif
386 #endif