]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/gamec/miscfunctions.c
Special escapes for say and team_say: %a = armor, %h = health, %l = location (nearest...
[divverent/nexuiz.git] / data / qcsrc / server / gamec / miscfunctions.c
1 string W_Name(float weaponid);
2 float(float index) weapon_translateindextoflag;
3
4 float logfile_open;
5 float logfile;
6
7 void(string s, float check_dangerous) ServerConsoleEcho =
8 {
9         local string ch;
10         localcmd("echo \"");
11         if(check_dangerous)
12         {
13                 while(strlen(s))
14                 {
15                         ch = substring(s, 0, 1);
16                         if(ch != "\"" && ch != "\r" && ch != "\n")
17                                 localcmd(ch);
18                         s = substring(s, 1, strlen(s) - 1);
19                 }
20         }
21         else
22         {
23                 localcmd(s);
24         }
25         localcmd("\"\n");
26 }
27
28 void(string s, float check_dangerous) GameLogEcho =
29 {
30         string fn;
31         float matches;
32
33         if(cvar("sv_eventlog_files"))
34         {
35                 if(!logfile_open)
36                 {
37                         logfile_open = TRUE;
38                         matches = cvar("sv_eventlog_files_counter") + 1;
39                         cvar_set("sv_eventlog_files_counter", ftos(matches));
40                         fn = ftos(matches);
41                         if(strlen(fn) < 8)
42                                 fn = strcat(substring("00000000", 0, 8 - strlen(fn)), fn);
43                         fn = strcat(cvar_string("sv_eventlog_files_nameprefix"), fn, cvar_string("sv_eventlog_files_namesuffix"));
44                         logfile = fopen(fn, FILE_APPEND);
45                 }
46                 if(logfile >= 0)
47                         fputs(logfile, strcat(s, "\n"));
48         }
49         if(cvar("sv_eventlog_console"))
50         {
51                 ServerConsoleEcho(s, check_dangerous);
52         }
53 }
54
55 void() GameLogInit =
56 {
57         logfile_open = 0;
58         // will be opened later
59 }
60
61 void() GameLogClose =
62 {
63         if(logfile_open && logfile >= 0)
64         {
65                 fclose(logfile);
66                 logfile = -1;
67         }
68 }
69
70 float math_mod(float a, float b)
71 {
72         return a - (floor(a / b) * b);
73 }
74
75 string linewrap(string s, float l)
76 {
77         string t;
78
79         t = "";
80         while(l < strlen(s))
81         {
82                 t = strcat(t, substring(s, 0, l), "\n");
83                 s = substring(s, l+1, strlen(s));
84         }
85         return strcat(t, s);
86 }
87
88 vector find_floor(vector org)
89 {
90         traceline(org + '0 0 5', org - '0 0 255', TRUE, self);
91         if (trace_fraction < 1)
92                 return trace_endpos;
93         else
94                 return org;
95 }
96
97 void relocate_spawnpoint()
98 {
99         if(self.noalign)
100                 return;
101
102         setorigin(self, find_floor(self.origin) + '0 0 30');
103 }
104
105 // NOTE: DO NOT USE THIS FUNCTION TOO OFTEN.
106 // IT WILL MOST PROBABLY DESTROY _ALL_ OTHER TEMP
107 // STRINGS AND TAKE QUITE LONG. haystack and needle MUST
108 // BE CONSTANT OR strzoneD!
109 float(string haystack, string needle, float offset) strstr =
110 {
111         float len, endpos;
112         string found;
113         len = strlen(needle);
114         endpos = strlen(haystack) - len;
115         while(offset < endpos)
116         {
117                 found = substring(haystack, offset, len);
118                 if(found == needle)
119                         return offset;
120                 offset = offset + 1;
121         }
122         return -1;
123 }
124
125 entity(vector point, .string field, string value, vector axismod) findnearest =
126 {
127         entity localhead;
128         entity best;
129         float bestlen;
130
131         float len;
132         vector dist;
133
134         best = world;
135         bestlen = 10000;
136         localhead = find(world, field, value);
137         while(localhead)
138         {
139                 if((localhead.items == IT_KEY1 || localhead.items == IT_KEY2) && localhead.target = "###item###")
140                         dist = localhead.oldorigin;
141                 else
142                         dist = localhead.origin;
143                 dist = dist - point;
144                 dist = dist_x * axismod_x * '1 0 0' + dist_y * axismod_y * '0 1 0' + dist_z * axismod_z * '0 0 1';
145                 len = vlen(dist);
146                 if(len < bestlen)
147                 {
148                         bestlen = len;
149                         best = localhead;
150                 }
151                 localhead = find(localhead, field, value);
152         }
153         return best;
154 }
155
156 string(string msg) formatmessage =
157 {
158         float p;
159         string msg_save;
160         string escape;
161         string replacement;
162         msg_save = strzone(msg);
163         p = 0;
164         while(1)
165         {
166                 p = strstr(msg_save, "%", p);
167                 if(p < 0)
168                         break;
169                 replacement = substring(msg_save, p, 2);
170                 escape = substring(msg_save, p + 1, 1);
171                 if(escape == "a")
172                         replacement = ftos(floor(self.armorvalue));
173                 else if(escape == "h")
174                         replacement = ftos(floor(self.health));
175                 else if(escape == "l")
176                 {
177                         entity loc;
178                         loc = findnearest(self.origin, classname, "info_location", '1 1 1');
179                         if(!loc)
180                                 loc = findnearest(self.origin, target, "###item###", '1 1 4');
181                         if(loc)
182                                 replacement = loc.netname;
183                         else
184                                 replacement = "someplace";
185                 }
186                 else if(escape == "x")
187                 {
188                         if(self.cursor_trace_ent)
189                                 replacement = self.cursor_trace_ent.netname;
190                         else
191                                 replacement = "nothing";
192                 }
193                 dprint(strcat(ftos(p), "<", msg, "\n"));
194                 msg = strcat(substring(msg_save, 0, p), replacement);
195                 msg = strcat(msg, substring(msg_save, p+2, strlen(msg_save) - (p+2)));
196                 dprint(strcat(ftos(p), ">", msg, "\n"));
197                 strunzone(msg_save);
198                 msg_save = strzone(msg);
199                 p = p + 2;
200         }
201         strunzone(msg_save);
202         return msg;
203 }