7

I'm writing a text game and I need a simple combat system, like in MUDs, you issue commands, and once in a while "tick" happens, when all those commands execute, player and monsters deal damage, all kinds of different stuff happens. How do I implement that concept? I thought about making a variable that holds last tick time, and a function that just puts events on stack and when that time is (time +x) executes them all simutaniously. Is there any easier or cleaner variant to do that?

What would be possible syntax for that?

double lastTickTime;
double currentTime;

void eventsPile(int event, int target)
{
// how do i implement stack of events? And send them to execute() when time is up?
}

void execute(int event, int target)
{
     if ((currentTime - lastTickTime) == 2)
     {
         eventsHandler(event, target);
     }    
     else 
     { // How do I put events on stack?
     }
}
Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
Dvole
  • 5,725
  • 10
  • 54
  • 87

3 Answers3

3

The problem with simple action stack is that the order of actions will probably be time based - whoever types fastest will strike a first hit. You should probably introduce priorities in the stack, so that for instance all global events trigger first, then creatures' action events, but those action events are ordered by some attribute like agility, or level. If a creature has higher agility then that it gets the first hit.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • 2
    Well that sounds hugely complicated to me, I'm just starting and it's my first programming project so – Dvole Nov 20 '10 at 19:54
2

From what I've seen, most such engines are event, rather than time, based. with a new tick being triggered some interval after the last tick ended. (thus mostly avoiding the issue of ticks taking longer than the interval)

This also simplifies implementation; you simply have a game loop that triggers a tick event, then sleeps/yields for the required interval. Which is trivial.

It can further be simplified by modeling the world as a tree, where each element manages propagating events (such as ticks) to their children. so long as you avoid / manage 'loops', this works well (I've done it).

This effectively reduces the tick system to something like this (psudocode):

while (isRunning) {
    world->tick();
    sleep(interval);
}

In most cases, theres little need to get much fancier than adjusting for the length of the previous duration.

Any individual entities actions would be part of their own action queue, and handled during their own "tick" events.

Usually user commands would be split into "ingame" and "meta" commands, anything ingame would merely amend their character's action queue, to be processed in their next tick, as per normal for any other entity.

Simple round-based combat follows naturally from this foundation. realtime can be modeled with a finer division of ticks, with optional 'time-pooling'.

Textmode
  • 509
  • 3
  • 18
-1

Use a timer executing every x ms (whereas x is your ticktime), execute any actions put on the stack in that method.

Femaref
  • 60,705
  • 7
  • 138
  • 176