region_map_v1.cpp 30 KB

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