Merge branch 'master' into samual/weapons
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / hitplot.qc
1 vector W_HitPlotUnnormalizedUntransform(vector screenforward, vector screenright, vector screenup, vector v)
2 {
3         vector ret;
4         ret_x = screenright * v;
5         ret_y = screenup * v;
6         ret_z = screenforward * v;
7         return ret;
8 }
9
10 vector W_HitPlotNormalizedUntransform(vector org, entity targ, vector screenforward, vector screenright, vector screenup, vector v)
11 {
12         float i, j, k;
13         vector mi, ma, thisv, myv, ret;
14
15         myv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, org);
16
17         // x = 0..1 relative to hitbox; y = 0..1 relative to hitbox; z = distance
18
19         mi = ma = targ.origin + 0.5 * (targ.mins + targ.maxs);
20         for(i = 0; i < 2; ++i) for(j = 0; j < 2; ++j) for(k = 0; k < 2; ++k)
21         {
22                 thisv = targ.origin;
23                 if(i) thisv_x += targ.maxs_x; else thisv_x += targ.mins_x;
24                 if(j) thisv_y += targ.maxs_y; else thisv_y += targ.mins_y;
25                 if(k) thisv_z += targ.maxs_z; else thisv_z += targ.mins_z;
26                 thisv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, thisv);
27                 if(i || j || k)
28                 {
29                         if(mi_x > thisv_x) mi_x = thisv_x; if(ma_x < thisv_x) ma_x = thisv_x;
30                         if(mi_y > thisv_y) mi_y = thisv_y; if(ma_y < thisv_y) ma_y = thisv_y;
31                         //if(mi_z > thisv_z) mi_z = thisv_z; if(ma_z < thisv_z) ma_y = thisv_z;
32                 }
33                 else
34                 {
35                         // first run
36                         mi = ma = thisv;
37                 }
38         }
39
40         thisv = W_HitPlotUnnormalizedUntransform(screenforward, screenright, screenup, v);
41         ret_x = (thisv_x - mi_x) / (ma_x - mi_x);
42         ret_y = (thisv_y - mi_y) / (ma_y - mi_y);
43         ret_z = thisv_z - myv_z;
44         return ret;
45 }
46
47 void W_HitPlotAnalysis(entity player, vector screenforward, vector screenright, vector screenup)
48 {
49         vector hitplot;
50         vector org;
51         float lag;
52
53         if(player.hitplotfh >= 0)
54         {
55                 lag = ANTILAG_LATENCY(player);
56                 if(lag < 0.001)
57                         lag = 0;
58                 if(!IS_REAL_CLIENT(player))
59                         lag = 0; // only antilag for clients
60
61                 org = player.origin + player.view_ofs;
62                 traceline_antilag_force(player, org, org + screenforward * MAX_SHOT_DISTANCE, MOVE_NORMAL, player, lag);
63                 if(IS_CLIENT(trace_ent) || (trace_ent.flags & FL_MONSTER))
64                 {
65                         antilag_takeback(trace_ent, time - lag);
66                         hitplot = W_HitPlotNormalizedUntransform(org, trace_ent, screenforward, screenright, screenup, trace_endpos);
67                         antilag_restore(trace_ent);
68                         fputs(player.hitplotfh, strcat(ftos(hitplot_x), " ", ftos(hitplot_y), " ", ftos(hitplot_z), " ", ftos(player.switchweapon), "\n"));
69                         //print(strcat(ftos(hitplot_x), " ", ftos(hitplot_y), " ", ftos(hitplot_z), "\n"));
70                 }
71         }
72 }
73
74 void W_HitPlotOpen(entity player)
75 {
76         if(autocvar_g_hitplots || strstrofs(strcat(" ", autocvar_g_hitplots_individuals, " "), strcat(" ", player.netaddress, " "), 0) >= 0)
77         {
78                 player.hitplotfh = fopen(strcat("hits-", matchid, "-", player.netaddress, "-", ftos(player.playerid), ".plot"), FILE_WRITE);
79                 fputs(player.hitplotfh, strcat("#name ", player.netname, "\n"));
80         }
81         else { player.hitplotfh = -1; }
82 }
83
84 void W_HitPlotClose(entity player)
85 {
86         if(player.hitplotfh >= 0)
87         {
88                 fclose(player.hitplotfh);
89                 player.hitplotfh = -1;
90         }
91 }