Finite State machine: What am I doing wrong?

Started by Isegrim, Tue 02/12/2003 17:32:27

Previous topic - Next topic

Isegrim

Hi!

I'm still working on a tournament game. For the Swordfight discipline I thought I could use a FSM.
Basic Idea:
Two knights (Knubblival and Knubbelot aka VAL and LOT) are fencing (they're both left-handed).
Due to the anatomy of the characters (Knubbels; my personal comic figure), the knights begin facing each other nose to nose (state_parallel) where they can't hit each other; If one knight moves 45° to the left (input_val if it's VAL or input_lot if it's LOT)  he can hit the other (state_attacklot or state_attackval); if he moves another 45° to the left, or the other to the right (see above), then the opponent gets his shield in the way and can parry the strike.

Code (global script):
Quote
// main global script file
#define FSM_sword 1

#define state_parallel 1
#define state_attackval 2
#define state_parrylot 3
#define state_attacklot 4
#define state_parryval 5

#define input_val 1
#define input_lot 2



function game_start() {
 // called when the game starts, before the first room is loaded
 
//Nasen aneinander (nose to nose)
createFSMState(state_parallel, 2);
addFSMStateTransition(state_parallel,input_val,state_attackval);
addFSMStateTransition(state_parallel,input_lot,state_attacklot);
addFSMState(FSM_sword,state_parallel);


//Knubblival im Vorteil (advantage VAL)
createFSMState(state_attackval, 2);
addFSMStateTransition(state_attackval,input_val,state_parrylot);
addFSMStateTransition(state_attackval,input_lot,state_parallel);
addFSMState(FSM_sword,state_attackval);


//Knubbelot pariert (LOT parries)
createFSMState(state_parrylot, 1);
addFSMStateTransition(state_parrylot,input_lot,state_attackval);
addFSMState(FSM_sword,state_parrylot);


//Knubbelot im Vorteil (advantage LOT)
createFSMState(state_attacklot, 2);
addFSMStateTransition(state_attacklot,input_val,state_parallel);
addFSMStateTransition(state_attacklot,input_lot,state_parryval);
addFSMState(FSM_sword,state_attacklot);


//Knubblival pariert (VAL parries)
createFSMState(state_parryval, 1);
addFSMStateTransition(state_parryval,input_val,state_attacklot);
addFSMState(FSM_sword,state_parryval);


//Schwertkampf-Maschine (Swordfight-FSM)
createFSM(FSM_sword,state_parryval);

Display("Status: %d",getCurrentFSMState(FSM_sword));  
 
}

function repeatedly_execute() {
 // put anything you want to happen every game cycle here
 int currentstate= getCurrentFSMState(FSM_sword);
 if (GetRoomProperty("Swordfight")){
   
   if (IsKeyPressed(375)) //Links oder defensiver Move von Lot (VAL left/LOT right)
   {if (currentstate != state_parrylot)
   Wait(15);
   doFSMStateTransition(FSM_sword, input_val);
   Display("Status: %d",currentstate);}

   if (IsKeyPressed(377)) //Rechts oder offensiver Move von Lot (VAL right/LOT left)
   {if (currentstate != state_parrylot)
   Wait(15);
   doFSMStateTransition(FSM_sword, input_lot);
   Display("Status: %d",currentstate);}
}

PROBLEM: As you may have noticed, that code shows the current state whenever I press one of the arrow keys. The output is always zero (except before the first transition, where it is 5 as it should be.)  

If anyone wants to try it out, it works with a blank room that has the property "Swordfight" set to 1.

Please help, for I have NO idea what's wrong!
This post was generated automatically and therefore bears no signature.

Scorpiorus

hello,

You have to create a FSM first. Then you add all the states.

on_game_start

createFSM(FSM_sword,state_parryval);

Also it's a good idea to check the value functions return:
int error = createFSM(FSM_sword,state_parryval);

so you can see if the error occured or not.

~Cheers

Isegrim

Oh, yeah, thanks!

And again you saved my game!
This post was generated automatically and therefore bears no signature.

SMF spam blocked by CleanTalk