1

i would like to fetch data by name, id(random object ID) or type.
c# has Dictionary.
but it should be pure c/c++ ... not something like Sqlite or anything dotnet.

  pSysBuffer = new D3DCOLOR[ winSizeX * winSizeY ]; // 3rd directx buffer
  // all my 2d stuf gets rendered here


  struct WorldInventory
    {
      Gameobject gameobject;
      // more properties
    } worldinventory[ winSizeX * winSizeY ];

    struct Gameobject
    {
       int[] x, y; 
       unsigned int id;
       Wall* wall;
       Enemy* enemy;
       Bullit bullit;
       Mine mine;
       // more properties
    } gameobject;

Is the GameObject struct a good design? it can either be a wall, enemy, bullit or whatever and can span multiple x,y coordinates.
i donnot wanna use a vector, deque ... because it's changing pretty much every frame ...
how to keep a collection of GameObjects that's ever changing .... but it does not have to be double linked ... i gues.
my technique is pixel precise and not tile or math vector based.
i want to achieve ... something like ... a internal database for objects collission and or other things.
1 object ie: could query and ask if the coordinates are occupied and avoid looping the array.
this is keeping me busy for quite a while

thank you in advance

2
  • You say you want to query without looping through the collection, but in essence that is just what a query is. The collection might be sorted or partitioned in a particular way so that the query needs to look at less items than a linear search might have to, but it is still a search. Commented Dec 15, 2012 at 23:00
  • maybe i should have a "id" string array in sync with the struct array and loop the light weight array to find the heavy object fastly ... i don't know that's my question Commented Dec 15, 2012 at 23:04

2 Answers 2

0

If you plan to do lookups based on coordinates, you can index the array based on those and sort it using divide&conquer, which should provide a fast sorting and lookup method.

For instance, if you have a maximum of 1000x1000 coordinates, you can convert that number in a single integer like this 10001000. 64x33 would turn to 640033. It's a bit crude, but it should work. As long as the array you're sorting with D&C is nicely randomized, you should be receiving good performance.

Ex. of a sorted array

        9720922
    8500088
        7770951
5300033
        450750
    3500112
        1200015
            750099

In this example, if you were inquiring what's going on with coordinate 64x33, you would compare 640033 with 5300033 then move toward 3500112 then toward 1200015 thne 750099. Then you would realize there aren't any further entries so you could place a pointer to that object in object_750099.lower.

The object structure would need some more elements.

GameObject struct {
   ...
   int greater;
   int lower;
}

Hopefully you understand what I'm trying to say because I'm explaining it really badly.

1
  • indeed ... i have seen this before ... i'll give it a try ... and report back Commented Dec 17, 2012 at 20:29
0

Sorry this is not exactly direct answer to your question because
well I think you over thinking this what about this (work for me all the time):

1.game objects

  • create an enum list of all the GO (game object) types

    enum _go_type_enum
     {
     _go_type_none=0,
     _go_type_wall,
     _go_type_mine,
     _go_type_tree,
     _go_type_hole,
     _go_type_roadNS,
     _go_type_roadWE,
     ...
     _go_type_enum_end
     };
    
  • create class/struct for every GO type

  • include grid item specific data (the same class items can have different values) like:
  • hit points,bullet holes,blood stains, blast oxidation,orientation for turnable objects...
  • and do not forget to include world x,y !!!

2.GO data storage

  • create dynamic lists of all GO types like this:

    List<GO_class1> go1;
    List<GO_class2> go2;
    List<GO_class3> go3;
    List<GO_class4> go4;
    ...
    
  • or use static arrays instead (with big enough max)

    //            array       used GOs
    GO_class1 go1[max]; int go1num=0;
    GO_class2 go2[max]; int go2num=0;
    GO_class3 go3[max]; int go3num=0;
    GO_class4 go4[max]; int go4num=0;
    ...
    
  • create world map

    int world_tp[winSizeX*winSizeY]; // _go_type_enum
    int world_ix[winSizeX*winSizeY]; // index inside go?[];
    
  • any GO is defined as tp,ix

  • for example tp=_go_type_wall; ix=5; means go?[ix]
  • where go? is the name of array for walls class

Now when you need to go through walls only the loop just through wall array
if you need to see the neighbouring area look in wolrd(tp,ix)[]
I think this is much better and faster then some class binding/search/query engine.
Also you use much less memory (if dynamic lists used) because GO classes hold just class specific data.
For universal GO class it hold all the data for all classes which is memory wasting and also slows down processing.

Hope it helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.