PHPXRef 0.7 : NEABExplorer : /commun_tools/make_island.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]
/commun_tools/ -> make_island.php (source)
[Summary view]
[Print]
[Text view]
1 <?php
2 /**
3 * Functions to create a new island
4 */
5
6 include_once "$basedir/libs/map_util.php";
7
8 $mtypes[0]=array(0,26,27,28,29,30,31,32,33,34,35,36,37);
9 $mtypes[1]=array(52,54,56,53,55,60,59,58,57,61,62,64,63);
10
11 function cell_type($n)
12 {
13 if($n >= 23 && $n <= 25) // sand
14 return 1;
15 if($n >= 26 && $n <= 37) // sand and grass
16 return 2;
17 if($n >= 0 && $n <= 7) // grass
18 return 0;
19 if($n == 52)
20 return 3;
21 if($n >= 53 && $n <= 64) // watter and sand
22 return 4;
23 }
24
25 function is_sandborder($n)
26 {
27 if($n >= 53 && $n <= 64) // watter and sand
28 return false;
29 if($n >= 23 && $n <= 25) // sand
30 return false;
31 return true;
32 }
33
34 function is_waterborder($n)
35 {
36 if($n >= 52 && $n <= 64) // watter and sand
37 return true;
38 return false;
39 }
40
41 function make_in_borders(&$map)
42 {
43 make_water_borders(&$map);
44 make_sand_borders(&$map);
45 }
46
47 function make_water_borders(&$map)
48 {
49 global $mtypes;
50
51 $t=1;
52 for($i=1;$i < 99;$i++)
53 {
54 for($j=1;$j < 99;$j++)
55 {
56 // Water and sand border
57 if(is_waterborder($map[$i][$j]) != true)
58 continue;
59 $n=is_waterborder($map[$i][$j-1]);
60 $s=is_waterborder($map[$i][$j+1]);
61 $e=is_waterborder($map[$i+1][$j]);
62 $w=is_waterborder($map[$i-1][$j]);
63 $ne=is_waterborder($map[$i+1][$j-1]);
64 $nw=is_waterborder($map[$i-1][$j-1]);
65 $sw=is_waterborder($map[$i-1][$j+1]);
66 $se=is_waterborder($map[$i+1][$j+1]);
67 if($n && $s && $w && $e && $ne && $nw && $sw && $se)
68 continue;
69
70 if(!$n && $s && $w && $e)
71 $map[$i][$j]=$mtypes[$t][4];
72 else if($n && !$s && $w && $e)
73 $map[$i][$j]=$mtypes[$t][1];
74 else if($n && $s && !$w && $e)
75 $map[$i][$j]=$mtypes[$t][2];
76 else if($n && $s && $w && !$e)
77 $map[$i][$j]=$mtypes[$t][3];
78
79 else if(!$n && $s && !$w && $e)
80 $map[$i][$j]=$mtypes[$t][5];
81 else if(!$n && $s && $w && !$e)
82 $map[$i][$j]=$mtypes[$t][6];
83 else if($n && !$s && $w && !$e)
84 $map[$i][$j]=$mtypes[$t][7];
85 else if($n && !$s && !$w && $e)
86 $map[$i][$j]=$mtypes[$t][8];
87
88 else if(!$ne)
89 $map[$i][$j]=$mtypes[$t][9];
90 else if(!$nw)
91 $map[$i][$j]=$mtypes[$t][10];
92 else if(!$se)
93 $map[$i][$j]=$mtypes[$t][11];
94 else if(!$sw)
95 $map[$i][$j]=$mtypes[$t][12];
96 }
97 }
98 }
99
100 function make_sand_borders(&$map)
101 {
102 global $mtypes;
103
104 $t=0;
105 for($i=1;$i < 99;$i++)
106 {
107 for($j=1;$j < 99;$j++)
108 {
109 // Sand and grass border
110 if(cell_type($map[$i][$j]) != 0)
111 continue;
112 $n=is_sandborder($map[$i][$j-1]);
113 $s=is_sandborder($map[$i][$j+1]);
114 $e=is_sandborder($map[$i+1][$j]);
115 $w=is_sandborder($map[$i-1][$j]);
116 $ne=is_sandborder($map[$i+1][$j-1]);
117 $nw=is_sandborder($map[$i-1][$j-1]);
118 $se=is_sandborder($map[$i+1][$j+1]);
119 $sw=is_sandborder($map[$i-1][$j+1]);
120
121 if($n && $s && $w && $e && $ne && $nw && $sw && $se)
122 continue;
123
124 if(!$n && !$s && $w && $e)
125 $map[$i][$j]=25;
126 else if($n && $s && !$w && !$e)
127 $map[$i][$j]=25;
128
129 else if(!$n && $s && $w && $e)
130 $map[$i][$j]=$mtypes[$t][4];
131 else if($n && !$s && $w && $e)
132 $map[$i][$j]=$mtypes[$t][1];
133 else if($n && $s && !$w && $e)
134 $map[$i][$j]=$mtypes[$t][2];
135 else if($n && $s && $w && !$e)
136 $map[$i][$j]=$mtypes[$t][3];
137
138 else if(!$n && $s && !$w && $e)
139 $map[$i][$j]=$mtypes[$t][5];
140 else if(!$n && $s && $w && !$e)
141 $map[$i][$j]=$mtypes[$t][6];
142 else if($n && !$s && $w && !$e)
143 $map[$i][$j]=$mtypes[$t][7];
144 else if($n && !$s && !$w && $e)
145 $map[$i][$j]=$mtypes[$t][8];
146
147 else if(!$ne)
148 $map[$i][$j]=$mtypes[$t][9];
149 else if(!$nw)
150 $map[$i][$j]=$mtypes[$t][10];
151 else if(!$se)
152 $map[$i][$j]=$mtypes[$t][11];
153 else if(!$sw)
154 $map[$i][$j]=$mtypes[$t][12];
155 }
156 }
157 }
158
159 function drawline(&$map,$x1,$y1,$x2,$y2,$c)
160 {
161 $dX = abs($x2-$x1); // store the change in X and Y of the line endpoints
162 $dY = abs($y2-$y1);
163
164 //------------------------------------------------------------------------
165 // DETERMINE "DIRECTIONS" TO INCREMENT X AND Y (REGARDLESS OF DECISION)
166 //------------------------------------------------------------------------
167 if ($x1 > $x2) { $Xincr=-1; } else { $Xincr=1; } // which direction in X?
168 if ($y1 > $y2) { $Yincr=-1; } else { $Yincr=1; } // which direction in Y?
169
170 //------------------------------------------------------------------------
171 // DETERMINE INDEPENDENT VARIABLE (ONE THAT ALWAYS INCREMENTS y2 1 (OR -1) )
172 // AND INITIATE APPROPRIATE LINE DRAWING ROUTINE (BASED ON FIRST OCTANT
173 // ALWAYS). THE X AND Y'S MAY BE FLIPPED IF Y IS THE INDEPENDENT VARIABLE.
174 //------------------------------------------------------------------------
175 if ($dX >= $dY) // if X is the independent variable
176 {
177 $dPr = $dY<<1; // amount to increment decision if right is chosen (always)
178 $dPru = $dPr - ($dX<<1); // amount to increment decision if up is chosen
179 $P = $dPr - $dX; // decision variable start value
180
181 for (; $dX>=0; $dX--) // process each point in the line one at a time (just use dX)
182 {
183 if($x1 >= 0 && $y1 >= 0 && $x1 < 100 && $y1 < 100)
184 {
185 $map[$x1][$y1]=$c;
186 }
187 if ($P > 0) // is the pixel going right AND up?
188 {
189 $x1+=$Xincr; // increment independent variable
190 $y1+=$Yincr; // increment dependent variable
191 $P+=$dPru; // increment decision (for up)
192 }
193 else // is the pixel just going right?
194 {
195 $x1+=$Xincr; // increment independent variable
196 $P+=$dPr; // increment decision (for right)
197 }
198 }
199 }
200 else // if Y is the independent variable
201 {
202 $dPr = $dX<<1; // amount to increment decision if right is chosen (always)
203 $dPru = $dPr - ($dY<<1); // amount to increment decision if up is chosen
204 $P = $dPr - $dY; // decision variable start value
205
206 for (; $dY>=0; $dY--) // process each point in the line one at a time (just use dY)
207 {
208 if($x1 >= 0 && $y1 >= 0 && $x1 < 100 && $y1 < 100)
209 {
210 $map[$x1][$y1]=$c;
211 }
212 //drawRGBPixel(r,g,b,x1, y1); // plot the pixel
213 if ($P > 0) // is the pixel going up AND right?
214 {
215 $x1+=$Xincr; // increment dependent variable
216 $y1+=$Yincr; // increment independent variable
217 $P+=$dPru; // increment decision (for up)
218 }
219 else // is the pixel just going up?
220 {
221 $y1+=$Yincr; // increment independent variable
222 $P+=$dPr; // increment decision (for right)
223 }
224 }
225 }
226 }
227
228 function borders_water($map,$x,$y)
229 {
230 if($map[$x][$y-1] == 52 || $map[$x][$y+1] == 52 || $map[$x-1][$y] == 52 || $map[$x+1][$y] == 52)
231 return true;
232 return false;
233 }
234
235 function borders_ground($map,$x,$y)
236 {
237 if($map[$x][$y-1] == 0 || $map[$x][$y+1] == 0 || $map[$x-1][$y] == 0 || $map[$x+1][$y] == 0)
238 return true;
239 if($map[$x][$y-1] == 23 || $map[$x][$y+1] == 23 || $map[$x-1][$y] == 23 || $map[$x+1][$y] == 23)
240 return true;
241 }
242
243 function make_island(&$map,&$obj)
244 {
245 // Init the arrays
246 for($i=0;$i < 100;$i++)
247 for($j=0;$j < 100;$j++)
248 $map[$i][$j]=52;
249
250 for($i=0;$i < 100;$i++)
251 for($j=0;$j < 100;$j++)
252 $obj[$i][$j]=0;
253
254 // Create the base islands
255 for($n=0;$n < mt_rand(1,10);$n++)
256 {
257 $w=mt_rand(3,50);
258 $h=mt_rand(5,20);
259 $mx=mt_rand(-30,30);
260 $my=mt_rand(-30,30);
261 for($i=0;$i < $h;$i++)
262 {
263 $wc=round($w*(($h-$i)/$h));
264 drawline($map,50-round($wc/2)+$mx,$my+49-$i,$mx+50+round($wc/2),$my+49-$i,0);
265 drawline($map,50-round($wc/2)+$mx,$my+49+$i,$mx+50+round($wc/2),$my+49+$i,0);
266 $w+=mt_rand(-4,4);
267 if($w <= 1)
268 break;
269 if($w > 90)
270 $w=90;
271 }
272 }
273
274 for($i=0;$i < 100;$i++)
275 for($j=0;$j < 100;$j++)
276 $dmap[$i][$j]=52;
277
278 $u=mt_rand(3,8);
279 $v=mt_rand(3,8);
280 $d=mt_rand(6,20);
281 $ang=mt_rand(0,360)/180*M_PI;
282
283 // Rotate and deform
284 for($i=0;$i < 100;$i++)
285 {
286 for($j=0;$j < 100;$j++)
287 {
288 $a=sin(($i+$j+$u)/$d)*3+$i;
289 $b=cos(($i+$j+$v)/$d)*3+$j;
290 $a=50-$a;
291 $b=50-$b;
292 $x = $a * cos($ang) - $b * sin($ang);
293 $y = $b * cos($ang) + $a * sin($ang);
294
295 $x=50-$x;
296 $y=50-$y;
297
298 if($x < 0)
299 $x=0;
300 if($x > 99)
301 $x=99;
302 if($y < 0)
303 $y=0;
304 if($y > 99)
305 $y=99;
306 $dmap[$i][$j]=$map[$x][$y];
307 }
308 }
309
310 // Cleanup the borders
311 drawline($dmap,0,0,99,0,52);
312 drawline($dmap,0,1,99,1,52);
313 drawline($dmap,0,2,99,2,52);
314
315 drawline($dmap,0,99,99,99,52);
316 drawline($dmap,0,98,99,98,52);
317 drawline($dmap,0,97,99,97,52);
318
319 drawline($dmap,0,0,0,99,52);
320 drawline($dmap,1,0,1,99,52);
321 drawline($dmap,2,0,2,99,52);
322
323 drawline($dmap,99,0,99,99,52);
324 drawline($dmap,98,0,98,99,52);
325 drawline($dmap,97,0,97,99,52);
326
327 $map=$dmap;
328
329 // Remove 2 corrners
330 for($i=1;$i < 99;$i++)
331 for($j=1;$j < 99;$j++)
332 {
333 if($map[$i+1][$j+1] == 0 && $map[$i-1][$j-1] == 0)
334 $map[$i][$j]=0;
335 if($map[$i-1][$j+1] == 0 && $map[$i+1][$j-1] == 0)
336 $map[$i][$j]=0;
337 }
338
339 // Remove water noise
340 for($i=1;$i < 99;$i++)
341 for($j=1;$j < 99;$j++)
342 {
343 if($map[$i][$j] == 52 && $map[$i][$j+1] == 0 && $map[$i][$j-1] == 0)
344 $map[$i][$j]=0;
345 if($map[$i][$j] == 52 && $map[$i+1][$j] == 0 && $map[$i-1][$j] == 0)
346 $map[$i][$j]=0;
347 if($map[$i][$j] == 52 && $map[$i][$j+1] == 0 && $map[$i][$j-1] == 0)
348 $map[$i][$j]=0;
349 }
350 // Remove land noise
351 for($i=1;$i < 99;$i++)
352 for($j=1;$j < 99;$j++)
353 {
354 if($map[$i][$j] == 0 && $map[$i][$j+1] == 52 && $map[$i][$j-1] == 52)
355 $map[$i][$j]=52;
356 if($map[$i][$j] == 0 && $map[$i+1][$j] == 52 && $map[$i-1][$j] == 52)
357 $map[$i][$j]=52;
358 }
359
360 // Randomize the grass
361 for($i=0;$i < 100;$i++)
362 for($j=0;$j < 100;$j++)
363 {
364 if($map[$i][$j] == 0 && mt_rand(0,6) == 0)
365 $map[$i][$j]=mt_rand(3,7);
366 else if($map[$i][$j] == 0)
367 $map[$i][$j]=mt_rand(0,2);
368 }
369
370 // Add trees on the grass
371 $trees=array(5,57,58,62,63,59,133);
372 for($i=0;$i < 100;$i++)
373 for($j=0;$j < 100;$j++)
374 if($map[$i][$j] < 8 && mt_rand(0,30) == 0)
375 $obj[$i][$j]=$trees[mt_rand(0,count($trees)-1)];
376 }
377 ?>
FUNC_DATA={
'is_sandborder': ['is_sandborder', '', [['commun_tools','make_island.php',25],['admin_tools/map_editor','map_editor.php',354]], 16],
'make_sand_borders': ['make_sand_borders', '', [['commun_tools','make_island.php',100],['admin_tools/map_editor','map_editor.php',519]], 2],
'borders_ground': ['borders_ground', '', [['commun_tools','make_island.php',235]], 0],
'drawline': ['drawline', '', [['commun_tools','make_island.php',159]], 14],
'make_island': ['make_island', '', [['commun_tools','make_island.php',243]], 2],
'make_water_borders': ['make_water_borders', '', [['commun_tools','make_island.php',47],['admin_tools/map_editor','map_editor.php',465]], 2],
'make_in_borders': ['make_in_borders', '', [['commun_tools','make_island.php',41],['admin_tools/map_editor','map_editor.php',386]], 3],
'cell_type': ['cell_type', '', [['commun_tools','make_island.php',11],['admin_tools/map_editor','map_editor.php',340]], 2],
'borders_water': ['borders_water', '', [['commun_tools','make_island.php',228]], 0],
'is_waterborder': ['is_waterborder', '', [['commun_tools','make_island.php',34],['admin_tools/map_editor','map_editor.php',377]], 18],
'mt_rand': ['mt_rand', '', [], 102],
'count': ['count', '', [], 113],
'abs': ['abs', '', [], 17],
'sin': ['sin', '', [], 3],
'time': ['time', '', [], 16],
'round': ['round', '', [], 19],
'cos': ['cos', '', [], 3]};
CLASS_DATA={
};
CONST_DATA={
};
titleDescriptionBody
titleDescriptionBody
titleDescriptionBody
titleBody
Generated: Sun Jul 8 18:11:25 2007
Cross-referenced by PHPXref 0.7
Wyszukiwarka