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
27 const int LINE_BUF = 1000;
28 const char *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);
52 if (fabs(*p - ceil(*p)) < MAX_ROUND_ERROR) {
53 *p = static_cast<float>( ceil(*p));
56 if (fabs(*p - floor(*p)) < MAX_ROUND_ERROR) {
57 *p = static_cast<float>( floor(*p));
61 bool CBspPortal::Build(char *def, unsigned int pointCnt, bool bInverse)
66 point_count = pointCnt;
68 if (point_count < 3) {
72 point = new CBspPoint[point_count];
74 for (n = 0; n < point_count; n++) {
75 for (; *c != 0 && *c != '('; c++) {}
85 x = point_count - n - 1;
90 sscanf(c, "%f %f %f", &point[x].p[0], &point[x].p[1], &point[x].p[2]);
92 ClampFloat(&point[x].p[0]);
93 ClampFloat(&point[x].p[1]);
94 ClampFloat(&point[x].p[2]);
102 memset(this, 0, sizeof(CPortals));
105 CPortals::~CPortals()
110 void CPortals::Purge()
119 void CPortals::Load()
121 char buf[LINE_BUF + 1];
123 memset(buf, 0, LINE_BUF + 1);
127 globalOutputStream() << MSG_PREFIX << "Loading portal file " << fn << ".\n";
131 in = fopen(fn, "rt");
134 globalOutputStream() << " ERROR - could not open file.\n";
139 if (!fgets(buf, LINE_BUF, in)) {
142 globalOutputStream() << " ERROR - File ended prematurely.\n";
147 if (strncmp("PRT1", buf, 4) != 0) {
150 globalOutputStream() << " ERROR - File header indicates wrong file type (should be \"PRT1\").\n";
155 if (!fgets(buf, LINE_BUF, in)) {
158 globalOutputStream() << " ERROR - File ended prematurely.\n";
163 sscanf(buf, "%u", &node_count);
165 if (node_count > 0xFFFF) {
170 globalOutputStream() << " ERROR - Extreme number of nodes, aborting.\n";
175 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)) {
193 globalOutputStream() << " ERROR - File ended prematurely.\n";
198 unsigned int p_count2;
199 sscanf(buf, "%u", &p_count2);
201 node = new CBspNode[node_count];
204 for (i = 0; i < p_count; i++) {
205 if (!fgets(buf, LINE_BUF, in)) {
210 globalOutputStream() << " 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++) {
223 if (!fgets(buf, LINE_BUF, in)) {
228 globalOutputStream() << " ERROR - File ended prematurely.\n";
233 unsigned int dummy, node1;
234 sscanf(buf, "%u %u", &dummy, &node1);
236 node[node1].portal_count++;
239 for (i = 0; i < node_count; i++) {
240 node[i].portal = new CBspPortal[node[i].portal_count];
245 in = fopen(fn, "rt");
247 fgets(buf, LINE_BUF, in);
248 fgets(buf, LINE_BUF, in);
249 fgets(buf, LINE_BUF, in);
250 fgets(buf, LINE_BUF, in);
253 for (n = 0; n < p_count; n++) {
254 if (!fgets(buf, LINE_BUF, in)) {
259 globalOutputStream() << " ERROR - Could not find information for portal number " << n + 1 << " of "
265 unsigned int pCount, node1, node2;
266 sscanf(buf, "%u %u %u", &pCount, &node1, &node2);
268 if (!node[node1].AddPortal(buf, pCount, false)) {
273 globalOutputStream() << " ERROR - Information for portal number " << n + 1 << " of " << p_count
274 << " is not formatted correctly.\n";
279 if (!node[node2].AddPortal(buf, pCount, true)) {
284 globalOutputStream() << " ERROR - Information for portal number " << n + 1 << " of " << p_count
285 << " is not formatted correctly.\n";
291 for (n = 0; n < p_count2; n++) {
292 if (!fgets(buf, LINE_BUF, in)) {
297 globalOutputStream() << " ERROR - Could not find information for portal number " << n + 1 << " of "
303 unsigned int pCount, node1;
304 sscanf(buf, "%u %u", &pCount, &node1);
306 if (!node[node1].AddPortal(buf, pCount, false)) {
311 globalOutputStream() << " ERROR - Information for portal number " << n + 1 << " of " << p_count
312 << " is not formatted correctly.\n";
328 CBspNode::~CBspNode()
330 if (portal != NULL) {
335 bool CBspNode::AddPortal(char *def, unsigned int pointCnt, bool bInverse)
337 return portal[portal_next++].Build(def, pointCnt, bInverse);