PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/map.php

https://bitbucket.org/jeremyfa/phpore
PHP | 804 lines | 623 code | 127 blank | 54 comment | 198 complexity | 578095364f8f8b085812923c58582485 MD5 | raw file
  1. <?php
  2. /*
  3. Program: phpore
  4. Author: Jeremy Faivre
  5. Contact: http://www.jeremyfaivre.com/about
  6. Year: 2005
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. */
  12. if ( !defined('IN_PHPORE') )
  13. {
  14. exit;
  15. }
  16. if ( !$user->logged_in )
  17. {
  18. header('Location: ' . $config->path . $config->index . '?mod=default');
  19. exit;
  20. }
  21. $mode = ( isset($_GET['mode']) ) ? 'GET.' . trim($_GET['mode']) : '';
  22. if ( $mode == 'GET.stop' )
  23. {
  24. message_die($lang->session_stop, $lang->session_stop_explain);
  25. }
  26. elseif ( $mode == 'GET.disp' )
  27. {
  28. require($config->path . 'includes/functions_map.' . $config->phpex);
  29. $serialized_script = serialize(array());
  30. $event_script = new event_script($serialized_script);
  31. $text = '';
  32. $script = '';
  33. while ( $data = $event_script->script(false, false) )
  34. {
  35. $i = $event_script->condition_id + 1;
  36. if ( isset($data[2]) && $data[2] == 1 )
  37. {
  38. $tab = array('<ul style="list-style-type:none">', '');
  39. }
  40. elseif ( isset($data[2]) && $data[2] == 2 )
  41. {
  42. $tab = array('', '</ul>');
  43. }
  44. elseif ( isset($data[2]) && $data[2] == 3 )
  45. {
  46. $tab = array('<ul style="list-style-type:none">', '</ul>');
  47. }
  48. else
  49. {
  50. $tab = array('', '');
  51. }
  52. $text .= '[ ' . $data[0] . ' ]<br />';
  53. $script .= $tab[1] . '<li><code title="' . $data[0] . '">' . $data[1] . '</code></li>' . $tab[0];
  54. }
  55. echo 'database : <br />' , $serialized_script , '<br /><br />';
  56. echo 'commands : <br />' , $text , '<br /><br />';
  57. echo 'script :<ul style="list-style-type:none">' , $script , '</ul>';
  58. }
  59. elseif ( $mode == 'GET.submit' )
  60. {
  61. require($config->path . 'includes/functions_map.' . $config->phpex);
  62. $refresh_id = 2;
  63. $map_sid = ( isset($_GET['map_sid']) ) ? intval($_GET['map_sid']) : 0;
  64. if ( $map_sid < $user->map_sid )
  65. {
  66. js_eval('map_session_stop();', $refresh_id);
  67. }
  68. if ( $user->refresh == 1 || $user->in_battle )
  69. {
  70. js_eval('map_session_restart();', $refresh_id);
  71. }
  72. $javascript = '';
  73. $event_script = new event_script(false, true);
  74. $event_key = $event_script->event_key;
  75. $script = array();
  76. for ( $i = $event_key - 1; $i >= 0; $i-- )
  77. {
  78. $script[] = '\'\'';
  79. }
  80. if ( !$event_script->script(false, true) )
  81. {
  82. js_eval('alert(\'Error !\');map_session_restart();', $refresh_id);
  83. }
  84. while ( $data = $event_script->script(true, false) )
  85. {
  86. // javascript instruction
  87. $script[] = '\'' . quotes($data) . '\'';
  88. }
  89. $javascript .= 'script_eval(' . $event_key . ', new Array(' . implode(', ', $script) . '));';
  90. js_eval($javascript, $refresh_id);
  91. }
  92. elseif ( $mode == 'GET.refresh' || $mode == 'GET.event')
  93. {
  94. require($config->path . 'includes/functions_map.' . $config->phpex);
  95. $result = $db->sql_query('SELECT m.id, m.name, m.blocs, t.tiles FROM ' . MAPS_TABLE . ' m, ' . TILESETS_TABLE . ' t WHERE m.id = ' . $user->map_id . ' AND m.tileset = t.id');
  96. if ( !$map = $db->sql_fetchobject($result) )
  97. {
  98. die('Could not get map data');
  99. }
  100. if ( $user->in_battle )
  101. {
  102. js_eval('map_session_restart();', $refresh_id);
  103. }
  104. if ( $mode == 'GET.refresh' )
  105. {
  106. $refresh_id = 1;
  107. }
  108. else
  109. {
  110. $refresh_id = 2;
  111. }
  112. $map->tiles = unserialize($map->tiles);
  113. $map->blocs = unserialize($map->blocs);
  114. $map->count_x = count($map->blocs[0][0]);
  115. $map->count_y = count($map->blocs[0]);
  116. $map->width = $map->count_x * $config->tile_size;
  117. $map->height = $map->count_y * $config->tile_size;
  118. // if the character is not in the limits of map
  119. if ( $user->map_left < 0 || $user->map_left >= $map->count_x || $user->map_top < 0 || $user->map_top >= $map->count_y )
  120. {
  121. if ( $user->start_location != '' )
  122. {
  123. list($user->map_id, $user->map_left, $user->map_top, $user->map_dir) = explode(',', $user->start_location);
  124. }
  125. else
  126. {
  127. list($user->map_id, $user->map_left, $user->map_top, $user->map_dir) = explode(',', $config->default_location);
  128. }
  129. $user->map_id = intval($user->map_id);
  130. $user->map_left = intval($user->map_left);
  131. $user->map_top = intval($user->map_top);
  132. $user->map_dir = intval($user->map_dir);
  133. $user->set('map_id', $user->map_id);
  134. $user->set('map_left', $user->map_left);
  135. $user->set('map_top', $user->map_top);
  136. $user->set('map_dir', $user->map_dir);
  137. }
  138. if ( ( $mode == 'GET.refresh' && ( !isset($_GET['table'], $_GET['move_id'], $_GET['map_sid'], $_GET['players']) || !is_numeric($_GET['map_sid']) || !preg_match('`^a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)$`', $_GET['table'], $table) || !preg_match('`^([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)$`', $_GET['move_id'], $move_id) || !preg_match('`^(p[0-9]+-[0-9]+)+$`', $_GET['players']) ) ) || ( $mode == 'GET.event' && ( !isset($_GET['table'], $_GET['move_id'], $_GET['map_sid'], $_GET['layer'], $_GET['event_pos']) || !is_numeric($_GET['map_sid']) || ( $_GET['layer'] != 0 && $_GET['layer'] != 1 ) || !preg_match('`^a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)_a([0-9]+)-([0-9]+)$`', $_GET['table'], $table) || !preg_match('`^([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)$`', $_GET['move_id'], $move_id) || !preg_match('`^([0-9]+-[0-9]+)$`', $_GET['event_pos'], $event_pos) ) ) )
  139. {
  140. js_eval('alert(\'Error\');map_session_restart();', $refresh_id);
  141. }
  142. if ( isset($_GET['chat_pos']) && preg_match('`^([0-9]+)-([0-9]+)-([0-9]+)$`', $_GET['chat_pos'], $matches) )
  143. {
  144. $user->set('chatbox_state', implode(',', array($matches[1], $matches[2], $matches[3])));
  145. }
  146. $user->set('map_last_visit', $config->server_time);
  147. $map_sid = ( isset($_GET['map_sid']) ) ? intval($_GET['map_sid']) : 0;
  148. if ( $map_sid < $user->map_sid )
  149. {
  150. js_eval('map_session_stop();', $refresh_id);
  151. }
  152. if ( $user->refresh == 1 )
  153. {
  154. js_eval('map_session_restart();', $refresh_id);
  155. }
  156. $javascript = '';
  157. if ( isset($_GET['chatbox']) && trim($_GET['chatbox']) != '' ) // submit chat messages
  158. {
  159. $chatbox_content = explode(',', trim($_GET['chatbox']));
  160. $lang->load_keys('chat');
  161. $i = 0;
  162. while ( isset($chatbox_content[$i]) )
  163. {
  164. // Who is slash command [Nuladion]
  165. if ( substr($chatbox_content[$i], 0, 5) == '/who ' )
  166. {
  167. $whois = new user('u.name = \'' . quotes(htmlspecialchars(substr($chatbox_content[$i], 5))) . '\'');
  168. if ( $whois->guest )
  169. {
  170. $javascript .= 'chat_add(\'' . quotes(htmlspecialchars(substr($chatbox_content[$i], 5))) . '\', \'' . quotes($lang->whois_char_not_found) . '\', true);';
  171. }
  172. else
  173. {
  174. $javascript .= 'chat_add(\'' . quotes($whois->name) . '\', \'' . quotes($lang->level . ' : ' . $whois->level . '<br />' . $lang->class . ' : ' . $whois->class_title) . '\', true);';
  175. }
  176. }
  177. elseif ( trim($chatbox_content[$i]) != '' )
  178. {
  179. $db->sql_query('INSERT INTO ' . CHATBOX_TABLE . '(cat_id, user_id, time, message) VALUES(\'m' . $user->map_id . '\', ' . $user->id . ', ' . $config->server_time . ', \'' . addslashes(htmlspecialchars(stripslashes(trim($chatbox_content[$i])))) . '\')');
  180. }
  181. $i++;
  182. }
  183. }
  184. $chat_last_id = ( isset($_GET['chat_last']) ) ? intval($_GET['chat_last']) : -1; // get chatbox messages
  185. if ( $chat_last_id > -1 )
  186. {
  187. $result = $db->sql_query('SELECT u.name, c.message, c.id FROM ' . USERS_TABLE . ' u, ' . CHATBOX_TABLE . ' c WHERE u.id = c.user_id AND u.id != ' . $user->id . ' AND c.cat_id = \'m' . $user->map_id . '\' AND c.id > ' . $chat_last_id . ' ORDER BY c.id ASC');
  188. while ( $row = $db->sql_fetchrow($result) )
  189. {
  190. $javascript .= 'chat_add(\'' . quotes($row['name']) . '\', \'' . quotes($row['message']) . '\');';
  191. $last_id = $row['id'];
  192. }
  193. if ( isset($last_id) && $last_id > $chat_last_id )
  194. {
  195. $javascript .= 'chat_last_id = ' . $last_id . ';';
  196. }
  197. }
  198. // update player moves
  199. $last_moves = array();
  200. $i = 1;
  201. while ( $i < 17 )
  202. {
  203. if ( $move_id[$i] > 0 )
  204. {
  205. $last_moves[$move_id[$i]] = array('left' => $table[2*$i-1], 'top' => $table[2*$i], 'bloc_id' => 'a' . $table[2*$i-1] . '-' . $table[2*$i]);
  206. }
  207. $i++;
  208. }
  209. ksort($last_moves);
  210. //print_r($last_moves);
  211. // events in map
  212. $events = array();
  213. $bloc_events = array();
  214. $event_script_data = array();
  215. $event_ids = array();
  216. $event_coords = array();
  217. $i = 0;
  218. while ( $i < $map->count_x )
  219. {
  220. $j = 0;
  221. while ( $j < $map->count_y )
  222. {
  223. if ( $map->blocs[2][$j][$i] > 0 )
  224. {
  225. $event_ids[] = $map->blocs[2][$j][$i];
  226. if ( !isset($event_coords[$map->blocs[2][$j][$i]]) )
  227. {
  228. $event_coords[$map->blocs[2][$j][$i]] = array();
  229. }
  230. $event_coords[$map->blocs[2][$j][$i]][] = array($i, $j);
  231. }
  232. $j++;
  233. }
  234. $i++;
  235. }
  236. if ( count($event_ids) > 0 )
  237. {
  238. // events in map
  239. $result = $db->sql_query('SELECT * FROM ' . EVENTS_TABLE . ' WHERE id = ' . implode(' OR id = ', $event_ids));
  240. while ( $row = $db->sql_fetchrow($result) )
  241. {
  242. foreach ( $event_coords[$row['id']] as $val )
  243. {
  244. $events['i' . $val[0] . '-' . $val[1]] = true;
  245. $event_script_data['i' . $val[0] . '-' . $val[1]] = $row['script'];
  246. if ( $row['layer'] == 1 )
  247. {
  248. $bloc_events['i' . $val[0] . '-' . $val[1]] = true;
  249. }
  250. }
  251. }
  252. }
  253. //$n_moves = $user->map_moves;
  254. $user->map_moves_table = unserialize($user->map_moves_table);
  255. foreach ( $last_moves as $key => $val )
  256. {
  257. if ( $key > $user->map_moves )
  258. {
  259. if ( $user->map_left < $val['left'] )
  260. {
  261. // move right
  262. if ( isset($map->blocs[0][$user->map_top][$user->map_left+1]) && $map->tiles[0][1][$map->blocs[0][$user->map_top][$user->map_left+1]] != 1 && $map->tiles[1][1][$map->blocs[1][$user->map_top][$user->map_left+1]] != 1 && !isset($bloc_events['i' . ($user->map_left+1) . '-' . $user->map_top]) )
  263. {
  264. $user->map_left++;
  265. }
  266. $user->map_dir = 3;
  267. }
  268. elseif ( $user->map_left > $val['left'] )
  269. {
  270. // move left
  271. if ( isset($map->blocs[0][$user->map_top][$user->map_left-1]) && $map->tiles[0][1][$map->blocs[0][$user->map_top][$user->map_left-1]] != 1 && $map->tiles[1][1][$map->blocs[1][$user->map_top][$user->map_left-1]] != 1 && !isset($bloc_events['i' . ($user->map_left-1) . '-' . $user->map_top]) )
  272. {
  273. $user->map_left--;
  274. }
  275. $user->map_dir = 1;
  276. }
  277. elseif ( $user->map_top < $val['top'] )
  278. {
  279. // move down
  280. if ( isset($map->blocs[0][$user->map_top+1][$user->map_left]) && $map->tiles[0][1][$map->blocs[0][$user->map_top+1][$user->map_left]] != 1 && $map->tiles[1][1][$map->blocs[1][$user->map_top+1][$user->map_left]] != 1 && !isset($bloc_events['i' . $user->map_left . '-' . ($user->map_top+1)]) )
  281. {
  282. $user->map_top++;
  283. }
  284. $user->map_dir = 0;
  285. }
  286. elseif ( $user->map_top > $val['top'] )
  287. {
  288. // move top
  289. if ( isset($map->blocs[0][$user->map_top-1][$user->map_left]) && $map->tiles[0][1][$map->blocs[0][$user->map_top-1][$user->map_left]] != 1 && $map->tiles[1][1][$map->blocs[1][$user->map_top-1][$user->map_left]] != 1 && !isset($bloc_events['i' . $user->map_left . '-' . ($user->map_top-1)]) )
  290. {
  291. $user->map_top--;
  292. }
  293. $user->map_dir = 2;
  294. }
  295. $user->map_moves = $key;
  296. $user->map_moves_table[$user->map_moves] = $val['bloc_id'];
  297. unset($user->map_moves_table[$user->map_moves-17]);
  298. }
  299. }
  300. $user->map_moves_table = serialize($user->map_moves_table);
  301. //$db->sql_query('UPDATE ' . USERS_TABLE . ' SET map_moves = ' . $user->map_moves . ', map_moves_table = \'' . quotes($user->map_moves_table) . '\', map_dir = ' . $user->map_dir . ', map_left = ' . $user->map_left . ', map_top = ' . $user->map_top . ' WHERE id = ' . $user->id);
  302. $user->set('map_moves', $user->map_moves);
  303. $user->set('map_moves_table', $user->map_moves_table);
  304. $user->set('map_dir', $user->map_dir);
  305. $user->set('map_left', $user->map_left);
  306. $user->set('map_top', $user->map_top);
  307. // if client and server player position are different, we must refresh the page to fix this problem
  308. if ( !isset($_GET['x'], $_GET['y'], $_GET['map_id']) || ( $user->teleport != 1 && ( $_GET['x'] != $user->map_left || $_GET['y'] != $user->map_top || $_GET['map_id'] != $user->map_id ) ) )
  309. {
  310. //js_eval('alert(\'invalid position : ' . $_GET['x'] . ';' . $_GET['y'] . ' => ' . $user->map_left . ';' . $user->map_top . '\');map_session_restart();', $refresh_id); // debug
  311. js_eval('map_session_restart();', $refresh_id);
  312. }
  313. if ( $mode == 'GET.refresh' ) // refresh map events, players
  314. {
  315. // get other players data
  316. $map_players = $_GET['players'];
  317. $players = array();
  318. while ( preg_match('`p([0-9]+)-([0-9]+)`', $map_players, $matches) )
  319. {
  320. $players[$matches[1]] = $matches[2];
  321. $map_players = str_replace($matches[0], '', $map_players);
  322. }
  323. unset($players[$user->id]);
  324. //js_eval('alert(\'' . serialize($players) . '\');');
  325. $players_sql = array();
  326. foreach ( $players as $key => $val )
  327. {
  328. $players_sql[] = $key;
  329. }
  330. if ( count($players_sql) > 0 )
  331. {
  332. $players_sql = ' OR ( ( u.map_previous_id = ' . $user->map_id . ' OR u.map_id = ' . $user->map_id . ' ) AND ( u.id = ' . implode(' OR u.id = ', $players_sql) . ' ) )';
  333. }
  334. else
  335. {
  336. $players_sql = '';
  337. }
  338. //js_eval('alert(\'' . quotes('SELECT u.name, u.id, u.map_left, u.map_id, u.map_top, u.map_dir, u.map_moves, u.map_moves_table, u.charaset, c.class_charaset FROM ' . USERS_TABLE . ' u, ' . CLASSES_TABLE . ' c WHERE u.id != ' . $user->id . ' AND ( u.map_id = ' . $user->map_id . ' OR ( u.map_previous_id = ' . $user->map_id . ' AND ( u.id = ' . implode(' OR u.id = ', $players_sql) . ' ) ) ) AND c.classname = u.classname') . '\');', 1);
  339. $result = $db->sql_query('SELECT u.name, u.id, u.map_last_visit, u.map_left, u.map_id, u.map_top, u.map_dir, u.map_moves, u.map_moves_table, u.charaset, u.pic_width, u.pic_height, u.battle_id, u.battle_state, c.charaset AS class_charaset, c.pic_width AS class_pic_width, c.pic_height AS class_pic_height FROM ' . USERS_TABLE . ' u, ' . CLASSES_TABLE . ' c WHERE u.id != ' . $user->id . ' AND ( ( u.map_last_visit > ' . ($config->server_time - 16) . ' AND u.map_id = ' . $user->map_id . ' AND u.teleport = 0 )' . $players_sql . ' ) AND c.classname = u.classname');
  340. //echo 'SELECT u.name, u.id, u.last_visit, u.map_left, u.map_id, u.map_top, u.map_dir, u.map_moves, u.map_moves_table, u.charaset, u.pic_width, u.pic_height, c.charaset AS class_charaset, c.pic_width AS class_pic_width, c.pic_height AS class_pic_height FROM ' . USERS_TABLE . ' u, ' . CLASSES_TABLE . ' c WHERE u.id != ' . $user->id . ' AND ( ( u.last_visit > ' . ($config->server_time - 16) . ' AND u.map_id = ' . $user->map_id . ' )' . $players_sql . ' ) AND c.classname = u.classname';
  341. while ( $row = $db->sql_fetchrow($result) )
  342. {
  343. // this is a new player, add it
  344. if ( !isset($players[$row['id']]) )
  345. {
  346. if ( empty($row['charaset']) )
  347. {
  348. $row['charaset'] = $row['class_charaset'];
  349. $row['pic_width'] = $row['class_pic_width'];
  350. $row['pic_height'] = $row['class_pic_height'];
  351. }
  352. $javascript .= 'add_player(' . $row['id'] . ', \'' . quotes($row['name']) . '\', \'' . quotes($row['charaset']) . '\', ' . $row['map_left'] . ', ' . $row['map_top'] . ', ' . $row['map_dir'] . ', ' . $row['map_moves'] . ', ' . ceil($row['pic_width'] / 4) . ', ' . ceil($row['pic_height'] / 4) . ');';
  353. $javascript .= 'setTimeout(\'' . quotes('chat_add(player[' . $row['id'] . '].name, \'' . quotes($lang->join_map) . '\', true);') . '\', 1000);';
  354. //$javascript .= 'alert(\'' . quotes('add_player(' . $row['id'] . ', \'' . quotes($row['name']) . '\', \'' . quotes((( empty($row['charaset']) ) ? $row['class_charaset'] : $row['charaset'] )) . '\', ' . $row['map_left'] . ', ' . $row['map_top'] . ', ' . $row['map_dir'] . ', ' . $row['map_moves'] . ', ' . $size[0] . ', ' . $size[1] . ');') . '\');';
  355. }
  356. else // the player exists, check if he has moved
  357. {
  358. if ( $row['map_last_visit'] < ($config->server_time - 15) )
  359. {
  360. $javascript .= 'chat_add(player[' . $row['id'] . '].name, \'' . quotes($lang->quit_map) . '\', true);';
  361. $javascript .= 'remove_player(' . $row['id'] . ');';
  362. }
  363. $row['map_moves_table'] = unserialize($row['map_moves_table']);
  364. if ( $players[$row['id']] + 15 < $row['map_moves'] )
  365. {
  366. js_eval('alert(\'to many moves !\');map_session_restart();', $refresh_id);
  367. }
  368. $javascript .= 'battle(' . $row['id'] . ', ' . $row['battle_id'] . ', ' . $row['battle_state'] . ');';
  369. while ( $players[$row['id']] < $row['map_moves'] )
  370. {
  371. $players[$row['id']]++;
  372. $javascript .= 'move_map_player(\'' . $row['map_moves_table'][$players[$row['id']]] . '\', ' . $players[$row['id']] . ', ' . $row['id'] . ', 1);';
  373. }
  374. unset($players[$row['id']]);
  375. }
  376. }
  377. /*while ( list($key, ) = each($players) ) // now these players are not in this map
  378. {
  379. $javascript .= 'remove_player(' . $key . ');';
  380. }*/
  381. // update map
  382. $javascript = 'map_session_refresh();' . $javascript;
  383. js_eval($javascript, $refresh_id);
  384. }
  385. else // exec event
  386. {
  387. $layer = $_GET['layer'];
  388. $event_pos = $event_pos[1];
  389. if ( ( $layer == 1 && isset($events['i' . $event_pos]) && ( $event_pos == $user->map_left . '-' . ($user->map_top + 1) || $event_pos == ($user->map_left - 1) . '-' . $user->map_top || $event_pos == $user->map_left . '-' . ($user->map_top - 1) || $event_pos == ($user->map_left + 1) . '-' . $user->map_top ) ) || ( $layer == 0 && isset($events['i' . $event_pos]) && $event_pos == $user->map_left . '-' . $user->map_top ) )
  390. {
  391. $event_script = new event_script($event_script_data['i' . $event_pos]);
  392. $script = array();
  393. while ( $data = $event_script->script(true, false) )
  394. {
  395. // javascript instruction
  396. $script[] = '\'' . quotes($data) . '\'';
  397. }
  398. $javascript .= 'script_eval(0, new Array(' . implode(', ', $script) . '));';
  399. }
  400. js_eval($javascript, $refresh_id);
  401. }
  402. }
  403. else
  404. {
  405. if ( $user->in_battle )
  406. {
  407. header('Location: ' . $config->path . $config->index . '?mod=battle');
  408. exit;
  409. }
  410. if ( $user->refresh == 1 )
  411. {
  412. $user->set('refresh', 0);
  413. }
  414. if ( $user->teleport == 1 )
  415. {
  416. $user->set('teleport', 0);
  417. }
  418. // get map data
  419. $result = $db->sql_query('SELECT m.id, m.name, m.blocs, m.music, m.optimized, t.tiles FROM ' . MAPS_TABLE . ' m, ' . TILESETS_TABLE . ' t WHERE m.id = ' . $user->map_id . ' AND m.tileset = t.id');
  420. if ( !$map = $db->sql_fetchobject($result) )
  421. {
  422. if ( $user->start_location != '' )
  423. {
  424. list($user->map_id, $user->map_left, $user->map_top, $user->map_dir) = explode(',', $user->start_location);
  425. }
  426. else
  427. {
  428. list($user->map_id, $user->map_left, $user->map_top, $user->map_dir) = explode(',', $config->default_location);
  429. }
  430. $user->map_id = intval($user->map_id);
  431. $user->map_left = intval($user->map_left);
  432. $user->map_top = intval($user->map_top);
  433. $user->map_dir = intval($user->map_dir);
  434. $user->set('map_id', $user->map_id);
  435. $user->set('map_left', $user->map_left);
  436. $user->set('map_top', $user->map_top);
  437. $user->set('map_dir', $user->map_dir);
  438. $user->update_db();
  439. $config->update_db();
  440. if ( !isset($_GET['redirect_header']) )
  441. {
  442. header('Location: ' . $config->path . $config->index . '?mod=map&redirect_header=1');
  443. }
  444. exit;
  445. }
  446. $map->tiles = unserialize($map->tiles);
  447. $map->blocs = unserialize($map->blocs);
  448. $map->count_x = count($map->blocs[0][0]);
  449. $map->count_y = count($map->blocs[0]);
  450. $map->width = $map->count_x * $config->tile_size;
  451. $map->height = $map->count_y * $config->tile_size;
  452. $map->optimized = ( $config->optimize_maps == 1 && $map->optimized == 1 && is_file($config->path . $config->cache_dir . 'map_' . $map->id . '_0.png') && is_file($config->path . $config->cache_dir . 'map_' . $map->id . '_1.png') ) ? true : false;
  453. $user->set('map_sid', $user->map_sid + 1);
  454. if ( $map->optimized )
  455. {
  456. $template->assign_block_vars('map_image', array(
  457. 'PICTURE' => $config->cache_dir . 'map_' . $map->id . '_0.png',
  458. 'Z_INDEX' => 1
  459. ));
  460. $template->assign_block_vars('map_image', array(
  461. 'PICTURE' => $config->cache_dir . 'map_' . $map->id . '_1.png',
  462. 'Z_INDEX' => 9995
  463. ));
  464. }
  465. for ( $x = 0; $x < $map->count_x; $x++ )
  466. {
  467. for ( $y = 0; $y < $map->count_y; $y++ )
  468. {
  469. // lower layer of map
  470. $template->assign_block_vars('lower_bloc', array(
  471. 'ID' => 'l' . $x . '-' . $y,
  472. 'LEFT' => $x,
  473. 'TOP' => $y,
  474. 'BACKGROUND_IMAGE' => (( $map->blocs[0][$y][$x] == 0 || ( $map->optimized && strtolower(substr($map->tiles[0][0][$map->blocs[0][$y][$x]], -4)) == '.png' ) ) ? '' : $map->tiles[0][0][$map->blocs[0][$y][$x]]),
  475. 'Z_INDEX' => (( $map->tiles[0][1][$map->blocs[0][$y][$x]] == 0 ) ? 1 : 2)
  476. ));
  477. // upper layer of map
  478. $template->assign_block_vars('upper_bloc', array(
  479. 'ID' => 'u' . $x . '-' . $y,
  480. 'LEFT' => $x,
  481. 'TOP' => $y,
  482. 'BACKGROUND_IMAGE' => (( $map->blocs[1][$y][$x] == 0 || ( $map->optimized && strtolower(substr($map->tiles[1][0][$map->blocs[1][$y][$x]], -4)) == '.png' ) ) ? '' : $map->tiles[1][0][$map->blocs[1][$y][$x]]),
  483. 'Z_INDEX' => (( $map->tiles[1][1][$map->blocs[1][$y][$x]] == 0 ) ? 3 : (( $map->tiles[1][1][$map->blocs[1][$y][$x]] == 1 ) ? 6 : 9995))
  484. ));
  485. }
  486. }
  487. $template->assign_block_vars('add_player_bloc', array(
  488. 'ID' => $user->id,
  489. 'BATTLE_ID' => $user->battle_id,
  490. 'BATTLE_STATE' => $user->battle_state,
  491. 'NAME' => $user->name,
  492. 'CHARASET' => $user->charaset,
  493. 'LEFT' => $user->map_left,
  494. 'TOP' => $user->map_top,
  495. 'DIR' => $user->map_dir,
  496. 'MOVES' => $user->map_moves,
  497. 'WIDTH' => ceil($user->pic_width / 4),
  498. 'HEIGHT' => ceil($user->pic_height / 4)
  499. ));
  500. // characters in map
  501. $result = $db->sql_query('SELECT u.name, u.id, u.map_left, u.map_top, u.map_dir, u.map_moves, u.charaset, u.pic_width, u.pic_height, u.battle_id, u.battle_state, c.charaset AS class_charaset, c.pic_width AS class_pic_width, c.pic_height AS class_pic_height FROM ' . USERS_TABLE . ' u, ' . CLASSES_TABLE . ' c WHERE u.map_last_visit > ' . ($config->server_time - 16) . ' AND u.id != ' . $user->id . ' AND u.map_id = ' . $user->map_id . ' AND c.classname = u.classname');
  502. while ( $row = $db->sql_fetchrow($result) )
  503. {
  504. if ( empty($row['charaset']) )
  505. {
  506. $row['charaset'] = $row['class_charaset'];
  507. $row['pic_width'] = $row['class_pic_width'];
  508. $row['pic_height'] = $row['class_pic_height'];
  509. }
  510. $template->assign_block_vars('add_player_bloc', array(
  511. 'ID' => $row['id'],
  512. 'BATTLE_ID' => $row['battle_id'],
  513. 'BATTLE_STATE' => $row['battle_state'],
  514. 'NAME' => $row['name'],
  515. 'CHARASET' => $row['charaset'],
  516. 'LEFT' => $row['map_left'],
  517. 'TOP' => $row['map_top'],
  518. 'DIR' => $row['map_dir'],
  519. 'MOVES' => $row['map_moves'],
  520. 'WIDTH' => ceil($row['pic_width'] / 4),
  521. 'HEIGHT' => ceil($row['pic_height'] / 4)
  522. ));
  523. }
  524. $event_ids = array();
  525. $event_coords = array();
  526. $i = 0;
  527. while ( $i < $map->count_x )
  528. {
  529. $j = 0;
  530. while ( $j < $map->count_y )
  531. {
  532. if ( $map->blocs[2][$j][$i] > 0 )
  533. {
  534. $event_ids[] = $map->blocs[2][$j][$i];
  535. if ( !isset($event_coords[$map->blocs[2][$j][$i]]) )
  536. {
  537. $event_coords[$map->blocs[2][$j][$i]] = array();
  538. }
  539. $event_coords[$map->blocs[2][$j][$i]][] = array($i, $j);
  540. }
  541. $j++;
  542. }
  543. $i++;
  544. }
  545. if ( count($event_ids) > 0 )
  546. {
  547. // events in map
  548. $result = $db->sql_query('SELECT * FROM ' . EVENTS_TABLE . ' WHERE id = ' . implode(' OR id = ', $event_ids));
  549. while ( $row = $db->sql_fetchrow($result) )
  550. {
  551. if ( $row['dir'] != '' )
  552. {
  553. $row['pic_width'] = ceil($row['pic_width'] / 4);
  554. $row['pic_height'] = ceil($row['pic_height'] / 4);
  555. }
  556. foreach ( $event_coords[$row['id']] as $val )
  557. {
  558. $template->assign_block_vars('event_bloc', array(
  559. 'ID' => 'i' . $val[0] . '-' . $val[1],
  560. 'LEFT' => $val[0],
  561. 'TOP' => $val[1],
  562. 'PICTURE' => $row['picture'],
  563. 'DIR' => (( $row['dir'] == '' ) ? 'false' : 'new Array(' . $row['dir'] . ')'),
  564. 'LAYER' => $row['layer'],
  565. 'WIDTH' => $row['pic_width'],
  566. 'HEIGHT' => $row['pic_height']
  567. ));
  568. }
  569. }
  570. }
  571. if ( $config->chat_history == 0 ) // no chat history
  572. {
  573. $result = $db->sql_query('SELECT MAX(id) AS last_id FROM ' . CHATBOX_TABLE . ' WHERE cat_id = \'m' . $user->map_id . '\'');
  574. if ( $last_id = $db->sql_fetchrow($result) )
  575. {
  576. $last_id = $last_id['last_id'];
  577. }
  578. if ( empty($last_id) )
  579. {
  580. $last_id = 0;
  581. }
  582. }
  583. else // chat history
  584. {
  585. $result = $db->sql_query('SELECT u.name, c.message, c.id FROM ' . USERS_TABLE . ' u, ' . CHATBOX_TABLE . ' c WHERE u.id = c.user_id AND c.cat_id = \'m' . $user->map_id . '\' ORDER BY c.id DESC LIMIT 0, ' . $config->chat_history);
  586. $row = array();
  587. while ( $value = $db->sql_fetchrow($result) )
  588. {
  589. $row[] = $value;
  590. }
  591. while( $value = array_pop($row) )
  592. {
  593. $template->assign_block_vars('add_chat', array(
  594. 'NAME' => $value['name'],
  595. 'MESSAGE' => $value['message']
  596. ));
  597. $last_id = $value['id'];
  598. }
  599. if ( !isset($last_id) )
  600. {
  601. $result = $db->sql_query('SELECT MAX(id) AS last_id FROM ' . CHATBOX_TABLE . ' WHERE cat_id = \'m' . $user->map_id . '\'');
  602. if ( $last_id = $db->sql_fetchrow($result) )
  603. {
  604. $last_id = $last_id['last_id'];
  605. }
  606. if ( empty($last_id) )
  607. {
  608. $last_id = 0;
  609. }
  610. }
  611. }
  612. /*$template->assign_block_vars('player_bloc', array(
  613. 'ID' => 'p' . $user->id,
  614. 'NAME' => $user->name,
  615. 'LEFT' => 'left:' . ( $user->map_left * $config->tile_size - 8 ) . 'px;',
  616. 'TOP' => 'top:' . ( $user->map_top * $config->tile_size - $config->tile_size ) . 'px;',
  617. 'CHARASET' => $user->charaset,
  618. 'BACKGROUND_IMAGE' => 'background-image:url(classes/images/' . (( $user->map_dir == 0 ) ? $user->charaset . '_01.png' : (( $user->map_dir == 1 ) ? $user->charaset . '_05.png' : (( $user->map_dir == 2 ) ? $user->charaset . '_13.png' : $user->charaset . '_09.png' ))) . ');',
  619. 'Z_INDEX' => 'z-index:' . (8 + $user->map_top) . ';'
  620. ));*/
  621. $lang->load_keys('map');
  622. if ( !empty($_GET['event_status']) && preg_match('`^([0-9a-f]{32})$`', $_GET['event_status']) && $_GET['event_status'] == $user->event_status )
  623. {
  624. $event_status = true;
  625. }
  626. else
  627. {
  628. $event_status = false;
  629. }
  630. $template->assign_vars(array(
  631. 'PAGE_NAME' => $lang->page_map . ' : ' . $map->name,
  632. 'MAP_BACKGROUND' => (( $map->optimized ) ? '' : 'background-image:url(images/tiles/' . $map->tiles[0][0][0] . ');'),
  633. 'MAP_NAME' => $map->name,
  634. 'MAP_WIDTH' => $map->width,
  635. 'MAP_HEIGHT' => $map->height,
  636. 'TILE_SIZE' => $config->tile_size,
  637. 'REFRESH_METHOD' => $config->refresh_method,
  638. 'CHARASET' => $user->charaset,
  639. 'MAP_MOVES' => $user->map_moves,
  640. 'MAP_SID' => $user->map_sid,
  641. 'MAP_ID' => $user->map_id,
  642. 'EVENT_STATUS' => (( $event_status ) ? $_GET['event_status'] : ''),
  643. 'LAST_CHAT_ID' => $last_id,
  644. 'PLAYER_X' => $user->map_left,
  645. 'PLAYER_Y' => $user->map_top,
  646. 'PLAYER_MOVING' => (( $event_status ) ? 'true' : 'false'),
  647. 'CHATBOX_STATE' => $user->chatbox_state,
  648. ));
  649. if ( !empty($map->music) && $user->allow_music )
  650. {
  651. if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
  652. {
  653. $template->assign_block_vars('bgsound_ie', array(
  654. 'MUSIC' => $map->music
  655. ));
  656. }
  657. else
  658. {
  659. $template->assign_block_vars('bgsound_ns', array(
  660. 'MUSIC' => $map->music
  661. ));
  662. }
  663. }
  664. $config->navigation[] = array('mod=map', 'nav_map');
  665. $template->set_filenames(array(
  666. 'header' => 'page_header.tpl',
  667. 'footer' => 'page_footer.tpl',
  668. 'body' => 'map.tpl'
  669. ));
  670. $template->pparse('header');
  671. $template->pparse('body');
  672. $template->pparse('footer');
  673. }
  674. ?>