2 BobToolz plugin for GtkRadiant
3 Copyright (C) 2001 Gordon Biggans
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 const int LINE_BUF = 1000;
29 const char *MSG_PREFIX = "bobToolz plugin: ";
31 // these classes are far less of a mess than my code was,
32 // thanq to G.DeWan 4 the prtview source on which it was based
34 CBspPortal::CBspPortal()
36 memset(this, 0, sizeof(CBspPortal));
39 CBspPortal::~CBspPortal()
44 void ClampFloat(float *p)
47 double frac = modf(*p, &i);
53 if (fabs(*p - ceil(*p)) < MAX_ROUND_ERROR) {
54 *p = static_cast<float>( ceil(*p));
57 if (fabs(*p - floor(*p)) < MAX_ROUND_ERROR) {
58 *p = static_cast<float>( floor(*p));
62 bool CBspPortal::Build(char *def, unsigned int pointCnt, bool bInverse)
67 point_count = pointCnt;
69 if (point_count < 3) {
73 point = new CBspPoint[point_count];
75 for (n = 0; n < point_count; n++) {
76 for (; *c != 0 && *c != '('; c++) {}
86 x = point_count - n - 1;
91 sscanf(c, "%f %f %f", &point[x].p[0], &point[x].p[1], &point[x].p[2]);
93 ClampFloat(&point[x].p[0]);
94 ClampFloat(&point[x].p[1]);
95 ClampFloat(&point[x].p[2]);
103 memset(this, 0, sizeof(CPortals));
106 CPortals::~CPortals()
111 void CPortals::Purge()
120 void CPortals::Load()
122 char buf[LINE_BUF + 1];
124 memset(buf, 0, LINE_BUF + 1);
128 globalOutputStream() << MSG_PREFIX << "Loading portal file " << fn << ".\n";
132 in = fopen(fn, "rt");
135 globalOutputStream() << " ERROR - could not open file.\n";
140 if (!fgets(buf, LINE_BUF, in)) {
143 globalOutputStream() << " ERROR - File ended prematurely.\n";
148 if (strncmp("PRT1", buf, 4) != 0) {
151 globalOutputStream() << " ERROR - File header indicates wrong file type (should be \"PRT1\").\n";
156 if (!fgets(buf, LINE_BUF, in)) {
159 globalOutputStream() << " ERROR - File ended prematurely.\n";
164 sscanf(buf, "%u", &node_count);
166 if (node_count > 0xFFFF) {
171 globalOutputStream() << " ERROR - Extreme number of nodes, aborting.\n";
176 if (!fgets(buf, LINE_BUF, in)) {
181 globalOutputStream() << " ERROR - File ended prematurely.\n";
186 unsigned int p_count;
187 sscanf(buf, "%u", &p_count);
189 if (!fgets(buf, LINE_BUF, in)) {
194 globalOutputStream() << " ERROR - File ended prematurely.\n";
199 unsigned int p_count2;
200 sscanf(buf, "%u", &p_count2);
202 node = new CBspNode[node_count];
205 for (i = 0; i < p_count; i++) {
206 if (!fgets(buf, LINE_BUF, in)) {
211 globalOutputStream() << " ERROR - File ended prematurely.\n";
216 unsigned int dummy, node1, node2;
217 sscanf(buf, "%u %u %u", &dummy, &node1, &node2);
219 node[node1].portal_count++;
220 node[node2].portal_count++;
223 for (i = 0; i < p_count2; i++) {
224 if (!fgets(buf, LINE_BUF, in)) {
229 globalOutputStream() << " ERROR - File ended prematurely.\n";
234 unsigned int dummy, node1;
235 sscanf(buf, "%u %u", &dummy, &node1);
237 node[node1].portal_count++;
240 for (i = 0; i < node_count; i++) {
241 node[i].portal = new CBspPortal[node[i].portal_count];
246 in = fopen(fn, "rt");
248 assert(fgets(buf, LINE_BUF, in));
249 assert(fgets(buf, LINE_BUF, in));
250 assert(fgets(buf, LINE_BUF, in));
251 assert(fgets(buf, LINE_BUF, in));
254 for (n = 0; n < p_count; n++) {
255 if (!fgets(buf, LINE_BUF, in)) {
260 globalOutputStream() << " ERROR - Could not find information for portal number " << n + 1 << " of "
266 unsigned int pCount, node1, node2;
267 sscanf(buf, "%u %u %u", &pCount, &node1, &node2);
269 if (!node[node1].AddPortal(buf, pCount, false)) {
274 globalOutputStream() << " ERROR - Information for portal number " << n + 1 << " of " << p_count
275 << " is not formatted correctly.\n";
280 if (!node[node2].AddPortal(buf, pCount, true)) {
285 globalOutputStream() << " ERROR - Information for portal number " << n + 1 << " of " << p_count
286 << " is not formatted correctly.\n";
292 for (n = 0; n < p_count2; n++) {
293 if (!fgets(buf, LINE_BUF, in)) {
298 globalOutputStream() << " ERROR - Could not find information for portal number " << n + 1 << " of "
304 unsigned int pCount, node1;
305 sscanf(buf, "%u %u", &pCount, &node1);
307 if (!node[node1].AddPortal(buf, pCount, false)) {
312 globalOutputStream() << " ERROR - Information for portal number " << n + 1 << " of " << p_count
313 << " is not formatted correctly.\n";
329 CBspNode::~CBspNode()
331 if (portal != NULL) {
336 bool CBspNode::AddPortal(char *def, unsigned int pointCnt, bool bInverse)
338 return portal[portal_next++].Build(def, pointCnt, bInverse);