]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/monsters/monster/mage.qc
efdd836d21e8af7bedd7de2ebffe8555031e173a
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / monsters / monster / mage.qc
1 #include "mage.qh"
2
3 #ifdef SVQC
4
5 SOUND(MageSpike_FIRE, W_Sound("electro_fire"));
6 void M_Mage_Attack_Spike(entity this, vector dir);
7 void M_Mage_Attack_Push(entity this);
8 METHOD(MageSpike, wr_think, void(MageSpike thiswep, entity actor, .entity weaponentity, int fire))
9 {
10     TC(MageSpike, thiswep);
11     if (fire & 1)
12     if (!IS_PLAYER(actor) || weapon_prepareattack(thiswep, actor, weaponentity, false, 0.2)) {
13         if (!actor.target_range) actor.target_range = autocvar_g_monsters_target_range;
14         actor.enemy = Monster_FindTarget(actor);
15         W_SetupShot_Dir(actor, weaponentity, v_forward, false, 0, SND_MageSpike_FIRE, CH_WEAPON_B, 0, DEATH_MONSTER_MAGE.m_id);
16         if (!IS_PLAYER(actor)) w_shotdir = normalize((actor.enemy.origin + '0 0 10') - actor.origin);
17         M_Mage_Attack_Spike(actor, w_shotdir);
18         weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, 0, w_ready);
19     }
20     if (fire & 2)
21     if (!IS_PLAYER(actor) || weapon_prepareattack(thiswep, actor, weaponentity, true, 0.5)) {
22         M_Mage_Attack_Push(actor);
23         weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, 0, w_ready);
24     }
25 }
26
27 void M_Mage_Attack_Teleport(entity this, entity targ);
28
29 CLASS(OffhandMageTeleport, OffhandWeapon)
30     .bool OffhandMageTeleport_key_pressed;
31     METHOD(OffhandMageTeleport, offhand_think, void(OffhandMageTeleport this, entity player, bool key_pressed))
32     {
33         TC(OffhandMageTeleport, this);
34         if (key_pressed && !player.OffhandMageTeleport_key_pressed)
35                 M_Mage_Attack_Teleport(player, player.enemy);
36         player.OffhandMageTeleport_key_pressed = key_pressed;
37     }
38 ENDCLASS(OffhandMageTeleport)
39 OffhandMageTeleport OFFHAND_MAGE_TELEPORT;
40 STATIC_INIT(OFFHAND_MAGE_TELEPORT) { OFFHAND_MAGE_TELEPORT = NEW(OffhandMageTeleport); }
41
42 float autocvar_g_monster_mage_health;
43 float autocvar_g_monster_mage_damageforcescale = 0.5;
44 float autocvar_g_monster_mage_attack_spike_damage;
45 float autocvar_g_monster_mage_attack_spike_radius;
46 float autocvar_g_monster_mage_attack_spike_delay;
47 float autocvar_g_monster_mage_attack_spike_accel;
48 float autocvar_g_monster_mage_attack_spike_decel;
49 float autocvar_g_monster_mage_attack_spike_turnrate;
50 float autocvar_g_monster_mage_attack_spike_speed_max;
51 float autocvar_g_monster_mage_attack_spike_smart;
52 float autocvar_g_monster_mage_attack_spike_smart_trace_min;
53 float autocvar_g_monster_mage_attack_spike_smart_trace_max;
54 float autocvar_g_monster_mage_attack_spike_smart_mindist;
55 float autocvar_g_monster_mage_attack_push_damage;
56 float autocvar_g_monster_mage_attack_push_radius;
57 float autocvar_g_monster_mage_attack_push_delay;
58 float autocvar_g_monster_mage_attack_push_force;
59 float autocvar_g_monster_mage_heal_self;
60 float autocvar_g_monster_mage_heal_allies;
61 float autocvar_g_monster_mage_heal_minhealth;
62 float autocvar_g_monster_mage_heal_range;
63 float autocvar_g_monster_mage_heal_delay;
64 float autocvar_g_monster_mage_shield_time;
65 float autocvar_g_monster_mage_shield_delay;
66 float autocvar_g_monster_mage_shield_blockpercent;
67 float autocvar_g_monster_mage_speed_stop;
68 float autocvar_g_monster_mage_speed_run;
69 float autocvar_g_monster_mage_speed_walk;
70
71 /*
72 const float mage_anim_idle              = 0;
73 const float mage_anim_walk              = 1;
74 const float mage_anim_attack    = 2;
75 const float mage_anim_pain              = 3;
76 const float mage_anim_death             = 4;
77 const float mage_anim_run               = 5;
78 */
79
80 void M_Mage_Defend_Heal(entity this);
81 void M_Mage_Defend_Shield(entity this);
82
83 .entity mage_spike;
84 .float mage_shield_delay;
85 .float mage_shield_time;
86
87 bool M_Mage_Defend_Heal_Check(entity this, entity targ)
88 {
89         if(targ == NULL)
90                 return false;
91         if(GetResource(targ, RES_HEALTH) <= 0)
92                 return false;
93         if(DIFF_TEAM(targ, this) && targ != this.monster_follow)
94                 return false;
95         if(STAT(FROZEN, targ))
96                 return false;
97         if(!IS_PLAYER(targ))
98                 return (IS_MONSTER(targ) && GetResource(targ, RES_HEALTH) < targ.max_health);
99         if(targ.items & ITEM_Shield.m_itemid)
100                 return false;
101
102         switch(this.skin)
103         {
104                 case 0: return (GetResource(targ, RES_HEALTH) < autocvar_g_balance_health_regenstable);
105                 case 1:
106                 {
107                         return ((GetResource(targ, RES_CELLS) && GetResource(targ, RES_CELLS) < g_pickup_cells_max)
108                                 ||  (GetResource(targ, RES_PLASMA) && GetResource(targ, RES_PLASMA) < g_pickup_plasma_max)
109                                 ||  (GetResource(targ, RES_ROCKETS) && GetResource(targ, RES_ROCKETS) < g_pickup_rockets_max)
110                                 ||  (GetResource(targ, RES_BULLETS) && GetResource(targ, RES_BULLETS) < g_pickup_nails_max)
111                                 ||  (GetResource(targ, RES_SHELLS) && GetResource(targ, RES_SHELLS) < g_pickup_shells_max)
112                                         );
113                 }
114                 case 2: return (GetResource(targ, RES_ARMOR) < autocvar_g_balance_armor_regenstable);
115                 case 3: return (GetResource(targ, RES_HEALTH) > 0);
116         }
117
118         return false;
119 }
120
121 void M_Mage_Attack_Spike_Explode(entity this, entity directhitentity)
122 {
123         this.event_damage = func_null;
124
125         sound(this, CH_SHOTS, SND_GRENADE_IMPACT, VOL_BASE, ATTEN_NORM);
126
127         this.realowner.mage_spike = NULL;
128
129         Send_Effect(EFFECT_EXPLOSION_SMALL, this.origin, '0 0 0', 1);
130         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),
131                                                 NULL, NULL, 0, DEATH_MONSTER_MAGE.m_id, DMG_NOWEP, directhitentity);
132
133         delete(this);
134 }
135
136 void M_Mage_Attack_Spike_Touch(entity this, entity toucher)
137 {
138         PROJECTILE_TOUCH(this, toucher);
139
140         M_Mage_Attack_Spike_Explode(this, toucher);
141 }
142
143 .float wait;
144
145 // copied from W_Seeker_Think
146 void M_Mage_Attack_Spike_Think(entity this)
147 {
148         if (time > this.ltime || (this.enemy && GetResource(this.enemy, RES_HEALTH) <= 0) || GetResource(this.owner, RES_HEALTH) <= 0) {
149                 this.projectiledeathtype |= HITTYPE_SPLASH;
150                 M_Mage_Attack_Spike_Explode(this, NULL);
151         }
152
153         float spd = vlen(this.velocity);
154         spd = bound(
155                 spd - (autocvar_g_monster_mage_attack_spike_decel) * frametime,
156                 (autocvar_g_monster_mage_attack_spike_speed_max),
157                 spd + (autocvar_g_monster_mage_attack_spike_accel) * frametime
158         );
159
160         if (this.enemy != NULL)
161                 if (this.enemy.takedamage != DAMAGE_AIM || IS_DEAD(this.enemy))
162                         this.enemy = NULL;
163
164         if (this.enemy != NULL)
165         {
166                 entity e = this.enemy;
167                 vector eorg = 0.5 * (e.absmin + e.absmax);
168                 float turnrate = (autocvar_g_monster_mage_attack_spike_turnrate); // how fast to turn
169                 vector desireddir = normalize(eorg - this.origin);
170                 vector olddir = normalize(this.velocity); // get my current direction
171
172                 // Do evasive maneuvers for world objects? ( this should be a cpu hog. :P )
173                 if ((autocvar_g_monster_mage_attack_spike_smart) && vdist(eorg - this.origin, >, autocvar_g_monster_mage_attack_spike_smart_mindist))
174                 {
175                         // Is it a better idea (shorter distance) to trace to the target itself?
176                         if ( vlen2(this.origin + olddir * this.wait) < vlen2(eorg - this.origin))
177                                 traceline(this.origin, this.origin + olddir * this.wait, false, this);
178                         else
179                                 traceline(this.origin, eorg, false, this);
180
181                         // Setup adaptive tracelength
182                         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));
183
184                         // Calc how important it is that we turn and add this to the desierd (enemy) dir.
185                         desireddir = normalize(((trace_plane_normal * (1 - trace_fraction)) + (desireddir * trace_fraction)) * 0.5);
186                 }
187
188                 vector newdir = normalize(olddir + desireddir * turnrate); // take the average of the 2 directions; not the best method but simple & easy
189                 this.velocity = newdir * spd; // make me fly in the new direction at my flight speed
190         }
191
192         ///////////////
193
194         //this.angles = vectoangles(this.velocity);                     // turn model in the new flight direction
195         this.nextthink = time;// + 0.05; // csqc projectiles
196         UpdateCSQCProjectile(this);
197 }
198
199 void M_Mage_Attack_Spike(entity this, vector dir)
200 {
201         makevectors(this.angles);
202
203         entity missile = spawn();
204         missile.owner = missile.realowner = this;
205         setthink(missile, M_Mage_Attack_Spike_Think);
206         missile.ltime = time + 7;
207         missile.nextthink = time;
208         missile.solid = SOLID_BBOX;
209         set_movetype(missile, MOVETYPE_FLYMISSILE);
210         missile.flags = FL_PROJECTILE;
211         IL_PUSH(g_projectiles, missile);
212         IL_PUSH(g_bot_dodge, missile);
213         setorigin(missile, this.origin + v_forward * 14 + '0 0 30' + v_right * -14);
214         setsize(missile, '0 0 0', '0 0 0');
215         missile.velocity = dir * 400;
216         missile.avelocity = '300 300 300';
217         missile.enemy = this.enemy;
218         settouch(missile, M_Mage_Attack_Spike_Touch);
219
220         this.mage_spike = missile;
221
222         CSQCProjectile(missile, true, PROJECTILE_MAGE_SPIKE, true);
223 }
224
225 void M_Mage_Defend_Heal(entity this)
226 {
227         float washealed = false;
228
229         FOREACH_ENTITY_RADIUS(this.origin, autocvar_g_monster_mage_heal_range, M_Mage_Defend_Heal_Check(this, it),
230         {
231                 washealed = true;
232                 entity fx = EFFECT_Null;
233                 if(IS_PLAYER(it))
234                 {
235                         switch(this.skin)
236                         {
237                                 case 0:
238                                 {
239                                         Heal(it, this, autocvar_g_monster_mage_heal_allies, autocvar_g_balance_health_regenstable);
240                                         fx = EFFECT_HEALING;
241                                         break;
242                                 }
243                                 case 1:
244                                 {
245                                         if(GetResource(this, RES_CELLS)) GiveResourceWithLimit(it, RES_CELLS, 1, g_pickup_cells_max);
246                                         if(GetResource(this, RES_PLASMA)) GiveResourceWithLimit(it, RES_PLASMA, 1, g_pickup_plasma_max);
247                                         if(GetResource(this, RES_ROCKETS)) GiveResourceWithLimit(it, RES_ROCKETS, 1, g_pickup_rockets_max);
248                                         if(GetResource(this, RES_SHELLS)) GiveResourceWithLimit(it, RES_SHELLS, 2, g_pickup_shells_max);
249                                         if(GetResource(this, RES_BULLETS)) GiveResourceWithLimit(it, RES_BULLETS, 5, g_pickup_nails_max);
250                                         // TODO: fuel?
251                                         fx = EFFECT_AMMO_REGEN;
252                                         break;
253                                 }
254                                 case 2:
255                                         if(GetResource(it, RES_ARMOR) < autocvar_g_balance_armor_regenstable)
256                                         {
257                                                 GiveResourceWithLimit(it, RES_ARMOR, autocvar_g_monster_mage_heal_allies, autocvar_g_balance_armor_regenstable);
258                                                 fx = EFFECT_ARMOR_REPAIR;
259                                         }
260                                         break;
261                                 case 3:
262                                         float hp = ((it == this) ? autocvar_g_monster_mage_heal_self : autocvar_g_monster_mage_heal_allies);
263                                         TakeResource(it, RES_HEALTH, hp); // TODO: use regular damage functions? needs a way to bypass friendly fire checks
264                                         fx = EFFECT_RAGE;
265                                         break;
266                         }
267
268                         Send_Effect(fx, it.origin, '0 0 0', 1);
269                 }
270                 else
271                 {
272                         Send_Effect(EFFECT_HEALING, it.origin, '0 0 0', 1);
273                         Heal(it, this, autocvar_g_monster_mage_heal_allies, RES_LIMIT_NONE);
274                         if(!(it.spawnflags & MONSTERFLAG_INVINCIBLE) && it.sprite)
275                                 WaypointSprite_UpdateHealth(it.sprite, GetResource(it, RES_HEALTH));
276                 }
277         });
278
279         if(washealed)
280         {
281                 setanim(this, this.anim_shoot, true, true, true);
282                 this.attack_finished_single[0] = time + (autocvar_g_monster_mage_heal_delay);
283                 this.anim_finished = time + 1.5;
284         }
285 }
286
287 void M_Mage_Attack_Push(entity this)
288 {
289         sound(this, CH_SHOTS, SND_TAGEXP1, 1, ATTEN_NORM);
290         RadiusDamage (this, this, (autocvar_g_monster_mage_attack_push_damage), (autocvar_g_monster_mage_attack_push_damage), (autocvar_g_monster_mage_attack_push_radius),
291                                                 NULL, NULL, (autocvar_g_monster_mage_attack_push_force), DEATH_MONSTER_MAGE.m_id, DMG_NOWEP, this.enemy);
292         Send_Effect(EFFECT_TE_EXPLOSION, this.origin, '0 0 0', 1);
293
294         setanim(this, this.anim_shoot, true, true, true);
295         this.attack_finished_single[0] = time + (autocvar_g_monster_mage_attack_push_delay);
296 }
297
298 void M_Mage_Attack_Teleport(entity this, entity targ)
299 {
300         if(!targ) return;
301         if(vdist(targ.origin - this.origin, >, 1500)) return;
302
303         makevectors(targ.angles);
304         tracebox(targ.origin + ((v_forward * -1) * 200), this.mins, this.maxs, this.origin, MOVE_NOMONSTERS, this);
305
306         if(trace_fraction < 1)
307                 return;
308
309         vector newpos = targ.origin + ((v_forward * -1) * 200);
310
311         Send_Effect(EFFECT_SPAWN_NEUTRAL, this.origin, '0 0 0', 1);
312         Send_Effect(EFFECT_SPAWN_NEUTRAL, newpos, '0 0 0', 1);
313
314         setorigin(this, newpos);
315
316         vector a = vectoangles(targ.origin - this.origin);
317         a.x = -a.x;
318         this.angles_x = a.x;
319         this.angles_y = a.y;
320         this.fixangle = true;
321         this.velocity *= 0.5;
322
323         this.attack_finished_single[0] = time + 0.2;
324 }
325
326 void M_Mage_Defend_Shield_Remove(entity this)
327 {
328         this.effects &= ~(EF_ADDITIVE | EF_BLUE);
329         SetResourceExplicit(this, RES_ARMOR, autocvar_g_monsters_armor_blockpercent);
330 }
331
332 void M_Mage_Defend_Shield(entity this)
333 {
334         this.effects |= (EF_ADDITIVE | EF_BLUE);
335         this.mage_shield_delay = time + (autocvar_g_monster_mage_shield_delay);
336         SetResourceExplicit(this, RES_ARMOR, autocvar_g_monster_mage_shield_blockpercent);
337         this.mage_shield_time = time + (autocvar_g_monster_mage_shield_time);
338         setanim(this, this.anim_shoot, true, true, true);
339         this.attack_finished_single[0] = time + 1;
340         this.anim_finished = time + 1;
341 }
342
343 bool M_Mage_Attack(int attack_type, entity actor, entity targ, .entity weaponentity)
344 {
345         switch(attack_type)
346         {
347                 case MONSTER_ATTACK_MELEE:
348                 {
349                         if(random() <= 0.7)
350                         {
351                                 Weapon wep = WEP_MAGE_SPIKE;
352
353                                 wep.wr_think(wep, actor, weaponentity, 2);
354                                 return true;
355                         }
356
357                         return false;
358                 }
359                 case MONSTER_ATTACK_RANGED:
360                 {
361                         if(!actor.mage_spike)
362                         {
363                                 if(random() <= 0.4)
364                                 {
365                                         OffhandWeapon off = OFFHAND_MAGE_TELEPORT;
366                                         off.offhand_think(off, actor, true);
367                                         return true;
368                                 }
369                                 else
370                                 {
371                                         setanim(actor, actor.anim_shoot, true, true, true);
372                                         actor.attack_finished_single[0] = time + (autocvar_g_monster_mage_attack_spike_delay);
373                                         actor.anim_finished = time + 1;
374                                         Weapon wep = WEP_MAGE_SPIKE;
375                                         wep.wr_think(wep, actor, weaponentity, 1);
376                                         return true;
377                                 }
378                         }
379
380                         if(actor.mage_spike)
381                                 return true;
382                         else
383                                 return false;
384                 }
385         }
386
387         return false;
388 }
389
390 spawnfunc(monster_mage) { Monster_Spawn(this, true, MON_MAGE.monsterid); }
391
392 #endif // SVQC
393
394 #ifdef SVQC
395 METHOD(Mage, mr_think, bool(Mage thismon, entity actor))
396 {
397     TC(Mage, thismon);
398     bool need_help = false;
399
400     FOREACH_CLIENT(IS_PLAYER(it) && it != actor,
401     {
402         if(vdist(it.origin - actor.origin, <=, autocvar_g_monster_mage_heal_range))
403         if(M_Mage_Defend_Heal_Check(actor, it))
404         {
405             need_help = true;
406             break;
407         }
408     });
409
410     if(!need_help)
411     {
412         IL_EACH(g_monsters, it != actor,
413         {
414                 if(vdist(it.origin - actor.origin, <=, autocvar_g_monster_mage_heal_range))
415                 if(M_Mage_Defend_Heal_Check(actor, it))
416                 {
417                     need_help = true;
418                     break;
419                 }
420         });
421     }
422
423     if(GetResource(actor, RES_HEALTH) < (autocvar_g_monster_mage_heal_minhealth) || need_help)
424     if(time >= actor.attack_finished_single[0])
425     if(random() < 0.5)
426         M_Mage_Defend_Heal(actor);
427
428     if(time >= actor.mage_shield_time && GetResource(actor, RES_ARMOR))
429         M_Mage_Defend_Shield_Remove(actor);
430
431     if(actor.enemy)
432     if(GetResource(actor, RES_HEALTH) < actor.max_health)
433     if(time >= actor.mage_shield_delay)
434     if(random() < 0.5)
435         M_Mage_Defend_Shield(actor);
436
437     return true;
438 }
439
440 METHOD(Mage, mr_pain, float(Mage this, entity actor, float damage_take, entity attacker, float deathtype))
441 {
442     TC(Mage, this);
443     return damage_take;
444 }
445
446 METHOD(Mage, mr_death, bool(Mage this, entity actor))
447 {
448     TC(Mage, this);
449     setanim(actor, actor.anim_die1, false, true, true);
450     return true;
451 }
452
453 #endif
454 #ifdef GAMEQC
455 METHOD(Mage, mr_anim, bool(Mage this, entity actor))
456 {
457     TC(Mage, this);
458     vector none = '0 0 0';
459     actor.anim_die1 = animfixfps(actor, '4 1 0.5', none); // 2 seconds
460     actor.anim_walk = animfixfps(actor, '1 1 1', none);
461     actor.anim_idle = animfixfps(actor, '0 1 1', none);
462     actor.anim_pain1 = animfixfps(actor, '3 1 2', none); // 0.5 seconds
463     actor.anim_shoot = animfixfps(actor, '2 1 5', none); // analyze models and set framerate
464     actor.anim_run = animfixfps(actor, '5 1 1', none);
465     return true;
466 }
467 #endif
468 #ifdef SVQC
469 .float speed;
470 METHOD(Mage, mr_setup, bool(Mage this, entity actor))
471 {
472     TC(Mage, this);
473     if(!GetResource(this, RES_HEALTH)) SetResourceExplicit(actor, RES_HEALTH, autocvar_g_monster_mage_health);
474     if(!actor.speed) { actor.speed = (autocvar_g_monster_mage_speed_walk); }
475     if(!actor.speed2) { actor.speed2 = (autocvar_g_monster_mage_speed_run); }
476     if(!actor.stopspeed) { actor.stopspeed = (autocvar_g_monster_mage_speed_stop); }
477     if(!actor.damageforcescale) { actor.damageforcescale = (autocvar_g_monster_mage_damageforcescale); }
478
479     actor.monster_loot = ITEM_HealthBig;
480     actor.monster_attackfunc = M_Mage_Attack;
481
482     return true;
483 }
484 #endif