4

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 action taking damage or removing 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.

Ilya Gazman
  • 285
  • 5
  • 14
  • "Unit can perform the action attack, only if the other unit performing the action taking damage or removing your self" Shouldn't this be backwards? – Doval Jul 14 '14 at 11:49
  • @Doval No, those two actions should happened in the same time. One unit performing attack while the other performing taking damage. The other unit cannot perform move while he been attacked. – Ilya Gazman Jul 14 '14 at 11:52
  • Taking damage is more of an impediment that is involuntary. That is, if a unit is under attack by another unit, whatever action was originally being undertaken by the unit being attacked would be slowed down, or cancelled entirely. – rwong Jul 14 '14 at 11:53
  • 1
    It would be clearer to say "if and only if" then. However, your problem is still ambiguous. You're implying attacks take 0 time (since the target of the attack must immediately be in "taking damage" or "removing your self" when the attack occurs) which means you can have circumstances where two opposing units attack each other simultaneously or one unit is attacked by more than one unit at once. No one here can tell you how such ties should be broken, because it's a game design issue. – Doval Jul 14 '14 at 11:56
  • @Doval I edited the question. I hope it is more clear now. – Ilya Gazman Jul 14 '14 at 12:22
  • 1
    Are you trying to find all possible actions that *could* be done in parallel, or are you trying to find all actions that are *currently* being done in parallel? In either case, what have you tried? You've said next to nothing about how your game is structured, so we don't know what information is available to you. – Doval Jul 14 '14 at 12:36
  • @Doval it's simple as: Input array of actions. Output: group of array of actions, that can be animated in parallel. This is one output solution, I did not specified this as I don't know how to implement it. My only constant is the input. – Ilya Gazman Jul 14 '14 at 12:43
  • What do you mean by "in parallel"? – Doval Jul 14 '14 at 12:49
  • @Doval in parallel = simultaneously. Will start their animation in the same time. Please edit my question if that wasn't clear to you. It's kind of what the question is about. – Ilya Gazman Jul 14 '14 at 13:34
  • Each action needs a startup-time and an action-time. For example, a jab might take 5 units to start and only take 10 units to finish, whereas an uppercut might take 15 units to startup and 15 units to finish. You then sort your actors by when their next action occurs and process each actor accordingly. After each action, the actor gets re-sorted back in the queue for their next action. If actions occur simultaneously then you decide how to handle the result (e.g. both targets take damage OR randomly pick who goes first OR defender always goes first etc..). – Dunk Jul 14 '14 at 15:24

2 Answers2

3

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:

  1. 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).
  2. wait for all threads to complete.
  3. create threads for all game characters and perform attacks.
  4. wait for all threads to complete.
  5. create threads for all game characters and calculate damage, and remove characters if they are dead.
  6. wait for all threads to complete.
  7. 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.

FrustratedWithFormsDesigner
  • 46,105
  • 7
  • 126
  • 176
2

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.

Mare Infinitus
  • 223
  • 1
  • 9
  • "Find actions possible for your units in parallel:" is the only thin that I am after. I think that scheduler is a great idea, but how can I map all my actions on it? The goal is to make the overall time as shorter as possible of course. Please tell me what info do you need? – Ilya Gazman Jul 14 '14 at 18:55
  • You need some environment model to calculate what actions are possible. Please provide information on kinds of action, your game model (2d, 3d) and whether your units are orientated (i.e. do they see in a direction, can they attack something that is behind them, can they move backwards, ...) – Mare Infinitus Jul 14 '14 at 19:18
  • The kind of action is part of game design and it will be probably change a lot. This is 2D game, and units can move to 4 directions and attack to many directions(range is bigger than 1) – Ilya Gazman Jul 14 '14 at 19:38
  • A 2D array for positions of your units would do here. If more than one unit can have the same position, then you will have an array of units in each position. Now you can easily calculate the possible actions. – Mare Infinitus Jul 15 '14 at 05:31