Author Topic: Finding objects  (Read 18196 times)

Nolithius

  • Newcomer
  • Posts: 25
  • Karma: +0/-0
  • Ebyan "Nolithius" Alvarez-Buylla
    • View Profile
    • Nolithius.com
Re: Finding objects
« Reply #15 on: August 02, 2010, 07:36:16 PM »
My implementation is similar to Bear's (excluding items):

Each area has a list of Actors (base class for Monster and Player), that is only iterated through if I need to find monsters within FOV (for ranged targeting) or other such effects.

For collision, I have each map Tile point to its Actor, null if none (only one Actor per tile). Similar to Hajo's approach, since the tiles are stored in a 2D array by coordinate, this allows for quick collision checks (passable = tile.isPassable && tile.actor == null) and the ability to draw a map by iterating through tiles and not having to iterate through any external monster/item list.

The downside to this approach is, of course, that when an Actor moves (Actors keep track of their x and y), it must notify the map so that the reference can be removed from the previous tile and placed on the new one. Minor, considering the benefits.

Hope this helps!

Ebyan "Nolithius" Alvarez-Buylla
http://www.nolithius.com

pol

  • Newcomer
  • Posts: 7
  • Karma: +0/-0
    • View Profile
    • Email
Re: Finding objects
« Reply #16 on: August 03, 2010, 01:23:10 AM »
And don't be afraid to have the same thing listed in multiple places. How else would you make an efficient list of Object->Doors without having a list (not a linked list) of all objects and a list of all doors. C++ std::maps are good, I use them for almost everything. If you delete objects, or remove them or save them to an off-screen map, you can keep an std::list of recently deleted numbers for your map to reuse before going on to NUM_ITEMS_IN_MAP +1 or however you manage your global lists of objects. And only use linked lists when they are appropriate and useful for the problem at hand, like if you want things to be in a specific order or if it doesn't matter if you need to get something that is located in the middle of the list. Check out all of the std:: things and see what does what and what is appropriate in which situations - a lot of the boost:: stuff is good too.

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: Finding objects
« Reply #17 on: August 05, 2010, 11:13:41 AM »
Another question, actually related to object finding. Let's say the player pushes a large boulder on water. How to check and destroy the boulder? If you need to check the entire list of boulders then it could become slow. How this is usually done? I was thinking of some kind of "destroy list" to add the object, then go through that list and destroy object after everything else is done in a turn.

Etinarg

  • Rogueliker
  • ***
  • Posts: 424
  • Karma: +1/-1
  • Idea archivist and game tinkerer.
    • View Profile
    • Gedankenweber Blog (German)
Re: Finding objects
« Reply #18 on: August 05, 2010, 12:00:32 PM »
I was thinking of some kind of "destroy list" to add the object, then go through that list and destroy object after everything else is done in a turn.

I did that in some projects. It also works to mark the objects as "to be destroyed" and go through the list of objects after a turn. In my roguelike projects there are seldom more than a few dozen objects on a level, so that is still fast enough to be checked each turn.

You can skip the check on turns when no object was flagged. You can track that separately in a simple boolean.

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: Finding objects
« Reply #19 on: August 05, 2010, 02:12:03 PM »
You can skip the check on turns when no object was flagged.

I think that's a good solution, because destroying objects happens rarely so going through the list doesn't make the basic strolling slow.