Author Topic: AI Scripting Help  (Read 2514 times)

Offline X_TheUnknown_X

  • Posts: 325
  • Cookies: 328
  • (Semi) Retired
AI Scripting Help
« on: May 10, 2010, 03:51:24 PM »
For my SB12 New Holland system, I've been adding ships and giving them some different AIs to make it more active. So far, I've been using stock AIs and scripts from KM1.0. Which leads to my first question (not really scripting, but...): are the AI scripts in the 'Earth' system from KM1.0 free to use? (I believe they are from the Sol multisystem mod, but no permissions are specified).

The main problem however, was when I tried to create a custom AI, based upon RouteAI script. My custom changes were to make the specified ship fly to set waypoints instead of planets. The script works up to a point, the ship starts at Waypoint 1 will fly at Waypoint 2, but when it reaches waypoint 2 it flies around it constantly and won't set a course back to the start point. I'm out of ideas here, I'm wondering is anyone else can assist. (I've attached a copy of the .py file beacuse it will almost certainly be necessary).

Offline Lurok91

  • Posts: 1309
  • Cookies: 2062
  • SPMod Developer (Retired)
    • Lurok91 Mods
Re: AI Scripting Help
« Reply #1 on: May 10, 2010, 04:02:48 PM »
Not sure how RouteWaypoint works, but when I want to mod or create a system with waypoints/stations/etc,  I use the pys in scripts/Systems folder (where the Starbase 12 one lives). 

Offline X_TheUnknown_X

  • Posts: 325
  • Cookies: 328
  • (Semi) Retired
Re: AI Scripting Help
« Reply #2 on: May 11, 2010, 12:20:05 PM »
The system file in scripts/Systems/... has all been set up and functioning, all the other ships' AI's are working. The problem is with my own custom AI, which I called RouteWaypointAI, which is making the ship fly incorrectly.
Also, I think I found the readme file for Dasher42's solar system which - I think - contains the AIs I've used in my mod. The permissions say I need to credit the creators, so unless I find something to the contrary I'll use the scripts with credits.

Offline Mario

  • Senior Software Developer
  • Administrator
  • Posts: 2200
  • Cookies: 1707
  • Life is life
Re: AI Scripting Help
« Reply #3 on: May 11, 2010, 05:22:51 PM »
System file

Code: [Select]
# initial waypoint
        kThis = App.Waypoint_Create("start way", sSetName, None)
        kThis.SetStatic(1)
        kThis.SetNavPoint(0)
        kThis.SetTranslateXYZ(x, y, z)
        kForward = App.TGPoint3()
        kForward.SetXYZ(0, 0, 0)
        kUp = App.TGPoint3()
        kUp.SetXYZ(0, 0, 0)
        kThis.AlignToVectors(kForward, kUp)
        kThis.SetSpeed(25.000000)
        kThis.Update(0)
        kThis = None

# 1st stop
        kThis = App.Waypoint_Create("wp1", sSetName, None)
        kThis.SetStatic(0)
        kThis.SetNavPoint(0)
        kThis.SetTranslateXYZ(x, y, z)
        kForward = App.TGPoint3()
        kForward.SetXYZ(0, 0 ,0)
        kUp = App.TGPoint3()
        kUp.SetXYZ(0, 0, 0)
        kThis.AlignToVectors(kForward, kUp)
        kThis.SetSpeed(25.000000)
        kThis.Update(0)
        kThis = None

# 2nd stop
        kThis = App.Waypoint_Create("wp2", sSetName, None)
        kThis.SetStatic(0)
        kThis.SetNavPoint(0)
        kThis.SetTranslateXYZ(x, y, z)
        kForward = App.TGPoint3()
        kForward.SetXYZ(0, 0 ,0)
        kUp = App.TGPoint3()
        kUp.SetXYZ(0, 0, 0)
        kThis.AlignToVectors(kForward, kUp)
        kThis.SetSpeed(25.000000)
        kThis.Update(0)
        kThis = None

# and so on

# Bind waypoints
        kThis = App.Waypoint_Cast( App.PlacementObject_GetObjectBySetName(sSetName, "start way") )
        kPrevious = App.Waypoint_Cast( App.PlacementObject_GetObjectBySetName(sSetName, "wp1") )
        kThis.InsertAfterObj( kPrevious )
        kThis = kPrevious = None

        kThis = App.Waypoint_Cast( App.PlacementObject_GetObjectBySetName(sSetName, "wp1") )
        kPrevious = App.Waypoint_Cast( App.PlacementObject_GetObjectBySetName(sSetName, "wp2") )
        kThis.InsertAfterObj( kPrevious )
        kThis = kPrevious = None

# and so on


AI

Code: [Select]
import App

def CreateAI(pShip):
#########################################
# Creating PlainAI Cruise at (288, 109)
pCruise = App.PlainAI_Create(pShip, "Cruise")
pCruise.SetScriptModule("FollowWaypoints")
pCruise.SetInterruptable(1)
pScript = pCruise.GetScriptInstance()
pScript.SetTargetWaypointName("start way")
# Done creating PlainAI Cruise
#########################################
#########################################
# Creating SequenceAI Sequence at (294, 258)
pSequence = App.SequenceAI_Create(pShip, "Sequence")
pSequence.SetInterruptable(1)
pSequence.SetLoopCount(-1)
pSequence.SetResetIfInterrupted(1)
pSequence.SetDoubleCheckAllDone(1)
pSequence.SetSkipDormant(1)
# SeqBlock is at (320, 192)
pSequence.AddAI(pCruise)
# Done creating SequenceAI Sequence
#########################################
return pSequence
Acta, non verba.
aka USS Sovereign

Offline X_TheUnknown_X

  • Posts: 325
  • Cookies: 328
  • (Semi) Retired
Re: AI Scripting Help
« Reply #4 on: May 12, 2010, 01:34:53 PM »
Problem solved! The cause - I'm embarrased to say - was a severe oversight on my part  :( . I was confused when your suggestion, Sovereign, didn't work either - but when pasteing the waypoint binding scripts into the Set script - it seems that I'd put the return waypoint just a few km from the Starbase's dome  :doh: . Because I wanted to keep the AvoidObstacles AI intact to stop the freighter ramming the other ships, whenever it approched the waypoint the AI couldn't decide between reaching the waypoint and crashing into SB12.

I guess this is a lesson to double-check the scripts, and for me to do my maths correctly.  :roll

Offline JimmyB76

  • Posts: 6423
  • Cookies: 421
Re: AI Scripting Help
« Reply #5 on: May 12, 2010, 04:09:54 PM »
I'm embarrased to say - was a severe oversight on my part  :(
feel no embarrassment - thats how you learn and become better :)

Offline Mario

  • Senior Software Developer
  • Administrator
  • Posts: 2200
  • Cookies: 1707
  • Life is life
Re: AI Scripting Help
« Reply #6 on: May 13, 2010, 09:53:32 AM »
The sample code snippet I posted works however you do need to adapt it for your own purposes.
Acta, non verba.
aka USS Sovereign