I am building a strategy game where multiple units(5 – 20) fighting each other.
I have a game logic that calculate all the actions that been done on each turn, and send it to the game engine to animate.
Those are the possible actions:
- perform attack
- perform taking damage
- perform moving
- perform missing
- perform removing your self(dying)
How can I find what actions I can perform in parallel and what are not?
Those are the limitations that I have:
- units can only perform one action at a time.
- each action may have different times, even if its the same action
- Unit can perform the action
attack
, only if the other unit performing the actiontaking damage
orremoving your self
- after unit performed the action:
removing your self
, he cannot perform any other action.
Edit:
Those are the situations that might happened:
- one unit is attacked by multiple units at once
- one unit taking damage while no units are attacking him
- all the units move at the same time
- all the units perform miss at the same time
This question is not about game design, but a programmatic solution to how I can find those parallel tiles of actions?
Reply to FrustratedWithFormsDesign, answer
Hi man, you took it to completely different direction, so I want to reply you here to clear things up.
This questions is not about how to make units to move in parallel. I also don’t think that your suggestion is a good idea.
What you did with your own game this is actually, the right direction. It will be also wise to work in MVC and separate the view and game logics.
So to implement a game engine I would use a game thread that will dispatch tick events, based on chosen frame rate. Only the views should be listening for that thread and implement they view logic based on it.
In my game I am using Adobe Air framework with some native Java and Objective-C support for mobiles, but it’s Air mostly. So all my animations are listening to onEnterFrameEvent, this would be tick in your example, and implement their animation logic.
My game logic is using a different worker(air threads implementations, it’s more like a process) to generate all the actions on particular turn. And than it sands it to the game engine to animate. I do not ask how to implement the animations, it’s handled by Air apis, and it doesn’t meter in here.
Think about those problems:
unit a attacks unit b while unit b attacks unit c, while unit c is moving.
This will resolute with a mess on the screen.
I want it to be:
unit a attack unit b, unit b waits for a to finish attacking him to
start attacking unit c, while unit c is moving. After a finish attacking unit b, unit b attacks unit c.
So the input of actions I received was:
- A attack
- B take damage
- B attack
- C take damage
- C move
And the output was two sets of animations
- A attack, C move, B take damage
- B attack, C take damage
This was a very simple example of how I would like to parallel my actions. I hope now it is more clear.
10
If you are talking about having one thread for every game character, you’re going to have some synchronization issues. And you might start running into performance issues if you have many game characters, each requiring its own thread.
If you really want to do things in parallel, you could do try to run multiple threads all performing the same task. For example:
- create threads for all game characters (or up to a limit of n threads) and move the characters by one increment (the distance that a character could move in one unit of game time).
- wait for all threads to complete.
- create threads for all game characters and perform attacks.
- wait for all threads to complete.
- create threads for all game characters and calculate damage, and remove characters if they are dead.
- wait for all threads to complete.
- return to begining.
You could also try using one thread per type of game activity (move characters, attack, calculate damage, etc…). You’d have fewer threads to manage but I think the coordination and synchronization between them would be more difficult, because you’d be running the “move character” and “attack” threads in parallel.
These are just ideas though, I’ve never tried to build a game in this fashion, but it may help.
In my experience with game code (admittedly, it was a few years ago, and on simpler games than what you might be working on), we usually did everything in the context of a single event loop. The loop was usually driven by some kind of timer, and with every tick of the timer, all actions would be processed. So if there were 3 game characters attacking each other on the screen, a single timer tick would:
- process any user input from the mouse or keyboard
- move all units’ on-screen image by a certain amount, if they were in motion.
- move projectiles and other moveable objects.
- check to see if they’d been hit by other projectiles, and if they had taken damage, and if so, how much.
- calculate points earned by the player.
- if time was important, check how much time has elapsed since the last mark and perform some action (such as end the game if they player was taking too long).
- …any other necessary calculations.
This way, there was one master process that was in charge of everything. I suppose we could have used multiple threads if we’d had some complicated calculations, but they’d still be run from the main loop and the main loop would have had to wait until they all finished.
1
There are still some questions open regarding your game and your question.
So, I put in small answers here to questions already arising from what you have given as information:
Get your animations for your actions in the right schedule:
Write a scheduler that starts animations with a given start-up time so that
actions will have a smooth look. For example “attack” has start-up 2 seconds
and performance-time 1 second and “block” has start-up 1 second and performance
1 second. So “attack” should start 1 second before “block” can start.
As there is no information given about programming language or paradigma, only
an educated guess is left: would investigate on Command-Pattern and Chain-of-Command
for this
Find actions possible for your units in parallel:
There is some tiny environment model necessary to see which other units are “in reach”
Think that some coordinate system, depending on your game, will do the job.
Perhaps you can provide more information on that.
See results of actions of your units in parallel:
Again, I would write a scheduler to solve that, too. What I have seen in a game
was a small table leveraging time-stamps (e.g. time with second, date and so on).
This table is read from an eventhandler, which takes every second out of that table,
and handles the results. It is just some kind of message loop, but I think the idea
behind that is very clear.
4