BlitzCoder Essentials
•
Home Page
•
About BlitzCoder
•
Contributors
•
Terms of Use
•
Email Us
Main Areas
•
BlitzCoder Chat
•
Discussions
•
Articles/Tutorials
•
Code Database
•
Link Database
•
Showcase Area
•
Worklogs
•
Competitions
Special Areas
•
Undocumented
Other Blitz Sites
•
Blitz Basic Home
•
Blitz Showcase
•
BlitzPlay Library
Forum Login
Username:
Password:
•
Register Now!
BlitzCoder Code Archives Page
Main Codes Page
"Simple Finite State Machine"
, by rick
This is a simple example, where the state of an enemy changes as the player<->enemy distance is changing.
Code
;This is my first try to create a Finite Sate Machine for the enemy ;If the enemy is within a range of 50 tot 30 from the player, the enemy sees you and gets suspisious, and start walking towards you. ;If the enemy is within a range of lower than 30 from the player, the enemy knows you are the enemy, and he runs towards you. ;Rick Overman ;init Graphics3D 800,600,0,2 SetBuffer BackBuffer() ;constants Const GROUND = 1, ENEMYBODY = 2, PLAYERBODY = 4 ; Entity types Const SLEEPSTATE = 1, SUSPISIOUSSTATE = 2, DETECTEDSTATE = 3 ; FSM States ;collisions (who, what and how) Collisions PAYERBODY,GROUND,2,3 Collisions ENEMYBODY,GROUND,2,3 Collisions PLAYERBODY,ENEMYBODY,2,3 ;types Type Enemy Field entity Field x#,y#,z# Field state%, statetxt$ Field playerdistance# End Type Type Player Field entity Field x#,y#,z# End Type ;ground surface plane = CreatePlane() EntityType plane,GROUND ;light please light = CreateLight() ;camera cam = CreateCamera() PositionEntity cam,0,70,0 RotateEntity cam,90,0,0 ;player Player1.Player=CreatePlayer(0,5,0) ;enemies Enemy1.Enemy=CreateEnemy(30,5,30) Enemy2.Enemy=CreateEnemy(10,5,30) ;And ..... action While Not KeyHit(1) UpdateGame() UpdateWorld RenderWorld ;Just some debugging output Color 0,0,255 Text 100,0,"player_x: "+EntityX(Player1\entity) Text 100,10,"player_y: "+EntityY(Player1\entity) Text 100,20,"p_z: "+EntityZ(Player1\entity) Color 255,0,0 Text 400,0,"enemy1_state(FSM): "+Enemy1\state+" >> "+Enemy1\statetxt; Text 400,10,"enemy1_playerdistance: "+Enemy1\playerdistance; Text 400,20,"enemy2_state(FSM): "+Enemy2\state+" >> "+Enemy2\statetxt; Text 400,30,"enemy2_playerdistance: "+Enemy2\playerdistance; Flip Wend End Function UpdateGame() For p.Player=Each Player UpdatePlayer( p ) Next For e.Enemy=Each Enemy UpdateEnemy( e ) Next End Function Function CreatePlayer.Player(x#,y#,z#) p.Player=New Player p\entity=CreateCube(); EntityColor p\entity,0,0,255 PositionEntity p\entity,x,y,z EntityType p\entity,PLAYERBODY ResetEntity p\entity Return p End Function Function UpdatePlayer( p.Player ) ;player movement If KeyDown(200);up MoveEntity p\entity,0,0,1 ElseIf KeyDown(208);down MoveEntity p\entity,0,0,-1 EndIf If KeyDown(203);left TurnEntity p\entity,0,2,0 ElseIf KeyDown(205);right TurnEntity p\entity,0,-2,0 EndIf End Function Function CreateEnemy.Enemy(x#,y#,z#) e.Enemy=New Enemy e\entity=CreateCube(); RotateEntity e\entity,0,0,90 e\state = SLEEPSTATE; EntityColor e\entity,255,0,0 PositionEntity e\entity,x,y,z EntityType e\entity,ENEMYBODY ResetEntity e\entity Return e End Function Function UpdateEnemy( e.Enemy ) For p.Player=Each Player e\playerdistance = EntityDistance(e\entity,p\entity) If e\playerdistance<50 And e\playerdistance>30 ; Enemy is Suspisious PointEntity e\entity,p\entity,90 MoveEntity e\entity,0,0,.05 e\state=SUSPISIOUSSTATE e\statetxt = "Carefull he is Suspisious!" ElseIf e\playerdistance<30 ; Enemy Angry PointEntity e\entity,p\entity,90 MoveEntity e\entity,0,0,.7 e\state=DETECTEDSTATE e\statetxt = "Run You are DETECTED!!!!" Else ; Enemy does nothing e\state=SLEEPSTATE e\statetxt = "zzzzz (sleeping)" End If Next End Function
Copyright(c) 2000-2004, BlitzCoder. All Rights Reserved.
Code software created by Krylar's Kreations