Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / multi-tiered arrays (not multi dimensional)

Author
Message
Suicidal Sledder
21
Years of Service
User Offline
Joined: 17th Aug 2004
Location: Tikrit, Iraq
Posted: 9th Oct 2010 07:04
I am wondering the best way to store a large amount of data on different tiers. The best way I can think to explain this would be imagine a fictional country. this country has multiple towns, each town has multiple houses and each house has multiple rooms.

At first I thought this was best accomplished using multi dimensional arrays but problems arose when each "tier" has different data type.

For example
the "town" tier might have number of houses, population, location in the world, etc...
The "house" tier might contain number of rooms, size of yard, house number, etc
and the room tier might have color,size,designation(i.e. kitchen bedroom etc...)

So then I went on to creating a seperate array for each tier. however this means creating an extremely large array for each individual room. ten towns, ten houses, ten rooms each we are already at 1000. Also, I cant concieve a way to logically reference this information.

For example suppose the character enters a town. I would like to reference the information about that town only. Then they walk into a house refence the data regarding that house only.

I am using user-defined types and I think there is a way to do it using multi user defined types i.e.



or something like that but i am not exactlly sure how to do it. anyone have some bright ideas? thanks a ton everyone!

Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 9th Oct 2010 08:52
QUADTREES!!!!!

Or... I dunno what you'd call them. N-trees. Something along those lines. The idea of quadtrees is that you have a plane, and you divide that plane into fourths, and then those sections into fourths, and then the subsections, etc. until you have every object in its own subsection.

What you would be doing would be similar. Here's an example I have of a quatree:

You'll need IanM's matrix dlls (I use the memory allocation and deallocation commands), and the d3dfunc dll (It draws a lot of lines, and barely runs about 9 fps for 100 objects with native commands, but 100 fps with 2000 objects with d3d_lines).

Now... I used memory allocation and deallocation so I could quickly change the structure of the tree. However, you could use an array, it doesnt really matter.

The idea is that you have a data structure that points to data structures like itself, or nothing. Thats why almost all my code is recursive (a recursive function calls itself), because every quadtree node is a quadtree (and every one of its nodes, and so on and so forth).


O'course, that might be all unnecessary complicated. You could just create 3 separate arrays of 10 towns, 100 houses, and 1000 rooms, and call it a day! I would think the best way would be to have town 0 point to houses 0-9, house 1 point to rooms 10-19, etc.

If you want to dynamically change the number of rooms/houses... You could either just create all the arrays the max size allowed, and then just have a list of excluded rooms/houses/buildings. Otherwise, your best bet is just the weird poking and prodding akin to the quadtree codez.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 9th Oct 2010 12:58
You might like to consider turning the problem the other way around by using basic entities that have various attributes. For example, you could have a single array of rooms consisting of various attributes such as size, type, house, street, town. That way you can easily have different numbers of rooms in each house, etc. A bit like Neuro Fuzzy's suggestion I suppose.
Suicidal Sledder
21
Years of Service
User Offline
Joined: 17th Aug 2004
Location: Tikrit, Iraq
Posted: 9th Oct 2010 20:22
that was one of my original ideas... however what hapens when a player enters a house? would I need need to then scan the entire array to find which rooms are part of that house? Yes this would work but it seems like a lot of overhead especially with over a thousand rooms.

Or am i mistaken?

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 9th Oct 2010 20:59
Quote: "would I need need to then scan the entire array to find which rooms are part of that house?"


That would be the naive fail-safe way of doing it. Your problem is a standard one in computer science and efficient techniques have been developed for it. I'll post back if I can find some relevant links (the books by Donald Knuth discuss this sort of thing at length but I wouldn't recommend starting there ).

In the meantime you might like to look up things like list processing and linked lists. If I recall correctly a lot depends on whether your list of rooms is ordered in some logical way - for example, town 1 first, then within the town 1 rooms, street 1 is first and so on. In that case simple binary search would locate a given house or room quickly.

Alternatively your player could just search the house ...
Math89
22
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 9th Oct 2010 21:31
What I would do: create arrays for each type of object (one for towns, one for the houses, etc.) and then using the memory commands (make memory, if I remember correctly) or the memblocks, you can create some kind of array inside your types, holding the ID of the sub-objects.
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 10th Oct 2010 03:57
Here's what I would do:
first, have the classes as follows


Then, create assign values to these variables:



Then, create three arrays:


If you wanted to find room a of house b of town c, the general format would be:
<c,c*maxHousesPerTown+b,(c*maxHousesPerTown+b)*maxRoomsPerHouse+a>
if you had indexing from 0 to townNum-1, 0 to maxHousesPerTown-1, and 0 to maxRoomsPerHouse-1.

Madscientist
16
Years of Service
User Offline
Joined: 23rd Aug 2009
Location: Between a rock and a hard place
Posted: 10th Oct 2010 05:19
How about not storing the information itself in an array but only store the array index. Then just read the correct data from a text file.

My computer surpasses all the technologies of the day. What computer do I have?

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 10th Oct 2010 14:14
Neuro Fuzzy

The problem with your approach is that it can be VERY wasteful of space. If you have one big town with, say, 1000 houses, and one big house somewhere with 12 rooms then with 20 towns in total you'll need an array with 1000x12x20 = 240000 entries. If the majority of houses have only 4 rooms and most towns have fewer than 100 houses then that's a lot of wasted array space. However, if you know the max sizes are not too big then the wasted space might be tolerable and is admittedly easier to deal with.

If the wasted space is likely to cause problems then a more elegant solution will be needed, ideally with an array with one entry per house and no wasted space plus possibly a few lookup arrays.

I'm sure this sort of issue has been discussed before but I can't immediately think of a suitable search phrase. Temporarily gone brain dead.
Zotoaster
21
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 10th Oct 2010 15:19
This might not answer your question exactly but it should help solve your problem. http://forum.thegamecreators.com/?m=forum_view&t=115298&b=1

"everyone forgets a semi-colon sometimes." - Phaelax
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 10th Oct 2010 19:50 Edited at: 10th Oct 2010 20:01
Hmmm, well I guess you could use array insert and array delete at location, but... does DBPro use linked lists or actual arrays? It could end up costing the processor too much.
[edit]
o'course it all depends on whether you want to have dynamic adding and removing of houses. If you have a static size, after setting all the variables, you could create arrays the desired sizes (The number of towns would be the value of the variable supplied, the number of houses would be the sum of the num of houses per town, and the number of rooms would be the sum of all those houses). Then, if you store what town each house is in, and what house each room is in, then you could do a lil' binary search to find a certain room or house (because the other option is summing up all the rooms or houses before it).

If you want it to be dynamic, then I would use the memory thing like with the quadtree.

Login to post a reply

Server time is: 2026-07-22 03:50:43
Your offset time is: 2026-07-22 03:50:43