コードは上下・左右の2種類で分けて書いています。(1種類にしたい)
なかなか時間がかかりましたが、これで探索ができそうです。
戦闘モジュール化もしたので、歩いていたら確率でMobが出てきます。
あとは出口。マップ中のどこか1つに設置する。
レアが入っている宝箱も作りたい・・・!
コードは今の所以下のようになってます。使う人いないと思うけどどうぞ。
$html_hunt .= "マップ生成<br>";
// 基礎データ設定
$HRM = []; // Map
$HRM2 = []; // 通路専用
// 部屋数、大きさ決定
$set_num = mt_rand(3,10);
$set_width = mt_rand(10,12);
$set_base = []; // 部屋の位置:$set_base["$x,$y"] = $i;
$set_room = []; // 部屋の位置、大きさ:$set_room[$i] = "$x_st,$y_st!$x_wd,$y_wd";
$set_root = []; // ルート設定:"$x1_st,$y1_st!$x1_ed,$y1_ed?$x2_st,$y2_st!$x2_ed,$y2_ed"; // 2本あり
// 基礎マップを生成
$x_set = $y_set = null;
$x_bef = $y_bef = null;
$set_rand = null;
for($i=0; $i<$set_num; $i++)
{
// 配置設定
$x_set = $x_set = null;
if($i == 0) // 初期設定
{
$x_set = 0;
$y_set = 0;
}
else
{
// 配置検索
$set_check = 0;
while(1)
{
// キー検索
$set_base_rand = mt_rand(0, count($set_base)-1);
$key = array_search($set_base_rand, $set_base);
// 空き地検索地設定
$pos = explode(",", $key);
$x = $x_bef = $pos[0];
$y = $y_bef = $pos[1];
// 検索開始
$while_n = 0;
while(1)
{
$set_rand = mt_rand(0, 3);
switch($set_rand)
{
case 0: // 上
$str = ($x) . ',' . ($y+1);
if(!isset($set_base[$str]))
{
$x_set = $x;
$y_set = $y+1;
$set_check = 1;
}
break;
case 1: // 右
$str = ($x+1) . ',' . ($y);
if(!isset($set_base[$str]))
{
$x_set = $x+1;
$y_set = $y;
$set_check = 1;
}
break;
case 2: // 下
$str = ($x) . ',' . ($y-1);
if(!isset($set_base[$str]))
{
$x_set = $x;
$y_set = $y-1;
$set_check = 1;
}
break;
case 3: // 左
$str = ($x-1) . ',' . ($y);
if(!isset($set_base[$str]))
{
$x_set = $x-1;
$y_set = $y;
$set_check = 1;
}
break;
}
// break
if($set_check){break 2;}
if($while_n > 5){break;}
$while_n++;
}
}
}
// 位置
$set_base["$x_set,$y_set"] = $i;
// 部屋大きさ
$x_room = mt_rand($set_width/3, $set_width - 3);
$y_room = mt_rand($set_width/3, $set_width - 3);
$x_st = mt_rand(1, $set_width - $x_room - 2);
$y_st = mt_rand(1, $set_width - $y_room - 2);
$x_wd = $x_room;
$y_wd = $y_room;
$set_room[$i] = "$x_st,$y_st!$x_wd,$y_wd";
// 設定
$x_room_bs = $set_width * $x_set + $x_st;
$y_room_bs = $set_width * $y_set + $y_st;
for($n=0; $n<=$x_wd; $n++)
{
for($o=0; $o<=$y_wd; $o++)
{
$keys = ($x_room_bs + $n) .','. ($y_room_bs + $o);
$HRM[$keys] = 1;
}
}
// ルート設定
if($i != 0) // $set_rand : 0上,1右,2下,3左
{
// 設定
$b_room = explode("!", $set_room[$set_base_rand]);
$b_st = explode(",", $b_room[0]);
$b_wd = explode(",", $b_room[1]);
$b_pos = explode(",", array_search($set_base_rand, $set_base));
$n_room = explode("!", $set_room[$i]);
$n_st = explode(",", $n_room[0]);
$n_wd = explode(",", $n_room[1]);
$n_pos = explode(",", array_search($i, $set_base));
switch($set_rand)
{
case 0: // 上(b下、n上)
$u_st_x = $b_pos[0]*$set_width + mt_rand(0, $b_wd[0]) + $b_st[0];
$u_st_y = $b_pos[1]*$set_width + $b_st[1] + $b_wd[1];
$d_st_x = $n_pos[0]*$set_width + mt_rand(0, $n_wd[0]) + $n_st[0];
$d_st_y = $n_pos[1]*$set_width + $n_st[1];
$a_ed_y = $n_pos[1]*$set_width - 1;
break;
case 1: // 右
$l_st_x = $b_pos[0]*$set_width + $b_st[0] + $b_wd[0];
$l_st_y = $b_pos[1]*$set_width + mt_rand(0, $b_wd[1]) + $b_st[1];
$r_st_x = $n_pos[0]*$set_width + $n_st[0];
$r_st_y = $n_pos[1]*$set_width + mt_rand(0, $n_wd[1]) + $n_st[1];
$a_ed_x = $n_pos[0]*$set_width - 1;
break;
case 2: // 下
$u_st_x = $n_pos[0]*$set_width + mt_rand(0, $n_wd[0]) + $n_st[0];
$u_st_y = $n_pos[1]*$set_width + $n_st[1] + $n_wd[1]; // 0まで上に
$d_st_x = $b_pos[0]*$set_width + mt_rand(0, $b_wd[0]) + $b_st[0];
$d_st_y = $b_pos[1]*$set_width + $b_st[1]; // 0まで下に
$a_ed_y = $b_pos[1]*$set_width - 1;
break;
case 3: // 左
$l_st_x = $n_pos[0]*$set_width + $n_st[0] + $n_wd[0];
$l_st_y = $n_pos[1]*$set_width + mt_rand(0, $n_wd[1]) + $n_st[1];
$r_st_x = $b_pos[0]*$set_width + $b_st[0];
$r_st_y = $b_pos[1]*$set_width + mt_rand(0, $b_wd[1]) + $b_st[1];
$a_ed_x = $b_pos[0]*$set_width - 1;
break;
}
if($set_rand == 0 || $set_rand == 2)
{
print "({$i}と" . ($set_base_rand) . ")基準線 y={$a_ed_y}<br>";
print "上開始座標({$u_st_x},{$u_st_y})<br>";
print "下開始座標({$d_st_x},{$d_st_y})<br>";
// 横線作成
if($u_st_x != $d_st_x)
{
$hl_st = $hl_ed = null;
if($u_st_x > $d_st_x)
{
$hl_st = $d_st_x;
$hl_ed = $u_st_x;
}
elseif($u_st_x < $d_st_x)
{
$hl_st = $u_st_x;
$hl_ed = $d_st_x;
}
// 追加
for($n=$hl_st; $n<=$hl_ed; $n++)
{
$HRM["{$n},{$a_ed_y}"] = 1;
$HRM2["{$n},{$a_ed_y}"] = 1;
}
}
// 縦線作成(上)
for($n=$u_st_y; $n<=$a_ed_y; $n++)
{
$HRM["{$u_st_x},{$n}"] = 1;
$HRM2["{$u_st_x},{$n}"] = 1;
}
// 縦線作成(下)
for($n=$a_ed_y; $n<=$d_st_y; $n++)
{
$HRM["{$d_st_x},{$n}"] = 1;
$HRM2["{$d_st_x},{$n}"] = 1;
}
}
elseif($set_rand == 1 || $set_rand == 3)
{
print "({$i}と" . ($set_base_rand) . ")基準線 x={$a_ed_x}<br>";
print "左開始座標({$l_st_x},{$l_st_y})<br>";
print "右開始座標({$r_st_x},{$r_st_y})<br>";
// 上線作成
if($l_st_y != $r_st_y)
{
$hl_st = $hl_ed = null;
if($l_st_y > $r_st_y)
{
$hl_st = $r_st_y;
$hl_ed = $l_st_y;
}
elseif($l_st_y < $r_st_y)
{
$hl_st = $l_st_y;
$hl_ed = $r_st_y;
}
// 追加
for($n=$hl_st; $n<=$hl_ed; $n++)
{
$HRM["{$a_ed_x},{$n}"] = 1;
$HRM2["{$a_ed_x},{$n}"] = 1;
}
}
// 横線作成(左)
for($n=$l_st_x; $n<=$a_ed_x; $n++)
{
$HRM["{$n},{$l_st_y}"] = 1;
$HRM2["{$n},{$l_st_y}"] = 1;
}
// 横作成(右)
for($n=$a_ed_x; $n<=$r_st_x; $n++)
{
$HRM["{$n},{$r_st_y}"] = 1;
$HRM2["{$n},{$r_st_y}"] = 1;
}
}
}
}
// 表示
print "部屋の数{$set_num}個、部屋の大きさ{$set_width}<br>";
foreach($set_base as $key => $val)
{
print "{$key}=>{$val}<br>";
}
print "<br>";
//print_r($set_room);
//print "<br>";
//
foreach($HRM as $key => $val)
{
print "{$key}<br>";
}
// div
$div = 10;
$div_base = 300;
foreach($set_room as $key => $val)
{
// タイプ取得
$type = explode("!", $val); // 0st,1wd
$pos = explode(",", $type[0]);
$wd = explode(",", $type[1]);
// 基礎キー取得
$base_key = explode("," ,array_search($key, $set_base));
// 設定
// 表示
$pos_x = ($pos[0] + $set_width * $base_key[0]) * $div + $div_base;
$pos_y = ($pos[1] + $set_width * $base_key[1]) * $div + $div_base;
$wd_x = ($wd[0]) * $div;
$wd_y = ($wd[1]) * $div;
// 基礎設定
$bp_x = ($set_width * $base_key[0]) * $div + $div_base;
$bp_y = ($set_width * $base_key[1]) * $div + $div_base;
$bs_x = $set_width * $div;
$bs_y = $set_width * $div;
// 表示
print "<div style='position: absolute; left: {$pos_x}px; top: {$pos_y}px; width: {$wd_x}px; height: {$wd_y}px; background-color: red; z-index: 1;'>$key</div>\n";
// 基礎表示
print "<div style='position: absolute; left: {$bp_x}px; top: {$bp_y}px; width: {$bs_x}px; height: {$bs_y}px; background-color: blue; z-index: 0;'>$key</div>\n";
}
// HRM
foreach($HRM as $key => $val)
{
// 設定
$hrm_pos = explode(",", $key);
// 表示
$hrm_pos_x = $hrm_pos[0] * $div + $div_base;
$hrm_pos_y = $hrm_pos[1] * $div + $div_base;
print "<div style='position: absolute; left: {$hrm_pos_x}px; top: {$hrm_pos_y}px; width: {$div}px; height: {$div}px; background-color: gray; z-index: 2;' title='{$hrm_pos[0]},{$hrm_pos[1]}'></div>\n";
}
// HRM2
foreach($HRM2 as $key => $val)
{
// 設定
$hrm_pos = explode(",", $key);
// 表示
$hrm_pos_x = $hrm_pos[0] * $div + $div_base;
$hrm_pos_y = $hrm_pos[1] * $div + $div_base;
print "<div style='position: absolute; left: {$hrm_pos_x}px; top: {$hrm_pos_y}px; width: {$div}px; height: {$div}px; background-color: red; z-index: 3;' title='{$hrm_pos[0]},{$hrm_pos[1]}'></div>\n";
}

