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