map util





PHPXRef 0.7 : NEABExplorer : /libs/map_util.php source









[ Index ]


PHP Cross Reference of NEABExplorer














if (gwGetCookie('xrefnav')=='off')
document.write('[ Show Explorer ]');
else
document.write('[ Hide Explorer ]');



[ Show Explorer ]
[ Hide Navbar ]





titleBody[close]
/libs/ -> map_util.php (source)

[Summary view]
[Print]
[Text view]


1 <?PHP
2 /**
3 * Some utilities to deal with the map data
4 */
5
6 /**
7 * Convert a encoded map string into a 2D array.
8 *
9 * @param string $str the encoded map data
10 * @return array the 2D map
11 */
12 function map_extract($str)
13 {
14 $map=array();
15 $a=0;
16 $b=0;
17 $p=0;
18
19 for($i=0;$i < strlen($str);)
20 {
21 $c=substr($str,$i,1);
22 if($c == '-')
23 {
24 $i++;
25 $l='';
26 for(;$i < strlen($str);$i++)
27 {
28 $c=substr($str,$i,1);
29 if(ord($c) >= 48 && ord($c) <= 57)
30 {
31 $l.=$c;
32 continue;
33 }
34 else
35 break;
36 }
37 $nb=$l+0;
38 $v=(ord(substr($str,$i,1))-97)*26+(ord(substr($str,$i+1,1))-97);
39 for($j=0;$j < $nb;$j++)
40 {
41 $map[$a][$b]=$v;
42 $a++;
43 if($a >= 100)
44 {
45 $a=0;
46 $b++;
47 }
48 }
49 $i+=2;
50 }
51 else
52 {
53 $v=(ord(substr($str,$i,1))-97)*26+(ord(substr($str,$i+1,1))-97);
54 $map[$a][$b]=$v;
55 $a++;
56 if($a >= 100)
57 {
58 $a=0;
59 $b++;
60 }
61 $i+=2;
62 }
63 }
64 return $map;
65 }
66
67 /**
68 * Convert a 2D map array into a encoded map string.
69 *
70 * @param array $map
71 * @return string
72 */
73 function map2str($map)
74 {
75 $str="";
76 $oldcell=$map[0][0];
77 $nb=0;
78
79 for($y=0;$y < 100;$y++)
80 {
81 for($x=0;$x < 100;$x++)
82 {
83 if($map[$x][$y] == $oldcell)
84 $nb++;
85 else
86 {
87 if($nb > 1)
88 $str.="-".$nb.chr(floor($oldcell/26)+97).chr(($oldcell%26)+97);
89 else
90 $str.=chr(floor($oldcell/26)+97).chr(($oldcell%26)+97);
91 $nb=1;
92 $oldcell=$map[$x][$y];
93 }
94 }
95 }
96
97 // Add the remaining...
98 if($nb > 0)
99 {
100 if($nb > 1)
101 $str.="-".$nb.chr(floor($oldcell/26)+97).chr(($oldcell%26)+97);
102 else
103 $str.=chr(floor($oldcell/26)+97).chr(($oldcell%26)+97);
104 }
105
106 return $str;
107 }
108
109 /**
110 * Run length encode the encoded map string. This saves space on
111 * the database as well as network during the transfer at
112 * the cost of some CPU.
113 *
114 * @param string $str
115 * @return string
116 */
117 function lenght_code($str)
118 {
119 if(strpos($str,"-") !== false) // Already compressed, skip this step.
120 return $str;
121 $data=array();
122 for($p=0;$p < strlen($str);$p+=2)
123 {
124 if(substr($str,$p,1) == "")
125 $n=0;
126 else
127 $n=(ord(substr($str,$p,1))-97)*26+(ord(substr($str,$p+1,1))-97);
128 $data[]=$n;
129 }
130
131 $res="";
132 for($i=0;$i < count($data);)
133 {
134 $d=$data[$i];
135 $n=0;
136 for($j=$i;$j < count($data);$j++)
137 {
138 if($data[$j] != $d)
139 break;
140 $n++;
141 }
142 if($n > 2)
143 {
144 $res.="-".$n.chr(floor($d/26)+97).chr(($d%26)+97);
145 $i+=$n;
146 }
147 else
148 {
149 $res.=chr(floor($d/26)+97).chr(($d%26)+97);
150 $i++;
151 }
152 }
153 return $res;
154 }
155
156 /**
157 * Determine is a cell type is walkable or not.
158 *
159 * @param integer $type
160 * @param boolean $boat
161 * @return boolean
162 */
163 function check_walkable_map($type,$boat = false)
164 {
165 global $objwalk,$bgwalk,$basedir;
166
167 if(!isset($bgwalk))
168 {
169 if(file_exists("$basedir/libs/walkable.php"))
170 include_once "$basedir/libs/walkable.php";
171 else
172 {
173 $objwalk=str_pad("",600,"Y");
174 $bgwalk=str_pad("",600,"Y");
175 }
176 }
177
178 if($boat == true && $type >= 52 && $type <= 64) // Water
179 return true;
180 if($boat == true && $type >= 121 && $type <= 132) // Ice & Water
181 return true;
182 if($flagboat == true && $type >= 165 && $type <= 172) // Ice & Water & Sand
183 return true;
184
185 if(substr($bgwalk,$type,1) == "N")
186 return false;
187 return true;
188 }
189
190 /**
191 * retreives the current player location and returns it in the global variables sx,sy;
192 *
193 */
194 function retreive_loc()
195 {
196 global $uservals,$db,$sx,$sy,$userid;
197
198 $mapstr="";
199 if($uservals["PREVLOC"] <> "")
200 $_GET["PREVLOC"]=$uservals["PREVLOC"];
201
202 if($_GET["X"] != "" && $_GET["Y"] != "")
203 {
204 $sx=$_GET["X"];
205 $sy=$_GET["Y"];
206 $db->Execute("UPDATE PLAYER SET MAPX=$sx, MAPY=$sy WHERE ID = $userid");
207 }
208 else if(($uservals['LOCATION']+0) < 0 && $_GET["PREVLOC"] <> "")
209 {
210 $sx=$sy=-1;
211 $sql="SELECT OBJDATA,MAPDATA FROM PLAYER_MAP WHERE USERID=$userid AND LEVEL = ".(-$uservals['LOCATION']);
212 $r=$db->Execute($sql);
213 $objstr=$r->fields[0];
214 $mapstr=$r->fields[1];
215 $r->Close();
216 $obj=map_extract($objstr);
217 $p=0;
218 for($j=0;$j < 100;$j++) // Convert the string into the map
219 {
220 for($i=0;$i < 100;$i++)
221 {
222 $n=$obj[$i][$j];
223 if(($n == 114 || $n == 118) && $_GET["DIR"] == "DOWN")
224 {
225 $sx=$i;
226 $sy=$j;
227 break;
228 }
229 if(($n == 115 || $n == 117) && $_GET["DIR"] == "UP")
230 {
231 $sx=$i;
232 $sy=$j;
233 break;
234 }
235 $p+=2;
236 }
237 if($sx != -1)
238 break;
239 }
240 if($sx == -1)
241 $sx=$sy=1;
242 $db->Execute("UPDATE PLAYER SET MAPX=$sx, MAPY=$sy WHERE ID = $userid");
243 }
244 else if($_GET["PREVLOC"] <> "" && $_GET["X"] <> "")
245 {
246 $sx=$_GET["X"];
247 $sy=$_GET["Y"];
248 $db->Execute("UPDATE PLAYER SET MAPX=$sx, MAPY=$sy WHERE ID = $userid");
249 }
250 else if($_GET["PREVLOC"] <> "")
251 {
252 if($_GET["X"] != "")
253 $sql="SELECT X,Y FROM MAPLINKS WHERE MAPID = {$uservals['LOCATION']} AND NEWLOC = {$_GET['PREVLOC']} AND X = ".$_GET["X"]." AND Y = ".$_GET["Y"];
254 else
255 $sql="SELECT X,Y FROM MAPLINKS WHERE MAPID = {$uservals['LOCATION']} AND NEWLOC = {$_GET['PREVLOC']}";
256 $r=$db->Execute($sql);
257 if($r->EOF)
258 {
259 $r->Close();
260 $db->Close();
261 echo "WHERE DO YOU COME FROM ?\n";
262 die();
263 }
264 $sx=$r->fields[0]+0;
265 $sy=$r->fields[1]+0;
266 $r->Close();
267 $db->Execute("UPDATE PLAYER SET MAPX=$sx, MAPY=$sy WHERE ID = $userid");
268 }
269 else
270 {
271 $sx=$uservals["MAPX"];
272 $sy=$uservals["MAPY"];
273 }
274
275 if(($uservals["LOCATION"]+0) < 0 && $mapstr == "")
276 {
277 $sql="SELECT MAPDATA FROM PLAYER_MAP WHERE USERID=$userid AND LEVEL = ".(-$uservals['LOCATION']);
278 $r=$db->Execute($sql);
279 $mapstr=$r->fields[0];
280 $r->Close();
281 }
282 else if(($uservals["LOCATION"]+0) > 0 && $mapstr == "")
283 {
284 $sql="SELECT MAPDATA FROM MAPS WHERE ID = ".$uservals['LOCATION'];
285 $r=$db->Execute($sql);
286 $mapstr=$r->fields[0];
287 $r->Close();
288 }
289 $map=map_extract($mapstr);
290 $boat=false;
291 if(inventory_check(4051) > 0 || inventory_check(4055) > 0)
292 $boat=true;
293 if(check_walkable_map($map[$sx][$sy],$boat) == false) // Some error occured here...
294 {
295 activity_performed(3);
296 $db->Execute("UPDATE PLAYER SET LOCATION=RESURECT WHERE ID = $userid");
297 ob_end_clean();
298 echo "Wrong position in the map.<BR>\n";
299 echo "[<A HREF=game.php>Mmmmm....</A>]\n";
300 $db->Close();
301 exit;
302 }
303 }
304
305 /**
306 * Insert an object on the map... used for a daily unique object.
307 *
308 * @param unknown_type $objid
309 */
310 function unique_place($objid)
311 {
312 global $db;
313
314 $maps=array(8,9,17,21,42);
315 $mapid=$maps[mt_rand(0,count($maps)-1)];
316 $r=$db->Execute("SELECT MAPDATA,OBJECTDATA FROM MAPS WHERE ID = $mapid");
317 $map=map_extract($r->fields[0]);
318 $obj=map_extract($r->fields[1]);
319 $r->Close();
320
321 while(true)
322 {
323 $x=mt_rand(1,98);
324 $y=mt_rand(1,98);
325
326 if(check_walkable_map($map[$x][$y]) == true && $obj[$x][$y] == 0)
327 break;
328 }
329 $db->Execute("DELETE FROM UNIQUE_ITEMS WHERE OBJECTID=$objid");
330 $db->Execute("INSERT INTO UNIQUE_ITEMS(MAPID,OBJECTID,X,Y) VALUES($mapid,$objid,$x,$y)");
331 }
332
333 /**
334 * Returns the link to go to a neirboor map locations
335 *
336 * @param integer $x
337 * @param integer $y
338 * @return string
339 */
340 function grid_link($x,$y)
341 {
342 global $db;
343 $r=$db->Execute("SELECT ID FROM MAP_GRID WHERE X = $x AND Y = $y");
344 $str="game.php?NEWLOC=".$r->fields[0];
345 $r->Close();
346 return $str;
347 }
348
349 /**
350 * Returns the number of cells with a given type
351 *
352 * @param array $map
353 * @param integer $type
354 * @return integer
355 */
356 function map_count($map,$type)
357 {
358 if(is_array($type))
359 $types=$type;
360 else
361 $types[]=$type;
362
363 $nb=0;
364 for($i=0;$i < 99;$i++)
365 for($j=0;$j < 99;$j++)
366 if(in_array($map[$i][$j],$types))
367 $nb++;
368 return $nb;
369 }
370 ?>



