06-18-2024, 06:34 AM
(06-18-2024, 02:09 AM)madscijr Wrote: Pretty neat, that'll be fun to play with. Thanks!
A quick and dirty demo of what I'm talking about for you:
Code: (Select All)
CONST ToggleMeander = -1
RANDOMIZE TIMER
$COLOR:32
DIM Map(50, 50) AS LONG 'let's create a 50x50 grid for our "dungeon"
DIM SHARED gridsize AS LONG
SCREEN _NEWIMAGE(801, 801, 32)
gridsize = 800 / UBOUND(Map)
roomcount = 6
roomsize_min = 1
roomsize_max = 3
DO
CLS
FOR x = 0 TO 800 STEP gridsize 'draw the grid
LINE (x, 0)-(x, 800), LightGray
LINE (0, x)-(800, x), LightGray
NEXT
FOR i = 1 TO roomcount
x1 = INT(RND * 40) + 5: y1 = INT(RND * 40) + 5
roomsize = (INT(RND * roomsize_max + roomsize_min)) * gridsize
'Draw the rooms
LINE (x1 * gridsize - roomsize, y1 * gridsize - roomsize)-_
((x1+1) * gridsize + roomsize , (y1+1) * gridsize + roomsize), Cyan, BF
'draw the center of these rooms
LINE (x1 * gridsize, y1 * gridsize)-STEP(16, 16), Red, BF
IF lastx <> 0 THEN
IF ToggleMeander = 0 THEN
'draw the directpath between these rooms in green
directpath x1, y1, lastx, lasty
ELSE
'draw a meandering path between these two points
MeanderingPath x1, y1, lastx, lasty
END IF
END IF
lastx = x1: lasty = y1
SLEEP
NEXT
lastx = 0: lasty = 0
SLEEP
LOOP
SUB directpath (tx1, ty1, tx2, ty2)
x1 = tx1: y1 = ty1: x2 = tx2: y2 = ty2 'preserve our orignal values without changing them.
DO
rise = x2 - x1
runn = y2 - y1
chance = ABS(rise) + ABS(runn)
roll = INT(RND * chance) + 1
IF roll < ABS(rise) THEN
x1 = x1 + SGN(rise)
ELSE
y1 = y1 + SGN(runn)
END IF
LINE (x1 * gridsize, y1 * gridsize)-STEP(16, 16), Green, BF
LOOP UNTIL chance = 1
END SUB
SUB MeanderingPath (tx1, ty1, tx2, ty2)
x1 = tx1: y1 = ty1: x2 = tx2: y2 = ty2 'preserve our orignal values without changing them.
DO
chance = INT(RND * 100) + 1
SELECT CASE chance
CASE IS <= 20: x1 = x1 + 1
CASE IS <= 40: y1 = y1 + 1
CASE IS <= 60: x1 = x1 - 1
CASE IS <= 80: y1 = y1 - 1
CASE ELSE
rise = x2 - x1
runn = y2 - y1
chance = ABS(rise) + ABS(runn)
roll = INT(RND * chance) + 1
IF roll < ABS(rise) THEN
x1 = x1 + SGN(rise)
ELSE
y1 = y1 + SGN(runn)
END IF
END SELECT
'stay in bounds of the map
IF x1 < 0 THEN x1 = 0
IF x1 > 50 THEN x1 = 50
IF y1 < 0 THEN y1 = 0
IF y1 > 50 THEN y1 = 50
LINE (x1 * gridsize, y1 * gridsize)-STEP(16, 16), Purple, BF
LOOP UNTIL ABS(x1 - x2) <= 1 AND ABS(y1 - y2) <= 1
END SUB
Toggle that first CONST and you can see the difference between a direct path from the center of one room to another, and a meandering path.
Change those values in the SELECT CASE for the SUB MeanderingPath, and you can alter how much it deviates from that basic direct path.
I tried to use color for this to showcase the room (blue), center of room (red), direct path (green), and wandering path (purple), but that wandering path tends to wander and cover up everything else if you're not careful. LOL! Try it imagine it all being one set color for your "dungeon" or "rogue-like play area", and you should be able to see how this creates a map very much like Rogue used to back in the days.
One thing I'd suggest to make this a ton better -- check to make certain the rooms don't overlap each other. The further apart the rooms are, the more room the paths have to stretch out and crisscross and such. Touching rooms kind of defeats the purpose of building roads between them.