1 string wordwrap_buffer;
3 void wordwrap_buffer_put(string s)
5 wordwrap_buffer = strcat(wordwrap_buffer, s);
8 string wordwrap(string s, float l)
12 wordwrap_cb(s, l, wordwrap_buffer_put);
19 void wordwrap_buffer_sprint(string s)
21 wordwrap_buffer = strcat(wordwrap_buffer, s);
24 sprint(self, wordwrap_buffer);
29 void wordwrap_sprint(string s, float l)
32 wordwrap_cb(s, l, wordwrap_buffer_sprint);
33 if(wordwrap_buffer != "")
34 sprint(self, strcat(wordwrap_buffer, "\n"));
40 void wordwrap_cb(string s, float l, void(string) callback)
43 local float lleft, i, j, wlen;
47 for (i = 0;i < strlen(s);i++)
49 if (substring(s, i, 2) == "\\n")
55 else if (substring(s, i, 1) == "\n")
60 else if (substring(s, i, 1) == " ")
70 for (j = i+1;j < strlen(s);j++)
71 // ^^ this skips over the first character of a word, which
72 // is ALWAYS part of the word
73 // this is safe since if i+1 == strlen(s), i will become
74 // strlen(s)-1 at the end of this block and the function
75 // will terminate. A space can't be the first character we
76 // read here, and neither can a \n be the start, since these
77 // two cases have been handled above.
79 c = substring(s, j, 1);
86 // we need to keep this tempstring alive even if substring is
87 // called repeatedly, so call strcat even though we're not
97 callback(substring(s, i, wlen));
105 float dist_point_line(vector p, vector l0, vector ldir)
107 ldir = normalize(ldir);
109 // remove the component in line direction
110 p = p - (p * ldir) * ldir;
112 // vlen of the remaining vector
116 void depthfirst(entity start, .entity up, .entity downleft, .entity right, void(entity, entity) funcPre, void(entity, entity) funcPost, entity pass)
145 float median(float a, float b, float c)
148 return bound(a, b, c);
149 return bound(c, b, a);
152 // converts a number to a string with the indicated number of decimals
153 // works for up to 10 decimals!
154 string ftos_decimals(float number, float decimals)
160 // if negative, cut off the sign first
162 return strcat("-", ftos_decimals(-number, decimals));
163 // it now is always positive!
166 number = floor(number * pow(10, decimals) + 0.5);
169 result = ftos(number);
170 len = strlen(result);
171 // does it have a decimal point (should not happen)? If there is one, it is always at len-7)
172 // if ftos had fucked it up, which should never happen: "34278.000000"
174 if(substring(result, len - 7, 1) == ".")
176 dprint("ftos(integer) has comma? Can't be. Affected result: ", result, "\n");
177 result = substring(result, 0, len - 7);
182 return result; // don't insert a point for zero decimals
183 // is it too short? If yes, insert leading zeroes
186 result = strcat(substring("0000000000", 0, decimals - len + 1), result);
189 // and now... INSERT THE POINT!
190 tmp = substring(result, len - decimals, decimals);
191 result = strcat(substring(result, 0, len - decimals), ".", tmp);
196 vector colormapPaletteColor(float c, float isPants)
200 case 0: return '0.733 0.733 0.733';
201 case 1: return '0.451 0.341 0.122';
202 case 2: return '0.000 0.733 0.733';
203 case 3: return '0.000 1.000 0.000';
204 case 4: return '1.000 0.000 0.000';
205 case 5: return '0.000 0.502 1.000';
206 case 6: return '0.812 0.561 0.169';
207 case 7: return '0.718 0.529 0.420';
208 case 8: return '0.765 0.545 0.667';
209 case 9: return '1.000 0.000 1.000';
210 case 10: return '0.639 0.529 0.482';
211 case 11: return '0.310 0.388 0.341';
212 case 12: return '1.000 1.000 0.000';
213 case 13: return '0.000 0.000 1.000';
214 case 14: return '1.000 0.502 0.000';
218 '1 0 0' * (0.502 + 0.498 * sin(time / 2.7182818285 + 0.0000000000))
219 + '0 1 0' * (0.502 + 0.498 * sin(time / 2.7182818285 + 2.0943951024))
220 + '0 0 1' * (0.502 + 0.498 * sin(time / 2.7182818285 + 4.1887902048));
223 '1 0 0' * (0.502 + 0.498 * sin(time / 3.1415926536 + 5.2359877560))
224 + '0 1 0' * (0.502 + 0.498 * sin(time / 3.1415926536 + 3.1415926536))
225 + '0 0 1' * (0.502 + 0.498 * sin(time / 3.1415926536 + 1.0471975512));
226 default: return '0.000 0.000 0.000';
230 // unzone the string, and return it as tempstring. Safe to be called on string_null
231 string fstrunzone(string s)
241 // Databases (hash tables)
242 #define DB_BUCKETS 8192
243 void db_save(float db, string pFilename)
246 fh = fopen(pFilename, FILE_WRITE);
248 error(strcat("Can't write DB to ", pFilename));
250 fputs(fh, strcat(ftos(DB_BUCKETS), "\n"));
251 for(i = 0; i < n; ++i)
252 fputs(fh, strcat(bufstr_get(db, i), "\n"));
261 float db_load(string pFilename)
263 float db, fh, i, j, n;
268 fh = fopen(pFilename, FILE_READ);
271 if(stof(fgets(fh)) == DB_BUCKETS)
274 while((l = fgets(fh)))
277 bufstr_set(db, i, l);
283 // different count of buckets?
284 // need to reorganize the database then (SLOW)
285 while((l = fgets(fh)))
287 n = tokenizebyseparator(l, "\\");
288 for(j = 2; j < n; j += 2)
289 db_put(db, argv(j-1), argv(j));
296 void db_dump(float db, string pFilename)
298 float fh, i, j, n, m;
299 fh = fopen(pFilename, FILE_WRITE);
301 error(strcat("Can't dump DB to ", pFilename));
304 for(i = 0; i < n; ++i)
306 m = tokenizebyseparator(bufstr_get(db, i), "\\");
307 for(j = 2; j < m; j += 2)
308 fputs(fh, strcat("\\", argv(j-1), "\\", argv(j), "\n"));
313 void db_close(float db)
318 string db_get(float db, string pKey)
321 h = mod(crc16(FALSE, pKey), DB_BUCKETS);
322 return infoget(bufstr_get(db, h), pKey);
325 void db_put(float db, string pKey, string pValue)
328 h = mod(crc16(FALSE, pKey), DB_BUCKETS);
329 bufstr_set(db, h, infoadd(bufstr_get(db, h), pKey, pValue));
336 db = db_load("foo.db");
337 print("LOADED. FILL...\n");
338 for(i = 0; i < DB_BUCKETS; ++i)
339 db_put(db, ftos(random()), "X");
340 print("FILLED. SAVE...\n");
341 db_save(db, "foo.db");
342 print("SAVED. CLOSE...\n");
347 // Multiline text file buffers
348 float buf_load(string pFilename)
355 fh = fopen(pFilename, FILE_READ);
359 while((l = fgets(fh)))
361 bufstr_set(buf, i, l);
368 void buf_save(float buf, string pFilename)
371 fh = fopen(pFilename, FILE_WRITE);
373 error(strcat("Can't write buf to ", pFilename));
374 n = buf_getsize(buf);
375 for(i = 0; i < n; ++i)
376 fputs(fh, strcat(bufstr_get(buf, i), "\n"));