Space Fight AI Tournament Idea

User contributed sources that have become inactive, deprecated, or generally unusable. But ... we don't really want to throw them away either.
Post Reply
mambazo
Posts: 652
Joined: Jul 17, 2005 13:02
Location: Ireland
Contact:

Space Fight AI Tournament Idea

Post by mambazo »

Artificial Intelligence!

Yes thats right. Anyone who is not busy, and is also bored (should be quite a few, I hope ;) I heartly invite you to a challenge!

Its called NanoShips and the idea is this:-

You write the AI for a freefoating space ship, using the Template_AI.bi provided... Then when we have at least 2 AI files submitted, we get them to battle eachother!

All the ground work has been done (admittedly it wasn't too much work), all that is left is for a few people to rise to the challenge!

Contents of the archive:-

NanoHost.bas - This is the core engine and must not be modified!

VsConfig.bi - This file is used to link the AI functions to a ship

Template_AI.bi - Use this as a template to fill in with your own AI code. Your AI code is a single function, given values from the NanoHost engine telling you your position, their position, the nearest enemy laser and so on. In here you also set a few unique things for your ship (name, colour, draw function)

The AI file you write will return an Action and a Message to NanoHost...

' The available return actions are:-

' IDLE
' THRUST
' BACKTHRUST
' TURNCCW
' TURNCW
' SHOOT

' The following Constants are available:-

' CONST PI = 3.1415926535897932384626433832795
' CONST Radians = PI / 180

' The values you may work with, are given in the function header...

' Your Health, Power, Angle, Position X, Position Y.
' The Enemy Positon X and Y.
' The X and Y of the nearest Enemy Laser (-1,-1) indicates inactive.

' There are 0 TO 15 flags available.
' You can store values in the Flag() array to use however you wish.
' The flag array is the only acceptable means of storing values
' between NanoHost cycles.

' The amount of damage your Lasers do to the Enemy is proportional
' to your current Power at the time of impact.

' You may do anything in this function, with the following exceptions:-

' You may not use SLEEP.
' You may not access the buffer of the screen.
' You may not change values outside of this function.
' You may not use INKEY$ or MULTIKEY, or any other form of user input.
' You may not use STATIC.

' You are allowed to write extra functions for your "host" AI function, and
' the function name must be prefixed with your alias.

' Be careful of constants, make sure they are prefixed too.

Here is a link to the source zip:-

http://mambazo.langfordtavern.com/zips/NanoShips_AI.zip

I wish you all the best of luck!
Last edited by mambazo on May 16, 2006 22:51, edited 2 times in total.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

Can I have multiple returnAI values at once? If I do:

Code: Select all

ReturnAI.action or = shoot
ReturnAI.action or = TurnCCW
will that work?
mambazo
Posts: 652
Joined: Jul 17, 2005 13:02
Location: Ireland
Contact:

Post by mambazo »

Nope. A single return Action only.

Hint: Don't forget about the Flag() array, you can use it to your advantage by storing data like, the state of your AI (i.e. tracking the enemy, ready to fire, dodging a laser, etc)
mambazo
Posts: 652
Joined: Jul 17, 2005 13:02
Location: Ireland
Contact:

Post by mambazo »

Here's some starter code to give an example of how to code the AI funtion...

This code will make the ship turn to face the opponent....

Code: Select all

	DIM AS DOUBLE DesiredAngle
			
	' Turn to face the enemy
	DesiredAngle = ATN(ABS(EnemyY - PosY) / ABS(EnemyX - PosX)) / (PI / 180)
	
	' NorthEast Quad
	IF EnemyX > PosX AND EnemyY < PosY THEN
		DesiredAngle = 360 - DesiredAngle
	END IF

	' NorthWest Quad
	IF EnemyX < PosX AND EnemyY < PosY THEN
		DesiredAngle = 180 + DesiredAngle
	END IF
	
	' SouthWest Quad
	IF EnemyX < PosX AND EnemyY > PosY THEN
		DesiredAngle = 180 - DesiredAngle
	END IF

	
	IF ABS(DesiredAngle - Angle) > .01 THEN
		IF DesiredAngle > Angle THEN
			IF (DesiredAngle - Angle) <= ((Angle + 360) - DesiredAngle) THEN
				ReturnAI.Action = TURNCW
			ELSE
				ReturnAI.Action = TURNCCW		
			END IF
		END IF

		IF DesiredAngle < Angle THEN
			IF (Angle - DesiredAngle) < ((DesiredAngle + 360) - Angle) THEN
				ReturnAI.Action = TURNCCW
			ELSE
				ReturnAI.Action = TURNCW		
			END IF
		END IF

	END IF
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

mambazo wrote:Hint: Don't forget about the Flag() array, you can use it to your advantage by storing data like, the state of your AI (i.e. tracking the enemy, ready to fire, dodging a laser, etc)
Oh yeah, forgot about that one :D

Honestly I don't know if I have time or energy to do this, but it sounds fun ^_~;; Maybe you can keep it open for a high length of time or store all AI in a database. Perhaps this will catch on :P
mambazo
Posts: 652
Joined: Jul 17, 2005 13:02
Location: Ireland
Contact:

Post by mambazo »

Note: there have been some bug fixes, you'll need the new NanoHost.bas
Zamaster
Posts: 1025
Joined: Jun 20, 2005 21:40
Contact:

Post by Zamaster »

Here's a stupid little one I just made, took 10 seconds so it aint much, just picks a random direction, speeds off as fast as it can in that direction and starts firing at the player, doesnt seem to wanna let me subtract 1 from Flag(4), fix that and it works fine : )

Code: Select all

DIM AS MessageAI ReturnAI
		
   '1 = Quit
   '2 = Turn to face and quit
    
   If Flag(0) = 0 Then
     Flag(0) = 1
     Flag(3) = INT(RND * 360) + 1
   Endif
   
   If Flag(3) > 0 Then
     ReturnAI.Action = TURNCW
     Flag(3) = Flag(3) - 1
     ReturnAI.Message = "Rotating..."
     goto 1
   Else
     Flag(1) = 1
     Flag(4) = 50
   endif
   
   If Flag(1) > 0 Then
     ReturnAI.Action = THRUST
     ReturnAI.Message = "Picking up speed..."
     Flag(4) = Flag(4) - 1
     If Flag(4) = 0 Then Flag(1) = -1
     goto 1
   endif
   
   if Flag(1) = -1 Then
     If Flag(2) < 1 Then 
       Flag(2) = 100
       ReturnAI.Action = SHOOT
       ReturnAI.Message = "!!FIRE!!"
       Goto 1
     Endif
     ReturnAI.Message = "Waiting to shoot while facing the player..."
     Flag(2) = Flag(2) - 1
     Goto 2
   Endif
     

 
   
   
   Goto 1

   
  
  
  
  
   2 DIM AS DOUBLE DesiredAngle 
   DesiredAngle = ATN(ABS(EnemyY - PosY) / ABS(EnemyX - PosX)) / (PI / 180) 
   IF EnemyX > PosX AND EnemyY < PosY THEN 
      DesiredAngle = 360 - DesiredAngle 
   END IF 
   IF EnemyX < PosX AND EnemyY < PosY THEN 
      DesiredAngle = 180 + DesiredAngle 
   END IF 
   IF EnemyX < PosX AND EnemyY > PosY THEN 
      DesiredAngle = 180 - DesiredAngle 
   END IF 
   IF ABS(DesiredAngle - Angle) > .01 THEN 
      IF DesiredAngle > Angle THEN 
         IF (DesiredAngle - Angle) <= ((Angle + 360) - DesiredAngle) THEN 
            ReturnAI.Action = TURNCW 
         ELSE 
            ReturnAI.Action = TURNCCW       
         END IF 
      END IF
      IF DesiredAngle < Angle THEN 
         IF (Angle - DesiredAngle) < ((DesiredAngle + 360) - Angle) THEN 
            ReturnAI.Action = TURNCCW 
         ELSE 
            ReturnAI.Action = TURNCW       
         END IF 
      END IF 
    END IF 
	
   
   
   1 RETURN ReturnAI
mambazo
Posts: 652
Joined: Jul 17, 2005 13:02
Location: Ireland
Contact:

Post by mambazo »

Cool, I've just made an AI for it too, I'll put them up against eachother and post the results of 5 matches...

http://mambazo.langfordtavern.com/zips/Mambazo_AI.bi

Good luck ;P
mambazo
Posts: 652
Joined: Jul 17, 2005 13:02
Location: Ireland
Contact:

Post by mambazo »

Oh btw...

Save your AI into its own file e.g. Zamaster_AI.bi

and rename the Template_AI function name to your own e.g. Zamaster_AI

that way I can store lots of AI files and make them compete (without tweaking everything :)

Let the battles begin!
Thrawn89
Posts: 477
Joined: Oct 08, 2005 13:12

Post by Thrawn89 »

Sounds like fun, give me a little while....

Thrawn
Zamaster
Posts: 1025
Joined: Jun 20, 2005 21:40
Contact:

Post by Zamaster »

@mambazo
does my code still say "picking up speed" even after its maxed out on its speed? If so its not working and I need to fix it... let me know
mambazo
Posts: 652
Joined: Jul 17, 2005 13:02
Location: Ireland
Contact:

Post by mambazo »

Zamaster, Flag(4) wont decrement because you keep setting it to 50. Flag(3) is always 0 after the initial rotation.
mambazo
Posts: 652
Joined: Jul 17, 2005 13:02
Location: Ireland
Contact:

Post by mambazo »

Code: Select all

   IF (Flag(3) > 0) THEN 
     ReturnAI.Action = TURNCW 
     Flag(3) = Flag(3) - 1 
     ReturnAI.Message = "Rotating..." 
     GOTO 1 
   ELSEIF (Flag(1) = 0) THEN
     Flag(3) = 1
     Flag(4) = 50 
     Flag(1) = 1 
   END IF 
that fixes it. But I'm afraid Zamaster_AI lost the battle, all 5 of them :P
Zamaster
Posts: 1025
Joined: Jun 20, 2005 21:40
Contact:

Post by Zamaster »

Im not suprised, Let me take time to actually put some thought into instead of "turn move and shoot"
mambazo
Posts: 652
Joined: Jul 17, 2005 13:02
Location: Ireland
Contact:

Post by mambazo »

Note: there are more bug fixes & tweaks to the NanoHost file.
Post Reply