Merge branch 'terencehill/hide_motd' into 'master'
[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     this.mr_precache(this);
22 #endif
23 }
24
25 #include "../../weapons/all.qh"
26 #include "../../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 void M_Mage_Attack_Spike(vector dir);
43 void M_Mage_Attack_Push();
44 METHOD(MageSpike, wr_think, void(MageSpike thiswep, entity actor, int slot, int fire)) {
45     if (fire & 1)
46     if (!IS_PLAYER(actor) || weapon_prepareattack(thiswep, actor, slot, false, 0.2)) {
47         if (!actor.target_range) actor.target_range = autocvar_g_monsters_target_range;
48         actor.enemy = Monster_FindTarget(actor);
49         W_SetupShot_Dir(actor, v_forward, false, 0, W_Sound("electro_fire"), CH_WEAPON_B, 0);
50         if (!IS_PLAYER(actor)) w_shotdir = normalize((actor.enemy.origin + '0 0 10') - actor.origin);
51         M_Mage_Attack_Spike(w_shotdir);
52         weapon_thinkf(actor, slot, WFRAME_FIRE1, 0, w_ready);
53     }
54     if (fire & 2)
55     if (!IS_PLAYER(actor) || weapon_prepareattack(thiswep, actor, slot, true, 0.5)) {
56         M_Mage_Attack_Push();
57         weapon_thinkf(actor, slot, 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.m_id, 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 .float wait;
167
168 // copied from W_Seeker_Think
169 void M_Mage_Attack_Spike_Think()
170 {SELFPARAM();
171         if (time > self.ltime || (self.enemy && self.enemy.health <= 0) || self.owner.health <= 0) {
172                 self.projectiledeathtype |= HITTYPE_SPLASH;
173                 M_Mage_Attack_Spike_Explode();
174         }
175
176         float spd = vlen(self.velocity);
177         spd = bound(
178                 spd - (autocvar_g_monster_mage_attack_spike_decel) * frametime,
179                 (autocvar_g_monster_mage_attack_spike_speed_max),
180                 spd + (autocvar_g_monster_mage_attack_spike_accel) * frametime
181         );
182
183         if (self.enemy != world)
184                 if (self.enemy.takedamage != DAMAGE_AIM || self.enemy.deadflag != DEAD_NO)
185                         self.enemy = world;
186
187         if (self.enemy != world)
188         {
189                 entity e = self.enemy;
190                 vector eorg = 0.5 * (e.absmin + e.absmax);
191                 float turnrate = (autocvar_g_monster_mage_attack_spike_turnrate); // how fast to turn
192                 vector desireddir = normalize(eorg - self.origin);
193                 vector olddir = normalize(self.velocity); // get my current direction
194                 float dist = vlen(eorg - self.origin);
195
196                 // Do evasive maneuvers for world objects? ( this should be a cpu hog. :P )
197                 if ((autocvar_g_monster_mage_attack_spike_smart) && (dist > (autocvar_g_monster_mage_attack_spike_smart_mindist)))
198                 {
199                         // Is it a better idea (shorter distance) to trace to the target itself?
200                         if ( vlen(self.origin + olddir * self.wait) < dist)
201                                 traceline(self.origin, self.origin + olddir * self.wait, false, self);
202                         else
203                                 traceline(self.origin, eorg, false, self);
204
205                         // Setup adaptive tracelength
206                         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));
207
208                         // Calc how important it is that we turn and add this to the desierd (enemy) dir.
209                         desireddir = normalize(((trace_plane_normal * (1 - trace_fraction)) + (desireddir * trace_fraction)) * 0.5);
210                 }
211
212                 vector newdir = normalize(olddir + desireddir * turnrate); // take the average of the 2 directions; not the best method but simple & easy
213                 self.velocity = newdir * spd; // make me fly in the new direction at my flight speed
214         }
215
216         ///////////////
217
218         //self.angles = vectoangles(self.velocity);                     // turn model in the new flight direction
219         self.nextthink = time;// + 0.05; // csqc projectiles
220         UpdateCSQCProjectile(self);
221 }
222
223 void M_Mage_Attack_Spike(vector dir)
224 {
225         SELFPARAM();
226         makevectors(self.angles);
227
228         entity missile = spawn();
229         missile.owner = missile.realowner = self;
230         missile.think = M_Mage_Attack_Spike_Think;
231         missile.ltime = time + 7;
232         missile.nextthink = time;
233         missile.solid = SOLID_BBOX;
234         missile.movetype = MOVETYPE_FLYMISSILE;
235         missile.flags = FL_PROJECTILE;
236         setorigin(missile, self.origin + v_forward * 14 + '0 0 30' + v_right * -14);
237         setsize(missile, '0 0 0', '0 0 0');
238         missile.velocity = dir * 400;
239         missile.avelocity = '300 300 300';
240         missile.enemy = self.enemy;
241         missile.touch = M_Mage_Attack_Spike_Touch;
242
243         self.mage_spike = missile;
244
245         CSQCProjectile(missile, true, PROJECTILE_MAGE_SPIKE, true);
246 }
247
248 void M_Mage_Defend_Heal()
249 {SELFPARAM();
250         entity head;
251         float washealed = false;
252
253         for(head = findradius(self.origin, (autocvar_g_monster_mage_heal_range)); head; head = head.chain) if(M_Mage_Defend_Heal_Check(head))
254         {
255                 washealed = true;
256                 string fx = "";
257                 if(IS_PLAYER(head))
258                 {
259                         switch(self.skin)
260                         {
261                                 case 0:
262                                         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);
263                                         fx = EFFECT_HEALING.eent_eff_name;
264                                         break;
265                                 case 1:
266                                         if(head.ammo_cells) head.ammo_cells = bound(head.ammo_cells, head.ammo_cells + 1, g_pickup_cells_max);
267                                         if(head.ammo_plasma) head.ammo_plasma = bound(head.ammo_plasma, head.ammo_plasma + 1, g_pickup_plasma_max);
268                                         if(head.ammo_rockets) head.ammo_rockets = bound(head.ammo_rockets, head.ammo_rockets + 1, g_pickup_rockets_max);
269                                         if(head.ammo_shells) head.ammo_shells = bound(head.ammo_shells, head.ammo_shells + 2, g_pickup_shells_max);
270                                         if(head.ammo_nails) head.ammo_nails = bound(head.ammo_nails, head.ammo_nails + 5, g_pickup_nails_max);
271                                         fx = "ammoregen_fx";
272                                         break;
273                                 case 2:
274                                         if(head.armorvalue < autocvar_g_balance_armor_regenstable)
275                                         {
276                                                 head.armorvalue = bound(0, head.armorvalue + (autocvar_g_monster_mage_heal_allies), autocvar_g_balance_armor_regenstable);
277                                                 fx = "armorrepair_fx";
278                                         }
279                                         break;
280                                 case 3:
281                                         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);
282                                         fx = EFFECT_RAGE.eent_eff_name;
283                                         break;
284                         }
285
286                         Send_Effect_(fx, head.origin, '0 0 0', 1);
287                 }
288                 else
289                 {
290                         Send_Effect(EFFECT_HEALING, head.origin, '0 0 0', 1);
291                         head.health = bound(0, head.health + (autocvar_g_monster_mage_heal_allies), head.max_health);
292                         if(!(head.spawnflags & MONSTERFLAG_INVINCIBLE) && head.sprite)
293                                 WaypointSprite_UpdateHealth(head.sprite, head.health);
294                 }
295         }
296
297         if(washealed)
298         {
299                 setanim(self, self.anim_shoot, true, true, true);
300                 self.attack_finished_single[0] = time + (autocvar_g_monster_mage_heal_delay);
301                 self.anim_finished = time + 1.5;
302         }
303 }
304
305 void M_Mage_Attack_Push()
306 {SELFPARAM();
307         sound(self, CH_SHOTS, SND_TAGEXP1, 1, ATTEN_NORM);
308         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.m_id, self.enemy);
309         Send_Effect(EFFECT_TE_EXPLOSION, self.origin, '0 0 0', 1);
310
311         setanim(self, self.anim_shoot, true, true, true);
312         self.attack_finished_single[0] = time + (autocvar_g_monster_mage_attack_push_delay);
313 }
314
315 void M_Mage_Attack_Teleport()
316 {SELFPARAM();
317         entity targ = self.enemy;
318         if (!targ) return;
319         if (vlen(targ.origin - self.origin) > 1500) return;
320
321         makevectors(targ.angles);
322         tracebox(targ.origin + ((v_forward * -1) * 200), self.mins, self.maxs, self.origin, MOVE_NOMONSTERS, self);
323
324         if(trace_fraction < 1)
325                 return;
326
327         Send_Effect(EFFECT_SPAWN_NEUTRAL, self.origin, '0 0 0', 1);
328         setorigin(self, targ.origin + ((v_forward * -1) * 200));
329
330         vector a = vectoangles(targ.origin - self.origin);
331         a.x = -a.x;
332         self.angles_x = a.x;
333         self.angles_y = a.y;
334         self.fixangle = true;
335         self.velocity *= 0.5;
336
337         self.attack_finished_single[0] = time + 0.2;
338 }
339
340 void M_Mage_Defend_Shield_Remove()
341 {SELFPARAM();
342         self.effects &= ~(EF_ADDITIVE | EF_BLUE);
343         self.armorvalue = autocvar_g_monsters_armor_blockpercent;
344 }
345
346 void M_Mage_Defend_Shield()
347 {SELFPARAM();
348         self.effects |= (EF_ADDITIVE | EF_BLUE);
349         self.mage_shield_delay = time + (autocvar_g_monster_mage_shield_delay);
350         self.armorvalue = (autocvar_g_monster_mage_shield_blockpercent);
351         self.mage_shield_time = time + (autocvar_g_monster_mage_shield_time);
352         setanim(self, self.anim_shoot, true, true, true);
353         self.attack_finished_single[0] = time + 1;
354         self.anim_finished = time + 1;
355 }
356
357 float M_Mage_Attack(float attack_type, entity targ)
358 {SELFPARAM();
359         switch(attack_type)
360         {
361                 case MONSTER_ATTACK_MELEE:
362                 {
363                         if(random() <= 0.7)
364                         {
365                                 Weapon wep = WEP_MAGE_SPIKE;
366                                 wep.wr_think(wep, self, 0, 2);
367                                 return true;
368                         }
369
370                         return false;
371                 }
372                 case MONSTER_ATTACK_RANGED:
373                 {
374                         if(!self.mage_spike)
375                         {
376                                 if(random() <= 0.4)
377                                 {
378                                         OffhandWeapon off = OFFHAND_MAGE_TELEPORT;
379                                         off.offhand_think(off, self, true);
380                                         return true;
381                                 }
382                                 else
383                                 {
384                                         setanim(self, self.anim_shoot, true, true, true);
385                                         self.attack_finished_single[0] = time + (autocvar_g_monster_mage_attack_spike_delay);
386                                         self.anim_finished = time + 1;
387                                         Weapon wep = WEP_MAGE_SPIKE;
388                                         wep.wr_think(wep, self, 0, 1);
389                                         return true;
390                                 }
391                         }
392
393                         if(self.mage_spike)
394                                 return true;
395                         else
396                                 return false;
397                 }
398         }
399
400         return false;
401 }
402
403 spawnfunc(monster_mage) { Monster_Spawn(MON_MAGE.monsterid); }
404
405 #endif // SVQC
406
407                 #ifdef SVQC
408                 METHOD(Mage, mr_think, bool(Monster thismon))
409                 {
410                         SELFPARAM();
411                         entity head;
412                         bool need_help = false;
413
414                         for(head = world; (head = findfloat(head, iscreature, true)); )
415                         if(head != self)
416                         if(vlen(head.origin - self.origin) <= (autocvar_g_monster_mage_heal_range))
417                         if(M_Mage_Defend_Heal_Check(head))
418                         {
419                                 need_help = true;
420                                 break;
421                         }
422
423                         if(self.health < (autocvar_g_monster_mage_heal_minhealth) || need_help)
424                         if(time >= self.attack_finished_single[0])
425                         if(random() < 0.5)
426                                 M_Mage_Defend_Heal();
427
428                         if(time >= self.mage_shield_time && self.armorvalue)
429                                 M_Mage_Defend_Shield_Remove();
430
431                         if(self.enemy)
432                         if(self.health < self.max_health)
433                         if(time >= self.mage_shield_delay)
434                         if(random() < 0.5)
435                                 M_Mage_Defend_Shield();
436
437                         return true;
438                 }
439                 METHOD(Mage, mr_pain, bool(Monster thismon))
440                 {
441                         return true;
442                 }
443                 METHOD(Mage, mr_death, bool(Monster thismon))
444                 {
445                         SELFPARAM();
446                         setanim(self, self.anim_die1, false, true, true);
447                         return true;
448                 }
449                 #endif
450                 #ifndef MENUQC
451                 METHOD(Mage, mr_anim, bool(Monster thismon))
452                 {
453                         SELFPARAM();
454                         vector none = '0 0 0';
455                         self.anim_die1 = animfixfps(self, '4 1 0.5', none); // 2 seconds
456                         self.anim_walk = animfixfps(self, '1 1 1', none);
457                         self.anim_idle = animfixfps(self, '0 1 1', none);
458                         self.anim_pain1 = animfixfps(self, '3 1 2', none); // 0.5 seconds
459                         self.anim_shoot = animfixfps(self, '2 1 5', none); // analyze models and set framerate
460                         self.anim_run = animfixfps(self, '5 1 1', none);
461
462                         return true;
463                 }
464                 #endif
465                 #ifdef SVQC
466                 .float speed;
467                 spawnfunc(item_health_large);
468                 METHOD(Mage, mr_setup, bool(Monster thismon))
469                 {
470                         SELFPARAM();
471                         if(!self.health) self.health = (autocvar_g_monster_mage_health);
472                         if(!self.speed) { self.speed = (autocvar_g_monster_mage_speed_walk); }
473                         if(!self.speed2) { self.speed2 = (autocvar_g_monster_mage_speed_run); }
474                         if(!self.stopspeed) { self.stopspeed = (autocvar_g_monster_mage_speed_stop); }
475                         if(!self.damageforcescale) { self.damageforcescale = (autocvar_g_monster_mage_damageforcescale); }
476
477                         self.monster_loot = spawnfunc_item_health_large;
478                         self.monster_attackfunc = M_Mage_Attack;
479
480                         return true;
481                 }
482                 METHOD(Mage, mr_precache, bool(Monster thismon))
483                 {
484                         return true;
485                 }
486                 #endif
487
488 #endif