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