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