Page 1 of 1

Linked lists in Map arrays

Posted: Mon Jun 23, 2003 5:10 pm
by Shopro
I am currently doing a 2D RPG engine, and happened to come across this article.

http://www-cs-students.stanford.edu/~am ... etech.html

In the article, it suggests using linked lists, instead of DIM Map(Layers, SizeX, SizeY), for flexibility. I think it's a good idea.

My problem is, I don't know how to do:
map[0][0]
LIST_HEADER -> map_tile -> map_tile -> NULL
map[1][0]
LIST_HEADER -> map_tile -> NULL

in PB. Can anyone give me a nudge in the right direction?

thanks

-Shopro

Posted: Mon Jun 23, 2003 5:25 pm
by LarsG
A hint might be to read up on Arrays, Lists & Structures in the Help file that comes with PB... :wink:

Posted: Mon Jun 23, 2003 5:36 pm
by Shopro
LarsG: Thanks for the advice.

I have already consulted the help, but couldn't figure how to use linked lists on arrays. for example, in the help:
NewList mylist.l()
AddElement(mylist())
mylist() = 10
was the example on the linked lists. I want to do something like:

NewList mylist.l(255, 255)
AddElement(mylist(0, 0))
mylist(0, 0) = 10

Is this possible in PB?

thanks

-Shopro

Posted: Mon Jun 23, 2003 5:51 pm
by tinman
I don't think you can do multiple dimension linked lists in Purebasic (any why would you want to put values in the brackets for the list - maximum size?).

What you could do is have a linked list for each row. Within each element of that linked list you could have a linked list for each column. Unfortunately you cannot do this in Purebasic either. However, I wrote some procedures which allow you to do that if you want. Search for "linked list" on the resources site (http://www.reelmediaproductions.com/pb). Be warned that it is a lot more awkward and slower than using a Purebasic linked list.

I don't know whether linked list stuff would be the best for a 2D map, because you would have to resort to process the map linearly, whereas with an array you can do things in any order and it remains fast.

What flexibility do you need? If it is only different sizes of map between levels (not map layers - I mean levels of your game or whatever) then you may as well just have an array where the size is changed before each level is loaded. Or are you trying to save memory by only having enough storage for the map blocks which are used? I guess that depends on map size and target system specifications, but are you really going to struggle for memory?

Posted: Mon Jun 23, 2003 7:38 pm
by GedB
Because in PB linked lists are global variables, I don't think the approach described would work.

I think the best way to implement it is as a 2d array of strings. You could either deliminate the string using the field command, or use fixed length codes of say three characters for your different objects. Something like:

Code: Select all


; PureBasic Visual Designer v3.62


;- Window Constants
;
#MapWindow = 0

;- Gadget Constants
;
#Cell11 = 0
#Cell12 = 1
#Cell13 = 2
#Cell21 = 3
#Cell22 = 4
#Cell23 = 5
#Cell31 = 6
#Cell32 = 7
#Cell33 = 8
#Row1Header = 9
#Row2Header = 10
#Row3Header = 11
#Col1Header = 12
#Col2Header = 13
#Col3Header = 14
#AddText = 15
#NewText = 16
#CellList = 17


Procedure Open_MapWindow()
  If OpenWindow(#MapWindow, 216, 0, 235, 322,  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar , "New window ( 0 )")
    If CreateGadgetList(WindowID())
      ButtonGadget(#Cell11, 60, 50, 40, 40, "A")
      ButtonGadget(#Cell12, 110, 50, 40, 40, "B")
      ButtonGadget(#Cell13, 160, 50, 40, 40, "C")
      ButtonGadget(#Cell21, 60, 100, 40, 40, "D")
      ButtonGadget(#Cell22, 110, 100, 40, 40, "E")
      ButtonGadget(#Cell23, 160, 100, 40, 40, "F")
      ButtonGadget(#Cell31, 60, 150, 40, 40, "G")
      ButtonGadget(#Cell32, 110, 150, 40, 40, "H")
      ButtonGadget(#Cell33, 160, 150, 40, 40, "I")
      TextGadget(#Row1Header, 40, 60, 10, 20, "1")
      TextGadget(#Row2Header, 40, 110, 10, 20, "2")
      TextGadget(#Row3Header, 40, 160, 10, 20, "3")
      TextGadget(#Col1Header, 60, 20, 40, 20, "1", #PB_Text_Center)
      TextGadget(#Col2Header, 110, 20, 40, 20, "2", #PB_Text_Center)
      TextGadget(#Col3Header, 160, 20, 40, 20, "3", #PB_Text_Center)
      StringGadget(#NewText, 10, 280, 150, 30, "")
      ButtonGadget(#AddText, 170, 280, 40, 30, "Add")
      ListViewGadget(#CellList, 10, 200, 200, 70)
      
    EndIf
  EndIf
EndProcedure

Dim Map.s(2, 2)
Map(0,0) = "Wall"
Map(0,1) = "Grass|Zombie"
Map(0,2) = "Grass"
Map(1,0) = "Wall"
Map(1,1) = "Wood"
Map(1,2) = "Wood|Wolf"
Map(2,0) = "Wood|Cave|Dragon"
Map(2,1) = "Sea"
Map(2,2) = "Sea|Boat"

Procedure SetList(Row, Col)
  CellContents.s = Map(row, col)
    ClearGadgetItemList(#CellList)
    i = 1
    Repeat
      Content.s = StringField(CellContents, i, "|")
      If Content <> ""
        AddGadgetItem(#CellList, -1, Content)
      EndIf
      i = i + 1
    Until content = ""
EndProcedure

Open_MapWindow()

Repeat
  Event = WaitWindowEvent()
    Select Event
      Case #PB_Event_Gadget
        GadgetID = EventGadgetID()
        If GadgetID <= 8
          Row = Int(GadgetID / 3)
          Col = GadgetID - ((Row) * 3)
          SetList(Row, Col)
        EndIf
    EndSelect
Until Event = #PB_EventCloseWindow
End


Posted: Mon Jun 23, 2003 8:11 pm
by Dreamflower
I read the page you linked.

for me it looks like he constructs a array of pointer so that the number of planes he has in the game is dynamicaly changeable for every single tile within the map

Posted: Tue Jun 24, 2003 8:29 am
by Shopro
tinman:
I understand now, that it isn't possible to use linked lists for 2D maps in PB efficiently, thanks for the explanation.
>I guess that depends on map size and target system specifications, but are you really going to struggle for memory?
Not that I really have to, but believe it's good practice to save memory whenever I can(I used to code in QuickBASIC :)) Also, the game I'm trying to do aims having may layers as well.

GedB:
Good idea! Using strings didn't occur to me. Thanks for the source code, too. It must have taken some time to do that. I will consult it more before implementing it into my engine.

Thanks again everyone!

-Shopro