region_map_v1.cpp 25 KB

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