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