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