Do traces for shot origin when the racer is actually going to fire, instead of every...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / vehicles / vehicle / racer_weapon.qc
1 #include "racer_weapon.qh"
2
3 #ifdef SVQC
4
5 void racer_fire_rocket(entity player, vector org, vector dir, entity trg);
6 METHOD(RacerAttack, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
7 {
8     bool isPlayer = IS_PLAYER(actor);
9     entity player = isPlayer ? actor : actor.owner;
10     entity veh = player.vehicle;
11     if (fire & 1)
12     if (weapon_prepareattack(thiswep, player, weaponentity, false, autocvar_g_vehicle_racer_cannon_refire)) {
13         if (veh) {
14             veh.vehicle_energy -= autocvar_g_vehicle_racer_cannon_cost;
15             veh.wait = time;
16         }
17         string tagname = (veh.cnt)
18             ? (veh.cnt = 0, "tag_fire1")
19             : (veh.cnt = 1, "tag_fire2");
20         vector shotorg = gettaginfo(veh, gettagindex(veh, tagname));
21         w_shotorg = shotorg;
22         w_shotdir = v_forward;
23         // Fix z-aim (for chase mode)
24         crosshair_trace(player);
25         w_shotdir.z = normalize(trace_endpos - shotorg).z * 0.5;
26
27         if (isPlayer) W_SetupShot_Dir(player, weaponentity, v_forward, false, 0, SND_Null, CH_WEAPON_B, 0, DEATH_VH_WAKI_GUN.m_id);
28         vector org = w_shotorg;
29         vector dir = w_shotdir;
30         entity bolt = vehicles_projectile(veh, EFFECT_RACER_MUZZLEFLASH, SND_LASERGUN_FIRE,
31                                org, normalize(v_forward + randomvec() * autocvar_g_vehicle_racer_cannon_spread) * autocvar_g_vehicle_racer_cannon_speed,
32                                autocvar_g_vehicle_racer_cannon_damage, autocvar_g_vehicle_racer_cannon_radius, autocvar_g_vehicle_racer_cannon_force,  0,
33                                DEATH_VH_WAKI_GUN.m_id, PROJECTILE_WAKICANNON, 0, true, true, player);
34         bolt.velocity = normalize(dir) * autocvar_g_vehicle_racer_cannon_speed;
35         weapon_thinkf(player, weaponentity, WFRAME_FIRE1, 0, w_ready);
36     }
37     if (fire & 2)
38     if (!isPlayer || weapon_prepareattack(thiswep, actor, weaponentity, false, 0.2)) {
39         if (isPlayer) W_SetupShot_Dir(actor, weaponentity, v_forward, false, 0, SND_Null, CH_WEAPON_B, 0, DEATH_VH_WAKI_ROCKET.m_id);
40         racer_fire_rocket(player, w_shotorg, w_shotdir, NULL);
41         weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, 0, w_ready);
42     }
43 }
44
45 METHOD(RacerAttack, wr_checkammo1, bool(RacerAttack thiswep, entity actor, .entity weaponentity))
46 {
47     bool isPlayer = IS_PLAYER(actor);
48     entity player = isPlayer ? actor : actor.owner;
49     entity veh = player.vehicle;
50     return isPlayer || veh.vehicle_energy >= autocvar_g_vehicle_racer_cannon_cost;
51 }
52
53 void racer_rocket_tracker(entity this);
54 void racer_rocket_groundhugger(entity this);
55
56 void racer_fire_rocket(entity player, vector org, vector dir, entity trg)
57 {
58     entity rocket = vehicles_projectile(player.vehicle, EFFECT_RACER_ROCKETLAUNCH, SND_ROCKET_FIRE,
59                            org, dir * autocvar_g_vehicle_racer_rocket_speed,
60                            autocvar_g_vehicle_racer_rocket_damage, autocvar_g_vehicle_racer_rocket_radius, autocvar_g_vehicle_racer_rocket_force, 3,
61                            DEATH_VH_WAKI_ROCKET.m_id, PROJECTILE_WAKIROCKET, 20, false, false, player);
62
63     rocket.lip                    = autocvar_g_vehicle_racer_rocket_accel * sys_frametime;
64     rocket.wait                  = autocvar_g_vehicle_racer_rocket_turnrate;
65     rocket.nextthink            = time;
66     rocket.enemy                        = trg;
67     rocket.cnt                    = time + 15;
68
69     if(trg)
70         setthink(rocket, racer_rocket_tracker);
71     else
72         setthink(rocket, racer_rocket_groundhugger);
73 }
74
75 void racer_rocket_tracker(entity this)
76 {
77     vector olddir, newdir;
78     float oldvel, newvel;
79
80     this.nextthink  = time;
81
82     if (IS_DEAD(this.owner) || this.cnt < time)
83     {
84         this.use(this, NULL, NULL);
85         return;
86     }
87
88     if(!this.realowner.vehicle)
89     {
90         UpdateCSQCProjectile(this);
91         return;
92     }
93
94     olddir = normalize(this.velocity);
95     oldvel = vlen(this.velocity);
96     newvel = oldvel + this.lip;
97     makevectors(vectoangles(olddir));
98
99     float time_to_impact = min(vlen(this.enemy.origin - this.origin) / vlen(this.velocity), 1);
100     vector predicted_origin = this.enemy.origin + this.enemy.velocity * time_to_impact;
101
102     traceline(this.origin, this.origin + v_forward * 64 - '0 0 32', MOVE_NORMAL, this);
103     newdir = normalize(predicted_origin - this.origin);
104
105     //vector
106     float height_diff = predicted_origin_z - this.origin_z;
107
108     if(vdist(newdir - v_forward, >, autocvar_g_vehicle_racer_rocket_locked_maxangle))
109     {
110         //bprint("Target lost!\n");
111         //dprint("OF:", ftos(vlen(newdir - v_forward)), "\n");
112         setthink(this, racer_rocket_groundhugger);
113         return;
114     }
115
116     if(trace_fraction != 1.0 && trace_ent != this.enemy)
117         newdir_z += 16 * sys_frametime;
118
119     this.velocity = normalize(olddir + newdir * autocvar_g_vehicle_racer_rocket_turnrate) * newvel;
120     this.velocity_z -= 800 * sys_frametime;
121     this.velocity_z += max(height_diff, autocvar_g_vehicle_racer_rocket_climbspeed) * sys_frametime ;
122
123     UpdateCSQCProjectile(this);
124     return;
125 }
126
127 void racer_rocket_groundhugger(entity this)
128 {
129     vector olddir, newdir;
130     float oldvel, newvel;
131
132     this.nextthink  = time;
133
134     if(IS_DEAD(this.owner) || this.cnt < time)
135     {
136         this.use(this, NULL, NULL);
137         return;
138     }
139
140     if(!this.realowner.vehicle)
141     {
142         UpdateCSQCProjectile(this);
143         return;
144     }
145
146     olddir = normalize(this.velocity);
147     oldvel = vlen(this.velocity);
148     newvel = oldvel + this.lip;
149
150     tracebox(this.origin, this.mins, this.maxs, this.origin + olddir * 64, MOVE_WORLDONLY,this);
151     if(trace_fraction <= 0.5)
152     {
153         // Hitting somethign soon, just speed ahead
154         this.velocity = olddir * newvel;
155         UpdateCSQCProjectile(this);
156         return;
157     }
158
159     traceline(trace_endpos, trace_endpos - '0 0 64', MOVE_NORMAL, this);
160     if(trace_fraction != 1.0)
161     {
162         newdir = normalize(trace_endpos + '0 0 64' - this.origin) * autocvar_g_vehicle_racer_rocket_turnrate;
163         this.velocity = normalize(olddir + newdir) * newvel;
164     }
165     else
166     {
167         this.velocity = olddir * newvel;
168         this.velocity_z -= 1600 * sys_frametime; // 2x grav looks better for this one
169     }
170
171     int cont = pointcontents(this.origin - '0 0 32');
172     if(cont == CONTENT_WATER || cont == CONTENT_LAVA || cont == CONTENT_SLIME)
173         this.velocity_z += 200;
174
175     UpdateCSQCProjectile(this);
176     return;
177 }
178
179 #endif