FOnline Development > Share Your Work

Turn-based combat shortkeys

(1/2) > >>

wladimiiir:
Let me share a simple solution how to use keyboard to end turn-based turn and combat.

First, let's create (unsafe) scripts on Server side, that will end turn and/or try to end combat. I have chosen combat.fos for that, but you can add these functions wherever suits you the best.
So this is the function for ending turn:

--- Code: ---void unsafe_EndTurnBasedTurn(Critter& player, int, int, int, string@, int[]@)
{
    Map@ map = player.GetMap();
    if(!valid(map))
        return;

    if(map.IsTurnBased() && (player.Stat[ST_CURRENT_AP] > 0 || player.Stat[ST_MOVE_AP] > 0))
    {
        player.StatBase[ST_CURRENT_AP] = 0;
        player.StatBase[ST_MOVE_AP] = 0;
    }
}
--- End code ---
Basically, what is done here (besides some basic checks), is we are setting Action points of player to 0 and also Move AP for Bonus move perk to 0 (thanks b_B), which makes engine to end current turn.

To end combat we have to use MODE_END_COMBAT for that, which says to engine that we would like to end combat. Function looks like this:

--- Code: ---void unsafe_EndTurnBasedTurn(Critter& player, int, int, int, string@, int[]@)
{
    Map@ map = player.GetMap();
    if(!valid(map))
        return;

    if(map.IsTurnBased() && (player.Stat[ST_CURRENT_AP] > 0 || player.Stat[ST_MOVE_AP] > 0))
    {
        player.ModeBase[MODE_END_COMBAT] = 1;
        player.StatBase[ST_CURRENT_AP] = 0;
        player.StatBase[ST_MOVE_AP] = 0;
    }
}
--- End code ---
That's it for server side.

Next think we need to do is to call these (unsafe) scripts from client side. We decided to use keyboard input for that, more specifically SPACE to end turn and CTRL+SPACE to end combat. Of course, you can use whatever shortkeys that suits you better. ;)

Let's create simple functions that will do some checking before calling our (unsafe) scripts. I will add them to client_main.fos for simplicity of the example, but again, add them wherever they suit better in your code (to make it cleaner).

--- Code: ---bool EndTurnBasedTurn()
{
   if(__ConsoleActive)
      return false;

   CritterCl@ chosen = GetChosen();
   if(valid(chosen) && chosen.IsTurnBasedTurn())
   {
      RunServerScriptUnsafe( "combat@unsafe_EndTurnBasedTurn", 0, 0, 0, null, null );
      return true;
   }
   
   return false;
}

bool EndTurnBasedCombat()
{
   if(__ConsoleActive)
      return false;

   CritterCl@ chosen = GetChosen();
   if(valid(chosen) && chosen.IsTurnBasedTurn())
   {
      RunServerScriptUnsafe( "combat@unsafe_EndTurnBasedCombat", 0, 0, 0, null, null );
      return true;
   }

   return false;
}
--- End code ---

And finally, let's use them in client_main.fos in function key_down. So add following lines before return statement:

--- Code: ---if(!AltDown && !ShiftDown && !CtrlDown && key == DIK_SPACE)
   if(EndTurnBasedTurn())
      return true;

if(!AltDown && !ShiftDown && CtrlDown && key == DIK_SPACE)
   if(EndTurnBasedCombat())
      return true;
--- End code ---

And now, let's enjoy turn-based combats even more. ;)


Implemented solution can be also found in FOnline: Vault 112 project.

JovankaB:
I think you should also set ST_MOVE_AP to 0, otherwise the turn might not end if you have bonus move perk (or something else that increases ST_MAX_MOVE_AP).

wladimiiir:

--- Quote from: b__B on July 22, 2013, 04:08:34 pm ---I think you should also set ST_MOVE_AP to 0, otherwise the turn might not end if you have bonus move perk (or something else that increases ST_MAX_MOVE_AP).

--- End quote ---
Good point, thank you. :)

Kilgore:
Thanks, I'll use it  :P

JovankaB:
2238 has a macro for ending turn: _EndTurn in _macros.fos

Navigation

[0] Message Index

[#] Next page

Go to full version