region_map_v1.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. #include "region_map_v1.h"
  2. #include "../../common/Log.h"
  3. #include "../client.h"
  4. #include "../Spawn.h"
  5. #include "../LuaInterface.h"
  6. #include <boost/filesystem.hpp>
  7. extern LuaInterface* lua_interface;
  8. RegionMapV1::RegionMapV1() {
  9. mVersion = 1;
  10. }
  11. RegionMapV1::~RegionMapV1() {
  12. map<Region_Node*, ZBSP_Node*>::const_iterator itr;
  13. int region_num = 0;
  14. for (itr = Regions.begin(); itr != Regions.end();)
  15. {
  16. Region_Node* node = itr->first;
  17. ZBSP_Node* bsp_node = itr->second;
  18. map<Region_Node*, ZBSP_Node*>::const_iterator deleteItr = itr;
  19. itr++;
  20. Regions.erase(deleteItr);
  21. safe_delete(node);
  22. safe_delete_array(bsp_node);
  23. }
  24. Regions.clear();
  25. }
  26. WaterRegionType RegionMapV1::ReturnRegionType(const glm::vec3& location, int32 gridid) const {
  27. return BSPReturnRegionType(1, glm::vec3(location.x, location.y, location.z), gridid);
  28. }
  29. bool RegionMapV1::InWater(const glm::vec3& location, int32 gridid) const {
  30. return ReturnRegionType(location, gridid) == RegionTypeWater;
  31. }
  32. bool RegionMapV1::InLava(const glm::vec3& location, int32 gridid) const {
  33. return ReturnRegionType(location, gridid) == RegionTypeLava;
  34. }
  35. bool RegionMapV1::InLiquid(const glm::vec3& location) const {
  36. return InWater(location) || InLava(location);
  37. }
  38. bool RegionMapV1::InPvP(const glm::vec3& location) const {
  39. return ReturnRegionType(location) == RegionTypePVP;
  40. }
  41. bool RegionMapV1::InZoneLine(const glm::vec3& location) const {
  42. return ReturnRegionType(location) == RegionTypeZoneLine;
  43. }
  44. std::string RegionMapV1::TestFile(std::string testFile)
  45. {
  46. std::string tmpStr(testFile);
  47. std::size_t pos = tmpStr.find(".");
  48. if ( pos != testFile.npos )
  49. tmpStr = testFile.substr (0, pos);
  50. string tmpScript("RegionScripts/");
  51. tmpScript.append(mZoneNameLower);
  52. tmpScript.append("/" + tmpStr + ".lua");
  53. printf("File to test : %s\n",tmpScript.c_str());
  54. std::ifstream f(tmpScript.c_str());
  55. return f.good() ? tmpScript : string("");
  56. }
  57. bool RegionMapV1::Load(FILE* fp, std::string inZoneNameLwr, int32 version) {
  58. mZoneNameLower = string(inZoneNameLwr.c_str());
  59. uint32 region_size;
  60. if (fread(&region_size, sizeof(region_size), 1, fp) != 1) {
  61. return false;
  62. }
  63. LogWrite(REGION__DEBUG, 0, "RegionMap", "region count = %u", region_size);
  64. for (int i = 0; i < region_size; i++)
  65. {
  66. uint32 region_num;
  67. if (fread(&region_num, sizeof(region_num), 1, fp) != 1) {
  68. return false;
  69. }
  70. uint32 region_type;
  71. if (fread(&region_type, sizeof(region_type), 1, fp) != 1) {
  72. return false;
  73. }
  74. float x, y, z, dist;
  75. if (fread(&x, sizeof(x), 1, fp) != 1) {
  76. return false;
  77. }
  78. if (fread(&y, sizeof(y), 1, fp) != 1) {
  79. return false;
  80. }
  81. if (fread(&z, sizeof(z), 1, fp) != 1) {
  82. return false;
  83. }
  84. if (fread(&dist, sizeof(dist), 1, fp) != 1) {
  85. return false;
  86. }
  87. int8 strSize;
  88. char envName[256] = {""};
  89. char regionName[256] = {""};
  90. uint32 grid_id = 0;
  91. if ( version > 1 )
  92. {
  93. fread(&strSize, sizeof(int8), 1, fp);
  94. LogWrite(REGION__DEBUG, 7, "Region", "Region environment strSize = %u", strSize);
  95. if(strSize)
  96. {
  97. size_t len = fread(&envName, sizeof(char), strSize, fp);
  98. envName[len] = '\0';
  99. }
  100. LogWrite(REGION__DEBUG, 7, "Region", "Region environment file name = %s", envName);
  101. fread(&strSize, sizeof(int8), 1, fp);
  102. LogWrite(REGION__DEBUG, 7, "Region", "Region name strSize = %u", strSize);
  103. if(strSize)
  104. {
  105. size_t len = fread(&regionName, sizeof(char), strSize, fp);
  106. regionName[len] = '\0';
  107. }
  108. LogWrite(REGION__DEBUG, 7, "Region", "Region name file name = %s", regionName);
  109. if (fread(&grid_id, sizeof(grid_id), 1, fp) != 1) {
  110. return false;
  111. }
  112. }
  113. uint32 bsp_tree_size;
  114. if (fread(&bsp_tree_size, sizeof(bsp_tree_size), 1, fp) != 1) {
  115. return false;
  116. }
  117. LogWrite(REGION__DEBUG, 0, "RegionMap", "region x,y,z,dist = %f, %f, %f, %f, region bsp tree size: %u", x, y, z, dist, bsp_tree_size);
  118. ZBSP_Node* BSP_Root = new ZBSP_Node[bsp_tree_size];
  119. if (fread(BSP_Root, sizeof(ZBSP_Node), bsp_tree_size, fp) != bsp_tree_size) {
  120. LogWrite(REGION__ERROR, 0, "RegionMap", "Failed to load region.");
  121. return false;
  122. }
  123. Region_Node* tmpNode = new Region_Node;
  124. tmpNode->x = x;
  125. tmpNode->y = y;
  126. tmpNode->z = z;
  127. tmpNode->dist = dist;
  128. tmpNode->region_type = region_type;
  129. tmpNode->regionName = string(regionName);
  130. tmpNode->regionEnvFileName = string(envName);
  131. tmpNode->grid_id = grid_id;
  132. tmpNode->regionScriptName = string("");
  133. tmpNode->regionScriptName = TestFile(regionName);
  134. if ( tmpNode->regionScriptName.size() < 1 )
  135. {
  136. tmpNode->regionScriptName = TestFile(envName);
  137. }
  138. if ( tmpNode->regionScriptName.size() < 1 )
  139. {
  140. tmpNode->regionScriptName = TestFile("default");
  141. }
  142. Regions.insert(make_pair(tmpNode, BSP_Root));
  143. }
  144. fclose(fp);
  145. LogWrite(REGION__DEBUG, 0, "RegionMap", "completed load!");
  146. return true;
  147. }
  148. void RegionMapV1::IdentifyRegionsInGrid(Client *client, const glm::vec3 &location) const
  149. {
  150. map<Region_Node*, ZBSP_Node*>::const_iterator itr;
  151. int region_num = 0;
  152. int32 grid = 0;
  153. if (client->GetPlayer()->GetMap() != nullptr)
  154. {
  155. grid = client->GetPlayer()->GetMap()->GetGrid()->GetGridIDByLocation(location.x, location.y, location.z);
  156. }
  157. else
  158. client->SimpleMessage(CHANNEL_COLOR_RED, "No map to establish grid id, using grid id 0 (attempt match all).");
  159. client->Message(2, "Region check against location %f / %f / %f. Grid to try: %u, player grid is %u", location.x, location.y, location.z, grid, client->GetPlayer()->appearance.pos.grid_id);
  160. for (itr = Regions.begin(); itr != Regions.end(); itr++)
  161. {
  162. Region_Node *node = itr->first;
  163. ZBSP_Node *BSP_Root = itr->second;
  164. if (grid == 0 || node->grid_id == grid)
  165. {
  166. float x1 = node->x - location.x;
  167. float y1 = node->y - location.y;
  168. float z1 = node->z - location.z;
  169. float dist = sqrt(x1 *x1 + y1 *y1 + z1 *z1);
  170. glm::vec3 testLoc(location.x, location.y, location.z);
  171. if (dist <= node->dist)
  172. {
  173. WaterRegionType regionType = RegionTypeUntagged;
  174. if (node->region_type == ClassWaterRegion)
  175. regionType = BSPReturnRegionWaterRegion(node, BSP_Root, 1, testLoc, dist);
  176. else
  177. regionType = BSPReturnRegionTypeNode(node, BSP_Root, 1, testLoc, dist);
  178. if (regionType != RegionTypeNormal)
  179. {
  180. client->Message(CHANNEL_COLOR_YELLOW, "[DETECTED IN REGION %i] Region %i, GridID: %u, RegionName: %s, RegionEnvFileName: %s, distance: %f, X/Y/Z: %f / %f / %f. Script: %s", regionType, region_num, node->grid_id, node->regionName.c_str(), node->regionEnvFileName.c_str(),
  181. node->dist, node->x, node->y, node->z, node->regionScriptName.c_str());
  182. }
  183. else
  184. {
  185. client->Message(CHANNEL_COLOR_RED, "[IN DIST RANGE] Region %i, GridID: %u, RegionName: %s, RegionEnvFileName: %s, distance: %f, X/Y/Z: %f / %f / %f. Script: %s", region_num, node->grid_id, node->regionName.c_str(), node->regionEnvFileName.c_str(),
  186. node->dist, node->x, node->y, node->z, node->regionScriptName.c_str());
  187. }
  188. }
  189. else
  190. client->Message(CHANNEL_COLOR_RED, "[OUT OF RANGE] Region %i, GridID: %u, RegionName: %s, RegionEnvFileName: %s, distance: %f, X/Y/Z: %f / %f / %f. Script: %s", region_num, node->grid_id, node->regionName.c_str(), node->regionEnvFileName.c_str(),
  191. node->dist, node->x, node->y, node->z, node->regionScriptName.c_str());
  192. }
  193. else
  194. client->Message(CHANNEL_COLOR_RED, "[OUT OF GRID] Region %i, GridID: %u, RegionName: %s, RegionEnvFileName: %s, distance: %f, X/Y/Z: %f / %f / %f. Script: %s", region_num, node->grid_id, node->regionName.c_str(), node->regionEnvFileName.c_str(),
  195. node->dist, node->x, node->y, node->z, node->regionScriptName.c_str());
  196. region_num++;
  197. }
  198. }
  199. void RegionMapV1::MapRegionsNearSpawn(Spawn *spawn, Client *client) const
  200. {
  201. map<Region_Node*, ZBSP_Node*>::const_iterator itr;
  202. int region_num = 0;
  203. spawn->RegionMutex.writelock();
  204. glm::vec3 testLoc(spawn->GetX(), spawn->GetY(), spawn->GetZ());
  205. for (itr = Regions.begin(); itr != Regions.end(); itr++)
  206. {
  207. Region_Node *node = itr->first;
  208. ZBSP_Node *BSP_Root = itr->second;
  209. if (node->regionScriptName.size() < 1) // only track ones that are used with LUA scripting
  210. continue;
  211. float x1 = node->x - testLoc.x;
  212. float y1 = node->y - testLoc.y;
  213. float z1 = node->z - testLoc.z;
  214. float dist = sqrt(x1 *x1 + y1 *y1 + z1 *z1);
  215. if (dist <= node->dist)
  216. {
  217. WaterRegionType regionType = RegionTypeUntagged;
  218. if (node->region_type == ClassWaterRegion)
  219. regionType = BSPReturnRegionWaterRegion(node, BSP_Root, 1, testLoc, dist);
  220. else
  221. regionType = BSPReturnRegionTypeNode(node, BSP_Root, 1, testLoc, dist);
  222. if (regionType != RegionTypeNormal)
  223. {
  224. if (!spawn->InRegion(node, BSP_Root))
  225. {
  226. spawn->DeleteRegion(node, BSP_Root);
  227. std::map<Region_Node*, ZBSP_Node*> newMap;
  228. newMap.insert(make_pair(node, BSP_Root));
  229. Region_Status status;
  230. status.inRegion = true;
  231. status.regionType = regionType;
  232. int32 returnValue = 0;
  233. lua_interface->RunRegionScript(node->regionScriptName, "EnterRegion", spawn->GetZone(), spawn, regionType, &returnValue);
  234. status.timerTic = returnValue;
  235. status.lastTimerTic = returnValue ? Timer::GetCurrentTime2() : 0;
  236. spawn->Regions.insert(make_pair(newMap, status));
  237. if (client)
  238. client->Message(CHANNEL_COLOR_YELLOW, "[ENTER REGION %i %u] Region %i, GridID: %u, RegionName: %s, RegionEnvFileName: %s, distance: %f, X/Y/Z: %f / %f / %f. Script: %s", regionType, returnValue, region_num, node->grid_id, node->regionName.c_str(), node->regionEnvFileName.c_str(),
  239. node->dist, node->x, node->y, node->z, node->regionScriptName.c_str());
  240. }
  241. }
  242. else
  243. {
  244. if (spawn->InRegion(node, BSP_Root))
  245. {
  246. if (client)
  247. client->Message(CHANNEL_COLOR_RED, "[LEAVE REGION] Region %i, GridID: %u, RegionName: %s, RegionEnvFileName: %s, distance: %f, X/Y/Z: %f / %f / %f. Script: %s", region_num, node->grid_id, node->regionName.c_str(), node->regionEnvFileName.c_str(),
  248. node->dist, node->x, node->y, node->z, node->regionScriptName.c_str());
  249. WaterRegionType whatWasRegionType = (WaterRegionType) spawn->GetRegionType(node, BSP_Root);
  250. lua_interface->RunRegionScript(node->regionScriptName, "LeaveRegion", spawn->GetZone(), spawn, whatWasRegionType);
  251. }
  252. spawn->DeleteRegion(node, BSP_Root);
  253. std::map<Region_Node*, ZBSP_Node*> newMap;
  254. newMap.insert(make_pair(node, BSP_Root));
  255. Region_Status status;
  256. status.inRegion = false;
  257. status.timerTic = 0;
  258. status.lastTimerTic = 0;
  259. status.regionType = RegionTypeNormal;
  260. spawn->Regions.insert(make_pair(newMap, status));
  261. if (client)
  262. client->Message(CHANNEL_COLOR_RED, "[NEAR REGION] Region %i, GridID: %u, RegionName: %s, RegionEnvFileName: %s, distance: %f, X/Y/Z: %f / %f / %f. Script: %s", region_num, node->grid_id, node->regionName.c_str(), node->regionEnvFileName.c_str(),
  263. node->dist, node->x, node->y, node->z, node->regionScriptName.c_str());
  264. }
  265. }
  266. region_num++;
  267. }
  268. spawn->RegionMutex.releasewritelock();
  269. }
  270. void RegionMapV1::UpdateRegionsNearSpawn(Spawn *spawn, Client *client) const
  271. {
  272. map<map<Region_Node*, ZBSP_Node*>, Region_Status>::iterator testitr;
  273. int region_num = 0;
  274. spawn->RegionMutex.writelock();
  275. glm::vec3 testLoc(spawn->GetX(), spawn->GetY(), spawn->GetZ());
  276. map<Region_Node*, ZBSP_Node*> deleteNodes;
  277. for (testitr = spawn->Regions.begin(); testitr != spawn->Regions.end(); testitr++)
  278. {
  279. map<Region_Node*, ZBSP_Node*>::const_iterator actualItr = testitr->first.begin();
  280. Region_Node *node = actualItr->first;
  281. ZBSP_Node *BSP_Root = actualItr->second;
  282. float x1 = node->x - testLoc.x;
  283. float y1 = node->y - testLoc.y;
  284. float z1 = node->z - testLoc.z;
  285. float dist = sqrt(x1 *x1 + y1 *y1 + z1 *z1);
  286. if (dist <= node->dist)
  287. {
  288. WaterRegionType regionType = RegionTypeUntagged;
  289. if (node->region_type == ClassWaterRegion)
  290. regionType = BSPReturnRegionWaterRegion(node, BSP_Root, 1, testLoc, dist);
  291. else
  292. regionType = BSPReturnRegionTypeNode(node, BSP_Root, 1, testLoc, dist);
  293. if (regionType != RegionTypeNormal)
  294. {
  295. if (!testitr->second.inRegion)
  296. {
  297. testitr->second.inRegion = true;
  298. int32 returnValue = 0;
  299. lua_interface->RunRegionScript(node->regionScriptName, "EnterRegion", spawn->GetZone(), spawn, regionType, &returnValue);
  300. if (client)
  301. client->Message(CHANNEL_COLOR_YELLOW, "[ENTER RANGE %i %u] Region %i, GridID: %u, RegionName: %s, RegionEnvFileName: %s, distance: %f, X/Y/Z: %f / %f / %f. Script: %s", regionType, returnValue, region_num, node->grid_id, node->regionName.c_str(), node->regionEnvFileName.c_str(),
  302. node->dist, node->x, node->y, node->z, node->regionScriptName.c_str());
  303. testitr->second.timerTic = returnValue;
  304. testitr->second.lastTimerTic = returnValue ? Timer::GetCurrentTime2() : 0;
  305. }
  306. }
  307. else
  308. {
  309. if (testitr->second.inRegion)
  310. {
  311. testitr->second.inRegion = false;
  312. testitr->second.timerTic = 0;
  313. testitr->second.lastTimerTic = 0;
  314. if (client)
  315. client->Message(CHANNEL_COLOR_RED, "[LEAVE RANGE] Region %i, GridID: %u, RegionName: %s, RegionEnvFileName: %s, distance: %f, X/Y/Z: %f / %f / %f. Script: %s", region_num, node->grid_id, node->regionName.c_str(), node->regionEnvFileName.c_str(),
  316. node->dist, node->x, node->y, node->z, node->regionScriptName.c_str());
  317. WaterRegionType whatWasRegionType = (WaterRegionType) spawn->GetRegionType(node, BSP_Root);
  318. lua_interface->RunRegionScript(node->regionScriptName, "LeaveRegion", spawn->GetZone(), spawn, whatWasRegionType);
  319. }
  320. }
  321. }
  322. else
  323. {
  324. if (client)
  325. client->Message(CHANNEL_COLOR_RED, "[LEAVE RANGE - OOR] Region %i, GridID: %u, RegionName: %s, RegionEnvFileName: %s, distance: %f, X/Y/Z: %f / %f / %f. Script: %s", region_num, node->grid_id, node->regionName.c_str(), node->regionEnvFileName.c_str(),
  326. node->dist, node->x, node->y, node->z, node->regionScriptName.c_str());
  327. deleteNodes.insert(make_pair(node, BSP_Root));
  328. }
  329. region_num++;
  330. }
  331. map<Region_Node*, ZBSP_Node*>::const_iterator deleteItr;
  332. for (deleteItr = deleteNodes.begin(); deleteItr != deleteNodes.end(); deleteItr++)
  333. {
  334. Region_Node *tmpNode = deleteItr->first;
  335. ZBSP_Node *bspNode = deleteItr->second;
  336. spawn->DeleteRegion(tmpNode, bspNode);
  337. }
  338. spawn->RegionMutex.releasewritelock();
  339. }
  340. void RegionMapV1::TicRegionsNearSpawn(Spawn *spawn, Client *client) const
  341. {
  342. map<map<Region_Node*, ZBSP_Node*>, Region_Status>::iterator testitr;
  343. int region_num = 0;
  344. spawn->RegionMutex.writelock();
  345. for (testitr = spawn->Regions.begin(); testitr != spawn->Regions.end(); testitr++)
  346. {
  347. map<Region_Node*, ZBSP_Node*>::const_iterator actualItr = testitr->first.begin();
  348. Region_Node *node = actualItr->first;
  349. ZBSP_Node *BSP_Root = actualItr->second;
  350. if (testitr->second.timerTic && testitr->second.inRegion && Timer::GetCurrentTime2() >= (testitr->second.lastTimerTic + testitr->second.timerTic))
  351. {
  352. testitr->second.lastTimerTic = Timer::GetCurrentTime2();
  353. if (client)
  354. client->Message(CHANNEL_COLOR_RED, "[TICK] Region %i, GridID: %u, RegionName: %s, RegionEnvFileName: %s, distance: %f, X/Y/Z: %f / %f / %f. Script: %s", region_num, node->grid_id, node->regionName.c_str(), node->regionEnvFileName.c_str(),
  355. node->dist, node->x, node->y, node->z, node->regionScriptName.c_str());
  356. WaterRegionType whatWasRegionType = RegionTypeNormal; // default will be 0
  357. if (BSP_Root->special == -3)
  358. whatWasRegionType = RegionTypeLava; // 2
  359. else if (BSP_Root->special == 1)
  360. whatWasRegionType = RegionTypeWater; // 1
  361. int32 returnValue = 0;
  362. lua_interface->RunRegionScript(node->regionScriptName, "Tick", spawn->GetZone(), spawn, whatWasRegionType, &returnValue);
  363. if (returnValue == 1)
  364. {
  365. testitr->second.lastTimerTic = 0;
  366. testitr->second.timerTic = 0;
  367. }
  368. }
  369. region_num++;
  370. }
  371. spawn->RegionMutex.releasewritelock();
  372. }
  373. WaterRegionType RegionMapV1::BSPReturnRegionType(int32 node_number, const glm::vec3& location, int32 gridid) const {
  374. map<Region_Node*, ZBSP_Node*>::const_iterator itr;
  375. int region_num = 0;
  376. for (itr = Regions.begin(); itr != Regions.end(); itr++)
  377. {
  378. Region_Node* node = itr->first;
  379. // did not match grid id of current region, skip
  380. //if ( gridid > 0 && gridid != node->grid_id)
  381. // continue;
  382. ZBSP_Node* BSP_Root = itr->second;
  383. float x1 = node->x - location.x;
  384. float y1 = node->y - location.y;
  385. float z1 = node->z - location.z;
  386. float dist = sqrt(x1 * x1 + y1 * y1 + z1 * z1);
  387. #ifdef REGIONDEBUG
  388. printf("Region %i (%i) dist %f / node dist %f. NodeXYZ: %f %f %f, XYZ: %f %f %f.\n", region_num, node->region_type, dist, node->dist, node->x, node->y, node->z, location.x, location.y, location.z);
  389. #endif
  390. if (dist <= node->dist)
  391. {
  392. ZBSP_Node* BSP_Root = itr->second;
  393. WaterRegionType regionType = RegionTypeUntagged;
  394. if (node->region_type == ClassWaterRegion)
  395. regionType = BSPReturnRegionWaterRegion(node, BSP_Root, node_number, location, dist);
  396. else
  397. regionType = BSPReturnRegionTypeNode(node, BSP_Root, node_number, location, dist);
  398. if (regionType != RegionTypeNormal)
  399. return regionType;
  400. }
  401. region_num++;
  402. }
  403. return(RegionTypeNormal);
  404. }
  405. WaterRegionType RegionMapV1::BSPReturnRegionTypeNode(const Region_Node* region_node, const ZBSP_Node* BSP_Root, int32 node_number, const glm::vec3& location, float distToNode) const {
  406. const ZBSP_Node* current_node = &BSP_Root[node_number - 1];
  407. float distance;
  408. #ifdef REGIONDEBUG
  409. printf("left = %u, right %u\n", current_node->left, current_node->right);
  410. #endif
  411. if (region_node->region_type == ClassWaterRegion2)
  412. {
  413. distance = (location.x * current_node->normal[0]) +
  414. (location.y * current_node->normal[1]) +
  415. (location.z * current_node->normal[2]) +
  416. current_node->splitdistance;
  417. }
  418. else {
  419. distance = (location.x * current_node->normal[0]) +
  420. (location.y * current_node->normal[1]) +
  421. (location.z * current_node->normal[2]) -
  422. current_node->splitdistance;
  423. }
  424. float absDistance = distance;
  425. if (absDistance < 0.0f)
  426. absDistance *= -1.0f;
  427. float absSplitDist = current_node->splitdistance;
  428. if (absSplitDist < 0.0f)
  429. absSplitDist *= -1.0f;
  430. #ifdef REGIONDEBUG
  431. printf("distance = %f, normals: %f %f %f, location: %f %f %f, split distance: %f\n", distance, current_node->left, current_node->right, current_node->normal[0], current_node->normal[1], current_node->normal[2],
  432. location.x, location.y, location.z, current_node->splitdistance);
  433. #endif
  434. if ((current_node->left == -2) &&
  435. (current_node->right == -1 || current_node->right == -2)) {
  436. if (region_node->region_type == ClassWaterOcean || region_node->region_type == ClassWaterOcean2)
  437. {
  438. if ( region_node->region_type == ClassWaterOcean && current_node->right == -1 &&
  439. current_node->normal[1] >= 0.9f && distance > 0 )
  440. return RegionTypeWater;
  441. else
  442. return EstablishDistanceAtAngle(region_node, current_node, distance, absDistance, absSplitDist, true);
  443. }
  444. else
  445. {
  446. if (distance > 0)
  447. return(RegionTypeWater);
  448. else
  449. return RegionTypeNormal;
  450. }
  451. }
  452. else if ((region_node->region_type == ClassWaterOcean || region_node->region_type == ClassWaterOcean2) && current_node->normal[1] != 1.0f && current_node->normal[1] != -1.0f)
  453. {
  454. float fraction = abs(current_node->normal[0] * current_node->normal[2]);
  455. float diff = distToNode / region_node->dist;
  456. if (distance > 0)
  457. diff = distance * diff;
  458. #ifdef REGIONDEBUG
  459. printf("Diff: %f (%f + %f), fraction %f\n", diff, distToNode, distance, fraction);
  460. #endif
  461. if ((abs(diff) / 2.0f) > (absSplitDist * (1.0f / fraction)) * 2.0f)
  462. return RegionTypeNormal;
  463. }
  464. if (distance == 0.0f) {
  465. return(RegionTypeNormal);
  466. }
  467. if (distance > 0.0f) {
  468. #ifdef REGIONDEBUG
  469. printf("to left node %i\n", current_node->left);
  470. #endif
  471. if (current_node->left == -2)
  472. {
  473. switch(region_node->region_type)
  474. {
  475. case ClassWaterVolume:
  476. case ClassWaterOcean:
  477. return RegionTypeWater;
  478. break;
  479. case ClassWaterOcean2:
  480. return EstablishDistanceAtAngle(region_node, current_node, distance, absDistance, absSplitDist, false);
  481. break;
  482. case ClassWaterCavern:
  483. return EstablishDistanceAtAngle(region_node, current_node, distance, absDistance, absSplitDist, true);
  484. break;
  485. default:
  486. return RegionTypeNormal;
  487. break;
  488. }
  489. }
  490. else if (current_node->left == -1) {
  491. return(RegionTypeNormal);
  492. }
  493. return BSPReturnRegionTypeNode(region_node, BSP_Root, current_node->left + 1, location, distToNode);
  494. }
  495. #ifdef REGIONDEBUG
  496. printf("to right node %i, sign bit %i\n", current_node->right, signbit(current_node->normal[1]));
  497. #endif
  498. if (current_node->right == -1) {
  499. if (region_node->region_type == ClassWaterOcean2 && signbit(current_node->normal[1]) == 0 && absDistance < absSplitDist)
  500. return RegionTypeWater;
  501. else if ((region_node->region_type == ClassWaterOcean || region_node->region_type == ClassWaterOcean2) &&
  502. (current_node->normal[1] > 0.0f && distance < 0.0f && absDistance < absSplitDist))
  503. {
  504. return(RegionTypeWater);
  505. }
  506. return(RegionTypeNormal);
  507. }
  508. return BSPReturnRegionTypeNode(region_node, BSP_Root, current_node->right + 1, location, distToNode);
  509. }
  510. WaterRegionType RegionMapV1::BSPReturnRegionWaterRegion(const Region_Node* region_node, const ZBSP_Node* BSP_Root, int32 node_number, const glm::vec3& location, float distToNode) const {
  511. const ZBSP_Node* current_node = &BSP_Root[node_number - 1];
  512. float distance;
  513. #ifdef REGIONDEBUG
  514. printf("left = %u, right %u\n", current_node->left, current_node->right);
  515. #endif
  516. distance = (location.x * current_node->normal[0]) +
  517. (location.y * current_node->normal[1]) +
  518. (location.z * current_node->normal[2]) -
  519. current_node->splitdistance;
  520. #ifdef REGIONDEBUG
  521. printf("distance = %f, normals: %f %f %f, location: %f %f %f, split distance: %f\n", distance, current_node->left, current_node->right, current_node->normal[0], current_node->normal[1], current_node->normal[2],
  522. location.x, location.y, location.z, current_node->splitdistance);
  523. #endif
  524. if (distance > 0.0f) {
  525. #ifdef REGIONDEBUG
  526. printf("to left node %i\n", current_node->left);
  527. #endif
  528. if (current_node->left == -1) {
  529. return(RegionTypeNormal);
  530. }
  531. else if (current_node->left == -2) {
  532. switch(current_node->special)
  533. {
  534. case -3:
  535. return(RegionTypeLava);
  536. break;
  537. case 1:
  538. return(RegionTypeWater);
  539. break;
  540. default:
  541. return(RegionTypeUntagged);
  542. break;
  543. }
  544. }
  545. return BSPReturnRegionWaterRegion(region_node, BSP_Root, current_node->left + 1, location, distToNode);
  546. }
  547. #ifdef REGIONDEBUG
  548. printf("to right node %i, sign bit %i\n", current_node->right, signbit(current_node->normal[1]));
  549. #endif
  550. if (current_node->right == -1) {
  551. return(RegionTypeNormal);
  552. }
  553. return BSPReturnRegionWaterRegion(region_node, BSP_Root, current_node->right + 1, location, distToNode);
  554. }
  555. WaterRegionType RegionMapV1::EstablishDistanceAtAngle(const Region_Node* region_node, const ZBSP_Node* current_node, float distance, float absDistance, float absSplitDist, bool checkEdgedAngle) const {
  556. float fraction = abs(current_node->normal[0] * current_node->normal[2]);
  557. #ifdef REGIONDEBUG
  558. printf("Distcheck: %f < %f\n", absDistance, absSplitDist);
  559. #endif
  560. if (absDistance < absSplitDist &&
  561. (current_node->normal[0] >= 1.0f || current_node->normal[0] <= -1.0f ||
  562. (current_node->normal[1] >= .9f && distance < 0.0f) ||
  563. (current_node->normal[1] <= -.9f && distance > 0.0f)))
  564. {
  565. return RegionTypeWater;
  566. }
  567. else if (fraction > 0.0f && (region_node->region_type == ClassWaterOcean2 || checkEdgedAngle))
  568. {
  569. if (current_node->normal[2] >= 1.0f || current_node->normal[2] <= -1.0f)
  570. return RegionTypeNormal;
  571. else if (current_node->normal[1] == 0.0f && (current_node->normal[0] < -0.5f || current_node->normal[0] > 0.5f) &&
  572. ((abs(absDistance * current_node->normal[0]) / 2.0f) < ((abs(absSplitDist * (1.0f / fraction))))))
  573. {
  574. return RegionTypeWater;
  575. }
  576. else if (current_node->normal[1] == 0.0f && (current_node->normal[2] < -0.5f || current_node->normal[2] > 0.5f) &&
  577. ((abs(absDistance * current_node->normal[2]) / 2.0f) < ((abs(absSplitDist * (1.0f / fraction))))))
  578. {
  579. return RegionTypeWater;
  580. }
  581. }
  582. return RegionTypeNormal;
  583. }