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
25 #define MSG_PREFIX "bobToolz plugin: "
27 // these classes are far less of a mess than my code was,
28 // thanq to G.DeWan 4 the prtview source on which it was based
30 CBspPortal::CBspPortal()
32 memset(this, 0, sizeof(CBspPortal));
35 CBspPortal::~CBspPortal()
40 void ClampFloat(float* p)
43 double frac = modf(*p, &i);
48 if(fabs(*p - ceil(*p)) < MAX_ROUND_ERROR)
51 if(fabs(*p - floor(*p)) < MAX_ROUND_ERROR)
55 bool CBspPortal::Build(char *def, unsigned int pointCnt, bool bInverse)
60 point_count = pointCnt;
65 point = new CBspPoint[point_count];
67 for(n = 0; n < point_count; n++)
69 for(; *c != 0 && *c != '('; c++);
78 x = point_count - n - 1;
82 sscanf(c, "%f %f %f", &point[x].p[0], &point[x].p[1], &point[x].p[2]);
84 ClampFloat(&point[x].p[0]);
85 ClampFloat(&point[x].p[1]);
86 ClampFloat(&point[x].p[2]);
94 memset(this, 0, sizeof(CPortals));
102 void CPortals::Purge()
110 void CPortals::Load()
112 char buf[LINE_BUF+1];
114 memset(buf, 0, LINE_BUF + 1);
118 Sys_Printf(MSG_PREFIX "Loading portal file %s.\n", fn);
122 in = fopen(fn, "rt");
126 Sys_Printf(" ERROR - could not open file.\n");
131 if(!fgets(buf, LINE_BUF, in))
135 Sys_Printf(" ERROR - File ended prematurely.\n");
140 if(strncmp("PRT1", buf, 4) != 0)
144 Sys_Printf(" ERROR - File header indicates wrong file type (should be \"PRT1\").\n");
149 if(!fgets(buf, LINE_BUF, in))
153 Sys_Printf(" ERROR - File ended prematurely.\n");
158 sscanf(buf, "%u", &node_count);
160 if(node_count > 0xFFFF)
166 Sys_Printf(" ERROR - Extreme number of nodes, aborting.\n");
171 if(!fgets(buf, LINE_BUF, in))
177 Sys_Printf(" ERROR - File ended prematurely.\n");
182 unsigned int p_count;
183 sscanf(buf, "%u", &p_count);
185 if(!fgets(buf, LINE_BUF, in))
191 Sys_Printf(" ERROR - File ended prematurely.\n");
196 unsigned int p_count2;
197 sscanf(buf, "%u", &p_count2);
199 node = new CBspNode[node_count];
202 for(i = 0; i < p_count; i++)
204 if(!fgets(buf, LINE_BUF, in))
210 Sys_Printf(" ERROR - File ended prematurely.\n");
215 unsigned int dummy, node1, node2;
216 sscanf(buf, "%u %u %u", &dummy, &node1, &node2);
218 node[node1].portal_count++;
219 node[node2].portal_count++;
222 for(i = 0; i < p_count2; i++)
224 if(!fgets(buf, LINE_BUF, in))
230 Sys_Printf(" ERROR - File ended prematurely.\n");
235 unsigned int dummy, node1;
236 sscanf(buf, "%u %u", &dummy, &node1);
238 node[node1].portal_count++;
241 for(i = 0; i < node_count; i++)
242 node[i].portal = new CBspPortal[node[i].portal_count];
246 in = fopen(fn, "rt");
248 fgets(buf, LINE_BUF, in);
249 fgets(buf, LINE_BUF, in);
250 fgets(buf, LINE_BUF, in);
251 fgets(buf, LINE_BUF, in);
254 for(n = 0; n < p_count; n++)
256 if(!fgets(buf, LINE_BUF, in))
262 Sys_Printf(" ERROR - Could not find information for portal number %d of %d.\n", n + 1, p_count);
267 unsigned int pCount, node1, node2;
268 sscanf(buf, "%u %u %u", &pCount, &node1, &node2);
270 if(!node[node1].AddPortal(buf, pCount, FALSE))
276 Sys_Printf(" ERROR - Information for portal number %d of %d is not formatted correctly.\n", n + 1, p_count);
281 if(!node[node2].AddPortal(buf, pCount, TRUE))
287 Sys_Printf(" ERROR - Information for portal number %d of %d is not formatted correctly.\n", n + 1, p_count);
293 for(n = 0; n < p_count2; n++)
295 if(!fgets(buf, LINE_BUF, in))
301 Sys_Printf(" ERROR - Could not find information for portal number %d of %d.\n", n + 1, p_count);
306 unsigned int pCount, node1;
307 sscanf(buf, "%u %u", &pCount, &node1);
309 if(!node[node1].AddPortal(buf, pCount, FALSE))
315 Sys_Printf(" ERROR - Information for portal number %d of %d is not formatted correctly.\n", n + 1, p_count);
331 CBspNode::~CBspNode()
337 bool CBspNode::AddPortal(char *def, unsigned int pointCnt, bool bInverse)
339 return portal[portal_next++].Build(def, pointCnt, bInverse);