]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/hitplot.qc
Merge branch 'martin-t/defaults' into martin-t/okc
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / hitplot.qc
1 #include "hitplot.qh"
2
3 #include <server/defs.qh>
4 #include <server/miscfunctions.qh>
5 #include "../antilag.qh"
6 #include "../g_subs.qh"
7 #include <common/weapons/_all.qh>
8 #include <common/state.qh>
9 #include <common/wepent.qh>
10
11 vector W_HitPlotUnnormalizedUntransform(vector screenforward, vector screenright, vector screenup, vector v)
12 {
13         vector ret;
14         ret.x = screenright * v;
15         ret.y = screenup * v;
16         ret.z = screenforward * v;
17         return ret;
18 }
19
20 vector W_HitPlotNormalizedUntransform(vector org, entity targ, vector screenforward, vector screenright, vector screenup, vector v)
21 {
22         float i, j, k;
23         vector mi, ma, thisv, myv, ret;
24
25         myv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, org);
26
27         // x = 0..1 relative to hitbox; y = 0..1 relative to hitbox; z = distance
28
29         mi = ma = targ.origin + 0.5 * (targ.mins + targ.maxs);
30         for(i = 0; i < 2; ++i) for(j = 0; j < 2; ++j) for(k = 0; k < 2; ++k)
31         {
32                 thisv = targ.origin;
33                 if(i) thisv.x += targ.maxs.x; else thisv.x += targ.mins.x;
34                 if(j) thisv.y += targ.maxs.y; else thisv.y += targ.mins.y;
35                 if(k) thisv.z += targ.maxs.z; else thisv.z += targ.mins.z;
36                 thisv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, thisv);
37                 if(i || j || k)
38                 {
39                         if(mi.x > thisv.x) mi.x = thisv.x; if(ma.x < thisv.x) ma.x = thisv.x;
40                         if(mi.y > thisv.y) mi.y = thisv.y; if(ma.y < thisv.y) ma.y = thisv.y;
41                         //if(mi_z > thisv_z) mi_z = thisv_z; if(ma_z < thisv_z) ma_y = thisv_z;
42                 }
43                 else
44                 {
45                         // first run
46                         mi = ma = thisv;
47                 }
48         }
49
50         thisv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, v);
51         ret.x = (thisv.x - mi.x) / (ma.x - mi.x);
52         ret.y = (thisv.y - mi.y) / (ma.y - mi.y);
53         ret.z = thisv.z - myv.z;
54         return ret;
55 }
56
57 void W_HitPlotAnalysis(entity player, .entity weaponentity, vector screenforward, vector screenright, vector screenup)
58 {
59         if(CS(player).hitplotfh >= 0)
60         {
61                 float lag = ANTILAG_LATENCY(player);
62                 if(lag < 0.001)
63                         lag = 0;
64                 if(!IS_REAL_CLIENT(player))
65                         lag = 0; // only antilag for clients
66
67                 vector org = player.origin + player.view_ofs;
68                 traceline_antilag_force(player, org, org + screenforward * max_shot_distance, MOVE_NORMAL, player, lag);
69                 if(IS_CLIENT(trace_ent) || IS_MONSTER(trace_ent))
70                 {
71                     entity store = IS_CLIENT(trace_ent) ? CS(trace_ent) : trace_ent;
72                         antilag_takeback(trace_ent, store, time - lag);
73                         vector hitplot = W_HitPlotNormalizedUntransform(org, trace_ent, screenforward, screenright, screenup, trace_endpos);
74                         antilag_restore(trace_ent, store);
75                         fputs(CS(player).hitplotfh, strcat(ftos(hitplot.x), " ", ftos(hitplot.y), " ", ftos(hitplot.z), " ", ftos(player.(weaponentity).m_switchweapon.m_id), "\n"));
76                         //print(strcat(ftos(hitplot_x), " ", ftos(hitplot_y), " ", ftos(hitplot_z), "\n"));
77                 }
78         }
79 }
80
81 void W_HitPlotOpen(entity player)
82 {
83         if(autocvar_g_hitplots || strhasword(autocvar_g_hitplots_individuals, player.netaddress))
84         {
85                 CS(player).hitplotfh = fopen(strcat("hits-", matchid, "-", player.netaddress, "-", ftos(player.playerid), ".plot"), FILE_WRITE);
86                 fputs(CS(player).hitplotfh, strcat("#name ", playername(player, false), "\n"));
87         }
88         else { CS(player).hitplotfh = -1; }
89 }
90
91 void W_HitPlotClose(entity player)
92 {
93         if(CS(player).hitplotfh >= 0)
94         {
95                 fclose(CS(player).hitplotfh);
96                 CS(player).hitplotfh = -1;
97         }
98 }