Remove SELFPARAM() from .think and .touch
[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 == world)
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 = world;
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), world, world, 0, DEATH_MONSTER_MAGE.m_id, other);
159
160         remove (this);
161 }
162
163 void M_Mage_Attack_Spike_Touch(entity this)
164 {
165         PROJECTILE_TOUCH;
166
167         M_Mage_Attack_Spike_Explode(self);
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 != world)
188                 if (this.enemy.takedamage != DAMAGE_AIM || IS_DEAD(this.enemy))
189                         this.enemy = world;
190
191         if (this.enemy != world)
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         entity head;
253         float washealed = false;
254
255         for(head = findradius(this.origin, (autocvar_g_monster_mage_heal_range)); head; head = head.chain) if(M_Mage_Defend_Heal_Check(this, head))
256         {
257                 washealed = true;
258                 string fx = "";
259                 if(IS_PLAYER(head))
260                 {
261                         switch(this.skin)
262                         {
263                                 case 0:
264                                         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);
265                                         fx = EFFECT_HEALING.eent_eff_name;
266                                         break;
267                                 case 1:
268                                         if(head.ammo_cells) head.ammo_cells = bound(head.ammo_cells, head.ammo_cells + 1, g_pickup_cells_max);
269                                         if(head.ammo_plasma) head.ammo_plasma = bound(head.ammo_plasma, head.ammo_plasma + 1, g_pickup_plasma_max);
270                                         if(head.ammo_rockets) head.ammo_rockets = bound(head.ammo_rockets, head.ammo_rockets + 1, g_pickup_rockets_max);
271                                         if(head.ammo_shells) head.ammo_shells = bound(head.ammo_shells, head.ammo_shells + 2, g_pickup_shells_max);
272                                         if(head.ammo_nails) head.ammo_nails = bound(head.ammo_nails, head.ammo_nails + 5, g_pickup_nails_max);
273                                         fx = "ammoregen_fx";
274                                         break;
275                                 case 2:
276                                         if(head.armorvalue < autocvar_g_balance_armor_regenstable)
277                                         {
278                                                 head.armorvalue = bound(0, head.armorvalue + (autocvar_g_monster_mage_heal_allies), autocvar_g_balance_armor_regenstable);
279                                                 fx = "armorrepair_fx";
280                                         }
281                                         break;
282                                 case 3:
283                                         head.health = bound(0, head.health - ((head == this)  ? (autocvar_g_monster_mage_heal_self) : (autocvar_g_monster_mage_heal_allies)), autocvar_g_balance_health_regenstable);
284                                         fx = EFFECT_RAGE.eent_eff_name;
285                                         break;
286                         }
287
288                         Send_Effect_(fx, head.origin, '0 0 0', 1);
289                 }
290                 else
291                 {
292                         Send_Effect(EFFECT_HEALING, head.origin, '0 0 0', 1);
293                         head.health = bound(0, head.health + (autocvar_g_monster_mage_heal_allies), head.max_health);
294                         if(!(head.spawnflags & MONSTERFLAG_INVINCIBLE) && head.sprite)
295                                 WaypointSprite_UpdateHealth(head.sprite, head.health);
296                 }
297         }
298
299         if(washealed)
300         {
301                 setanim(this, this.anim_shoot, true, true, true);
302                 this.attack_finished_single[0] = time + (autocvar_g_monster_mage_heal_delay);
303                 this.anim_finished = time + 1.5;
304         }
305 }
306
307 void M_Mage_Attack_Push(entity this)
308 {
309         sound(this, CH_SHOTS, SND_TAGEXP1, 1, ATTEN_NORM);
310         RadiusDamage (this, this, (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.m_id, this.enemy);
311         Send_Effect(EFFECT_TE_EXPLOSION, this.origin, '0 0 0', 1);
312
313         setanim(this, this.anim_shoot, true, true, true);
314         this.attack_finished_single[0] = time + (autocvar_g_monster_mage_attack_push_delay);
315 }
316
317 void M_Mage_Attack_Teleport(entity this, entity targ)
318 {
319         if(!targ) return;
320         if(vdist(targ.origin - this.origin, >, 1500)) return;
321
322         makevectors(targ.angles);
323         tracebox(targ.origin + ((v_forward * -1) * 200), this.mins, this.maxs, this.origin, MOVE_NOMONSTERS, this);
324
325         if(trace_fraction < 1)
326                 return;
327
328         vector newpos = targ.origin + ((v_forward * -1) * 200);
329
330         Send_Effect(EFFECT_SPAWN_NEUTRAL, this.origin, '0 0 0', 1);
331         Send_Effect(EFFECT_SPAWN_NEUTRAL, newpos, '0 0 0', 1);
332
333         setorigin(this, newpos);
334
335         vector a = vectoangles(targ.origin - this.origin);
336         a.x = -a.x;
337         this.angles_x = a.x;
338         this.angles_y = a.y;
339         this.fixangle = true;
340         this.velocity *= 0.5;
341
342         this.attack_finished_single[0] = time + 0.2;
343 }
344
345 void M_Mage_Defend_Shield_Remove(entity this)
346 {
347         this.effects &= ~(EF_ADDITIVE | EF_BLUE);
348         this.armorvalue = autocvar_g_monsters_armor_blockpercent;
349 }
350
351 void M_Mage_Defend_Shield(entity this)
352 {
353         this.effects |= (EF_ADDITIVE | EF_BLUE);
354         this.mage_shield_delay = time + (autocvar_g_monster_mage_shield_delay);
355         this.armorvalue = (autocvar_g_monster_mage_shield_blockpercent);
356         this.mage_shield_time = time + (autocvar_g_monster_mage_shield_time);
357         setanim(this, this.anim_shoot, true, true, true);
358         this.attack_finished_single[0] = time + 1;
359         this.anim_finished = time + 1;
360 }
361
362 bool M_Mage_Attack(int attack_type, entity actor, entity targ)
363 {
364     .entity weaponentity = weaponentities[0];
365         switch(attack_type)
366         {
367                 case MONSTER_ATTACK_MELEE:
368                 {
369                         if(random() <= 0.7)
370                         {
371                                 Weapon wep = WEP_MAGE_SPIKE;
372
373                                 wep.wr_think(wep, actor, weaponentity, 2);
374                                 return true;
375                         }
376
377                         return false;
378                 }
379                 case MONSTER_ATTACK_RANGED:
380                 {
381                         if(!actor.mage_spike)
382                         {
383                                 if(random() <= 0.4)
384                                 {
385                                         OffhandWeapon off = OFFHAND_MAGE_TELEPORT;
386                                         off.offhand_think(off, actor, true);
387                                         return true;
388                                 }
389                                 else
390                                 {
391                                         setanim(actor, actor.anim_shoot, true, true, true);
392                                         actor.attack_finished_single[0] = time + (autocvar_g_monster_mage_attack_spike_delay);
393                                         actor.anim_finished = time + 1;
394                                         Weapon wep = WEP_MAGE_SPIKE;
395                                         wep.wr_think(wep, actor, weaponentity, 1);
396                                         return true;
397                                 }
398                         }
399
400                         if(actor.mage_spike)
401                                 return true;
402                         else
403                                 return false;
404                 }
405         }
406
407         return false;
408 }
409
410 spawnfunc(monster_mage) { Monster_Spawn(this, MON_MAGE.monsterid); }
411
412 #endif // SVQC
413
414 #ifdef SVQC
415 METHOD(Mage, mr_think, bool(Mage thismon, entity actor))
416 {
417     TC(Mage, thismon);
418     bool need_help = false;
419
420     FOREACH_ENTITY_FLOAT(iscreature, true,
421     {
422         if(it != actor)
423         if(vdist(it.origin - actor.origin, <=, autocvar_g_monster_mage_heal_range))
424         if(M_Mage_Defend_Heal_Check(actor, it))
425         {
426             need_help = true;
427             break;
428         }
429     });
430
431     if(actor.health < (autocvar_g_monster_mage_heal_minhealth) || need_help)
432     if(time >= actor.attack_finished_single[0])
433     if(random() < 0.5)
434         M_Mage_Defend_Heal(actor);
435
436     if(time >= actor.mage_shield_time && actor.armorvalue)
437         M_Mage_Defend_Shield_Remove(actor);
438
439     if(actor.enemy)
440     if(actor.health < actor.max_health)
441     if(time >= actor.mage_shield_delay)
442     if(random() < 0.5)
443         M_Mage_Defend_Shield(actor);
444
445     return true;
446 }
447
448 METHOD(Mage, mr_pain, bool(Mage this, entity actor))
449 {
450     TC(Mage, this);
451     return true;
452 }
453
454 METHOD(Mage, mr_death, bool(Mage this, entity actor))
455 {
456     TC(Mage, this);
457     setanim(actor, actor.anim_die1, false, true, true);
458     return true;
459 }
460
461 #endif
462 #ifndef MENUQC
463 METHOD(Mage, mr_anim, bool(Mage this, entity actor))
464 {
465     TC(Mage, this);
466     vector none = '0 0 0';
467     actor.anim_die1 = animfixfps(actor, '4 1 0.5', none); // 2 seconds
468     actor.anim_walk = animfixfps(actor, '1 1 1', none);
469     actor.anim_idle = animfixfps(actor, '0 1 1', none);
470     actor.anim_pain1 = animfixfps(actor, '3 1 2', none); // 0.5 seconds
471     actor.anim_shoot = animfixfps(actor, '2 1 5', none); // analyze models and set framerate
472     actor.anim_run = animfixfps(actor, '5 1 1', none);
473     return true;
474 }
475 #endif
476 #ifdef SVQC
477 .float speed;
478 spawnfunc(item_health_large);
479 METHOD(Mage, mr_setup, bool(Mage this, entity actor))
480 {
481     TC(Mage, this);
482     if(!actor.health) actor.health = (autocvar_g_monster_mage_health);
483     if(!actor.speed) { actor.speed = (autocvar_g_monster_mage_speed_walk); }
484     if(!actor.speed2) { actor.speed2 = (autocvar_g_monster_mage_speed_run); }
485     if(!actor.stopspeed) { actor.stopspeed = (autocvar_g_monster_mage_speed_stop); }
486     if(!actor.damageforcescale) { actor.damageforcescale = (autocvar_g_monster_mage_damageforcescale); }
487
488     actor.monster_loot = spawnfunc_item_health_large;
489     actor.monster_attackfunc = M_Mage_Attack;
490
491     return true;
492 }
493
494 METHOD(Mage, mr_precache, bool(Mage this))
495 {
496     TC(Mage, this);
497     return true;
498 }
499 #endif
500
501 #endif