Skip to content

Instantly share code, notes, and snippets.

@Crayder
Last active August 29, 2015 14:16
Show Gist options
  • Save Crayder/9a65c5a1e301443e5c3e to your computer and use it in GitHub Desktop.
Save Crayder/9a65c5a1e301443e5c3e to your computer and use it in GitHub Desktop.
Maze Generating Gamemode
#include <a_samp>
#include <YSI\y_iterate>
//#include <YSI\y_commands>
main(){}
#define xsize 36
#define ysize 24
forward initialize();
forward generate();
enum info { bool:in, bool:up, bool:left, prevx, prevy, platform }
new Iterator:IN<(xsize*ysize)+1>, Iterator:FRONTIER<(xsize*ysize)+1>,
Current[2], numin, CELL[xsize][ysize][info];
public OnGameModeInit()
{
initialize();
generate();
return 1;
}
public OnPlayerSpawn(playerid)
{
SetPlayerPos(playerid, 7.5, 7.5, 201.0);
return 1;
}
public OnPlayerRequestClass(playerid,classid)
{
SetPlayerCameraPos(playerid, (xsize*5)/2, (ysize*5)/2, 10.0);
SetPlayerCameraLookAt(playerid, (xsize*5)/2, (ysize*5)/2, 250.0);
return 1;
}
public initialize()
{
for(new X=0;X<xsize;X++) for(new Y=0;Y<ysize;Y++)
{
CELL[X][Y][in] = (X==0||X==xsize-1||Y==0||Y==ysize-1) ? true : false;
CELL[X][Y][up] = (X==0||X==xsize-1||Y==0) ? false : true;
CELL[X][Y][left] = (X==0||Y==0||Y==ysize-1) ? false : true;
}
return 1;
}
public generate()
{
new bool:success, Index[3];
do
{
Index[0]++;
/*while(CELL[Current[0]][Current[1]-1][in]&&CELL[Current[0]][Current[1]+1][in]&&
CELL[Current[0]-1][Current[1]][in]&&CELL[Current[0]+1][Current[1]][in])
{
Index[1]++;
new xcur2=CELL[Current[0]][Current[1]][prevx];
Current[1]=CELL[Current[0]][Current[1]][prevy];
Current[0]=xcur2;
if(Index[1] > (xsize*ysize))
{
print("Index[1] = Force Break");
break;
}
}*/
do
{
Index[1]++;
Current[0]=(random(((xsize-2)+1)-1)+1);
Current[1]=(random(((xsize-2)+1)-1)+1);
if(Index[1] > (xsize*ysize))
{
print("Index[1] = Force Break");
break;
}
}
while(!CELL[Current[0]][Current[1]][in]||
(CELL[Current[0]][Current[1]-1][in]&&CELL[Current[0]][Current[1]+1][in]&&
CELL[Current[0]-1][Current[1]][in]&&CELL[Current[0]+1][Current[1]][in]));
Index[1] = 0;
do
{
Index[2]++;
success=false;
switch(random(4))
{
case 0: if(!CELL[Current[0]][Current[1]-1][in])
{
success=true;
CELL[Current[0]][Current[1]][up]=false;
CELL[Current[0]][Current[1]-1][prevx]=Current[0];
CELL[Current[0]][Current[1]-1][prevy]=Current[1];
Current[1]--;
}
case 1: if(!CELL[Current[0]][Current[1]+1][in])
{
success=true;
CELL[Current[0]][Current[1]+1][up]=false;
CELL[Current[0]][Current[1]+1][prevx]=Current[0];
CELL[Current[0]][Current[1]+1][prevy]=Current[1];
Current[1]++;
}
case 2: if(!CELL[Current[0]-1][Current[1]][in])
{
success=true;
CELL[Current[0]][Current[1]][left]=false;
CELL[Current[0]-1][Current[1]][prevx]=Current[0];
CELL[Current[0]-1][Current[1]][prevy]=Current[1];
Current[0]--;
}
case 3: if(!CELL[Current[0]+1][Current[1]][in])
{
success=true;
CELL[Current[0]+1][Current[1]][left]=false;
CELL[Current[0]+1][Current[1]][prevx]=Current[0];
CELL[Current[0]+1][Current[1]][prevy]=Current[1];
Current[0]++;
}
}
if(Index[2] > (xsize*ysize))
{
print("Index[2] = Force Break");
break;
}
}
while(!success);
Index[2] = 0;
CELL[Current[0]][Current[1]][in]=true;
numin++;
}
while(numin<(xsize-2)*(ysize-2));
foreach(new i: IN) DestroyObject(i);
Iter_Clear(IN);
for(new X=0;X<xsize;X++) for(new Y=0;Y<ysize;Y++)
{
if(X%2 == 1 && Y%2 == 1)
{
CELL[X][Y][platform] = CreateObject(19790, 2.5+(X*5), 2.5+(Y*5), CELL[X/2+1][Y/2+1][in]?195.0:200.0, 0.0, 0.0, 0.0);
SetObjectMaterialText(CELL[X][Y][platform], " ", .backcolor = CELL[X/2+1][Y/2+1][in]?0xFFFFFFFF:0xFF000000);
Iter_Add(IN, CELL[X][Y][platform]);
}
else if(X%2 == 0 && Y%2 == 0)
{
CELL[X][Y][platform] = CreateObject(19790, 2.5+(X*5), 2.5+(Y*5), 200.0, 0.0, 0.0, 0.0);
SetObjectMaterialText(CELL[X][Y][platform], " ", .backcolor = 0xFF000000);
Iter_Add(IN, CELL[X][Y][platform]);
}
else if(X%2 == 0 && Y%2 == 1)
{
CELL[X][Y][platform] = CreateObject(19790, 2.5+(X*5), 2.5+(Y*5), CELL[X/2+1][Y/2+1][left]?200.0:195.0, 0.0, 0.0, 0.0);
SetObjectMaterialText(CELL[X][Y][platform], " ", .backcolor = CELL[X/2+1][Y/2+1][left]?0xFF000000:0xFFFFFFFF);
Iter_Add(IN, CELL[X][Y][platform]);
}
else if(X%2 == 1 && Y%2 == 0)
{
CELL[X][Y][platform] = CreateObject(19790, 2.5+(X*5), 2.5+(Y*5), CELL[X/2+1][Y/2+1][up]?200.0:195.0, 0.0, 0.0, 0.0);
SetObjectMaterialText(CELL[X][Y][platform], " ", .backcolor = CELL[X/2+1][Y/2+1][up]?0xFF000000:0xFFFFFFFF);
Iter_Add(IN, CELL[X][Y][platform]);
}
}
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment