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