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