Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AndrewAMD, Ayumi), 1,393 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 3 1 2 3
Maze generation algorithm #430560
09/28/13 19:05
09/28/13 19:05
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline OP
Senior Expert
3run  Offline OP
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
As my math really does suck, I want to ask someone to lend me a hand of help, to create a maze generator.
There are those several algorithms on the Wiki, and I would like to see, how one of them will work with lite-c.
Here is the link to the Wiki page:
Maze generation algorithm

There is even an example, in Python, but I don't get a damn thing:
Code:
import numpy
from numpy.random import random_integers as rand
import matplotlib.pyplot as pyplot
 
def maze(width=81, height=51, complexity=.75, density=.75):
    # Only odd shapes
    shape = ((height // 2) * 2 + 1, (width // 2) * 2 + 1)
    # Adjust complexity and density relative to maze size
    complexity = int(complexity * (5 * (shape[0] + shape[1])))
    density    = int(density * (shape[0] // 2 * shape[1] // 2))
    # Build actual maze
    Z = numpy.zeros(shape, dtype=bool)
    # Fill borders
    Z[0, :] = Z[-1, :] = 1
    Z[:, 0] = Z[:, -1] = 1
    # Make isles
    for i in range(density):
        x, y = rand(0, shape[1] // 2) * 2, rand(0, shape[0] // 2) * 2
        Z[y, x] = 1
        for j in range(complexity):
            neighbours = []
            if x > 1:             neighbours.append((y, x - 2))
            if x < shape[1] - 2:  neighbours.append((y, x + 2))
            if y > 1:             neighbours.append((y - 2, x))
            if y < shape[0] - 2:  neighbours.append((y + 2, x))
            if len(neighbours):
                y_,x_ = neighbours[rand(0, len(neighbours) - 1)]
                if Z[y_, x_] == 0:
                    Z[y_, x_] = 1
                    Z[y_ + (y - y_) // 2, x_ + (x - x_) // 2] = 1
                    x, y = x_, y_
    return Z
 
pyplot.figure(figsize=(10, 5))
pyplot.imshow(maze(80, 40), cmap=pyplot.cm.binary, interpolation='nearest')
pyplot.xticks([]), pyplot.yticks([])
pyplot.show()

This could really help me (and not only ME) out! And could be an awesome contribution.


Best greets


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: Maze generation algorithm [Re: 3run] #430566
09/29/13 01:11
09/29/13 01:11
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Hi,
since i like this kind of things, I wrote the first maze generation algorithm of the wiki: Depth first

Code:
#include <acknex.h>

#define PRAGMA_POINTER

PANEL *panMaze;

int make_odd ( int i )
{
	return i - ( i % 2 ) + 1;
}

#define array_cell(array,x,y) *(*(array+x)+y)

var **array_create ( int size_x, int size_y )
{
	var **array = (var**)sys_malloc ( sizeof(var*) * size_x );
	var **c = array;
	var **cl = c + size_x;
	for ( ; c<cl; c++ )
		*c = (var*)sys_malloc ( sizeof(var) * size_y );
	
	return array;
}

void array_remove ( var **array, int size_x )
{
	var **c = array;
	var **cl = c + size_x;
	for ( ; c<cl; c++ )
	{
		var *r = *c;
		sys_free ( r );
	}
	sys_free ( array );
}

#define MAZEX      81 // odd!
#define MAZEY      61 // odd!
#define ENDX       41  // odd!
#define ENDY       31  // odd!

#define UNVISITED   0
#define END         1
#define WALL        2
#define OPEN        3

void array_draw ( var **maze, int size_x, int size_y, BMAP *bmp )
{
	var cell_x = bmap_width ( bmp ) / size_x;
	var cell_y = bmap_height ( bmp ) / size_y;
	
	bmap_rendertarget ( bmp, 0, 0 );
	int c = 0;
	for ( ; c<size_x; c+=1 )
	{
		int r = 0;
		for ( ; r<size_y; r+=1 )
		{
			switch ( array_cell(maze,c,r) )
			{
				case UNVISITED:
				case WALL:      
					break;
				case END:    draw_quad ( NULL, vector(c*cell_x, r*cell_y, 0), NULL, vector(cell_x+1, cell_y+1, 0), NULL, COLOR_RED, 100, 0 );    break;
				case OPEN:   draw_quad ( NULL, vector(c*cell_x, r*cell_y, 0), NULL, vector(cell_x+1, cell_y+1, 0), NULL, COLOR_GREY, 100, 0 );   break;
				default:     draw_quad ( NULL, vector(c*cell_x, r*cell_y, 0), NULL, vector(cell_x+1, cell_y+1, 0), NULL, COLOR_WHITE, 100, 0 );  break;
			}
		}
	}
	bmap_rendertarget ( NULL, 0, 0 );
}

var no[4][2] =
{
	-1, 0,
	0, -1,
	1, 0,
	0, 1
};

void maze_depth_first ( var **array, int size_x, int size_y, int end_x, int end_y )
{
	var **c = array;
	var **cl = c + size_x;
	for ( ; c<cl; c+=2 )
	{
		var *r = *c;
		var *rl = r + size_y;
		for ( ; r<rl; r++ )
		{
			*r = WALL;
		}
	}
	
	var **c = array + 1;
	var **cl = c + size_x - 1;
	for ( ; c<cl; c+=2 )
	{
		var *r = *c;
		var *rl = r + size_y;
		for ( ; r<rl; r+=2 )
		{
			*r = WALL;
		}
	}
	
	int x = end_x;
	int y = end_y;
	array_cell(array,x,y) = END;
	int cells_to_end = OPEN;
	
	while (1)
	{
		int noi = integer ( random(4) );
		int i = 0;
		for ( ; i<4; i+=1 )
		{
			int nx = x + no[noi][0] * 2;
			int ny = y + no[noi][1] * 2;
			if ( ( nx < 0 ) || ( nx >= size_x ) || ( ny < 0 ) || ( ny >= size_y ) )
			{
				noi = cycle ( noi + 1, 0, 4 );
				continue;
			}
			if ( array_cell(array,nx,ny) > UNVISITED )
			{
				noi = cycle ( noi + 1, 0, 4 );
				continue;
			}
			int nx2 = x + no[noi][0];
			int ny2 = y + no[noi][1];
			array_cell(array,nx2,ny2) = OPEN;
			array_cell(array,nx,ny) = OPEN;
			x = nx;
			y = ny;
			cells_to_end += 2;
			break;
		}
		
		if ( i == 4 )
		{
			if ( array_cell(array,x,y) == END )
				break;
			array_cell(array,x,y) = cells_to_end;
			cells_to_end -= 1;
			int i = 0;
			for ( ; i<4; i+=1 )
			{
				int nx = x + no[i][0];
				int ny = y + no[i][1];
				if ( array_cell(array,nx,ny) != OPEN )
					continue;
				array_cell(array,nx,ny) = cells_to_end;
				cells_to_end -= 1;
				x += no[i][0] * 2;
				y += no[i][1] * 2;
				break;
			}
		}
		
		// delete his two code lines to get a straight maze generation
		array_draw ( array, size_x, size_y, panMaze->bmap );
		wait(1);
	}
	array_draw ( array, size_x, size_y, panMaze->bmap );
}


void main ()
{
	video_mode = 12;
	fps_max = 20;
	random_seed ( 0 );
	wait(3);
	panMaze = pan_create ( "flags=SHOW;", 1 );
	panMaze->bmap = bmap_createblack ( screen_size.x, screen_size.y, 24 );
	
	var **maze = array_create ( MAZEX, MAZEY );
	maze_depth_first ( maze, MAZEX, MAZEY, ENDX, ENDY );
	
	while ( !key_esc )
		wait(1);
	
	bmap_remove ( panMaze->bmap );
	pan_remove ( panMaze );
	array_remove ( maze, MAZEX );
	
	sys_exit ( NULL );
}



Enjoy it!

Re: Maze generation algorithm [Re: txesmi] #430567
09/29/13 01:19
09/29/13 01:19
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
The maze generation looks pretty cute, I like it! Could be made into a simple screen saver.


"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Re: Maze generation algorithm [Re: Superku] #430568
09/29/13 05:13
09/29/13 05:13
Joined: Nov 2006
Posts: 497
Ohio
xbox Offline
Senior Member
xbox  Offline
Senior Member

Joined: Nov 2006
Posts: 497
Ohio
This works amazingly! The only thing I have a problem with, is where is the size of the maze defined? I would like to make a maze like 10 by 10 cells or 20 by 20 cells instead of the full screen.

Also, I get an error after every iteration because I have A8 Free and render target is not a valid feature. frown

Re: Maze generation algorithm [Re: xbox] #430574
09/29/13 07:36
09/29/13 07:36
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline
Expert
Kartoffel  Offline
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
@^:

#define MAZEX 81 // odd!
#define MAZEY 61 // odd!


POTATO-MAN saves the day! - Random
Re: Maze generation algorithm [Re: Kartoffel] #430577
09/29/13 07:45
09/29/13 07:45
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline OP
Senior Expert
3run  Offline OP
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
txesmi@ it works flawlessly man, thank you very much! I'll try to create a 3D level with it.


Greets


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: Maze generation algorithm [Re: 3run] #430578
09/29/13 08:50
09/29/13 08:50
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain


I'll write more algorithms today. Sunday entertainment wink

Salud!

Re: Maze generation algorithm [Re: txesmi] #430580
09/29/13 09:07
09/29/13 09:07
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline OP
Senior Expert
3run  Offline OP
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
I've got it to work already grin Thank you man, you've made my weekend!

Download link

But I yet can't find a way, to define the end of the maze :<
Plus, I'm thinking about placing some models, randomly on the maze to scare player grin


Greets


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: Maze generation algorithm [Re: 3run] #430582
09/29/13 09:12
09/29/13 09:12
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
@3run
Check for the highest value into the array and make it the starting point. That is the reason of 'cells_to_end' variable. Note that only odd cells can be a deadend cell wink


Last edited by txesmi; 09/29/13 09:15.
Re: Maze generation algorithm [Re: txesmi] #430583
09/29/13 09:29
09/29/13 09:29
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline OP
Senior Expert
3run  Offline OP
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
Well, that sounds a little bit more difficult for me, I'll try to get more into details of the algorithm, if I can grin


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Page 1 of 3 1 2 3

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1