]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/common/util.qc
attempt to fix "backslash at end of line" bug
[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;j < strlen(s);j++)
29                         {
30                                 c = substring(s, j, 1);
31                                 if (c == " ")
32                                         break;
33                                 if (c == "\\")
34                                         if(j+1 < strlen(s))
35                                                 break;
36                                 // we need to keep this tempstring alive even if substring is
37                                 // called repeatedly, so call strcat even though we're not
38                                 // doing anything
39                                 t = strcat(t);
40                         }
41                         wlen = j - i;
42                         if (lleft < wlen)
43                         {
44                                 t = strcat(t, "\n");
45                                 lleft = l;
46                         }
47                         t = strcat(t, substring(s, i, wlen));
48                         lleft = lleft - wlen;
49                         i = j - 1;
50                 }
51         }
52         strunzone(s);
53         return t;
54
55 /*
56         string t;
57         string word;
58
59         float lleft;
60         float i;
61
62         float startidx;
63
64         startidx = 0;
65
66         t = "";
67         word = "";
68         for (i = 0;i < strlen(s);i++)
69         {
70                 c = substring(s, i, 1);
71                 forceflush = false;
72                 if (c == " ")
73                         dowhat = 0;
74                 else if (c == "\\" && substring(s, i + 1, 1) == "n")
75                         dowhat = 1;
76                 else
77                 {
78                         dowhat = 2;
79                         word = strcat(word, c);
80                 }
81
82                 if (dowhat < 2)
83                 {
84                         // a space may add some whitespace to the output, and flushes the word buffer
85                         if (word != "")
86                         {
87                                 if (lleft < strlen(word) + 1)
88                                 {
89                                         // add a newline
90                                         t = strcat(t, "\n");
91                                         lleft = l;
92                                 }
93                                 else
94                                 {
95                                         // otherwise just add a space if there's already text in
96                                         // this line
97                                         if (lleft != l)
98                                                 t = strcat(t, " ");
99                                 }
100                                 t = strcat(t, word);
101                                 word = "";
102                         }
103                         if (dowhat == 0)
104                         {
105                                 // if this is a double space, add the space
106                                 if (lleft)
107                                         t = strcat(t, " ");
108                         }
109                         else if (dowhat == 1)
110                         {
111                                 t = strcat(t, "\n");
112                                 lleft = l;
113                         }
114                 }
115                 // we need to keep these tempstrings alive even if substring is
116                 // called repeatedly, so call strcat even though we're not doing
117                 // anything
118                 t = strcat(t);
119                 word = strcat(word);
120         }
121         return t;
122 */
123
124 /*
125         string t;
126         string word;
127
128         float lleft;
129         float i;
130
131         float startidx;
132
133         startidx = 0;
134
135         t = "";
136
137         lleft = l;
138         for(i = 0; i <= strlen(s); ++i)
139         {
140                 if(i != strlen(s) && substring(s, i, 1) != " ")
141                 {
142                         // we need to keep this tempstring alive even if substring is
143                         // called repeatedly, so call strcat even though we're not doing
144                         // anything
145                         t = strcat(t);
146                         continue;
147                 }
148
149                 word = substring(s, startidx, i - startidx);
150                 startidx = i + 1;
151
152                 if(word == "+++")
153                 {
154                         t = strcat(t, "\n\n");
155                         lleft = l;
156                 }
157                 else if(!l || (strlen(word) < lleft))
158                 {
159                         if(lleft != l)
160                         {
161                                 t = strcat(t, " ");
162                                 lleft = lleft - 1;
163                         }
164                         t = strcat(t, word);
165                         lleft = lleft - strlen(word);
166                 }
167                 else
168                 {
169                         t = strcat(t, "\n", word);
170                         lleft = l - strlen(word);
171                 }
172         }
173         return t;
174 */
175 }
176
177 float dist_point_line(vector p, vector l0, vector ldir)
178 {
179         ldir = normalize(ldir);
180         
181         // remove the component in line direction
182         p = p - (p * ldir) * ldir;
183
184         // vlen of the remaining vector
185         return vlen(p);
186 }