How I can create a double linked list with GLBasic

Previous topic - Next topic

zxevious

Hi there,

I'm trying to deploy a double linked list only using types

Code (glbasic) Select
TYPE node
  X%
  Y%
  previous as node
  next as node
ENDTYPE


Is this possible? How can assing the address or the objects and move in into the list, adding and removing nodes?

Thank you in advance.

Regards.

Best Regards.

Mass Effect 2 Fan!

MrTAToad

Unfortunately if you want emulate a doubly-linked list in GLBasic, you would need to use the array index - currently is the only real way of moving backwards or forwards.

If you want to go INLINE, I do have a C routine for it somewhere.

zxevious

Thank you very much indeed for the clarification.
Best Regards.

Mass Effect 2 Fan!

Kitty Hello

yes. And how much easier would it be to write screwed code. Pointers are a totally no-go these days. Usually if you need a pointer there's a C++ class wrapper that suits your need. For double linked list that would be std::list.

What are you trying to do in the first place? Maybe there's a better way. People always scream for linked lists, but in practice usually an array is lots faster.

zxevious

Hi both,

My question was oriented to know the way to create this kind of lists using native code of GLBasic, therefore, to use inline or C or C++ was outside of my objective. From my perspective and experience, I prefer to use only one language without any calls to other dlls or inline code, because if you have a pointer issue, you will have a real problem in your program, and it's very difficult to identify the root cause.

I'm trying to deploy this

http://www.youtube.com/watch?v=_XKphuYviE0&feature=related

and the first phrase of the design document says: you need to use double linked list .... next? to ask inside of the forum. :)

I'm trying to create a type like a doubled link according to the first answer said me: using arrays. My objective wasn't to start a little flame :) Thank you for your support.

Regards.
Best Regards.

Mass Effect 2 Fan!

Slydog

Here's something you can play with, I'm not too sure how this will relate to driving AI:
Code (glbasic) Select
TYPE TNode
id%
id_prev%
id_next%
x%
y%

FUNCTION FindNext:
RETURN nodes.Find(self.id_next)
ENDFUNCTION

FUNCTION FindPrev:
RETURN nodes.Find(self.id_prev)
ENDFUNCTION
ENDTYPE

TYPE TNodeList
node_list[] AS TNode

FUNCTION Initialize:
DIM node_list[0]
ENDFUNCTION

FUNCTION Add: id%, x%, y%, id_next%=-1, id_prev%=-1
LOCAL node AS TNode
node.id = id
node.x = x
node.y = y
node.id_next = id_next
node.id_prev = id_prev
DIMPUSH self.node_list[], node
ENDFUNCTION

FUNCTION Find: id%
LOCAL nx%
FOR nx = 0 TO LEN(self.node_list[]) - 1
IF self.node_list[nx].id = id THEN
RETURN nx
ENDIF
NEXT
RETURN -1 // Node id not found
ENDFUNCTION

ENDTYPE

GLOBAL nodes AS TNodeList


It keeps all of your nodes in a list/array.  Nothing fancy. 
Then you reference the other nodes using an 'id'.
The 'Find' functions return the array index, and not the ids.

But if your main objective is driving AI, maybe there are other methods.
I have thought about this topic a few times and wondered how I would do it.
Could the AI be accomplished by having a spline / line in the center of the road that follows the ideal path a car should follow?  Have this line hug the correct sides in corners, etc.  This line would have a direction also.  And, you could track the ideal speeds along the line too, to slow down or speed up a car if the corner requires it.

Then when the AI driver is off this line (due to other drivers or conditions) have the AI adjust its own vector to align better with the desired ideal path.  But not sure how this will work in the real world, or how you'd get the AI driver to slowly merge the two vectors until he's heading in the right direction again.  Some kind of interpolation over time.
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

zxevious

awesome! Thank you very much indeed. In this case each track is splitted into a little squares and rectangules. You can use the list to save each info. You can start from one of them and move forwards or backwards :) the IA is a A* that use an specific points to reach :)=

Best Regards.

Mass Effect 2 Fan!