Skip to content

Instantly share code, notes, and snippets.

@Misiur
Created March 8, 2015 13:20
Show Gist options
  • Save Misiur/99d23545ebdefdde4f57 to your computer and use it in GitHub Desktop.
Save Misiur/99d23545ebdefdde4f57 to your computer and use it in GitHub Desktop.
#include <a_samp>
#include <YSI\y_iterate>
//#include <YSI\y_commands>
main(){}
#define xsize 25
#define ysize 25
enum info { bool:in, bool:up, bool:left, prevx, prevy, platform }
new Iterator:IN<(xsize*ysize)+1>,
Iterator:Cells[xsize]<ysize>,
Iterator:Rows<xsize>,
/*, Iterator:FRONTIER<(xsize*ysize)+1>,*/
Current[2], numin, CELL[xsize][ysize][info];
public OnGameModeInit()
{
Iter_Init(Cells);
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;
}
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);
CELL[X][Y][up] = !(X == 0 || X == xsize-1 || Y==0);
CELL[X][Y][left] = !(X == 0 || Y == 0||Y == ysize-1);
}
return 1;
}
ReloadCells()
{
Iter_Clear(Rows);
for (new i = 0; i != xsize; ++i) {
Iter_Clear(Cells[i]);
}
new bool:isBoundary = false;
for(new X = 0; X != xsize; ++X)
{
isBoundary = X == 0 || X == xsize - 1;
if(!isBoundary) Iter_Add(Rows, X);
for(new Y = 0; Y != ysize; ++Y)
{
if (!(isBoundary || Y == 0 || Y== ysize - 1)) {
Iter_Add(Cells[X], Y);
}
}
}
}
generate()
{
new bool:success, Index[3];
do
{
//Each time we want all cells
ReloadCells();
Index[0]++;
do
{
Index[1]++;
Current[0] = Iter_Random(Rows);
Current[1] = Iter_Random(Cells[Current[0]]);
if(Index[1] > (xsize * ysize))
{
print("Index[1] = Force Break");
break;
}
Iter_Remove(Cells[Current[0]], Current[1]);
//If this row is empty, we won't use it anymore
if (!Iter_Count(Cells[Current[0]])) {
Iter_Remove(Rows, Current[0]);
if (!Iter_Count(Rows)) {
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);
new
colour[2] = { 0xFFFFFFFF, 0xFF000000 },
Float:coord[2] = { 195.0, 200.0 }
;
new grid[2048] = { 0, ... };
for(new X=0;X<xsize;X++)
{
for(new Y=0;Y<ysize;Y++)
{
new
idx = -1
;
if(X%2 == 1 && Y%2 == 1)
{
idx = _:!CELL[X/2+1][Y/2+1][in];
strcat(grid, (CELL[X/2+1][Y/2+1][in] ? "M" : "_"));
}
else if(X%2 == 0 && Y%2 == 0)
{
idx = 1;
strcat(grid, "M");
}
else if(X%2 == 0 && Y%2 == 1)
{
idx = _:CELL[X/2+1][Y/2+1][left];
strcat(grid, (CELL[X/2+1][Y/2+1][left] ? "M" : "_"));
}
else if(X%2 == 1 && Y%2 == 0)
{
idx = _:CELL[X/2+1][Y/2+1][up];
strcat(grid, (CELL[X/2+1][Y/2+1][up] ? "M" : "_"));
}
else continue;
CELL[X][Y][platform] = CreateObject(19790, 2.5+(X*5), 2.5+(Y*5), coord[idx], 0.0, 0.0, 0.0);
SetObjectMaterialText(CELL[X][Y][platform], " ", .backcolor = colour[idx]);
Iter_Add(IN, CELL[X][Y][platform]);
}
strcat(grid, "\n");
}
print(grid);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment