How to use the Go order

Do you have a question on map editing, how use the map manager or want to make your new map public? Post here.
posted on February 7th, 2012, 5:39 am
I want to order a ship to travel to several waypoints in my mission map. Looking at the MMM documentation, a Go order seems like the ticket.  Nothing I try works though. I can get a GoSingle order to work just fine, and I suppose I could string several of them together with monitors to fire the next order when the ship arrives at a way point. But I'm hoping the Go order is easier.

Does anyone have an example of using the Go order?  Any advice appreciated.

V
posted on February 26th, 2012, 5:42 am
Here's how I solved the problem.  I created a GoMultiOrder class that handles sending GoSingle commands to a ship through several waypoints.  The class uses monitors to watch the ship and is smart enough to turn them off when the ship reaches the last waypoint or is destroyed. 

Example usage:

  local ship = Entity.add( "rom_fakairu.odf", 3, Vector(8556,0,-720) )
  local waypoints = { Vector(4543,0,2680) , Vector(4285,0,8494) }
  local theOrder = giveGoMultiOrder(ship, waypoints)

Note that the class can pause if needed:  Call the Stop method to stop managing the ship, then call the Continue method to resume moving to the next waypoint not yet reached.

Hope this helps somebody,
V
-------------------------

GoMultiOrder = {}

function GoMultiOrder:new(o)
  o = o or {}
  setmetatable(o, self)
  self.__index = self
  o.inprogess = false
  o.hasarrived = false
  o.waypointradius = 50
  return o
end

function GoMultiOrder:Start()
  if self.ship and self.waypoints then
      self.inprogress = true
      self.hasarrived = false
      self.currentwaypoint = 1
      self:Continue()
  end
end

function GoMultiOrder:Continue()
  self.inprogress = true

  -- create the monitor for the destruction of the ship
  if not self.healthmonitor then
      self:CreateHealthMonitor()
  end

  -- command the ship to move to the current way point.
  self:GiveOrder()

  -- create the monitor for arriving at current waypoint
  self:CreateWaypointMonitor()

end

function GoMultiOrder:GiveOrder()
  ship:giveOrder( GameObject.Order.GoSingle, self.waypoints[self.currentwaypoint] )
end

function GoMultiOrder:CreateHealthMonitor()
  -- Create a monitor that stops the GoMultiOrder if the ship is dead
  function isShipAlive( monitor, arg )
      return self.ship and self.ship.valid and not self.ship.destroyed
  end

  function onShipDead( monitor, arg )
      if self.healthmonitor then
        self.healthmonitor:stop()
        self.healthmonitor = nil
      end
      self:Stop()
  end

  self.healthmonitor = Monitor( "GoMultiShipMonitor", false, nil, isShipAlive, nil )
  self.healthmonitor:hook( "GoMultiOnShipDead", MonitorHook.Type.OnFalse, nil, onShipDead, nil )
end

function GoMultiOrder:CreateWaypointMonitor()

  -- Create a monitor that starts the next GoSingle order when the ship reaches the next waypoint
  function hasShipArrived( monitor, arg )
      if self.ship and self.ship.valid then
        local destvector = self.waypoints[self.currentwaypoint] - self.ship.position
        local dist = destvector.length
        return not self.ship.destroyed and dist < self.waypointradius
      end
  end

  function onShipArrived( monitor, arg )
      if self.currentwaypoint < #(self.waypoints) then
        self.currentwaypoint = self.currentwaypoint + 1
        self:Continue()
      else
        self.hasarrived = true
        self:Stop()
      end
  end

  if self.waypointmonitor ~= nil then
      self.waypointmonitor:stop()
      self.waypointmonitor = nil
  end

  --Create the monitor that will check for borg ships being alive.
  self.waypointmonitor = Monitor( "GoMultiWPMonitor", false, nil, hasShipArrived, nil )
  self.waypointmonitor:hook( "GoMultiOnShipArrived", MonitorHook.Type.OnTrue, nil, onShipArrived, nil )

end

function GoMultiOrder:Stop()
  if self.inprogress then
      if self.healthmonitor then
        self.healthmonitor:stop()
        self.healthmonitor = nil
      end
      if self.waypointmonitor ~= nil then
        self.waypointmonitor:stop()
        self.waypointmonitor = nil
      end
      self.inprogress = false
  end
end

-- Global functions for GoMulti order
function giveGoMultiOrder(ship, waypoints)
  local order = GoMultiOrder:new()
  order.ship = ship
  order.waypoints = waypoints
  order:Start()
  return order
end
posted on February 26th, 2012, 5:50 am
Impressive, nice work  :thumbsup:
Reply

Who is online

Users browsing this forum: No registered users and 4 guests