]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/turrets/turret/ewheel.qc
Merge branch 'Mario/intrusive_2' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / turrets / turret / ewheel.qc
1 #include "ewheel.qh"
2
3 #ifdef IMPLEMENTATION
4
5 #ifdef SVQC
6
7 float autocvar_g_turrets_unit_ewheel_speed_fast;
8 float autocvar_g_turrets_unit_ewheel_speed_slow;
9 float autocvar_g_turrets_unit_ewheel_speed_slower;
10 float autocvar_g_turrets_unit_ewheel_speed_stop;
11 float autocvar_g_turrets_unit_ewheel_turnrate;
12
13 const float ewheel_anim_stop = 0;
14 const float ewheel_anim_fwd_slow = 1;
15 const float ewheel_anim_fwd_fast = 2;
16 const float ewheel_anim_bck_slow = 3;
17 const float ewheel_anim_bck_fast = 4;
18
19 void ewheel_move_path(entity this)
20 {
21 #ifdef EWHEEL_FANCYPATH
22     // Are we close enougth to a path node to switch to the next?
23     if(vdist(this.origin - this.pathcurrent.origin, <, 64))
24         if (this.pathcurrent.path_next == NULL)
25         {
26             // Path endpoint reached
27             pathlib_deletepath(this.pathcurrent.owner);
28             this.pathcurrent = NULL;
29
30             if (this.pathgoal)
31             {
32                 if (this.pathgoal.use)
33                     this.pathgoal.use(this.pathgoal, NULL, NULL);
34
35                 if (this.pathgoal.enemy)
36                 {
37                     this.pathcurrent = pathlib_astar(this, this.pathgoal.origin,this.pathgoal.enemy.origin);
38                     this.pathgoal = this.pathgoal.enemy;
39                 }
40             }
41             else
42                 this.pathgoal = NULL;
43         }
44         else
45             this.pathcurrent = this.pathcurrent.path_next;
46
47 #else
48     if(vdist(this.origin - this.pathcurrent.origin, <, 64))
49         this.pathcurrent = this.pathcurrent.enemy;
50 #endif
51
52     if (this.pathcurrent)
53     {
54
55         this.moveto = this.pathcurrent.origin;
56         this.steerto = steerlib_attract2(this, this.moveto, 0.5, 500, 0.95);
57
58         movelib_move_simple(this, v_forward, (autocvar_g_turrets_unit_ewheel_speed_fast), 0.4);
59     }
60 }
61
62 void ewheel_move_enemy(entity this)
63 {
64     float newframe;
65
66     this.steerto = steerlib_arrive(this, this.enemy.origin,this.target_range_optimal);
67
68     this.moveto  = this.origin + this.steerto * 128;
69
70     if (this.tur_dist_enemy > this.target_range_optimal)
71     {
72         if ( this.tur_head.spawnshieldtime < 1 )
73         {
74             newframe = ewheel_anim_fwd_fast;
75             movelib_move_simple(this, v_forward, (autocvar_g_turrets_unit_ewheel_speed_fast), 0.4);
76         }
77         else if (this.tur_head.spawnshieldtime < 2)
78         {
79
80             newframe = ewheel_anim_fwd_slow;
81             movelib_move_simple(this, v_forward, (autocvar_g_turrets_unit_ewheel_speed_slow), 0.4);
82        }
83         else
84         {
85             newframe = ewheel_anim_fwd_slow;
86             movelib_move_simple(this, v_forward, (autocvar_g_turrets_unit_ewheel_speed_slower), 0.4);
87         }
88     }
89     else if (this.tur_dist_enemy < this.target_range_optimal * 0.5)
90     {
91         newframe = ewheel_anim_bck_slow;
92         movelib_move_simple(this, v_forward * -1, (autocvar_g_turrets_unit_ewheel_speed_slow), 0.4);
93     }
94     else
95     {
96         newframe = ewheel_anim_stop;
97         movelib_brake_simple(this, (autocvar_g_turrets_unit_ewheel_speed_stop));
98     }
99
100     turrets_setframe(this, newframe, false);
101 }
102
103 void ewheel_move_idle(entity this)
104 {
105     if(this.frame != 0)
106     {
107         this.SendFlags |= TNSF_ANIM;
108         this.anim_start_time = time;
109     }
110
111     this.frame = 0;
112     if(this.velocity)
113         movelib_brake_simple(this, (autocvar_g_turrets_unit_ewheel_speed_stop));
114 }
115
116 spawnfunc(turret_ewheel) { if(!turret_initialize(this, TUR_EWHEEL)) delete(this); }
117
118 METHOD(EWheel, tr_think, void(EWheel thistur, entity it))
119 {
120     float vz;
121     vector wish_angle, real_angle;
122
123     vz = it.velocity_z;
124
125     it.angles_x = anglemods(it.angles_x);
126     it.angles_y = anglemods(it.angles_y);
127
128     fixedmakevectors(it.angles);
129
130     wish_angle = normalize(it.steerto);
131     wish_angle = vectoangles(wish_angle);
132     real_angle = wish_angle - it.angles;
133     real_angle = shortangle_vxy(real_angle, it.tur_head.angles);
134
135     it.tur_head.spawnshieldtime = fabs(real_angle_y);
136     real_angle_y  = bound(-it.tur_head.aim_speed, real_angle_y, it.tur_head.aim_speed);
137     it.angles_y = (it.angles_y + real_angle_y);
138
139     if(it.enemy)
140         ewheel_move_enemy(it);
141     else if(it.pathcurrent)
142         ewheel_move_path(it);
143     else
144         ewheel_move_idle(it);
145
146     it.velocity_z = vz;
147
148     if(it.velocity)
149         it.SendFlags |= TNSF_MOVE;
150 }
151
152 METHOD(EWheel, tr_death, void(EWheel this, entity it))
153 {
154     it.velocity = '0 0 0';
155
156 #ifdef EWHEEL_FANCYPATH
157     if (it.pathcurrent)
158         pathlib_deletepath(it.pathcurrent.owner);
159 #endif
160     it.pathcurrent = NULL;
161 }
162
163 METHOD(EWheel, tr_setup, void(EWheel this, entity it))
164 {
165     entity e;
166
167     if(it.move_movetype == MOVETYPE_WALK)
168     {
169         it.velocity = '0 0 0';
170         it.enemy = NULL;
171
172         setorigin(it, it.pos1);
173
174         if (it.target != "")
175         {
176             e = find(NULL, targetname, it.target);
177             if (!e)
178             {
179                 LOG_TRACE("Initital waypoint for ewheel does NOT exsist, fix your map!");
180                 it.target = "";
181             }
182
183             if (e.classname != "turret_checkpoint")
184                 LOG_TRACE("Warning: not a turrret path");
185             else
186             {
187
188 #ifdef EWHEEL_FANCYPATH
189                 it.pathcurrent = WALKER_PATH(it, it.origin, e.origin);
190                 it.pathgoal = e;
191 #else
192                 it.pathcurrent  = e;
193 #endif
194             }
195         }
196     }
197
198     it.iscreature                               = true;
199     it.teleportable                     = TELEPORT_NORMAL;
200     it.damagedbycontents                = true;
201     IL_PUSH(g_damagedbycontents, it);
202     set_movetype(it, MOVETYPE_WALK);
203     it.solid                                    = SOLID_SLIDEBOX;
204     it.takedamage                               = DAMAGE_AIM;
205     it.idle_aim                         = '0 0 0';
206     it.pos1                                     = it.origin;
207     it.target_select_flags      = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_RANGELIMITS | TFL_TARGETSELECT_TEAMCHECK | TFL_TARGETSELECT_LOS;
208     it.target_validate_flags    = TFL_TARGETSELECT_PLAYERS | TFL_TARGETSELECT_RANGELIMITS | TFL_TARGETSELECT_TEAMCHECK | TFL_TARGETSELECT_LOS;
209     it.frame                                    = it.tur_head.frame = 1;
210     it.ammo_flags                               = TFL_AMMO_ENERGY | TFL_AMMO_RECHARGE | TFL_AMMO_RECIEVE;
211
212     // Convert from dgr / sec to dgr / tic
213     it.tur_head.aim_speed = (autocvar_g_turrets_unit_ewheel_turnrate);
214     it.tur_head.aim_speed = it.tur_head.aim_speed / (1 / it.ticrate);
215 }
216
217 #endif // SVQC
218 #ifdef CSQC
219
220 void ewheel_draw(entity this)
221 {
222     float dt;
223
224     dt = time - this.move_time;
225     this.move_time = time;
226     if(dt <= 0)
227         return;
228
229     fixedmakevectors(this.angles);
230     setorigin(this, this.origin + this.velocity * dt);
231     this.tur_head.angles += dt * this.tur_head.avelocity;
232
233     if (this.health < 127)
234     if(random() < 0.05)
235         te_spark(this.origin + '0 0 40', randomvec() * 256 + '0 0 256', 16);
236 }
237
238         METHOD(EWheel, tr_setup, void(EWheel this, entity it))
239         {
240             it.gravity          = 1;
241             set_movetype(it, MOVETYPE_BOUNCE);
242             it.move_time                = time;
243             it.draw                     = ewheel_draw;
244         }
245
246 #endif // CSQC
247 #endif