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 #define MSG_PREFIX "bobToolz plugin: "
30 // these classes are far less of a mess than my code was,
31 // thanq to G.DeWan 4 the prtview source on which it was based
33 CBspPortal::CBspPortal()
35 memset(this, 0, sizeof(CBspPortal));
38 CBspPortal::~CBspPortal()
43 void ClampFloat(float* p)
46 double frac = modf(*p, &i);
51 if(fabs(*p - ceil(*p)) < MAX_ROUND_ERROR)
52 *p = static_cast<float>(ceil(*p));
54 if(fabs(*p - floor(*p)) < MAX_ROUND_ERROR)
55 *p = static_cast<float>(floor(*p));
58 bool CBspPortal::Build(char *def, unsigned int pointCnt, bool bInverse)
63 point_count = pointCnt;
68 point = new CBspPoint[point_count];
70 for(n = 0; n < point_count; n++)
72 for(; *c != 0 && *c != '('; c++);
81 x = point_count - n - 1;
85 sscanf(c, "%f %f %f", &point[x].p[0], &point[x].p[1], &point[x].p[2]);
87 ClampFloat(&point[x].p[0]);
88 ClampFloat(&point[x].p[1]);
89 ClampFloat(&point[x].p[2]);
97 memset(this, 0, sizeof(CPortals));
100 CPortals::~CPortals()
105 void CPortals::Purge()
113 void CPortals::Load()
115 char buf[LINE_BUF+1];
117 memset(buf, 0, LINE_BUF + 1);
121 globalOutputStream() << MSG_PREFIX "Loading portal file " << fn << ".\n";
125 in = fopen(fn, "rt");
129 globalOutputStream() << " ERROR - could not open file.\n";
134 if(!fgets(buf, LINE_BUF, in))
138 globalOutputStream() << " ERROR - File ended prematurely.\n";
143 if(strncmp("PRT1", buf, 4) != 0)
147 globalOutputStream() << " ERROR - File header indicates wrong file type (should be \"PRT1\").\n";
152 if(!fgets(buf, LINE_BUF, in))
156 globalOutputStream() << " ERROR - File ended prematurely.\n";
161 sscanf(buf, "%u", &node_count);
163 if(node_count > 0xFFFF)
169 globalOutputStream() << " ERROR - Extreme number of nodes, aborting.\n";
174 if(!fgets(buf, LINE_BUF, in))
180 globalOutputStream() << " ERROR - File ended prematurely.\n";
185 unsigned int p_count;
186 sscanf(buf, "%u", &p_count);
188 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++)
207 if(!fgets(buf, LINE_BUF, in))
213 globalOutputStream() << " ERROR - File ended prematurely.\n";
218 unsigned int dummy, node1, node2;
219 sscanf(buf, "%u %u %u", &dummy, &node1, &node2);
221 node[node1].portal_count++;
222 node[node2].portal_count++;
225 for(i = 0; i < p_count2; i++)
227 if(!fgets(buf, LINE_BUF, in))
233 globalOutputStream() << " ERROR - File ended prematurely.\n";
238 unsigned int dummy, node1;
239 sscanf(buf, "%u %u", &dummy, &node1);
241 node[node1].portal_count++;
244 for(i = 0; i < node_count; i++)
245 node[i].portal = new CBspPortal[node[i].portal_count];
249 in = fopen(fn, "rt");
251 fgets(buf, LINE_BUF, in);
252 fgets(buf, LINE_BUF, in);
253 fgets(buf, LINE_BUF, in);
254 fgets(buf, LINE_BUF, in);
257 for(n = 0; n < p_count; n++)
259 if(!fgets(buf, LINE_BUF, in))
265 globalOutputStream() << " ERROR - Could not find information for portal number " << n + 1 << " of " << p_count << ".\n";
270 unsigned int pCount, node1, node2;
271 sscanf(buf, "%u %u %u", &pCount, &node1, &node2);
273 if(!node[node1].AddPortal(buf, pCount, false))
279 globalOutputStream() << " ERROR - Information for portal number " << n + 1 << " of " << p_count << " is not formatted correctly.\n";
284 if(!node[node2].AddPortal(buf, pCount, true))
290 globalOutputStream() << " ERROR - Information for portal number " << n + 1 << " of " << p_count << " is not formatted correctly.\n";
296 for(n = 0; n < p_count2; n++)
298 if(!fgets(buf, LINE_BUF, in))
304 globalOutputStream() << " ERROR - Could not find information for portal number " << n + 1 << " of " << p_count << ".\n";
309 unsigned int pCount, node1;
310 sscanf(buf, "%u %u", &pCount, &node1);
312 if(!node[node1].AddPortal(buf, pCount, false))
318 globalOutputStream() << " ERROR - Information for portal number " << n + 1 << " of " << p_count << " is not formatted correctly.\n";
334 CBspNode::~CBspNode()
340 bool CBspNode::AddPortal(char *def, unsigned int pointCnt, bool bInverse)
342 return portal[portal_next++].Build(def, pointCnt, bInverse);