日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > php >内容正文

php

php制作随机地图,随机生成Roguelike游戏地图算法

發(fā)布時(shí)間:2024/4/13 php 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php制作随机地图,随机生成Roguelike游戏地图算法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

www.roguebasin.com 有很多相關(guān)的roguelike資料 相當(dāng)有價(jià)值

首先隨機(jī)生成一套地圖世界有幾個(gè)概念。 數(shù)據(jù)結(jié)構(gòu) 1.world 2.map 3.room 4.corridor

地圖的類型也有很多種 1.maze 2.dungeon 3.island 4.cave 5.castle

生成房間的算法也分為很多類別 我先實(shí)現(xiàn)了 最簡(jiǎn)單的一個(gè)地圖類型 類似于dungeon的地圖類型

生成的數(shù)據(jù)大概是這樣

算法邏輯 1.在world創(chuàng)建room 2.判斷room是否碰撞 碰撞重新生成 3.房間生成門 4.查找最近的房間 使用astar 生成房間直接連通的街道。

一些c#代碼

public void generatePCGBasic(byte[,] g) { base.generatePCG(g); // Init grid this.initRooms(); // Initialize rooms //this.initCorridors(); // Initialize corridors }

public void initRooms()

{

this.rooms = new ArrayList();

//room_num = 1;

// New room arraylist

//Log.info("room_num",this.room_num);

for (int n = 0; n < this.room_num; n++)

{

this.room_blocked = false;

// Unblock

Room rm = new Room(pcgrid_width, pcgrid_height, this.room_base, this.room_radix, this.corridor_num);

// Create new room

this.room_blocked = this.blockRoom(rm);

// Check if room is blocked

if (this.room_blocked)

{

n--;

// Remake room

this.redo--;

// Stops if taking too long

if ((this.redo == 0))

{

this.room_num--;

this.redo = 1000;

// Recursion limit

}

}

else {

//Log.info("room_blocked",this.room_blocked);

this.rooms.Add(rm);

// Create room

for (int j = rm.room_y1; (j <= rm.room_y2); j++)

{

for (int i = rm.room_x1; (i <= rm.room_x2); i++)

{

pcgrid[i, j] = 1;

}

}

// Create room walls

for (int i = rm.wall_x1; (i <= rm.wall_x2); i++)

{

if (pcgrid[i, rm.wall_y1] != 1)

{

pcgrid[i, rm.wall_y1] = 2;

}

if (pcgrid[i, rm.wall_y2] != 1)

{

pcgrid[i, rm.wall_y2] = 2;

}

}

for (int j = rm.wall_y1; (j <= rm.wall_y2); j++)

{

if (pcgrid[rm.wall_x1, j] != 1)

{

pcgrid[rm.wall_x1, j] = 2;

}

if (pcgrid[rm.wall_x2, j] != 1)

{

pcgrid[rm.wall_x2, j] = 2;

}

}

// Place openings

for (int k = 0; (k < rm.opening_num); k++)

{

if ((pcgrid[rm.opening[k, 0], rm.opening[k, 1]] != 1))

{

pcgrid[rm.opening[k, 0], rm.opening[k, 1]] = 3;

}

}

/*

var sb = new StringBuilder();

sb.Append("\n");

for (int i = 0; i < pcgrid.GetLength(0); i++)

{

for (int j = 0; j < pcgrid.GetLength(1); j++)

{

sb.Append( pcgrid[i, j] );

}

sb.Append("\n");

}

Log.info(sb.ToString());

*/ }

}

}

public bool blockRoom(Room rm)

{

// If outside of grid

if ((!bounded(rm.wall_x1, rm.wall_y1)

|| (!bounded(rm.wall_x2, rm.wall_y1)

|| (!bounded(rm.wall_x1, rm.wall_y2)

|| !bounded(rm.wall_x2, rm.wall_y2)))))

{

return true;

}

// If blocked by another room

if ((this.room_type != 3))

{

for (int i = (rm.wall_x1 - 1); (i

< (rm.wall_x2 + 1)); i++)

{

// Check upper and lower bound

if ((bounded(i, (rm.wall_y1 - 1))

&& !blocked(i, (rm.wall_y1 - 1), 0)))

{

return true;

}

if ((bounded(i, (rm.wall_y2 + 1))

&& !blocked(i, (rm.wall_y2 + 1), 0)))

{

return true;

}

}

for (int j = (rm.wall_y1 - 1); (j

< (rm.wall_y2 + 1)); j++)

{

// Check left and right bound

if ((bounded((rm.wall_x1 - 1), j)

&& !blocked((rm.wall_x1 - 1), j, 0)))

{

return true;

}

if ((bounded((rm.wall_x2 + 1), j)

&& !blocked((rm.wall_x2 + 1), j, 0)))

{

return true;

}

}

}

return false;

}

public void initCorridors() {

if ((this.room_type != 3)) {

for (int i = 0; (i < this.rooms.Count); i++) {

// Go through each room and connect its first opening to the first opening of the next room

Room rm1 = ((Room)(this.rooms[i]));

Room rm2;

if ((i

== (this.rooms.Count - 1))) {

rm2 = ((Room)(this.rooms[0]));

}

else {

rm2 = ((Room)(this.rooms[(i + 1)]));

}

// If not last room

// Connect rooms

//basicAStar(pcgrid, rm1.opening[0,0], rm1.opening[0,1], rm2.opening[0,0], rm2.opening[0,1], this.corridor_weight, this.turning_weight);

// Random tunneling

for (int j = 1; (j < rm1.opening_num); j++) {

this.tunnelRandom(rm1.opening[j,0], rm1.opening[j,1], rm1.opening[j,2], 3);

}

}

}

else {

// If complex

Room rm1 = ((Room)(this.rooms[0]));

for (int i = 1; (i < this.rooms.Count); i++) {

// Go through each room and connect its first opening to the first opening of the first room

Room rm2 = ((Room)(this.rooms[i]));

// Connect rooms

//basicAStar(pcgrid, rm1.opening[0,0], rm1.opening[0,1], rm2.opening[0,0], rm2.opening[0,1], this.corridor_weight, this.turning_weight);

}

// Random tunneling

for (int i = 0; (i < this.rooms.Count); i++) {

Room rm3 = ((Room)(this.rooms[i]));

for (int j = 1; (j < rm3.opening_num); j++) {

this.tunnelRandom(rm3.opening[j,0], rm3.opening[j,1], rm3.opening[j,2], 3);

}

}

}

}

public void tunnel(int x, int y, int dir)

{

if (((pcgrid[x, y] == 2)

|| (pcgrid[x, y] == 3)))

{

pcgrid[x, y] = 3;

}

// If on top of wall or door

pcgrid[x, y] = 4;

// Set cell to corridor

this.tunnelRandom(x, y, this.shuffleDir(dir, 85), 3);

// Randomly choose next cell to go to

}

public void tunnelRandom(int x, int y, int dir, int iteration)

{

if ((iteration == 0))

{

return;

}

// End of recursion iteration

// Choose a random direction and check to see if that cell is occupied, if not, head in that direction

switch (dir)

{

case 0:

if (!this.blockCorridor(x, (y - 1), 0))

{

this.tunnel(x, (y - 1), dir);

}

// North

this.tunnelRandom(x, y, this.shuffleDir(dir, 0), (iteration - 1));

// Try again

break;

case 1:

if (!this.blockCorridor((x + 1), y, 1))

{

this.tunnel((x + 1), y, dir);

}

// East

this.tunnelRandom(x, y, this.shuffleDir(dir, 0), (iteration - 1));

// Try again

break;

case 2:

if (!this.blockCorridor(x, (y + 1), 0))

{

this.tunnel(x, (y + 1), dir);

}

// South

this.tunnelRandom(x, y, this.shuffleDir(dir, 0), (iteration - 1));

// Try again

break;

case 3:

if (!this.blockCorridor((x - 1), y, 1))

{

this.tunnel((x - 1), y, dir);

}

// West

this.tunnelRandom(x, y, this.shuffleDir(dir, 0), (iteration - 1));

// Try again

break;

}

}吃的

總結(jié)

以上是生活随笔為你收集整理的php制作随机地图,随机生成Roguelike游戏地图算法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。