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