]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/miscfunctions.qc
try to improve player networking: let playerchecker do ALL the work for adding/removi...
[divverent/nexuiz.git] / data / qcsrc / client / miscfunctions.qc
1 var float(string text, float handleColors) stringwidth;
2
3 entity players;
4 entity teams;
5
6 void AuditLists()
7 {
8         entity e;
9         entity prev;
10
11         prev = players;
12         for(e = prev.sort_next; e; prev = e, e = e.sort_next)
13         {
14                 if(prev != e.sort_prev)
15                         error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
16         }
17
18         prev = teams;
19         for(e = prev.sort_next; e; prev = e, e = e.sort_next)
20         {
21                 if(prev != e.sort_prev)
22                         error(strcat("sort list chain error\nplease submit the output of 'prvm_edicts client' to the developers"));
23         }
24 }
25
26
27 float RegisterPlayer(entity player)
28 {
29         entity pl;
30         AuditLists();
31         for(pl = players.sort_next; pl; pl = pl.sort_next)
32                 if(pl == player)
33                         error("Player already registered!");
34         player.sort_next = players.sort_next;
35         player.sort_prev = players;
36         if(players.sort_next)
37                 players.sort_next.sort_prev = player;
38         players.sort_next = player;
39         AuditLists();
40         return true;
41 }
42
43 void RemovePlayer(entity player)
44 {
45         entity pl, parent;
46         AuditLists();
47         parent = players;
48         for(pl = players.sort_next; pl && pl != player; pl = pl.sort_next)
49                 parent = pl;
50
51         if(!pl)
52         {
53                 error("Trying to remove a player which is not in the playerlist!");
54                 return;
55         }
56         parent.sort_next = player.sort_next;
57         if(player.sort_next)
58                 player.sort_next.sort_prev = parent;
59         AuditLists();
60 }
61
62 void MoveToLast(entity e)
63 {
64         AuditLists();
65         other = e.sort_next;
66         while(other)
67         {
68                 SORT_SWAP(other, e);
69                 other = e.sort_next;
70         }
71         AuditLists();
72 }
73
74 // warning: Local "team" defined with name of a global
75 // FU FTEQCC, .float team is a ENTVAR shitty piece of crap!!!
76 float RegisterTeam(entity Team)
77 {
78         entity tm;
79         AuditLists();
80         for(tm = teams.sort_next; tm; tm = tm.sort_next)
81                 if(tm == Team)
82                         error("Team already registered!");
83         Team.sort_next = teams.sort_next;
84         Team.sort_prev = teams;
85         if(teams.sort_next)
86                 teams.sort_next.sort_prev = Team;
87         teams.sort_next = Team;
88         AuditLists();
89         return true;
90 }
91
92 void RemoveTeam(entity Team)
93 {
94         entity tm, parent;
95         AuditLists();
96         parent = teams;
97         for(tm = teams.sort_next; tm && tm != Team; tm = tm.sort_next)
98                 parent = tm;
99
100         if(!tm)
101         {
102                 print("Trying to remove a team which is not in the teamlist!");
103                 return;
104         }
105         parent.sort_next = Team.sort_next;
106         if(Team.sort_next)
107                 Team.sort_next.sort_prev = parent;
108         AuditLists();
109 }
110
111 entity GetTeam(float Team, float add)
112 {
113         float num;
114         entity tm;
115         num = (Team == COLOR_SPECTATOR) ? 16 : Team;
116         if(teamslots[num])
117                 return teamslots[num];
118         if not(add)
119                 return NULL;
120         tm = spawn();
121         tm.team = Team;
122         teamslots[num] = tm;
123         RegisterTeam(tm);
124         return tm;
125 }
126
127 float stringwidth_oldfont(string text, float handleColors)
128 {
129         float i, len, ch, width;
130         len = strlen(text);
131         if(!handleColors)
132                 return len;
133         width = 0;
134         for(i = 0; i < len; ++i)
135         {
136                 if(substring(text, i, 1) == "^")
137                 {
138                         ch = str2chr(text, i+1);
139                         if(ch >= '0' && ch <= '9')
140                                 ++i;
141                         else
142                                 ++width;
143                 }
144                 else
145                         ++width;
146         }
147         return width;
148 }
149
150 void CSQC_CheckEngine()
151 {
152         /*
153         registercvar("csqc_flags", "0");
154         csqc_flags = cvar("csqc_flags");
155         */
156
157         csqc_flags = 0;
158         
159         if(checkextension("DP_SV_WRITEPICTURE"))
160         {
161                 stringwidth = stringwidth_engine;
162                 sbar_font = FONT_USER+1;
163                 sbar_bigfont = FONT_USER+2;
164                 csqc_flags |= CSQC_FLAG_READPICTURE;
165         } else {
166                 stringwidth = stringwidth_oldfont;
167                 sbar_font = FONT_DEFAULT;
168                 sbar_bigfont = FONT_DEFAULT;
169         }
170 }
171
172 vector Sbar_GetFontsize()
173 {
174         if(csqc_flags & CSQC_FLAG_READPICTURE)
175         {
176                 vector v;
177                 v = stov(cvar_string("sbar_fontsize"));
178                 if(v_x == 0)
179                         v = '8 8 0';
180                 if(v_y == 0)
181                         v_y = v_x;
182                 v_z = 0;
183                 return v;
184         }
185         return '8 8 0' ;
186 }
187
188 float Sbar_GetWidth(float teamcolumnwidth)
189 {
190         if(csqc_flags & CSQC_FLAG_READPICTURE)
191         {
192                 float f;
193                 f = stof(cvar_string("sbar_width"));
194                 if(f == 0)
195                         f = 640;
196                 if(f < 320)
197                         f = 320;
198                 if(f > vid_conwidth - 2 * teamcolumnwidth)
199                         f = vid_conwidth - 2 * teamcolumnwidth;
200                 return f;
201         }
202         return 640;
203 }
204
205 float PreviewExists(string name)
206 {
207         float f;
208         string file;
209
210         if(cvar("cl_readpicture_force"))
211                 return false;
212
213         file = strcat(name, ".tga");
214         f = fopen(file, FILE_READ);
215         if(f >= 0)
216         {
217                 fclose(f);
218                 return true;
219         }
220         file = strcat(name, ".png");
221         f = fopen(file, FILE_READ);
222         if(f >= 0)
223         {
224                 fclose(f);
225                 return true;
226         }
227         file = strcat(name, ".jpg");
228         f = fopen(file, FILE_READ);
229         if(f >= 0)
230         {
231                 fclose(f);
232                 return true;
233         }
234         file = strcat(name, ".pcx");
235         f = fopen(file, FILE_READ);
236         if(f >= 0)
237         {
238                 fclose(f);
239                 return true;
240         }
241         return false;
242 }
243
244 float PI      = 3.14159265359;
245 float DEG2RAD = 0.01745329252;
246 vector rotate(vector v, float a)
247 {
248         vector w;
249         // FTEQCC SUCKS AGAIN
250         w_x =      v_x * cos(a) + v_y * sin(a);
251         w_y = -1 * v_x * sin(a) + v_y * cos(a);
252         return w;
253 }