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