cportals.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. BobToolz plugin for GtkRadiant
  3. Copyright (C) 2001 Gordon Biggans
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include "StdAfx.h"
  17. #include "CPortals.h"
  18. //#include "misc.h"
  19. #define LINE_BUF 1000
  20. #define MSG_PREFIX "bobToolz plugin: "
  21. // these classes are far less of a mess than my code was,
  22. // thanq to G.DeWan 4 the prtview source on which it was based
  23. CBspPortal::CBspPortal()
  24. {
  25. memset(this, 0, sizeof(CBspPortal));
  26. }
  27. CBspPortal::~CBspPortal()
  28. {
  29. delete[] point;
  30. }
  31. void ClampFloat(float* p)
  32. {
  33. double i;
  34. double frac = modf(*p, &i);
  35. if(!frac)
  36. return;
  37. if(fabs(*p - ceil(*p)) < MAX_ROUND_ERROR)
  38. *p = static_cast<float>(ceil(*p));
  39. if(fabs(*p - floor(*p)) < MAX_ROUND_ERROR)
  40. *p = static_cast<float>(floor(*p));
  41. }
  42. bool CBspPortal::Build(char *def, unsigned int pointCnt, bool bInverse)
  43. {
  44. char *c = def;
  45. unsigned int n;
  46. point_count = pointCnt;
  47. if(point_count < 3)
  48. return FALSE;
  49. point = new CBspPoint[point_count];
  50. for(n = 0; n < point_count; n++)
  51. {
  52. for(; *c != 0 && *c != '('; c++);
  53. if(*c == 0)
  54. return FALSE;
  55. c++;
  56. int x;
  57. if(bInverse)
  58. x = point_count - n - 1;
  59. else
  60. x = n;
  61. sscanf(c, "%f %f %f", &point[x].p[0], &point[x].p[1], &point[x].p[2]);
  62. ClampFloat(&point[x].p[0]);
  63. ClampFloat(&point[x].p[1]);
  64. ClampFloat(&point[x].p[2]);
  65. }
  66. return TRUE;
  67. }
  68. CPortals::CPortals()
  69. {
  70. memset(this, 0, sizeof(CPortals));
  71. }
  72. CPortals::~CPortals()
  73. {
  74. Purge();
  75. }
  76. void CPortals::Purge()
  77. {
  78. if(node)
  79. delete[] node;
  80. node = NULL;
  81. node_count = 0;
  82. }
  83. void CPortals::Load()
  84. {
  85. char buf[LINE_BUF+1];
  86. memset(buf, 0, LINE_BUF + 1);
  87. Purge();
  88. Sys_Printf(MSG_PREFIX "Loading portal file %s.\n", fn);
  89. FILE *in;
  90. in = fopen(fn, "rt");
  91. if(in == NULL)
  92. {
  93. Sys_Printf(" ERROR - could not open file.\n");
  94. return;
  95. }
  96. if(!fgets(buf, LINE_BUF, in))
  97. {
  98. fclose(in);
  99. Sys_Printf(" ERROR - File ended prematurely.\n");
  100. return;
  101. }
  102. if(strncmp("PRT1", buf, 4) != 0)
  103. {
  104. fclose(in);
  105. Sys_Printf(" ERROR - File header indicates wrong file type (should be \"PRT1\").\n");
  106. return;
  107. }
  108. if(!fgets(buf, LINE_BUF, in))
  109. {
  110. fclose(in);
  111. Sys_Printf(" ERROR - File ended prematurely.\n");
  112. return;
  113. }
  114. sscanf(buf, "%u", &node_count);
  115. if(node_count > 0xFFFF)
  116. {
  117. fclose(in);
  118. node_count = 0;
  119. Sys_Printf(" ERROR - Extreme number of nodes, aborting.\n");
  120. return;
  121. }
  122. if(!fgets(buf, LINE_BUF, in))
  123. {
  124. fclose(in);
  125. node_count = 0;
  126. Sys_Printf(" ERROR - File ended prematurely.\n");
  127. return;
  128. }
  129. unsigned int p_count;
  130. sscanf(buf, "%u", &p_count);
  131. if(!fgets(buf, LINE_BUF, in))
  132. {
  133. fclose(in);
  134. node_count = 0;
  135. Sys_Printf(" ERROR - File ended prematurely.\n");
  136. return;
  137. }
  138. unsigned int p_count2;
  139. sscanf(buf, "%u", &p_count2);
  140. node = new CBspNode[node_count];
  141. unsigned int i;
  142. for(i = 0; i < p_count; i++)
  143. {
  144. if(!fgets(buf, LINE_BUF, in))
  145. {
  146. fclose(in);
  147. node_count = 0;
  148. Sys_Printf(" ERROR - File ended prematurely.\n");
  149. return;
  150. }
  151. unsigned int dummy, node1, node2;
  152. sscanf(buf, "%u %u %u", &dummy, &node1, &node2);
  153. node[node1].portal_count++;
  154. node[node2].portal_count++;
  155. }
  156. for(i = 0; i < p_count2; i++)
  157. {
  158. if(!fgets(buf, LINE_BUF, in))
  159. {
  160. fclose(in);
  161. node_count = 0;
  162. Sys_Printf(" ERROR - File ended prematurely.\n");
  163. return;
  164. }
  165. unsigned int dummy, node1;
  166. sscanf(buf, "%u %u", &dummy, &node1);
  167. node[node1].portal_count++;
  168. }
  169. for(i = 0; i < node_count; i++)
  170. node[i].portal = new CBspPortal[node[i].portal_count];
  171. fclose(in);
  172. in = fopen(fn, "rt");
  173. fgets(buf, LINE_BUF, in);
  174. fgets(buf, LINE_BUF, in);
  175. fgets(buf, LINE_BUF, in);
  176. fgets(buf, LINE_BUF, in);
  177. unsigned int n;
  178. for(n = 0; n < p_count; n++)
  179. {
  180. if(!fgets(buf, LINE_BUF, in))
  181. {
  182. fclose(in);
  183. Purge();
  184. Sys_Printf(" ERROR - Could not find information for portal number %d of %d.\n", n + 1, p_count);
  185. return;
  186. }
  187. unsigned int pCount, node1, node2;
  188. sscanf(buf, "%u %u %u", &pCount, &node1, &node2);
  189. if(!node[node1].AddPortal(buf, pCount, FALSE))
  190. {
  191. fclose(in);
  192. Purge();
  193. Sys_Printf(" ERROR - Information for portal number %d of %d is not formatted correctly.\n", n + 1, p_count);
  194. return;
  195. }
  196. if(!node[node2].AddPortal(buf, pCount, TRUE))
  197. {
  198. fclose(in);
  199. Purge();
  200. Sys_Printf(" ERROR - Information for portal number %d of %d is not formatted correctly.\n", n + 1, p_count);
  201. return;
  202. }
  203. }
  204. for(n = 0; n < p_count2; n++)
  205. {
  206. if(!fgets(buf, LINE_BUF, in))
  207. {
  208. fclose(in);
  209. Purge();
  210. Sys_Printf(" ERROR - Could not find information for portal number %d of %d.\n", n + 1, p_count);
  211. return;
  212. }
  213. unsigned int pCount, node1;
  214. sscanf(buf, "%u %u", &pCount, &node1);
  215. if(!node[node1].AddPortal(buf, pCount, FALSE))
  216. {
  217. fclose(in);
  218. Purge();
  219. Sys_Printf(" ERROR - Information for portal number %d of %d is not formatted correctly.\n", n + 1, p_count);
  220. return;
  221. }
  222. }
  223. fclose(in);
  224. }
  225. CBspNode::CBspNode()
  226. {
  227. portal = NULL;
  228. portal_count = 0;
  229. portal_next = 0;
  230. }
  231. CBspNode::~CBspNode()
  232. {
  233. if(portal != NULL)
  234. delete[] portal;
  235. }
  236. bool CBspNode::AddPortal(char *def, unsigned int pointCnt, bool bInverse)
  237. {
  238. return portal[portal_next++].Build(def, pointCnt, bInverse);
  239. }