Rubies and pointers

I am programming a Dungeon generator for litte game.

Dungeons are made up of rooms. A roomhas connectionsfor other rooms.

room.connections = [room_a, room_b]
and
room.number = 1 # unique id

Now I need to select a number by this number.

I did this first with a method recursive_scanthat didn’t work, because the rooms can circle, which throws a StackOverflowError. Therefore, I put an array with the name of the already_scannedroom numbers that were already selected in the method arguments. Then he did not scan all the rooms - by the way, I have no idea why, in my logical unreliability, this should have worked.

Then I tried to put all the rooms in an array, and then iterate over the array in the desired room - but here I get the problem, each room is basically connected to every other room, at least to some other rooms between them; therefore, the array becomes as large as dungeon_size * array_of_rooms.length.

Now I need an explicit pointer - I know that almost every var in ruby ​​is a pointer, except Fixnums and Float (and maybe some others). Despite this, the array is getting large, so I need a real pointer.

(I also tried setting up an array of object_ids and loading them via ObectSpace, but unfortunately, because I often have to load rooms, rooms with the required object_id have already been reworked, as explained by the error message.)

This is the recursive scan method:

def room(number)
  recursive_scan(@map, number, []) # @map is the entrance room
end

private

def recursive_scan(room, number, scanned)
  scanned << room.room_number
  if room.room_number == number
    room
  else
    r = nil
    room.connections.each do |next_room|
      if !scanned.include?(next_room.room_number)
        r = recursive_scan(next_room, number, scanned)
      end
    end
    r
  end
end
+3
4

Ruby .

?

rooms[room.number] = room

- rooms[i]. , .

def initialize
  rooms[self.number] = self
  . . .
end

, - , . , , , , , - .

- ( ), rooms[x] = nil .

, , , FWIW "", , , , .

+6

. . rgl.

. .

require "rgl/adjacency"

map = RGL::AdjacencyGraph.new
rooms.each {|room| map.add_vertex room}
rooms.connections.each {|room1, room2| map.add_edge room1, room2}

, O (1):

map.has_edge? room1, room2

:

map.vertices

:

map.adjacent_vertices(room)
+3

hackish :

all_rooms = ObjectSpace.each_object(Room).to_a
+1

You can look at the NArray crystal , which will speed up the use of arrays of numbers. Perhaps you are trying to force a square snap into a circular hole with this approach.

0
source

All Articles