]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/miscfunctions.qc
terrencehill's color codes patch. Please test thoroughly!
[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 float RegisterTeam(entity Team)
75 {
76         entity tm;
77         AuditLists();
78         for(tm = teams.sort_next; tm; tm = tm.sort_next)
79                 if(tm == Team)
80                         error("Team already registered!");
81         Team.sort_next = teams.sort_next;
82         Team.sort_prev = teams;
83         if(teams.sort_next)
84                 teams.sort_next.sort_prev = Team;
85         teams.sort_next = Team;
86         AuditLists();
87         return true;
88 }
89
90 void RemoveTeam(entity Team)
91 {
92         entity tm, parent;
93         AuditLists();
94         parent = teams;
95         for(tm = teams.sort_next; tm && tm != Team; tm = tm.sort_next)
96                 parent = tm;
97
98         if(!tm)
99         {
100                 print("Trying to remove a team which is not in the teamlist!");
101                 return;
102         }
103         parent.sort_next = Team.sort_next;
104         if(Team.sort_next)
105                 Team.sort_next.sort_prev = parent;
106         AuditLists();
107 }
108
109 entity GetTeam(float Team, float add)
110 {
111         float num;
112         entity tm;
113         num = (Team == COLOR_SPECTATOR) ? 16 : Team;
114         if(teamslots[num])
115                 return teamslots[num];
116         if not(add)
117                 return NULL;
118         tm = spawn();
119         tm.team = Team;
120         teamslots[num] = tm;
121         RegisterTeam(tm);
122         return tm;
123 }
124
125 float stringwidth_oldfont(string text, float handleColors)
126 {
127         float i, len, ch, width;
128         len = strlen(text);
129         if(!handleColors)
130                 return len;
131         width = 0;
132         for(i = 0; i < len; ++i)
133         {
134                 if(substring(text, i, 1) == "^")
135                 {
136                         ch = str2chr(text, i+1);
137                         if(ch >= '0' && ch <= '9')
138                                 ++i;
139                         else if(i+4 < len && ch == 'x')
140                         {
141                                 ch = str2chr(text, i+2);
142                                 if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') )
143                                 {
144                                         ch = str2chr(text, i+3);
145                                         if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') )
146                                         {
147                                                 ch = str2chr(text, i+4);
148                                                 if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') )
149                                                         i+=4;
150                                                 else ++width;
151                                         } else ++width;
152                                 } else ++width;
153                         } else ++width;
154                 } else ++width;
155         }
156         return width;
157 }
158
159 void CSQC_CheckEngine()
160 {
161         /*
162         registercvar("csqc_flags", "0");
163         csqc_flags = cvar("csqc_flags");
164         */
165
166         csqc_flags = 0;
167         
168         if(checkextension("DP_SV_WRITEPICTURE"))
169         {
170                 stringwidth = stringwidth_engine;
171                 sbar_font = FONT_USER+1;
172                 sbar_bigfont = FONT_USER+2;
173                 csqc_flags |= CSQC_FLAG_READPICTURE;
174         } else {
175                 stringwidth = stringwidth_oldfont;
176                 sbar_font = FONT_DEFAULT;
177                 sbar_bigfont = FONT_DEFAULT;
178         }
179 }
180
181 vector Sbar_GetFontsize()
182 {
183         if(csqc_flags & CSQC_FLAG_READPICTURE)
184         {
185                 vector v;
186                 v = stov(cvar_string("sbar_fontsize"));
187                 if(v_x == 0)
188                         v = '8 8 0';
189                 if(v_y == 0)
190                         v_y = v_x;
191                 v_z = 0;
192                 return v;
193         }
194         return '8 8 0' ;
195 }
196
197 float Sbar_GetWidth(float teamcolumnwidth)
198 {
199         if(csqc_flags & CSQC_FLAG_READPICTURE)
200         {
201                 float f;
202                 f = stof(cvar_string("sbar_width"));
203                 if(f == 0)
204                         f = 640;
205                 if(f < 320)
206                         f = 320;
207                 if(f > vid_conwidth - 2 * teamcolumnwidth)
208                         f = vid_conwidth - 2 * teamcolumnwidth;
209                 return f;
210         }
211         return 640;
212 }
213
214 float PreviewExists(string name)
215 {
216         float f;
217         string file;
218
219         if(cvar("cl_readpicture_force"))
220                 return false;
221
222         file = strcat(name, ".tga");
223         f = fopen(file, FILE_READ);
224         if(f >= 0)
225         {
226                 fclose(f);
227                 return true;
228         }
229         file = strcat(name, ".png");
230         f = fopen(file, FILE_READ);
231         if(f >= 0)
232         {
233                 fclose(f);
234                 return true;
235         }
236         file = strcat(name, ".jpg");
237         f = fopen(file, FILE_READ);
238         if(f >= 0)
239         {
240                 fclose(f);
241                 return true;
242         }
243         file = strcat(name, ".pcx");
244         f = fopen(file, FILE_READ);
245         if(f >= 0)
246         {
247                 fclose(f);
248                 return true;
249         }
250         return false;
251 }
252
253 float PI      = 3.14159265359;
254 float DEG2RAD = 0.01745329252;
255 vector rotate(vector v, float a)
256 {
257         vector w;
258         // FTEQCC SUCKS AGAIN
259         w_x =      v_x * cos(a) + v_y * sin(a);
260         w_y = -1 * v_x * sin(a) + v_y * cos(a);
261         return w;
262 }