PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/EQEmuServer/utils/apathing/load_db.cpp

http://projecteqemu.googlecode.com/
C++ | 276 lines | 208 code | 51 blank | 17 comment | 49 complexity | 4882220a8ff43b53bf242add8a32f4c9 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. Fear Pathing generation utility.
  3. (c) 2005 Father Nitwit
  4. */
  5. #include "../common/types.h"
  6. #include "../zone/map.h"
  7. #include "../common/rdtsc.h"
  8. #include "quadtree.h"
  9. #include "apathing.h"
  10. #include <stdio.h>
  11. #include <mysql.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <gd.h>
  15. bool load_paths_from_db(MYSQL *m, Map *map, const char *zone, list<PathGraph*> &db_paths, list<PathNode*> &end_points) {
  16. char query[512];
  17. sprintf(query,
  18. "SELECT x,y,z,gridid FROM grid_entries,zone "
  19. "WHERE zone.zoneidnumber=zoneid AND short_name='%s' "
  20. "ORDER BY gridid,number", zone);
  21. if(mysql_query(m, query) != 0) {
  22. printf("Unable to query: %s\n", mysql_error(m));
  23. return(false);
  24. }
  25. MYSQL_RES *res = mysql_store_result(m);
  26. if(res == NULL) {
  27. printf("Unable to store res: %s\n", mysql_error(m));
  28. return(false);
  29. }
  30. MYSQL_ROW row;
  31. PathNode *cur = NULL, *last = NULL, *first = NULL;
  32. PathGraph *g = NULL;
  33. int cur_g = -1,last_g = -1;
  34. // int lid = 0;
  35. while((row = mysql_fetch_row(res))) {
  36. last = cur;
  37. cur = new PathNode;
  38. // cur->load_id = lid++;
  39. cur->x = atof(row[0]);
  40. cur->y = atof(row[1]);
  41. cur->z = atof(row[2]);
  42. cur_g = atoi(row[3]);
  43. if(cur_g != last_g) {
  44. if(g != NULL) {
  45. //if we have a first and last node for this path
  46. //and they are not the same node, try to connect them.
  47. if(first != NULL && last != NULL && first != last && last->Dist2(first) < ENDPOINT_CONNECT_MAX_DISTANCE*ENDPOINT_CONNECT_MAX_DISTANCE) {
  48. if(CheckLOS(map, last, first))
  49. g->add_edge(last, first);
  50. }
  51. #ifdef LINK_PATH_ENDPOINTS
  52. if(first != last && last != NULL)
  53. end_points.push_back(last);
  54. #endif
  55. db_paths.push_back(g);
  56. }
  57. g = new PathGraph();
  58. first = cur;
  59. last_g = cur_g;
  60. last = NULL;
  61. }
  62. g->nodes.push_back(cur);
  63. #ifdef LINK_PATH_ENDPOINTS
  64. //this is a begining point
  65. if(last == NULL)
  66. end_points.push_back(cur);
  67. #endif
  68. if(last != NULL) {
  69. #ifdef SPLIT_INVALID_PATHS
  70. if(CheckLOS(map, last, cur)) {
  71. g->edges.push_back(new PathEdge(last, cur));
  72. } else {
  73. //no LOS, split the path into two
  74. load_split_paths++;
  75. last_g = -1; //tell this thing to start over
  76. }
  77. #else
  78. g->edges.push_back(new PathEdge(last, cur));
  79. #endif
  80. }
  81. }
  82. //handle the last active path
  83. if(g != NULL) {
  84. if(first != NULL && cur->Dist2(first) < ENDPOINT_CONNECT_MAX_DISTANCE*ENDPOINT_CONNECT_MAX_DISTANCE) {
  85. if(CheckLOS(map, cur, first))
  86. g->add_edge(cur, first);
  87. }
  88. db_paths.push_back(g);
  89. }
  90. mysql_free_result(res);
  91. return(true);
  92. }
  93. bool load_spawns_from_db(MYSQL *m, const char *zone, list<PathNode*> &db_spawns) {
  94. char query[512];
  95. sprintf(query,
  96. "SELECT x,y,z FROM spawn2 "
  97. "WHERE zone='%s'", zone);
  98. if(mysql_query(m, query) != 0) {
  99. printf("Unable to query: %s\n", mysql_error(m));
  100. return(false);
  101. }
  102. MYSQL_RES *res = mysql_store_result(m);
  103. if(res == NULL) {
  104. printf("Unable to store res: %s\n", mysql_error(m));
  105. return(false);
  106. }
  107. MYSQL_ROW row;
  108. PathNode *cur = NULL;
  109. while((row = mysql_fetch_row(res))) {
  110. cur = new PathNode;
  111. cur->x = atof(row[0]);
  112. cur->y = atof(row[1]);
  113. cur->z = atof(row[2]);
  114. db_spawns.push_back(cur);
  115. }
  116. mysql_free_result(res);
  117. return(true);
  118. }
  119. bool load_doors_from_db(MYSQL *m, const char *zone, list<PathNode*> &db_spawns) {
  120. char query[512];
  121. sprintf(query,
  122. "SELECT pos_x,pos_y,pos_z FROM doors "
  123. "WHERE zone='%s'", zone);
  124. if(mysql_query(m, query) != 0) {
  125. printf("Unable to query: %s\n", mysql_error(m));
  126. return(false);
  127. }
  128. MYSQL_RES *res = mysql_store_result(m);
  129. if(res == NULL) {
  130. printf("Unable to store res: %s\n", mysql_error(m));
  131. return(false);
  132. }
  133. MYSQL_ROW row;
  134. PathNode *cur = NULL;
  135. while((row = mysql_fetch_row(res))) {
  136. cur = new PathNode;
  137. cur->x = atof(row[0]);
  138. cur->y = atof(row[1]);
  139. cur->z = atof(row[2]);
  140. //TODO: it would be nice if we could get to the middle of these
  141. //doors, not the edge of them which I assume these points are
  142. db_spawns.push_back(cur);
  143. }
  144. mysql_free_result(res);
  145. return(true);
  146. }
  147. bool load_hints_from_db(MYSQL *m, const char *zone, list<PathNode*> &db_spawns) {
  148. char query[512];
  149. sprintf(query,
  150. "SELECT x,y,z,forced,disjoint FROM fear_hints "
  151. "WHERE zone='%s'", zone);
  152. if(mysql_query(m, query) != 0) {
  153. printf("Unable to query: %s\n", mysql_error(m));
  154. return(false);
  155. }
  156. MYSQL_RES *res = mysql_store_result(m);
  157. if(res == NULL) {
  158. printf("Unable to store res: %s\n", mysql_error(m));
  159. return(false);
  160. }
  161. MYSQL_ROW row;
  162. PathNode *cur = NULL;
  163. while((row = mysql_fetch_row(res))) {
  164. cur = new PathNode;
  165. cur->x = atof(row[0]);
  166. cur->y = atof(row[1]);
  167. cur->z = atof(row[2]);
  168. cur->forced = atoi(row[3])?true:false;
  169. cur->disjoint = atoi(row[4])?true:false;
  170. db_spawns.push_back(cur);
  171. }
  172. mysql_free_result(res);
  173. return(true);
  174. }
  175. bool load_settings_from_db(MYSQL *m, const char *zone) {
  176. char query[512];
  177. sprintf(query,
  178. "SELECT use_doors, min_fix_z, max_fear_distance, image_scale, split_invalid_paths,"
  179. " link_path_endpoints, end_distance, split_long_min, split_long_step, same_dist, node_combine_dist,"
  180. " grid_combine_dist, close_all_los, cross_count, cross_min_length, cross_max_z_diff, cross_combine_dist,"
  181. " second_link_dist, link_max_dist, link_count"
  182. " FROM fear_settings"
  183. " WHERE zone='%s'", zone);
  184. if(mysql_query(m, query) != 0) {
  185. // printf("Unable to query: %s\n", mysql_error(m));
  186. return(false);
  187. }
  188. MYSQL_RES *res = mysql_store_result(m);
  189. if(res == NULL) {
  190. // printf("Unable to store res: %s\n", mysql_error(m));
  191. return(false);
  192. }
  193. MYSQL_ROW row;
  194. int r = 0;
  195. if((row = mysql_fetch_row(res))) {
  196. INCLUDE_DOORS = atoi(row[r++])?true:false;
  197. MIN_FIX_Z = atof(row[r++]);
  198. FEAR_MAXIMUM_DISTANCE = atof(row[r++]);
  199. IMAGE_SCALE = atoi(row[r++]);
  200. SPLIT_INVALID_PATHS = atoi(row[r++])?true:false;
  201. LINK_PATH_ENDPOINTS = atoi(row[r++])?true:false;
  202. ENDPOINT_CONNECT_MAX_DISTANCE = atof(row[r++]);
  203. SPLIT_LINE_LENGTH = atof(row[r++]);
  204. SPLIT_LINE_INTERVAL = atof(row[r++]);
  205. CLOSE_ENOUGH = atof(row[r++]);
  206. CLOSE_ENOUGH_COMBINE = atof(row[r++]);
  207. MERGE_MIN_SECOND_DIST = atof(row[r++]);
  208. COMBINE_CHECK_ALL_LOS = atoi(row[r++])?true:false;
  209. CROSS_REDUCE_COUNT = atoi(row[r++]);
  210. CROSS_MIN_LENGTH = atof(row[r++]);
  211. CROSS_MAX_Z_DIFF = atof(row[r++]);
  212. CLOSE_ENOUGH_CROSS = atof(row[r++]);
  213. SPAWN_MIN_SECOND_DIST = atof(row[r++]);
  214. MAX_LINK_SPAWN_DIST = atof(row[r++]);
  215. int sc = atoi(row[r++]);
  216. SPAWN_LINK_TWICE = sc >= 2?true:false;
  217. SPAWN_LINK_THRICE = sc >= 3?true:false;
  218. mysql_free_result(res);
  219. return(true);
  220. }
  221. mysql_free_result(res);
  222. return(false);
  223. }