]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/common/util.qc
And this time, a REAL fix.
[divverent/nexuiz.git] / data / qcsrc / common / util.qc
1
2 string wordwrap(string s, float l)
3 {
4         local string t, c;
5         local float lleft, i, j, wlen;
6
7         s = strzone(s);
8         t = "";
9         lleft = l;
10         for (i = 0;i < strlen(s);i++)
11         {
12                 if (substring(s, i, 2) == "\\n")
13                 {
14                         t = strcat(t, "\n");
15                         lleft = l;
16                         i++;
17                 }
18                 else if (substring(s, i, 1) == " ")
19                 {
20                         if (lleft > 0)
21                         {
22                                 t = strcat(t, " ");
23                                 lleft = lleft - 1;
24                         }
25                 }
26                 else
27                 {
28                         for (j = i+1;j < strlen(s);j++)
29                                 //    ^^ this skips over the first character of a word, which
30                                 //       is ALWAYS part of the word
31                                 //       this is safe since if i+1 == strlen(s), i will become
32                                 //       strlen(s)-1 at the end of this block and the function
33                                 //       will terminate. A space can't be the first character we
34                                 //       read here, and neither can a \n be the start, since these
35                                 //       two cases have been handled above.
36                         {
37                                 c = substring(s, j, 1);
38                                 if (c == " ")
39                                         break;
40                                 if (c == "\\")
41                                         break;
42                                 // we need to keep this tempstring alive even if substring is
43                                 // called repeatedly, so call strcat even though we're not
44                                 // doing anything
45                                 t = strcat(t);
46                         }
47                         wlen = j - i;
48                         if (lleft < wlen)
49                         {
50                                 t = strcat(t, "\n");
51                                 lleft = l;
52                         }
53                         t = strcat(t, substring(s, i, wlen));
54                         lleft = lleft - wlen;
55                         i = j - 1;
56                 }
57         }
58         strunzone(s);
59         return t;
60
61 /*
62         string t;
63         string word;
64
65         float lleft;
66         float i;
67
68         float startidx;
69
70         startidx = 0;
71
72         t = "";
73         word = "";
74         for (i = 0;i < strlen(s);i++)
75         {
76                 c = substring(s, i, 1);
77                 forceflush = false;
78                 if (c == " ")
79                         dowhat = 0;
80                 else if (c == "\\" && substring(s, i + 1, 1) == "n")
81                         dowhat = 1;
82                 else
83                 {
84                         dowhat = 2;
85                         word = strcat(word, c);
86                 }
87
88                 if (dowhat < 2)
89                 {
90                         // a space may add some whitespace to the output, and flushes the word buffer
91                         if (word != "")
92                         {
93                                 if (lleft < strlen(word) + 1)
94                                 {
95                                         // add a newline
96                                         t = strcat(t, "\n");
97                                         lleft = l;
98                                 }
99                                 else
100                                 {
101                                         // otherwise just add a space if there's already text in
102                                         // this line
103                                         if (lleft != l)
104                                                 t = strcat(t, " ");
105                                 }
106                                 t = strcat(t, word);
107                                 word = "";
108                         }
109                         if (dowhat == 0)
110                         {
111                                 // if this is a double space, add the space
112                                 if (lleft)
113                                         t = strcat(t, " ");
114                         }
115                         else if (dowhat == 1)
116                         {
117                                 t = strcat(t, "\n");
118                                 lleft = l;
119                         }
120                 }
121                 // we need to keep these tempstrings alive even if substring is
122                 // called repeatedly, so call strcat even though we're not doing
123                 // anything
124                 t = strcat(t);
125                 word = strcat(word);
126         }
127         return t;
128 */
129
130 /*
131         string t;
132         string word;
133
134         float lleft;
135         float i;
136
137         float startidx;
138
139         startidx = 0;
140
141         t = "";
142
143         lleft = l;
144         for(i = 0; i <= strlen(s); ++i)
145         {
146                 if(i != strlen(s) && substring(s, i, 1) != " ")
147                 {
148                         // we need to keep this tempstring alive even if substring is
149                         // called repeatedly, so call strcat even though we're not doing
150                         // anything
151                         t = strcat(t);
152                         continue;
153                 }
154
155                 word = substring(s, startidx, i - startidx);
156                 startidx = i + 1;
157
158                 if(word == "+++")
159                 {
160                         t = strcat(t, "\n\n");
161                         lleft = l;
162                 }
163                 else if(!l || (strlen(word) < lleft))
164                 {
165                         if(lleft != l)
166                         {
167                                 t = strcat(t, " ");
168                                 lleft = lleft - 1;
169                         }
170                         t = strcat(t, word);
171                         lleft = lleft - strlen(word);
172                 }
173                 else
174                 {
175                         t = strcat(t, "\n", word);
176                         lleft = l - strlen(word);
177                 }
178         }
179         return t;
180 */
181 }
182
183 float dist_point_line(vector p, vector l0, vector ldir)
184 {
185         ldir = normalize(ldir);
186         
187         // remove the component in line direction
188         p = p - (p * ldir) * ldir;
189
190         // vlen of the remaining vector
191         return vlen(p);
192 }