Weapons: remove useless weapon return values and implementations
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / monsters / monster / mage.qc
1 #ifndef MAGE_H
2 #define MAGE_H
3
4 #ifndef MENUQC
5 MODEL(MON_MAGE, "models/monsters/mage.dpm");
6 #endif
7
8 CLASS(Mage, Monster)
9     ATTRIB(Mage, spawnflags, int, MON_FLAG_MELEE | MON_FLAG_RANGED);
10     ATTRIB(Mage, mins, vector, '-36 -36 -24');
11     ATTRIB(Mage, maxs, vector, '36 36 50');
12 #ifndef MENUQC
13     ATTRIB(Mage, m_model, Model, MDL_MON_MAGE);
14 #endif
15     ATTRIB(Mage, netname, string, "mage");
16     ATTRIB(Mage, monster_name, string, _("Mage"));
17 ENDCLASS(Mage)
18
19 REGISTER_MONSTER(MAGE, NEW(Mage)) {
20 #ifndef MENUQC
21     MON_ACTION(this, MR_PRECACHE);
22 #endif
23 }
24
25 #include "../../weapons/all.qh"
26
27 CLASS(MageSpike, PortoLaunch)
28 /* flags     */ ATTRIB(MageSpike, spawnflags, int, WEP_TYPE_OTHER | WEP_FLAG_HIDDEN);
29 /* impulse   */ ATTRIB(MageSpike, impulse, int, 9);
30 /* refname   */ ATTRIB(MageSpike, netname, string, "magespike");
31 /* wepname   */ ATTRIB(MageSpike, message, string, _("Mage spike"));
32 ENDCLASS(MageSpike)
33 REGISTER_WEAPON(MAGE_SPIKE, NEW(MageSpike));
34
35 #endif
36
37 #ifdef IMPLEMENTATION
38
39 #ifdef SVQC
40
41 void M_Mage_Attack_Spike(vector dir);
42 void M_Mage_Attack_Push();
43 METHOD(MageSpike, wr_think, void(MageSpike thiswep, bool fire1, bool fire2)) {
44     SELFPARAM();
45     if (fire1)
46     if (!IS_PLAYER(self) || weapon_prepareattack(false, 0.2)) {
47         if (!self.target_range) self.target_range = autocvar_g_monsters_target_range;
48         self.enemy = Monster_FindTarget(self);
49         W_SetupShot_Dir(self, v_forward, false, 0, W_Sound("electro_fire"), CH_WEAPON_B, 0);
50         if (!IS_PLAYER(self)) w_shotdir = normalize((self.enemy.origin + '0 0 10') - self.origin);
51         M_Mage_Attack_Spike(w_shotdir);
52         weapon_thinkf(WFRAME_FIRE1, 0, w_ready);
53     }
54     if (fire2)
55     if (!IS_PLAYER(self) || weapon_prepareattack(true, 0.5)) {
56         M_Mage_Attack_Push();
57         weapon_thinkf(WFRAME_FIRE2, 0, w_ready);
58     }
59 }
60
61 void M_Mage_Attack_Teleport();
62
63 CLASS(OffhandMageTeleport, OffhandWeapon)
64     .bool OffhandMageTeleport_key_pressed;
65     METHOD(OffhandMageTeleport, offhand_think, void(OffhandMageTeleport this, entity player, bool key_pressed))
66     {
67         if (key_pressed && !player.OffhandMageTeleport_key_pressed)
68             WITH(entity, self, player, M_Mage_Attack_Teleport());
69         player.OffhandMageTeleport_key_pressed = key_pressed;
70     }
71 ENDCLASS(OffhandMageTeleport)
72 OffhandMageTeleport OFFHAND_MAGE_TELEPORT; STATIC_INIT(OFFHAND_MAGE_TELEPORT) { OFFHAND_MAGE_TELEPORT = NEW(OffhandMageTeleport); }
73
74 float autocvar_g_monster_mage_health;
75 float autocvar_g_monster_mage_damageforcescale = 0.5;
76 float autocvar_g_monster_mage_attack_spike_damage;
77 float autocvar_g_monster_mage_attack_spike_radius;
78 float autocvar_g_monster_mage_attack_spike_delay;
79 float autocvar_g_monster_mage_attack_spike_accel;
80 float autocvar_g_monster_mage_attack_spike_decel;
81 float autocvar_g_monster_mage_attack_spike_turnrate;
82 float autocvar_g_monster_mage_attack_spike_speed_max;
83 float autocvar_g_monster_mage_attack_spike_smart;
84 float autocvar_g_monster_mage_attack_spike_smart_trace_min;
85 float autocvar_g_monster_mage_attack_spike_smart_trace_max;
86 float autocvar_g_monster_mage_attack_spike_smart_mindist;
87 float autocvar_g_monster_mage_attack_push_damage;
88 float autocvar_g_monster_mage_attack_push_radius;
89 float autocvar_g_monster_mage_attack_push_delay;
90 float autocvar_g_monster_mage_attack_push_force;
91 float autocvar_g_monster_mage_heal_self;
92 float autocvar_g_monster_mage_heal_allies;
93 float autocvar_g_monster_mage_heal_minhealth;
94 float autocvar_g_monster_mage_heal_range;
95 float autocvar_g_monster_mage_heal_delay;
96 float autocvar_g_monster_mage_shield_time;
97 float autocvar_g_monster_mage_shield_delay;
98 float autocvar_g_monster_mage_shield_blockpercent;
99 float autocvar_g_monster_mage_speed_stop;
100 float autocvar_g_monster_mage_speed_run;
101 float autocvar_g_monster_mage_speed_walk;
102
103 /*
104 const float mage_anim_idle              = 0;
105 const float mage_anim_walk              = 1;
106 const float mage_anim_attack    = 2;
107 const float mage_anim_pain              = 3;
108 const float mage_anim_death             = 4;
109 const float mage_anim_run               = 5;
110 */
111
112 void() M_Mage_Defend_Heal;
113 void() M_Mage_Defend_Shield;
114
115 .entity mage_spike;
116 .float mage_shield_delay;
117 .float mage_shield_time;
118
119 float M_Mage_Defend_Heal_Check(entity e)
120 {SELFPARAM();
121         if(e == world)
122                 return false;
123         if(e.health <= 0)
124                 return false;
125         if(DIFF_TEAM(e, self) && e != self.monster_follow)
126                 return false;
127         if(e.frozen)
128                 return false;
129         if(!IS_PLAYER(e))
130                 return (IS_MONSTER(e) && e.health < e.max_health);
131         if(e.items & ITEM_Shield.m_itemid)
132                 return false;
133
134         switch(self.skin)
135         {
136                 case 0: return (e.health < autocvar_g_balance_health_regenstable);
137                 case 1: return ((e.ammo_cells && e.ammo_cells < g_pickup_cells_max) || (e.ammo_plasma && e.ammo_plasma < g_pickup_plasma_max) || (e.ammo_rockets && e.ammo_rockets < g_pickup_rockets_max) || (e.ammo_nails && e.ammo_nails < g_pickup_nails_max) || (e.ammo_shells && e.ammo_shells < g_pickup_shells_max));
138                 case 2: return (e.armorvalue < autocvar_g_balance_armor_regenstable);
139                 case 3: return (e.health > 0);
140         }
141
142         return false;
143 }
144
145 void M_Mage_Attack_Spike_Explode()
146 {SELFPARAM();
147         self.event_damage = func_null;
148
149         sound(self, CH_SHOTS, SND_GRENADE_IMPACT, VOL_BASE, ATTEN_NORM);
150
151         self.realowner.mage_spike = world;
152
153         Send_Effect(EFFECT_EXPLOSION_SMALL, self.origin, '0 0 0', 1);
154         RadiusDamage (self, self.realowner, (autocvar_g_monster_mage_attack_spike_damage), (autocvar_g_monster_mage_attack_spike_damage) * 0.5, (autocvar_g_monster_mage_attack_spike_radius), world, world, 0, DEATH_MONSTER_MAGE, other);
155
156         remove (self);
157 }
158
159 void M_Mage_Attack_Spike_Touch()
160 {
161         PROJECTILE_TOUCH;
162
163         M_Mage_Attack_Spike_Explode();
164 }
165
166 // copied from W_Seeker_Think
167 void M_Mage_Attack_Spike_Think()
168 {SELFPARAM();
169         if (time > self.ltime || (self.enemy && self.enemy.health <= 0) || self.owner.health <= 0) {
170                 self.projectiledeathtype |= HITTYPE_SPLASH;
171                 M_Mage_Attack_Spike_Explode();
172         }
173
174         float spd = vlen(self.velocity);
175         spd = bound(
176                 spd - (autocvar_g_monster_mage_attack_spike_decel) * frametime,
177                 (autocvar_g_monster_mage_attack_spike_speed_max),
178                 spd + (autocvar_g_monster_mage_attack_spike_accel) * frametime
179         );
180
181         if (self.enemy != world)
182                 if (self.enemy.takedamage != DAMAGE_AIM || self.enemy.deadflag != DEAD_NO)
183                         self.enemy = world;
184
185         if (self.enemy != world)
186         {
187                 entity e = self.enemy;
188                 vector eorg = 0.5 * (e.absmin + e.absmax);
189                 float turnrate = (autocvar_g_monster_mage_attack_spike_turnrate); // how fast to turn
190                 vector desireddir = normalize(eorg - self.origin);
191                 vector olddir = normalize(self.velocity); // get my current direction
192                 float dist = vlen(eorg - self.origin);
193
194                 // Do evasive maneuvers for world objects? ( this should be a cpu hog. :P )
195                 if ((autocvar_g_monster_mage_attack_spike_smart) && (dist > (autocvar_g_monster_mage_attack_spike_smart_mindist)))
196                 {
197                         // Is it a better idea (shorter distance) to trace to the target itself?
198                         if ( vlen(self.origin + olddir * self.wait) < dist)
199                                 traceline(self.origin, self.origin + olddir * self.wait, false, self);
200                         else
201                                 traceline(self.origin, eorg, false, self);
202
203                         // Setup adaptive tracelength
204                         self.wait = bound((autocvar_g_monster_mage_attack_spike_smart_trace_min), vlen(self.origin - trace_endpos), self.wait = (autocvar_g_monster_mage_attack_spike_smart_trace_max));
205
206                         // Calc how important it is that we turn and add this to the desierd (enemy) dir.
207                         desireddir = normalize(((trace_plane_normal * (1 - trace_fraction)) + (desireddir * trace_fraction)) * 0.5);
208                 }
209
210                 vector newdir = normalize(olddir + desireddir * turnrate); // take the average of the 2 directions; not the best method but simple & easy
211                 self.velocity = newdir * spd; // make me fly in the new direction at my flight speed
212         }
213
214         ///////////////
215
216         //self.angles = vectoangles(self.velocity);                     // turn model in the new flight direction
217         self.nextthink = time;// + 0.05; // csqc projectiles
218         UpdateCSQCProjectile(self);
219 }
220
221 void M_Mage_Attack_Spike(vector dir)
222 {
223         SELFPARAM();
224         makevectors(self.angles);
225
226         entity missile = spawn();
227         missile.owner = missile.realowner = self;
228         missile.think = M_Mage_Attack_Spike_Think;
229         missile.ltime = time + 7;
230         missile.nextthink = time;
231         missile.solid = SOLID_BBOX;
232         missile.movetype = MOVETYPE_FLYMISSILE;
233         missile.flags = FL_PROJECTILE;
234         setorigin(missile, self.origin + v_forward * 14 + '0 0 30' + v_right * -14);
235         setsize(missile, '0 0 0', '0 0 0');
236         missile.velocity = dir * 400;
237         missile.avelocity = '300 300 300';
238         missile.enemy = self.enemy;
239         missile.touch = M_Mage_Attack_Spike_Touch;
240
241         self.mage_spike = missile;
242
243         CSQCProjectile(missile, true, PROJECTILE_MAGE_SPIKE, true);
244 }
245
246 void M_Mage_Defend_Heal()
247 {SELFPARAM();
248         entity head;
249         float washealed = false;
250
251         for(head = findradius(self.origin, (autocvar_g_monster_mage_heal_range)); head; head = head.chain) if(M_Mage_Defend_Heal_Check(head))
252         {
253                 washealed = true;
254                 string fx = "";
255                 if(IS_PLAYER(head))
256                 {
257                         switch(self.skin)
258                         {
259                                 case 0:
260                                         if(head.health < autocvar_g_balance_health_regenstable) head.health = bound(0, head.health + (autocvar_g_monster_mage_heal_allies), autocvar_g_balance_health_regenstable);
261                                         fx = EFFECT_HEALING.eent_eff_name;
262                                         break;
263                                 case 1:
264                                         if(head.ammo_cells) head.ammo_cells = bound(head.ammo_cells, head.ammo_cells + 1, g_pickup_cells_max);
265                                         if(head.ammo_plasma) head.ammo_plasma = bound(head.ammo_plasma, head.ammo_plasma + 1, g_pickup_plasma_max);
266                                         if(head.ammo_rockets) head.ammo_rockets = bound(head.ammo_rockets, head.ammo_rockets + 1, g_pickup_rockets_max);
267                                         if(head.ammo_shells) head.ammo_shells = bound(head.ammo_shells, head.ammo_shells + 2, g_pickup_shells_max);
268                                         if(head.ammo_nails) head.ammo_nails = bound(head.ammo_nails, head.ammo_nails + 5, g_pickup_nails_max);
269                                         fx = "ammoregen_fx";
270                                         break;
271                                 case 2:
272                                         if(head.armorvalue < autocvar_g_balance_armor_regenstable)
273                                         {
274                                                 head.armorvalue = bound(0, head.armorvalue + (autocvar_g_monster_mage_heal_allies), autocvar_g_balance_armor_regenstable);
275                                                 fx = "armorrepair_fx";
276                                         }
277                                         break;
278                                 case 3:
279                                         head.health = bound(0, head.health - ((head == self)  ? (autocvar_g_monster_mage_heal_self) : (autocvar_g_monster_mage_heal_allies)), autocvar_g_balance_health_regenstable);
280                                         fx = EFFECT_RAGE.eent_eff_name;
281                                         break;
282                         }
283
284                         Send_Effect_(fx, head.origin, '0 0 0', 1);
285                 }
286                 else
287                 {
288                         Send_Effect(EFFECT_HEALING, head.origin, '0 0 0', 1);
289                         head.health = bound(0, head.health + (autocvar_g_monster_mage_heal_allies), head.max_health);
290                         if(!(head.spawnflags & MONSTERFLAG_INVINCIBLE) && head.sprite)
291                                 WaypointSprite_UpdateHealth(head.sprite, head.health);
292                 }
293         }
294
295         if(washealed)
296         {
297                 setanim(self, self.anim_shoot, true, true, true);
298                 self.attack_finished_single = time + (autocvar_g_monster_mage_heal_delay);
299                 self.anim_finished = time + 1.5;
300         }
301 }
302
303 void M_Mage_Attack_Push()
304 {SELFPARAM();
305         sound(self, CH_SHOTS, SND_TAGEXP1, 1, ATTEN_NORM);
306         RadiusDamage (self, self, (autocvar_g_monster_mage_attack_push_damage), (autocvar_g_monster_mage_attack_push_damage), (autocvar_g_monster_mage_attack_push_radius), world, world, (autocvar_g_monster_mage_attack_push_force), DEATH_MONSTER_MAGE, self.enemy);
307         Send_Effect(EFFECT_TE_EXPLOSION, self.origin, '0 0 0', 1);
308
309         setanim(self, self.anim_shoot, true, true, true);
310         self.attack_finished_single = time + (autocvar_g_monster_mage_attack_push_delay);
311 }
312
313 void M_Mage_Attack_Teleport()
314 {SELFPARAM();
315         entity targ = self.enemy;
316         if (!targ) return;
317         if (vlen(targ.origin - self.origin) > 1500) return;
318
319         makevectors(targ.angles);
320         tracebox(targ.origin + ((v_forward * -1) * 200), self.mins, self.maxs, self.origin, MOVE_NOMONSTERS, self);
321
322         if(trace_fraction < 1)
323                 return;
324
325         Send_Effect(EFFECT_SPAWN_NEUTRAL, self.origin, '0 0 0', 1);
326         setorigin(self, targ.origin + ((v_forward * -1) * 200));
327
328         vector a = vectoangles(targ.origin - self.origin);
329         a.x = -a.x;
330         self.angles_x = a.x;
331         self.angles_y = a.y;
332         self.fixangle = true;
333         self.velocity *= 0.5;
334
335         self.attack_finished_single = time + 0.2;
336 }
337
338 void M_Mage_Defend_Shield_Remove()
339 {SELFPARAM();
340         self.effects &= ~(EF_ADDITIVE | EF_BLUE);
341         self.armorvalue = autocvar_g_monsters_armor_blockpercent;
342 }
343
344 void M_Mage_Defend_Shield()
345 {SELFPARAM();
346         self.effects |= (EF_ADDITIVE | EF_BLUE);
347         self.mage_shield_delay = time + (autocvar_g_monster_mage_shield_delay);
348         self.armorvalue = (autocvar_g_monster_mage_shield_blockpercent);
349         self.mage_shield_time = time + (autocvar_g_monster_mage_shield_time);
350         setanim(self, self.anim_shoot, true, true, true);
351         self.attack_finished_single = time + 1;
352         self.anim_finished = time + 1;
353 }
354
355 float M_Mage_Attack(float attack_type, entity targ)
356 {SELFPARAM();
357         switch(attack_type)
358         {
359                 case MONSTER_ATTACK_MELEE:
360                 {
361                         if(random() <= 0.7)
362                         {
363                                 Weapon wep = WEP_MAGE_SPIKE;
364                                 wep.wr_think(wep, false, true);
365                                 return true;
366                         }
367
368                         return false;
369                 }
370                 case MONSTER_ATTACK_RANGED:
371                 {
372                         if(!self.mage_spike)
373                         {
374                                 if(random() <= 0.4)
375                                 {
376                                         OffhandWeapon off = OFFHAND_MAGE_TELEPORT;
377                                         off.offhand_think(off, self, true);
378                                         return true;
379                                 }
380                                 else
381                                 {
382                                         setanim(self, self.anim_shoot, true, true, true);
383                                         self.attack_finished_single = time + (autocvar_g_monster_mage_attack_spike_delay);
384                                         self.anim_finished = time + 1;
385                                         Weapon wep = WEP_MAGE_SPIKE;
386                                         wep.wr_think(wep, true, false);
387                                         return true;
388                                 }
389                         }
390
391                         if(self.mage_spike)
392                                 return true;
393                         else
394                                 return false;
395                 }
396         }
397
398         return false;
399 }
400
401 void spawnfunc_monster_mage() { Monster_Spawn(MON_MAGE.monsterid); }
402
403 #endif // SVQC
404
405                 #ifdef SVQC
406                 METHOD(Mage, mr_think, bool(Monster thismon))
407                 {
408                         SELFPARAM();
409                         entity head;
410                         bool need_help = false;
411
412                         for(head = world; (head = findfloat(head, iscreature, true)); )
413                         if(head != self)
414                         if(vlen(head.origin - self.origin) <= (autocvar_g_monster_mage_heal_range))
415                         if(M_Mage_Defend_Heal_Check(head))
416                         {
417                                 need_help = true;
418                                 break;
419                         }
420
421                         if(self.health < (autocvar_g_monster_mage_heal_minhealth) || need_help)
422                         if(time >= self.attack_finished_single)
423                         if(random() < 0.5)
424                                 M_Mage_Defend_Heal();
425
426                         if(time >= self.mage_shield_time && self.armorvalue)
427                                 M_Mage_Defend_Shield_Remove();
428
429                         if(self.enemy)
430                         if(self.health < self.max_health)
431                         if(time >= self.mage_shield_delay)
432                         if(random() < 0.5)
433                                 M_Mage_Defend_Shield();
434
435                         return true;
436                 }
437                 METHOD(Mage, mr_pain, bool(Monster thismon))
438                 {
439                         return true;
440                 }
441                 METHOD(Mage, mr_death, bool(Monster thismon))
442                 {
443                         SELFPARAM();
444                         setanim(self, self.anim_die1, false, true, true);
445                         return true;
446                 }
447                 #endif
448                 #ifndef MENUQC
449                 METHOD(Mage, mr_anim, bool(Monster thismon))
450                 {
451                         SELFPARAM();
452                         vector none = '0 0 0';
453                         self.anim_die1 = animfixfps(self, '4 1 0.5', none); // 2 seconds
454                         self.anim_walk = animfixfps(self, '1 1 1', none);
455                         self.anim_idle = animfixfps(self, '0 1 1', none);
456                         self.anim_pain1 = animfixfps(self, '3 1 2', none); // 0.5 seconds
457                         self.anim_shoot = animfixfps(self, '2 1 5', none); // analyze models and set framerate
458                         self.anim_run = animfixfps(self, '5 1 1', none);
459
460                         return true;
461                 }
462                 #endif
463                 #ifdef SVQC
464                 METHOD(Mage, mr_setup, bool(Monster thismon))
465                 {
466                         SELFPARAM();
467                         if(!self.health) self.health = (autocvar_g_monster_mage_health);
468                         if(!self.speed) { self.speed = (autocvar_g_monster_mage_speed_walk); }
469                         if(!self.speed2) { self.speed2 = (autocvar_g_monster_mage_speed_run); }
470                         if(!self.stopspeed) { self.stopspeed = (autocvar_g_monster_mage_speed_stop); }
471                         if(!self.damageforcescale) { self.damageforcescale = (autocvar_g_monster_mage_damageforcescale); }
472
473                         self.monster_loot = spawnfunc_item_health_large;
474                         self.monster_attackfunc = M_Mage_Attack;
475
476                         return true;
477                 }
478                 METHOD(Mage, mr_precache, bool(Monster thismon))
479                 {
480                         return true;
481                 }
482                 #endif
483
484 #endif