]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/common/util.qc
cmd info (something) prints the content of sv_info_(something), for server admins...
[divverent/nexuiz.git] / data / qcsrc / common / util.qc
1 string wordwrap_buffer;
2
3 void wordwrap_buffer_put(string s)
4 {
5         wordwrap_buffer = strcat(wordwrap_buffer, s);
6 }
7
8 string wordwrap(string s, float l)
9 {
10         wordwrap_buffer = "";
11         wordwrap_cb(s, l, wordwrap_buffer_put);
12         return wordwrap_buffer;
13 }
14
15 #ifndef MENUQC
16 void wordwrap_buffer_sprint(string s)
17 {
18         wordwrap_buffer = strcat(wordwrap_buffer, s);
19         if(s == "\n")
20         {
21                 sprint(self, wordwrap_buffer);
22                 wordwrap_buffer = "";
23         }
24 }
25
26 void wordwrap_sprint(string s, float l)
27 {
28         wordwrap_buffer = "";
29         wordwrap_cb(s, l, wordwrap_buffer_sprint);
30         if(wordwrap_buffer != "")
31                 sprint(self, strcat(wordwrap_buffer, "\n"));
32         return;
33 }
34 #endif
35
36 void wordwrap_cb(string s, float l, void(string) callback)
37 {
38         local string c;
39         local float lleft, i, j, wlen;
40
41         s = strzone(s);
42         lleft = l;
43         for (i = 0;i < strlen(s);i++)
44         {
45                 if (substring(s, i, 2) == "\\n")
46                 {
47                         callback("\n");
48                         lleft = l;
49                         i++;
50                 }
51                 else if (substring(s, i, 1) == " ")
52                 {
53                         if (lleft > 0)
54                         {
55                                 callback(" ");
56                                 lleft = lleft - 1;
57                         }
58                 }
59                 else
60                 {
61                         for (j = i+1;j < strlen(s);j++)
62                                 //    ^^ this skips over the first character of a word, which
63                                 //       is ALWAYS part of the word
64                                 //       this is safe since if i+1 == strlen(s), i will become
65                                 //       strlen(s)-1 at the end of this block and the function
66                                 //       will terminate. A space can't be the first character we
67                                 //       read here, and neither can a \n be the start, since these
68                                 //       two cases have been handled above.
69                         {
70                                 c = substring(s, j, 1);
71                                 if (c == " ")
72                                         break;
73                                 if (c == "\\")
74                                         break;
75                                 // we need to keep this tempstring alive even if substring is
76                                 // called repeatedly, so call strcat even though we're not
77                                 // doing anything
78                                 callback("");
79                         }
80                         wlen = j - i;
81                         if (lleft < wlen)
82                         {
83                                 callback("\n");
84                                 lleft = l;
85                         }
86                         callback(substring(s, i, wlen));
87                         lleft = lleft - wlen;
88                         i = j - 1;
89                 }
90         }
91         strunzone(s);
92 }
93
94 float dist_point_line(vector p, vector l0, vector ldir)
95 {
96         ldir = normalize(ldir);
97         
98         // remove the component in line direction
99         p = p - (p * ldir) * ldir;
100
101         // vlen of the remaining vector
102         return vlen(p);
103 }