2d space game

Miscellaneous Forums/Blitz Showcase/2d space game

No, the AI I developed was quite simple. I chose a state based method allowing the NPC to sort out its first task and when finished move onto the next. Each one of these tasks can be programmed separately and allows for quite complex assortments.

1. Target a station
2. Go to within 120 units of target
3. Dock with station
4. wait for 200 seconds
5. undock
6. choose random system
7. go to system

--------
1. choose random trader ship
2. go to within 60 units of trader ship
3. attack until target = dead
4. choose more tasks


Method update()
	Local i:Task=Task(currentTasks.first())
	If i
		Select i.mode
			Case "choose random station"
				target = randomStation()
				taskComplete()
				
			Case "go to "
				If distanceToTarget > 50
					flyTo(target.x,target.y)
				Else
					taskComplete()
				EndIf
				
			Case "undock"
				undockShip()
				taskComplete()
				
			Case "wait"
				If MilliSecs()-waitStart > waitTime
					taskComplete()
				EndIf
		End Select	
	EndIf
End Method


As for the programming of randomly created objects, it's quite easy really. The SeedRnd function is your friend. Here's an example: Use keys 1-5 to create a "random" planet...


Rem

	Consider worms. The levels were procedurally produced from a number. This means
	you could create the same level again by just remembering the number.
	It also means that you don't have to save each level.
	
	RndSeed is a function that tells blitzmax to produce the same result for random functions
	See example below...
	Check the Output

EndRem

Graphics 640,480,0


While KeyHit(KEY_ESCAPE)=0
	Cls
	
	
	txt.DrawTxt(0,0)
	
	If KeyHit(KEY_1) createAPlanet(1)
	If KeyHit(KEY_2) createAPlanet(2)
	If KeyHit(KEY_3) createAPlanet(3)
	If KeyHit(KEY_4) createAPlanet(4)
	If KeyHit(KEY_5) createAPlanet(MilliSecs())
	
	DrawText "1,2,3,4,5",0,GraphicsHeight()-25
	
	Flip
	Delay 1
Wend
End


Function createAPlanet(seed)
	SeedRnd(seed) ' any 'rnd' or 'rand' call after this will produce the same result.

	Local atmosphereType$[]=["gas","harsh","lush","barren","jungle","tropical","temperate","desert"]
	Local sunType$[]=["white","red","yellow","blue","dwarf","giant","black hole"]

	txt.clear()
	txt.AddTxt("Generating planet from seed "+seed,255,0,0)
	txt.AddTxt("Atmosphere: "+atmosphereType[Rand(0,atmosphereType.length-1)])
	txt.AddTxt("Population: "+Rnd(0,4.5)+" Billion")
	txt.AddTxt("Sun Type: "+sunType[Rand(0,sunType.length-1)])
	txt.AddTxt("")
	

	SeedRnd(MilliSecs()) ' re-randomise so future rnd and rand will be that, random.
End Function




Type Txt
	Global txtList:TList = CreateList()

	Field content$
	Field red%, green%, blue%
	
	Function clear()
		txt.txtList.clear()
	End Function
	
	Function AddTxt(content$, red% = 255, green% = 255, blue% = 255, limit = 10)
		Local t:Txt = New Txt
		t.content = content
		t.red = red
		t.green = green
		t.blue = blue
		txtList.AddLast t
		DebugLog content
		If txt.txtList.Count() > limit Then txt.txtList.RemoveFirst()
	End Function	
	
	Function DrawTxt(x% = 20, y% = 20, yStep% = 20)
		For Local t:Txt = EachIn txt.txtList
			SetColor t.red, t.green, t.blue
			DrawText t.content,x,y
			y :+ yStep
		Next 
		SetColor 255,255,255
	End Function
	
End Type