Author Topic: Turn-based combat shortkeys  (Read 6012 times)

Offline wladimiiir

  • Rotator
  • Independent FOnline developer
Turn-based combat shortkeys
« on: July 22, 2013, 03:37:24 pm »
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: [Select]
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;
    }
}
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: [Select]
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;
    }
}
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: [Select]
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;
}

And finally, let's use them in client_main.fos in function key_down. So add following lines before return statement:
Code: [Select]
if(!AltDown && !ShiftDown && !CtrlDown && key == DIK_SPACE)
   if(EndTurnBasedTurn())
      return true;

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

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


Implemented solution can be also found in FOnline: Vault 112 project.
« Last Edit: July 23, 2013, 09:45:51 am by wladimiiir »

JovankaB

  • Guest
Re: Turn-based combat shortkeys
« Reply #1 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).

Offline wladimiiir

  • Rotator
  • Independent FOnline developer
Re: Turn-based combat shortkeys
« Reply #2 on: July 22, 2013, 04:11:28 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).
Good point, thank you. :)

Re: Turn-based combat shortkeys
« Reply #3 on: July 22, 2013, 04:16:32 pm »
Thanks, I'll use it  :P

JovankaB

  • Guest
Re: Turn-based combat shortkeys
« Reply #4 on: July 22, 2013, 04:22:03 pm »
2238 has a macro for ending turn: _EndTurn in _macros.fos
« Last Edit: July 22, 2013, 04:31:45 pm by b__B »

Offline wladimiiir

  • Rotator
  • Independent FOnline developer
Re: Turn-based combat shortkeys
« Reply #5 on: July 23, 2013, 09:49:34 am »
After some testing, I found out, that when having Bonus Move perk and player uses all its APs except the one from Bonus Move, end turn is not invoked, because of check in the (unsafe) scripts on Server side.

So I have changed the checks from:
Code: [Select]
if(map.IsTurnBased() && player.Stat[ST_CURRENT_AP] > 0)to:
Code: [Select]
if(map.IsTurnBased() && (player.Stat[ST_CURRENT_AP] > 0 || player.Stat[ST_MOVE_AP] > 0))