Merge branch 'terencehill/lms_itemtimes_fix' 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, 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     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(actor, 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(actor);
58         weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, 0, w_ready);
59     }
60 }
61
62 void M_Mage_Attack_Teleport(entity this, entity targ);
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                 M_Mage_Attack_Teleport(player, player.enemy);
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(entity this);
114 void M_Mage_Defend_Shield(entity this);
115
116 .entity mage_spike;
117 .float mage_shield_delay;
118 .float mage_shield_time;
119
120 bool M_Mage_Defend_Heal_Check(entity this, entity targ)
121 {
122         if(targ == world)
123                 return false;
124         if(targ.health <= 0)
125                 return false;
126         if(DIFF_TEAM(targ, this) && targ != this.monster_follow)
127                 return false;
128         if(STAT(FROZEN, targ))
129                 return false;
130         if(!IS_PLAYER(targ))
131                 return (IS_MONSTER(targ) && targ.health < targ.max_health);
132         if(targ.items & ITEM_Shield.m_itemid)
133                 return false;
134
135         switch(this.skin)
136         {
137                 case 0: return (targ.health < autocvar_g_balance_health_regenstable);
138                 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));
139                 case 2: return (targ.armorvalue < autocvar_g_balance_armor_regenstable);
140                 case 3: return (targ.health > 0);
141         }
142
143         return false;
144 }
145
146 void M_Mage_Attack_Spike_Explode(entity this)
147 {
148         this.event_damage = func_null;
149
150         sound(this, CH_SHOTS, SND_GRENADE_IMPACT, VOL_BASE, ATTEN_NORM);
151
152         this.realowner.mage_spike = world;
153
154         Send_Effect(EFFECT_EXPLOSION_SMALL, this.origin, '0 0 0', 1);
155         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);
156
157         remove (this);
158 }
159
160 void M_Mage_Attack_Spike_Touch()
161 {
162         PROJECTILE_TOUCH;
163
164         M_Mage_Attack_Spike_Explode(self);
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(self);
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
196                 // Do evasive maneuvers for world objects? ( this should be a cpu hog. :P )
197                 if ((autocvar_g_monster_mage_attack_spike_smart) && vdist(eorg - self.origin, >, 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 ( vlen2(self.origin + olddir * self.wait) < vlen2(eorg - self.origin))
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(entity this, vector dir)
224 {
225         makevectors(this.angles);
226
227         entity missile = spawn();
228         missile.owner = missile.realowner = this;
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, this.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 = this.enemy;
240         missile.touch = M_Mage_Attack_Spike_Touch;
241
242         this.mage_spike = missile;
243
244         CSQCProjectile(missile, true, PROJECTILE_MAGE_SPIKE, true);
245 }
246
247 void M_Mage_Defend_Heal(entity this)
248 {
249         entity head;
250         float washealed = false;
251
252         for(head = findradius(this.origin, (autocvar_g_monster_mage_heal_range)); head; head = head.chain) if(M_Mage_Defend_Heal_Check(this, head))
253         {
254                 washealed = true;
255                 string fx = "";
256                 if(IS_PLAYER(head))
257                 {
258                         switch(this.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 == this)  ? (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(this, this.anim_shoot, true, true, true);
299                 this.attack_finished_single[0] = time + (autocvar_g_monster_mage_heal_delay);
300                 this.anim_finished = time + 1.5;
301         }
302 }
303
304 void M_Mage_Attack_Push(entity this)
305 {
306         sound(this, CH_SHOTS, SND_TAGEXP1, 1, ATTEN_NORM);
307         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);
308         Send_Effect(EFFECT_TE_EXPLOSION, this.origin, '0 0 0', 1);
309
310         setanim(this, this.anim_shoot, true, true, true);
311         this.attack_finished_single[0] = time + (autocvar_g_monster_mage_attack_push_delay);
312 }
313
314 void M_Mage_Attack_Teleport(entity this, entity targ)
315 {
316         if(!targ) return;
317         if(vdist(targ.origin - this.origin, >, 1500)) return;
318
319         makevectors(targ.angles);
320         tracebox(targ.origin + ((v_forward * -1) * 200), this.mins, this.maxs, this.origin, MOVE_NOMONSTERS, this);
321
322         if(trace_fraction < 1)
323                 return;
324
325         vector newpos = targ.origin + ((v_forward * -1) * 200);
326
327         Send_Effect(EFFECT_SPAWN_NEUTRAL, this.origin, '0 0 0', 1);
328         Send_Effect(EFFECT_SPAWN_NEUTRAL, newpos, '0 0 0', 1);
329         
330         setorigin(this, newpos);
331
332         vector a = vectoangles(targ.origin - this.origin);
333         a.x = -a.x;
334         this.angles_x = a.x;
335         this.angles_y = a.y;
336         this.fixangle = true;
337         this.velocity *= 0.5;
338
339         this.attack_finished_single[0] = time + 0.2;
340 }
341
342 void M_Mage_Defend_Shield_Remove(entity this)
343 {
344         this.effects &= ~(EF_ADDITIVE | EF_BLUE);
345         this.armorvalue = autocvar_g_monsters_armor_blockpercent;
346 }
347
348 void M_Mage_Defend_Shield(entity this)
349 {
350         this.effects |= (EF_ADDITIVE | EF_BLUE);
351         this.mage_shield_delay = time + (autocvar_g_monster_mage_shield_delay);
352         this.armorvalue = (autocvar_g_monster_mage_shield_blockpercent);
353         this.mage_shield_time = time + (autocvar_g_monster_mage_shield_time);
354         setanim(this, this.anim_shoot, true, true, true);
355         this.attack_finished_single[0] = time + 1;
356         this.anim_finished = time + 1;
357 }
358
359 bool M_Mage_Attack(int attack_type, entity actor, entity targ)
360 {
361     .entity weaponentity = weaponentities[0];
362         switch(attack_type)
363         {
364                 case MONSTER_ATTACK_MELEE:
365                 {
366                         if(random() <= 0.7)
367                         {
368                                 Weapon wep = WEP_MAGE_SPIKE;
369
370                                 wep.wr_think(wep, actor, weaponentity, 2);
371                                 return true;
372                         }
373
374                         return false;
375                 }
376                 case MONSTER_ATTACK_RANGED:
377                 {
378                         if(!actor.mage_spike)
379                         {
380                                 if(random() <= 0.4)
381                                 {
382                                         OffhandWeapon off = OFFHAND_MAGE_TELEPORT;
383                                         off.offhand_think(off, actor, true);
384                                         return true;
385                                 }
386                                 else
387                                 {
388                                         setanim(actor, actor.anim_shoot, true, true, true);
389                                         actor.attack_finished_single[0] = time + (autocvar_g_monster_mage_attack_spike_delay);
390                                         actor.anim_finished = time + 1;
391                                         Weapon wep = WEP_MAGE_SPIKE;
392                                         wep.wr_think(wep, actor, weaponentity, 1);
393                                         return true;
394                                 }
395                         }
396
397                         if(actor.mage_spike)
398                                 return true;
399                         else
400                                 return false;
401                 }
402         }
403
404         return false;
405 }
406
407 spawnfunc(monster_mage) { Monster_Spawn(this, MON_MAGE.monsterid); }
408
409 #endif // SVQC
410
411                 #ifdef SVQC
412                 METHOD(Mage, mr_think, bool(Mage thismon, entity actor))
413                 {
414                         bool need_help = false;
415
416                         FOREACH_ENTITY_FLOAT(iscreature, true,
417                         {
418                                 if(it != actor)
419                                 if(vdist(it.origin - actor.origin, <=, autocvar_g_monster_mage_heal_range))
420                                 if(M_Mage_Defend_Heal_Check(actor, it))
421                                 {
422                                         need_help = true;
423                                         break;
424                                 }
425                         });
426
427                         if(actor.health < (autocvar_g_monster_mage_heal_minhealth) || need_help)
428                         if(time >= actor.attack_finished_single[0])
429                         if(random() < 0.5)
430                                 M_Mage_Defend_Heal(actor);
431
432                         if(time >= actor.mage_shield_time && actor.armorvalue)
433                                 M_Mage_Defend_Shield_Remove(actor);
434
435                         if(actor.enemy)
436                         if(actor.health < actor.max_health)
437                         if(time >= actor.mage_shield_delay)
438                         if(random() < 0.5)
439                                 M_Mage_Defend_Shield(actor);
440
441                         return true;
442                 }
443                 METHOD(Mage, mr_pain, bool(Mage thismon, entity actor))
444                 {
445                         return true;
446                 }
447                 METHOD(Mage, mr_death, bool(Mage thismon, entity actor))
448                 {
449                         setanim(actor, actor.anim_die1, false, true, true);
450                         return true;
451                 }
452                 #endif
453                 #ifndef MENUQC
454                 METHOD(Mage, mr_anim, bool(Mage thismon, entity actor))
455                 {
456                         vector none = '0 0 0';
457                         actor.anim_die1 = animfixfps(actor, '4 1 0.5', none); // 2 seconds
458                         actor.anim_walk = animfixfps(actor, '1 1 1', none);
459                         actor.anim_idle = animfixfps(actor, '0 1 1', none);
460                         actor.anim_pain1 = animfixfps(actor, '3 1 2', none); // 0.5 seconds
461                         actor.anim_shoot = animfixfps(actor, '2 1 5', none); // analyze models and set framerate
462                         actor.anim_run = animfixfps(actor, '5 1 1', none);
463                         return true;
464                 }
465                 #endif
466                 #ifdef SVQC
467                 .float speed;
468                 spawnfunc(item_health_large);
469                 METHOD(Mage, mr_setup, bool(Mage thismon, entity actor))
470                 {
471                         if(!actor.health) actor.health = (autocvar_g_monster_mage_health);
472                         if(!actor.speed) { actor.speed = (autocvar_g_monster_mage_speed_walk); }
473                         if(!actor.speed2) { actor.speed2 = (autocvar_g_monster_mage_speed_run); }
474                         if(!actor.stopspeed) { actor.stopspeed = (autocvar_g_monster_mage_speed_stop); }
475                         if(!actor.damageforcescale) { actor.damageforcescale = (autocvar_g_monster_mage_damageforcescale); }
476
477                         actor.monster_loot = spawnfunc_item_health_large;
478                         actor.monster_attackfunc = M_Mage_Attack;
479
480                         return true;
481                 }
482                 METHOD(Mage, mr_precache, bool(Mage thismon))
483                 {
484                         return true;
485                 }
486                 #endif
487
488 #endif