Jump to content
  • Advertisement
3dmodelerguy

C# Managing Item Decay In Turn Based Survival

Recommended Posts

So I am building a survival / roguelike? (not sure of the "real" definition of that but the game has perma-death, randomly / procedurally generated worlds, etc.) and I am starting to prototype item decay. Let me example how the turn based system is currently simulated as that is probably important for this discussion.

The way the turn based simulation works is there is a singleton GameActionManager object that keeps track of the action units that have pasted since the being of the game. 1 action unit = 1 second in the game and generally this is progressed from the the player performing an action. When the player does anything that has an action unit cost associated with it, it calls a method on the GameActionManager to increase the current action units. The other things the GameActionManager exposes are methods to register / unregister "event listeners" (currently using delegates). When the GameActionManager increases the action units, it notifies all of these listeners so the can perform the right action (like a burning tile decreasing the health of the structure that is burning, an enemy can move toward or attack something, etc.).

I am also only simulating a small portion of the world at a given time since well, the world eventually will be huge and well computers can only do so much. My current goal for the simulated area is 120 x 80 tiles (with is a total of 9600 tiles). So as the player moves, game entities (enemies, structures, items, etc.) come in and out of the simulation area (and register / unregister with the GameActionManager). With this size in mind, none of the things that currently attach listeners to the GameActionManager has the chance of getting big enough to have me think that I really need to think about an alternative solution for when it becomes an issue because I don't see it becoming an issue any time soon (at least at the current stage of development). Items however is a different story.

The game is going to have a ton of different items and 1 or more items can be on any tile. If each tile averaged 2.5 items (which I will grant you seem ridiculous but I always think crazy extremes when it comes to this kind of thing), that would be close for 25000 items that would possible have to manage decay. So instead of having each item attach a "listener", since the action for managing decay is going to be exactly the same thing for each item (just a method call), I figured having 1 listener that knows about all the items would be better.

The general approach that I am taking for this first prototype is to have a singleton manager class (lets call it ItemDecayManager). Any time a item that has decay come into the simulated area, it would register itself with the manager and when it leaves the simulated area, it would unregister itself (which would just add and remove itself from a private List the manager is maintaining). Any time the action units are increased, the ItemDecayManager's listener on the would fire and just loop through the List of registered item and just perform the required function call.

I have done some crude benchmarking and it currently can handle 100000 items at which point it starts to have a little effect on the FPS (but still around 80) even when moving about 15 times per second (and each move causes an action unit increase). Also bear in mind these numbers of from running the game in the Unity editor which has a bit of extra overhead that running the real game build not not have.

While I am going to run with this solution for now I want to throw out this idea and get any kind of feedback i can because I imagine when more is happen as the action units increase (and even imagining the process for managing decay become more even just slight more complex than the current simple calculation), this solution might not hold up and I would like to have so ideas ready for the situations. I also think of anything that is going to be simulation, this is going to be the first things to have issues  from the shear number of items that could be in the simulation area.

Thanks in advance for any and all comments.

 

Share this post


Link to post
Share on other sites
Advertisement

Try to determine the absolute maximum number of items that might realistically occur in the typical game session (be generous), then increase it by 10%. Benchmark for that number of items, and if it performs acceptably then move on. Don't worry about optimizing for the "crazy extremes" cases. (I have a hard time believing that 100000 items is anywhere in the neighborhood of realistic; no player has time to sort through that amount of stuff.)

Share this post


Link to post
Share on other sites

Is it actually necessary to simulate all the intermediate ticks of decay, or do you only care about the end result?

For example, let's say the player lights a barrel on fire, walks away for 20 minutes, and walks back. Is it important that you simulated 1,200 discrete action units, or is it only important that the barrel has taking 1,200 points of decay (and presumably has long since been destroyed)?

If the latter, then there is no need to actually simulate anything beyond the viewing radius of the player. Just record the timestamp/turn on which a status effect (like burning) was applied to a given object, and stop updating it once the player can't see it anymore. If the player wanders back, you can just update the state based on the timestamp.

This can work fine even for broader effects, such as fire that spreads over time. As long as you know where the fire was lit, and how long it has been, you can put an upper bound on the extent of the fire, and only need to update individual items when that extent intersects the player's viewing radius.

TL;DR: processor cycles are valuable. If the player can't see a thing, might as well not perform detailed simulation of it.

Share this post


Link to post
Share on other sites

My advice is to think of how far a person could be and see the item. That amount of steps *2.5 will allow the person to pick it up if they want it. If they don't want it, it should decay.

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!