Petr Schreiber
17-04-2009, 07:01
Hi Frank,
nice mod. There is no need to EntitySetDirection :), it is enough to supply waypoints in reverse order.
Try the code above, F1 changes direction.
'
' Test of hovercraft AI
' Petr Schreiber, started on 09-26-2008
' Lionhearts example, 14. april 2009 :)
Uses "TBGL"
randomize timer
BEGIN CONST
' -- Scene IDs
%sScene = 1
' -- Entity IDs
%eCamera = 1
%eLight
%eLight1
%eHovercraftPrecise
%eHovercraftMedium
%eHovercraftBrutus
%eHovercraftBox
%eHovercraftLazy
%eWayPointStart
END CONST
GLOBAL FrameRate AS DOUBLE
GLOBAL DirectionSwap AS LONG = %TRUE
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_MINIMIZEBOX 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_Lighttype_Directional)
TBGL_EntitySetPos(%sScene, %eLight, 15, 12, 5)
TBGL_NewList 1
TBGL_UseLighting %FALSE
TBGL_BeginPoly %GL_TRIANGLES
TBGL_Vertex -0.5, -1.5
TBGL_Vertex 0.5, -1.5
TBGL_Vertex 0.0, 1.5
TBGL_ENDPOLY
TBGL_UseLighting %TRUE
TBGL_EndList
TBGL_NewList %eLight1
TBGL_UseLighting %FALSE
TBGL_BeginPoly %GL_TRIANGLES
TBGL_Vertex -0.5, -1.5
TBGL_Vertex 0.5, -1.5
TBGL_Vertex 0.0, 1.5
TBGL_ENDPOLY
TBGL_UseLighting %TRUE
TBGL_EndList
TBGL_NewList 2
TBGL_UseLighting %FALSE
TBGL_BeginPoly %GL_QUADS
TBGL_Vertex -0.5, -1.5
TBGL_Vertex 0.5, -1.5
TBGL_Vertex 0.5, 1.5
TBGL_Vertex -0.5, 1.5
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
DirectionSwap as double
END TYPE
dim HoverInfo as tHoverCraft
TBGL_EntityCreateDLSlot(%sScene, %eHovercraftPrecise, 0, 1)
TBGL_EntitySetColor(%sScene, %eHovercraftPrecise, 128, 255+sin(GetTickCount/1000)*128, 0)
' -- Assign data to it
with HoverInfo
.nextWayPoint = %eWayPointStart
.numWayPoint = 13
.tolerance = 1
.minSpeed = 5
.maxSpeed = 100
.steerFactor = 3
.DirectionSwap = 2
END WITH
TBGL_EntitySetUserData(%sScene, %eHovercraftPrecise, HoverInfo)
TBGL_EntityCreateDLSlot(%sScene, %eHovercraftBox, 0, 2)
TBGL_EntitySetColor(%sScene, %eHovercraftBox, 28, 255+sin(GetTickCount/1000)*128, 0)
' -- Assign data to it
with HoverInfo
.nextWayPoint = %eWayPointStart
.numWayPoint = 13
.tolerance = 2
.minSpeed = 2
.maxSpeed = 50
.steerFactor = 2
.DirectionSwap = 3
END WITH
TBGL_EntitySetUserData(%sScene, %eHovercraftBox, HoverInfo)
TBGL_EntityCreateDLSlot(%sScene, %eHovercraftMedium, 0, 1)
TBGL_EntitySetColor(%sScene, %eHovercraftMedium, 0, 255, 128+sin(GetTickCount/1000)*128)
' -- Assign data to it
with HoverInfo
.nextWayPoint = %eWayPointStart
.numWayPoint = 13
.tolerance = 3
.minSpeed = 2
.maxSpeed = 15
.steerFactor = 3
.DirectionSwap = 4
END WITH
TBGL_EntitySetUserData(%sScene, %eHovercraftMedium, HoverInfo)
TBGL_EntityCreateDLSlot(%sScene, %eHovercraftBrutus, 0, 1)
TBGL_EntitySetColor(%sScene, %eHovercraftBrutus, 50, 55+sin(GetTickCount/1000)*128, 128)
' -- Assign data to it
with HoverInfo
.nextWayPoint = %eWayPointStart
.numWayPoint = 13
.tolerance = 4
.minSpeed = 3
.maxSpeed = 6
.steerFactor = 3
.DirectionSwap = 2
END WITH
TBGL_EntitySetUserData(%sScene, %eHovercraftBrutus, HoverInfo)
TBGL_EntityCreateDLSlot(%sScene, %eHovercraftLazy, 0, 1)
TBGL_EntitySetColor(%sScene, %eHovercraftLazy, 255, 0, 128)
' -- Assign data to it
with HoverInfo
.nextWayPoint = %eWayPointStart
.numWayPoint = 13
.tolerance = 5
.minSpeed = 1
.maxSpeed = 40
.steerFactor = 2
.DirectionSwap = 1
END WITH
TBGL_EntitySetUserData(%sScene, %eHovercraftLazy, HoverInfo)
TBGL_EntityCreateDLSlot(%sScene, %eLight1, 0, 2)
TBGL_EntitySetColor(%sScene, %eLight1, 255+sin(GetTickCount/1000)*127,10+sin(GetTickCount/1000+1)*127,140+sin(GetTickCount/1000+2)*127)
' -- Assign data to it
with HoverInfo
.nextWayPoint = %eWayPointStart
.numWayPoint = 13
.tolerance = 4
.minSpeed = 2
.maxSpeed = 80
.steerFactor = 2
.DirectionSwap = 2
END WITH
TBGL_EntitySetUserData(%sScene, %eLight1, HoverInfo)
local i as long
for i = %eWayPointStart to %eWayPointStart + 11
TBGL_EntityCreateBox(%sScene, i, 0, 0.25, 0.45, 0.25)
TBGL_EntitySetColor(%sScene, i, rnd(1, 255), 28, rnd(1,80))
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, 8, 6, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+5, 5, 11, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+6, -6, 8, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+7, -10, 4, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+8, -6, -10, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+9, -2, -7, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+10, 5, -5, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+11, 4, -8, 0)
TBGL_EntitySetPos(%sScene, %eWayPointStart+12, 2, -5, 0)
' -- Resets status of all keys
TBGL_ResetKeyState()
DIM Sheep AS LONG
' -- Main loop
While TBGL_IsWindow(hWnd)
FrameRate = TBGL_GetFrameRate
Sheep += 1
IF Sheep > 200 THEN
Sheep = 0
TBGL_SetWindowTitle(hWnd, "Hello lovely AI, use F1 to change direction, FPS: " + FrameRate)
END IF
TBGL_ClearFrame
HoverCraft_AI(%eHovercraftPrecise)
HoverCraft_AI(%eHovercraftMedium)
HoverCraft_AI(%eHovercraftBrutus)
HoverCraft_AI(%eHovercraftBox)
HoverCraft_AI(%eHovercraftLazy)
HoverCraft_AI(%eLight1)
TBGL_SceneRender(%sScene)
TBGL_DrawFrame
' -- ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While
If TBGL_GetWindowKeyState(hWnd, %VK_Space) Then Exit While
If TBGL_GetWindowKeyOnce(hWnd, %VK_F1) Then DirectionSwap = not DirectionSwap
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_EntityTurn(%sScene, entity, 0, 0, 10*angle/FrameRate )
TBGL_EntityTurn(%sScene, entity, 0, 0, min(hoverData.DirectionSwap*angle)/FrameRate )
TBGL_EntityPush(%sScene, entity, 0, min(hoverData.minSpeed+distance, hoverData.maxSpeed)/FrameRate, 0)
' -- Check for next waypoints
if DirectionSwap then
IF distance < hoverData.tolerance then
if hoverData.nextWayPoint < %eWayPointStart+hoverData.numWayPoint then
incr hoverData.nextWayPoint
else
hoverData.nextWayPoint = %eWayPointStart
END IF
END IF
else
IF distance < hoverData.tolerance then
if hoverData.nextWayPoint > %eWayPointStart then
decr hoverData.nextWayPoint
else
hoverData.nextWayPoint = %eWayPointStart+hoverData.numWayPoint
END IF
END IF
end if
END SUB
Petr
Lionheart008
17-04-2009, 18:41
hi petr, hi all :)
uhps... :agree: thank you for the waypoints in reverse order ! not so bad ;) , this idea I haven't had at night... was too tired... damned... good idea more for "F1" ...
I have updated and improved the script again, I like it... cause it's a basically example for some kind of game ai, isn't it ??? One new door opens for me :D
ok, have a nice day, I am freezing, raining... no sunshine over my head, springtime gone ?
ciao, Lionheart with increasing skills about rnd(1,10) per cent :yes:
qt: somebody else like this kind of tbgl example too ??? anybody out there having some new ideas to start a little new hunter/prey game??? ;)