• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
sooner123

Predator-Prey simulation

10 posts in this topic

I want to write a simplistic predator-prey simulation in which predators and prey move about on a grid, avoiding/devouring each other.

For the first iteration, I want an Entity base class, a Creature class which inherits from Entity. (later on a Food class which inherits from Entity as well), and Predator and Prey classes which inherit from Entity.

Predator and Prey will move about on the grid randomly (for now) and if a predator happens to stumble on a prey's square, said prey gets deleted and said predator's fitness is augmented.

I'm having some trouble coming up with a good design for the logic of everything.

Basically my first idea was to have 'grid' be a 2d array of pointers to type Entity and 'entities' be an array of pointers to type Entity. This way I can loop through the grid to output the state to the screen and loop through entities to perform their actions AND have access to the state of a specific square via 'grid' in order to determine the result of a member of 'entities' moving to its target square.

This seems redundant and runs into the following issue: what if two predators or two prey occupy the same square. An element of 'grid' is a pointer to only one Entity. Is the solution to make 'grid' be a 2d array of pointers to type ListOfEntities? I can't decide how to handle this issue because the other solution seems to be to have Entities being aware of their x,y location, and then when I'm outputting the grid, I need to scan through every entity, for every square, in order to determine where everything is. And in the logic of predator/prey moving around, I have to scan through every entity to see which entities are at the target square.

Also my thinking is that an Entity should not be aware of its x and y location and that that logic should be handled fully by the indexing of the 'grid' array. Does this seem like good design of the data structures? Edited by sooner123
0

Share this post


Link to post
Share on other sites
I would avoid the class hierarchy if at all possible. I don't think you'll get much from it.

I don't know enough details of what you are trying to do to suggest a design. In particular, will the animals have internal state? (hit points, hunger level, age...)
1

Share this post


Link to post
Share on other sites
Storing creatures in two separate structures (a list and a grid of lists) is a practical idea. You can iterate over the items in the list to perform general creature actions and use the grid to sample for neighboring animals.

Removing an item from one set means having to remove it from the other as well, though. To avoid having to let entities keep a reference to their current grid-cell, you could in a first pass perform the preying/eating/etc., flag killed animals for deletion, and then later sweep over the grid to remove flagged items.

Using a class hierarchy seems needlessly complicated. What are the similarities between prey and predator to have them both derive from a single base-class? And how would you (virtually) distinct between items when you grab an 'Entity'-item from a list? Edited by eppo
1

Share this post


Link to post
Share on other sites
Was going to have a member of the class called 'designation' that would be equal to "predator" or "prey." Seemed like a bad way to do it, but it also seemed better than messing with typeinfo stuff.

I'll ditch the class hierarchy. It wasn't benefiting me at all.

I was going to do it the way you mentioned (with the pass over the creature list, then the sweep over the grid) Hadn't thought about how I needed to delete both though. The flagging seems like a perfect way to handle it.

As for internal state of animals, for now the only information creatures will store is a fitness value for predators. Later on there will be reproduction counters and some genetic data that hopefully I can map to behavior (probably a series of simplistic weights on vectors of proximity to food/predator/prey)
0

Share this post


Link to post
Share on other sites
One problem with that two pass method is that the consequences of actions of an entity can depend on an action taken by an entity encountered earlier in the same pass.

So I'm back to not being sure how to handle this without entities being explicitly aware of their location which just seems wrong to me.

Example: a prey moves off a square, while I'm cycling through all the creatures. i need to remove this creature from the list of creatures for that square. in order to do that i need to know which square the creature was on.

Is it wrong of me to think that entities being aware of their location is wrong? I'm not sure why it strikes me that way but I'm trying to look at it from the point of view of the creature, who doesn't understand their x/y location. Only their location relative to other creatures. Edited by sooner123
0

Share this post


Link to post
Share on other sites
Making the location part of the entity doesn't fell wrong to me at all. If you insist on not having it, you'll have to pass the position to the function that allows a prey to move [and many other functions].

EDIT: You could also make the function return what the creature wants to do, and the code that asked the creature (which knows about the location) is now responsible for executing the action. Edited by Álvaro
1

Share this post


Link to post
Share on other sites
If a creature object has no concept of if or where it is in a list, you'll have to do a linear search over all the list's elements until you find it, before you can remove it. If you have a lot of creatures, this might be too expensive. If you quickly and immediately need to remove an object, the following isn't too bad:

[CODE]
class Grid{

class Cel{ List<Animal> animals; }

Cel cels[][];
}

class Animal{

Grid::Cel *cel;

setPos(Coord pos){ cel->remove(this); (cel = grid->celGet(pos))->add(this); }
}
[/CODE]

Unless even a single cel-list becomes too long to traverse, in which case you could make the animal point directly to a container object that encapsulates a pointer to itself. Edited by eppo
1

Share this post


Link to post
Share on other sites
Wow. A lot of that syntax is totally different from anything I ever knew would work. I had to do some googling to understand it all. I wasn't even aware that assignment returned a reference to the lefthand operand. That's awesome. Love the way that single line of code reassigns the cel reference for that Animal and simultaneously adds the Animal to that cel in the grid. Very clever. I have tons to learn.

I really appreciate both your help. Thanks a lot :)
1

Share this post


Link to post
Share on other sites
If you are worried by race conditions which cause the simulation results to depend on the order in which entities are processed within an update, you are trying to implement incorrect and inconsistent rules. There are at least two rather incompatible fixes:[list]
[*]Process an event queue. After an initial random assignment of priorities to the starting population, you process one entity at a time, putting it back into the priority queue to schedule its next move. No conflicts, and you can support different durations for different actions. You can purge dead entities when they come to the top of the queue, no need to search whenever someone is eaten. Downsides: probably, low cache coherence (successive scheduled entities aren't close in the map).
[*]Process simultaneous actions, keeping two separate state representations for each update: "before" (read-only) and "after" (write-only). If entities can only affect adjacent cells (by moving there, for example) you could split the map into 3 or 9 subsets of cells that cannot possibly interfere with each other, and process them in a round-robin fashion; or you can more simply choose tie-breaking rules (e.g. if multiple entities try to enter a square, all but the eldest fail and skip a turn)
[/list]
0

Share this post


Link to post
Share on other sites
Yep I am very concerned about simultaneity issues. An issue that also plagued an implementation of a neural network simulation I helped someone write a few years ago.

I'm not sure what you mean by the "two separate state representations for each update" though. Example, prey on A1, predator on C1, prey wants to move to B1 and predator wants to move to B1 in same round, which I might ideally want to result in prey being eaten, regardless of the order in which I process their actions. How would your second suggestion apply to this scenario?
0

Share this post


Link to post
Share on other sites
I'm not entirely certain if this is what Lorenzo was suggesting, but if you have an array indicating where everything would be after their moves on a given turn, you could populate it by looping through all of your entities and assigning movements to them. Then, after they have all decided where to move, check which areas contain two or more entities (you wouldn't need to check every tile, only ones with at least one entity) and process their actions that way (if one is a prey and the other is a predator, just kill the prey; or, if both are predators, move one of them to an adjacent empty tile, etc.).

I think that would give you your desired effect.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0