christianssen
25-02-2010, 10:16
hello all.
the first time I have taken "console" modal part. I've tested this script just for fun. it's a random behaviour test for values, here: for enemies. I hope the example is correct. if not please say possible improvements to me. thanks.
'-- fun test for enemies and random values by denis
Uses "console"
Dim i, counter As Integer
Dim values As Double
Dim p,f,v,g,h As Long
counter = 0
Randomize Timer
Console_WriteLine("------ random test for values with enemy behaviour ------ ")
Console_WriteLine
v += 1
p = Console_SetTextAttribute(%CONSOLE_FOREGROUND_RED)
Sleep 0
f = Console_GetTextAttribute()
Console_WriteLine("enemy number: " + Format$(i, "00") + Format$(v, "00") + " This is red and the last error was: " + p + " attribute: " + IIf$(f=%CONSOLE_FOREGROUND_RED, "OK", "NOTOK"))
For i = 1 To 50
values = Rnd
If values >= 0.25 Then 'value between 0.25 and 1.0
Console_WriteLine "Enemy is still standing"
Else 'value between 0 and 0.25
Console_WriteLine "Enemy is knocking down"
counter += 1
v += 1
g = Console_SetTextAttribute(%CONSOLE_FOREGROUND_GREEN)
Sleep 0
h = Console_GetTextAttribute()
Console_WriteLine("enemy number: " + Format$(i, "00") + Format$(v, "00") + " This is green and the last error was: " + g + " attribute: " + IIf$(h=%CONSOLE_FOREGROUND_GREEN, "OK", "NOTOK"))
End If
Next i
i -= 1
Console_WriteLine "----result----"
Console_WriteLine
Console_WriteLine "in " & counter & " from " & i & " cases enemy is knocking down (quote: " & Int(counter*100/i) & "%)."
Waitkey
denis
Lionheart008
25-02-2010, 19:07
nice idea denis :)
perhaps you can use this code part for game development at a later moment, include it or make similar things for "AI" handling. good idea. I like it. improvement could be to change colour every time if enemy is knocking down (red) or still standing (blue or green), that's may be perfect.
servus, bye, frank
ps: dear denis, although it's more fitted to advanced users (in my eyes) you may have a look at this very interesting code example "TrackAI Test", like petr has said at another place at the board (I have not read all posts the last few weeks! and overview at the moment, sorry)
"ai" example with primitives:
'
' Test of hovercraft AI
' Petr Schreiber, started on 09-26-2008
'
Uses "TBGL"
BEGIN CONST
' -- Scene IDs
%sScene = 1
' -- Entity IDs
%eCamera = 1
%eLight
%eHovercraftPrecise
%eHovercraftMedium
%eHovercraftLazy
%eWayPointStart
END CONST
GLOBAL FrameRate AS DOUBLE
FUNCTION TBMAIN()
LOCAL hWnd As DWORD
' -- Create and show window
hWnd = TBGL_CreateWindowEx("Test for hovercraft elemental AI", 800, 600, 32, %TBGL_WS_WINDOWED or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
' -- Create scene
TBGL_SceneCreate(%sScene)
' -- Create basic entities
' -- Create camera to look from 15, 15, 15 to 0, 0, 0
TBGL_EntityCreateCamera(%sScene, %eCamera)
TBGL_EntitySetPos(%sScene, %eCamera, 0, 0, 30)
TBGL_EntitySetTargetPos(%sScene, %eCamera, 0, 0, 0)
' -- Create point light
TBGL_EntityCreateLight(%sScene, %eLight)
TBGL_EntitySetPos(%sScene, %eLight, 15, 10, 5)
TBGL_NewList 1
TBGL_UseLighting %FALSE
TBGL_BeginPoly %GL_TRIANGLES
TBGL_Vertex -0.5, -1.0
TBGL_Vertex 0.5, -1.0
TBGL_Vertex 0.0, 1.0
TBGL_ENDPOLY
TBGL_UseLighting %TRUE
TBGL_EndList
' -- Create something to look at
TYPE tHoverCraft
nextWayPoint AS LONG
numWayPoint AS LONG
tolerance AS double ' -- Tolerance to reach waypoint
minSpeed AS double
maxSpeed AS double
steerFactor AS double
END TYPE
dim HoverInfo as tHoverCraft
TBGL_EntityCreateDLSlot(%sScene, %eHovercraftPrecise, 0, 1)
TBGL_EntitySetColor(%sScene, %eHovercraftPrecise, 128, 255, 0)
' -- Assign data to it
with HoverInfo
.nextWayPoint = %eWayPointStart
.numWayPoint = 10
.tolerance = 1
.minSpeed = 5
.maxSpeed = 100
.steerFactor = 3
END WITH
TBGL_EntitySetUserData(%sScene, %eHovercraftPrecise, HoverInfo)
TBGL_EntityCreateDLSlot(%sScene, %eHovercraftMedium, 0, 1)
TBGL_EntitySetColor(%sScene, %eHovercraftMedium, 0, 255, 128)
' -- Assign data to it
with HoverInfo
.nextWayPoint = %eWayPointStart
.numWayPoint = 10
.tolerance = 2
.minSpeed = 2
.maxSpeed = 5
.steerFactor = 3
END WITH
TBGL_EntitySetUserData(%sScene, %eHovercraftMedium, HoverInfo)
TBGL_EntityCreateDLSlot(%sScene, %eHovercraftLazy, 0, 1)
TBGL_EntitySetColor(%sScene, %eHovercraftLazy, 255, 0, 128)
' -- Assign data to it
with HoverInfo
.nextWayPoint = %eWayPointStart
.numWayPoint = 10
.tolerance = 4
.minSpeed = 1
.maxSpeed = 3
.steerFactor = 2
END WITH
TBGL_EntitySetUserData(%sScene, %eHovercraftLazy, HoverInfo)
local i as long
for i = %eWayPointStart to %eWayPointStart + 9
TBGL_EntityCreateBox(%sScene, i, 0, 0.25, 0.25, 0.25)
TBGL_EntitySetColor(%sScene, i, 255, 128, 0)
next
' -- Hardcoded waypoints
TBGL_EntitySetPos(%sScene, %eWayPointStart+0, 0, 2, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+1, 4, 3, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+2, 10, 5, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+3, 10, 10, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+4, 5, 11, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+5, -6, 8, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+6, -10, 3, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+7, -6, -10, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+8, -2, -7, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+9, 5, -5, 0)
' -- Resets status of all keys
TBGL_ResetKeyState()
' -- Main loop
While TBGL_IsWindow(hWnd)
FrameRate = TBGL_GetFrameRate
TBGL_SetWindowTitle(hWnd, FrameRate)
TBGL_ClearFrame
HoverCraft_AI(%eHovercraftPrecise)
HoverCraft_AI(%eHovercraftMedium)
HoverCraft_AI(%eHovercraftLazy)
TBGL_SceneRender(%sScene)
TBGL_DrawFrame
' -- ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While
Wend
TBGL_DestroyWindow
END FUNCTION
Sub HoverCraft_AI( entity AS LONG )
local hoverData AS tHoverCraft ptr
' -- Retrieve data from entity
hoverData = TBGL_EntityGetUserDataPointer(%sScene, entity)
' -- Get angle and distance to next waypoint
LOCAL angle AS DOUBLE = TBGL_EntityGetAngleXY(%sScene, entity, hoverData.nextWayPoint, %TBGL_Y) ' -- The front part is on Y axis
local distance as double = TBGL_EntityGetDistance(%sScene, entity, hoverData.nextWayPoint)
' -- Move according to parametrization
TBGL_EntityTurn(%sScene, entity, 0, 0, (hoverData.steerFactor*angle)/FrameRate)
TBGL_EntityPush(%sScene, entity, 0, min(hoverData.minSpeed+distance, hoverData.maxSpeed)/FrameRate, 0)
' -- Check for next waypoints
IF distance < hoverData.tolerance then
if hoverData.nextWayPoint < %eWayPointStart+hoverData.numWayPoint then
incr hoverData.nextWayPoint
else
hoverData.nextWayPoint = %eWayPointStart
END IF
END IF
END SUB
and you can see more about these TrackAI modifications from my side here:
http://community.thinbasic.com/index.php?topic=2623.0