Author Topic: Generic Random Maze Maker  (Read 6369 times)

0 Members and 1 Guest are viewing this topic.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Generic Random Maze Maker
« Reply #15 on: September 01, 2019, 01:01:13 am »
here is a very old maze generator I used to use, can't remember where I dug it up. It may have come from the ABC(All Basic Code) archives.

xz- the X size of generated maze
yz- the Y size of generated maze
C- color of maze
S- room size
offx- X offset to place map on screen
offy- Y offset to place map on screen

Code: [Select]
SCREEN _NEWIMAGE(640, 480, 32)
Temp& = _NEWIMAGE(320, 200, 13)
_DEST Temp&
map 319, 141, 1, 2, 0, 0
_PUTIMAGE (0, 0)-STEP(639, 399), Temp&, _DISPLAY, (0, 0)-STEP(319, 199)


SUB map (xz, yz, C, S, offx, offy)
 DIM xv(50), yv(50)
 OFX = offx
 OFY = offy
 LINE (OFX + 0, OFY + 0)-(OFX + xz - 1, OFY + yz - 1), 2, B
 120: KEY OFF:: ZZC = C
 130 DEF SEG = &HF000: a = PEEK(&HC05D): DEF SEG: IF a = &H4C THEN ZZC = 3
 140 YD = S: XD = YD: YN = INT(yz \ YD): XN = INT(xz \ XD)
 150 DIM p1(XN + 2, YN + 2), q(XN, YN), a%(9)
 160 a%(0) = &H10: a%(1) = &H8: a%(2) = &HF003: a%(3) = &HF003: a%(4) = &HC000
 170 a%(5) = &HFF3F: a%(6) = &HC000: a%(7) = &H3003: a%(8) = &HC0C: a%(9) = &H330
 180 xv(0) = 1: xv(1) = 0: xv(2) = -1: xv(3) = 0
 190 yv(0) = 0: yv(1) = 1: yv(2) = 0: yv(3) = -1
 200 GOSUB 290: FOR OKL = 1 TO 5000: NEXT
 210 GOTO 200
 270 'CLS : SCREEN 0, , 0, 0: WIDTH 80: COLOR 7, 0, 0: LOCATE , , 1: RUN "demo"'op
 280 END
 290 '
 300 'RANDOMIZE TIMER
 310 FOR x = 0 TO XN + 2: p1(x, 0) = 1: p1(x, 1) = 1: p1(x, YN + 1) = 1: p1(x, YN + 2) = 1: NEXT
 320 FOR y = y TO YN + 2: p1(0, y) = 1: p1(1, y) = 1: p1(XN + 1, y) = 1: p1(XN + 2, y) = 1: NEXT
 330 FOR x = 2 TO XN: FOR y = 2 TO YN: p1(x, y) = 0: NEXT: NEXT
 340 FOR x = 0 TO XN: FOR y = 0 TO YN: q(x, y) = 0: NEXT: NEXT
 350 'CLS
 360 LINE (OFX + XD * 0, OFY + YD * 0)-(OFX + XD * XN, OFY + YD * 0), ZZC, B
 370 LINE -(OFX + XD * XN, OFY + YD * (YN - 1)), ZZC, B
 380 LINE (OFX + XD * 0, OFY + YD * 1)-(OFX + XD * 0, OFY + YD * YN), ZZC, B
 390 LINE -(OFX + XD * XN, OFY + YD * YN), ZZC, B
 400 C = ZZC: GOSUB 440: C = 2
 410 IF ZZC = 3 THEN C = 3
 420 GOSUB 630
 430 RETURN
 440 '
 450 XMIN = 0: XMAX = XN: YMIN = 0: YMAX = YN
 460 WHILE (XMIN < XMAX AND YMIN < YMAX)
  470 FOR XO = XMIN TO XMAX: x = XO: y = YMIN: GOSUB 530: NEXT: YMIN = YMIN + 1
  480 FOR YO = YMIN TO YMAX: y = YO: x = XMAX: GOSUB 530: NEXT: XMAX = XMAX - 1
  490 FOR XO = XMIN TO XMAX: x = XO: y = YMAX: GOSUB 530: NEXT: YMAX = YMAX - 1
  500 FOR YO = YMIN TO YMAX: y = YO: x = XMIN: GOSUB 530: NEXT: XMIN = XMIN + 1
 510 WEND
 520 RETURN
 530 '
 540 S = INT(RND(1) * 4)
 550 FOR N = S TO 3 + S: V = N MOD 4
  560 IF p1(x + xv(V) + 1, y + yv(V) + 1) GOTO 610
  570 p1(x + xv(V) + 1, y + yv(V) + 1) = 1
  580 LINE (OFX + x * XD, OFY + y * YD)-((x + xv(V)) * XD + OFX, (y + yv(V)) * YD + OFY), C, B
  590 x = x + xv(V): y = y + yv(V)
  600 GOTO 540
 610 NEXT
 620 RETURN
 630 '
 LINE (OFX + 0, OFY + 0)-(OFX + 0, OFY + 10), 1
END SUB

mazeExample.jpg
* mazeExample.jpg (Filesize: 108.38 KB, Dimensions: 684x540, Views: 163)
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Generic Random Maze Maker
« Reply #16 on: September 01, 2019, 09:13:25 am »
Hi Colbalt,

Can you move a square or something around in it, arrow keys or mouse?

Eeeh, Peek and Poke, where is maze array produced?

« Last Edit: September 01, 2019, 09:46:33 am by bplus »

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Generic Random Maze Maker
« Reply #17 on: September 01, 2019, 12:42:53 pm »
Hi Colbalt,

Can you move a square or something around in it, arrow keys or mouse?

Eeeh, Peek and Poke, where is maze array produced?

I never pretended to understand just how this thing worked, with all them GOTOs and GOSUBs.

What I usually did was generate the map and then use POINT to convert the maze into an array or I would use it to convert the maze into a psudo 3d look by projecting POINTs in a direction from inside the maze. This was from a time that I used DirectQB heavily, so I would have the maze on one layer and use DQBPOINT to read around it. We could accomplish the same with _SOURCE and _DEST now.

With a map as dense as the screen cap there it works great for my way, but if you lessen the density by increasing the room size(S) you can make a more visually usable maze map.

I'm not 100% certain, as it has been years(10+) since I've played with this, but I believe its line 150 that has the array(s) used to generate the maze.
Granted after becoming radioactive I only have a half-life!