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