how to restrict a drunkard walker from going backwards or turning twice in a row

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Sonu

hi im more of a artist when it comes to games design and wanted to get into coding i kinda know some of the basics and wanted to know how i would modify a drunkard walker to walk away from the spawn point in a random way like a cave system. I found a tutorial on how to make the walker and i somewhat understand how it works, now i would like to make it walk away sice it keeps doubling back on itself.

i did ask for help on reddit and someone did help me with the process but i feel like opening 50 posts and ask how its done over and over again would be annoying so i thought i would ask here once to get some help
https://www.reddit.com/r/godot/comments/izaxii/how_to_do_procedural_generation_with_fixed_boss/?utm_source=share&utm_medium=web2x&context=3

Any help on how to write the code or a point in the right direction would be appreciated

extends TileMap

Set tiles 0 and 1 for tile map make sure you match up walls to floor in the correct order

enum TILES {Rock, Floor}

Map size vector 2 with walkers as a arrey with chance to spawn with walkers

var current_map_size = Vector2(10,20) # Mas size
var total_walkers = []
var spawn_chance = 0.005
var percentage_to_fill = 0.35

class walker extends Reference:
	var pos

func _init(starting_position):
	pos = starting_position

Randomize and loop through the whole map to set tile as walls

func _ready():
	randomize()
	for x in current_map_size.x:
		for y in current_map_size.y:
			set_cell(x,y, TILES.Rock)
	move_walkers()

when you use while loop use a iteration so it is not infinate

func move_walkers():
	var iteration = 0
#var pos = Vector2((current_map_size.x / 2), current_map_size.y / 2) #Position of walker


var pos = Vector2(5, 0)
	var w = walker.new(pos)
	total_walkers.append(w)
	while iteration < 10000:
		for walk in total_walkers:
			var num = randi() % 4
			match num:
				0:
					walk.pos += Vector2(1,0)
				1:
					walk.pos += Vector2(0,1)
				2:
					walk.pos += Vector2(-1,0)
				3:
					walk.pos += Vector2(0,-1)
			
			walk.pos.x = clamp(walk.pos.x, 1, current_map_size.x - 2)
			walk.pos.y = clamp(walk.pos.y, 1, current_map_size.y - 2)
			set_cell(walk.pos.x, walk.pos.y, TILES.Floor)

Walker limit roation

#if walker rotation
		

Chance to spawn Walker total walers e.g. more then 1

#			if randf() < spawn_chance and total_walkers.size() < 10:
#				var y  = walker.new(walk.pos)
#				total_walkers.append(y)
		
		# Chance to delete Walker
		if  randf() <spawn_chance / 2 and total_walkers.size() > 1:
			total_walkers.erase(walk)
  • help function to see how many floor tiles there are in %*
iteration += 1
		yield(get_tree(), "idle_frame")

		if count_floors() / (current_map_size.x * current_map_size.y) > percentage_to_fill:
			break

func count_floors():
	var floor_count = 0
	for x in current_map_size.x:
		for y in current_map_size.y:
			var current_cell = get_cell(x,y)
			if current_cell == TILES.Floor:
				floor_count += 1


	return floor_count}

i think i should elabrate i did google how to “restrict movement / restrict rotation” but it kept coming up with kinematic bodies but since this is an algorithm i was not sure if the same process would work for this too.

Sonu | 2020-09-28 02:13

:bust_in_silhouette: Reply From: jonbonazza

The simplest way I can think to do this, especially since you already seem to be restricting movement to the 4 cardinal directions is to just keep track of the last movement direction in a variable. Then, just keep calculating a random direction until the new direction is not equal to the negation of the last direction.