FUNC_DATA={
'map2str': ['map2str', 'Convert a 2D map array into a encoded map string. ', [['libs','map_util.php',67]], 10],
'lenght_code': ['lenght_code', 'Run length encode the encoded map string. This saves space on the database as well as network during the transfer at the cost of some CPU. ', [['libs','map_util.php',109]], 11],
'map_extract': ['map_extract', 'Convert a encoded map string into a 2D array. ', [['libs','map_util.php',6]], 20],
'activity_performed': ['activity_performed', 'Increment a counter for the realtime statistics. Check the table ACTIVITIES to see the possible $acttype. ', [['libs','misc_util.php',227]], 5],
'unique_place': ['unique_place', 'Insert an object on the map... used for a daily unique object. ', [['libs','map_util.php',305]], 0],
'close': ['close', '', [['libs','db_conn_5.php',35],['libs','db_conn_5.php',87],['libs','db_conn_4.php',35],['libs','db_conn_4.php',87]], 380],
'retreive_loc': ['retreive_loc', 'retreives the current player location and returns it in the global variables sx,sy; ', [['libs','map_util.php',190]], 2],
'check_walkable_map': ['check_walkable_map', '', [['locations_modules/2dmap','map_script.js',389],['libs','map_util.php',156]], 21],
'map_count': ['map_count', 'Returns the number of cells with a given type ', [['libs','map_util.php',349]], 0],
'inventory_check': ['inventory_check', 'Checks if the player have this item. ', [['libs','objects_util.php',425]], 44],
'grid_link': ['grid_link', 'Returns the link to go to a neirboor map locations ', [['libs','map_util.php',333]], 4],
'execute': ['execute', '', [['libs','db_conn_5.php',37],['libs','db_conn_4.php',37]], 652],
'file_exists': ['file_exists', '', [], 48],
'floor': ['floor', '', [], 45],
'substr': ['substr', '', [], 91],
'is_array': ['is_array', '', [], 1],
'mt_rand': ['mt_rand', '', [], 102],
'strpos': ['strpos', '', [], 28],
'str_pad': ['str_pad', '', [], 11],
'count': ['count', '', [], 113],
'chr': ['chr', '', [], 19],
'ord': ['ord', '', [], 26],
'in_array': ['in_array', '', [], 27],
'ob_end_clean': ['ob_end_clean', '', [], 6]};
CLASS_DATA={
};
CONST_DATA={
};

titleDescriptionBody
titleDescriptionBody
titleDescriptionBody
titleBody



Generated: Sun Jul 8 18:11:25 2007
Cross-referenced by PHPXref 0.7





Wyszukiwarka

Podobne podstrony:
image map util
map util
image map util
image map util
image map util
map util
map util
Util js
browse map
map settings
view map
group util ?lay
Map js
TSM UTIL
Andorra mountain map
map settings

więcej podobnych podstron