Jump to content

PravusJSB

ED Closed Beta Testers Team
  • Posts

    324
  • Joined

  • Last visited

Posts posted by PravusJSB

  1. If you sleep the Lua mission thread then you'll pause the simulation, but for future thought when I do need to make Lua sleep I have a function that solves some complex math in a for loop and i've roughly got it to take a float to determine how long the loop should run (keep solving) and thus pause the thread, in seconds.

  2. There's little control you can do on AI landing shenanigans and you'll maybe find that they act like cats if you try to to get specific things to happen lols. Getting them to the location at a specific time should be doable though.

    • Like 1
  3. Yea, I mean it's not all relevant to your needs but the way I build my task and the expectations you should be able to extract, if not happy to help further. My code is normally for me only so some of the older stuff I'd yet to figure out the importance of naming conventions and readability so its a bit tougher than it should be. The PointTask func you should be able to copy paste, just replace the function that names it with your own, and theres some duplication because of overloads with the 'ASTAR' that you can stop reading from. So that last task with the looped waypoints is important and the rest you should be OK with. Does not need to be in a coroutine either (sorry if thats obvious), I just copied my live implementation straight out of my AI so its just how its setup but not needed here for you.

  4. 1 hour ago, Habu_69 said:

    Would appreciate your guidance here. I am trying to get P-51 wingman to rearm/refuel. I set flight path to WP with task RearmRefuel, 5 min on ground, then additional waypoints. I land, taxi to apron. --2 lands, parks, shuts down. He does not exit airplane. I rearm/refuel, takeoff. But after 15 min. --2 still sitting, engine off, not rearmed.

    Hopefully you can glean from this the right struct to pass, its a constructor for all tasks so i dont remember the specifics of how i wrote... it wont be copy/pasteable but you should be able to just see the right struct, thats half of the problem with tasks and getting the desired results. EDIT: from the if obj.trg.pers is the rearm reful, but it looks like i add in the WPs for going back up again that might be what makes the magic happen. Yea im giving them an infinite patrol task after they get back up to give me time to pick them back up again and re-task them.... and work out the glide path for them so they go straight in with no extra delays.

      -- internal function -- air waypoint generate
      local function PointTask(pos,type,_speed,action,tasking,alt,spdlock,radio,wrap,push)
        local point = {['x'] = pos.x, ['y'] = pos.y or alt, ['z'] = pos.z}
        local speed = _speed
        if not action then action = "Turning Point" end
        if not type then type = "Turning Point" end
        local task = wrap or {
            ["id"] = "ComboTask",
            ["params"] =
            {
                ["tasks"] = tasking or {},
            },
        }
        return
        {
            ["properties"] =
            {
                ["addopt"] =
                {
                }, -- end of ["addopt"]
            }, -- end of ["properties"]
            ["type"] = type,
            ["name"] = new_point_name(),
            ["x"] = point.x,
            ["y"] = point.z,
            ["alt"] = alt or (point.y + Disposition.getPointHeight(pos)),
            ["alt_type"] = radio or "RADIO",
            ["speed"] = speed,
            ["action"] = action,
            ["task"] = task,
            ["ETA"] = push or 0,
            ["ETA_locked"] = push == true,
            ["speed_locked"] = spdlock or false,
            ["formation_template"] = "",
        }
      end;
      local function land_function(_group)
        local group = _group
        local obj = optDB.aiV4[group]
        -- our running thread, stopping when the object is dead or we flag it as dead
        while (obj.alive == true) do
          local start = os.clock()
          if not obj:isAlive() then
            obj.alive = false
          else
            local res,fun = pcall(function()
              if obj.flag == 0 then -- setup our objects tasking
                local vec = obj:vec3()
                local pnt
                if obj:inAir() and vec and vec.y and vec.y > 800 and obj.trg.perField then
                  obj:clearTask()
                  local base = Airbase.getByName(obj.trg.perField)
                  local ret
                  if not base then
                    ret, base = pcall(function() _log("JSB V4 AI: Error in tasking land_func, had to find a base;\n%s",jsb.tbl2(obj)) return aiMed.fun.getJMR().getBaseClose(vec, 1) end)
                  end
                  if base then
                    local rwObj = base:getRunways()[1]
                    pnt = rwObj.position
                    local rw = rwObj.course * -1
                    local dir = rw + (math.pi)
                    local glide = { ['x'] = pnt.x + 2200 * math.cos( dir ), ['y'] = 0, ['z'] = pnt.z + 2200 * math.sin( dir ) }
                    optDB.aiV4[group].task = {
                      ['id'] = 'Mission', -- Mission
                      ['params'] = {
                        ['airborne'] = true,
                        ['route'] = {
                          ['points'] = {
                            PointTask(vec, nil, mToKnots(0.75), 'Fly Over Point', nil, 10500/3.35),
                            -- PointTask(shiftPoint(pnt,2000),nil,mToKnots(0.85),nil,nil,obj.trg.alt or (35000/3.3)),
                          },
                        },
                      },
                    }
                    if obj.trg.pers then
                      -- TODO needs testing
                      local patrol = {
                      {
                          ["number"] = 1,
                          ["auto"] = false,
                          ["id"] = "WrappedAction",
                          ["enabled"] = true,
                          ["params"] =
                          {
                            ["action"] =
                            {
                              ["id"] = "SwitchWaypoint",
                              ["params"] =
                              {
                                  ["goToWaypointIndex"] = #obj.task.params.route.points+1,
                                  ["fromWaypointIndex"] = #obj.task.params.route.points+2,
                              },
                            },
                          },
                        },
                      }
                      obj.task.params.route.points[#obj.task.params.route.points+1] = PointTask(glide,nil,mToKnots(0.45),nil,nil,450) -- landing phase
                      obj.task.params.route.points[#obj.task.params.route.points+1] = PointTask(pnt,'LandingReFuAr',130,'LandingReFuAr',nil,100)
                      obj.task.params.route.points[#obj.task.params.route.points]["airdromeId"] = base:getID()
                      obj.task.params.route.points[#obj.task.params.route.points+1] = PointTask(pnt,nil,mToKnots(0.85),'Fly Over Point',nil,25500/3.35)
                      obj.task.params.route.points[#obj.task.params.route.points+1] = PointTask(pnt,nil,mToKnots(0.85),'Fly Over Point',patrol,25500/3.35)
                      -- obj:unitTask(true)
                      -- optDB.aiV4[group].task = {
                      --   ['id'] = 'EngageTargets',
                      --   ['params'] = {
                      --     ['maxDistEnabled'] = true,
                      --     ['maxDist']        = 3000,
                      --     ['targetTypes']    = { obj.trg.target_types or "All" },
                      --     ['priority']       = 1,
                      --     ['noTargetTypes'] = obj.trg.trg_not,
                      --   },
                      -- }
                      obj:unitTask()
                    elseif obj.trg.points and #obj.trg.points > 0 then -- new astar code
                      for i = 1, #obj.trg.points do
                        obj.task.params.route.points[#obj.task.params.route.points+1] = PointTask({x = obj.trg.points[i][1], y = 0, z = obj.trg.points[i][2]}, nil, mToKnots(0.85), nil, nil, obj.trg.alt or obj.trg.points[i][3] or (42000/3.3)) -- specific route
                      end
                      obj.task.params.route.points[#obj.task.params.route.points+1] = PointTask(glide,nil,mToKnots(0.45),nil,nil,450) -- landing phase
                      obj.task.params.route.points[#obj.task.params.route.points+1] = PointTask(pnt,'LandingReFuAr',130,'LandingReFuAr',nil,100)
                      obj.task.params.route.points[#obj.task.params.route.points]["airdromeId"] = base:getID()
                      obj.task.params.route.points[#obj.task.params.route.points - 2].alt = (10000/3.3)
                      obj.task.params.route.points[#obj.task.params.route.points - 3].alt = (14000/3.3)
                      -- obj.task.params.route.points[#obj.task.params.route.points - 4].alt = (17000/3.3)
                      -- obj.task.params.route.points[#obj.task.params.route.points - 5].alt = (25000/3.3)
                      -- obj.task.params.route.points[#obj.task.params.route.points - 6].alt = (30000/3.3)
                      obj:unitTask()
                      _log("Pushed new ASTAR route %s number points %d", _group, #obj.task.params.route.points)
                    else
                      obj.task.params.route.points[#obj.task.params.route.points+1] = PointTask(glide,nil,mToKnots(0.45),nil,nil,450) -- landing phase
                      obj.task.params.route.points[#obj.task.params.route.points+1] = PointTask(pnt,'LandingReFuAr',130,'LandingReFuAr',nil,100)
                      obj.task.params.route.points[#obj.task.params.route.points]["airdromeId"] = base:getID()
                      obj:unitTask()
                    end
                    obj.flag = 1
                    -- _log("Has moved %s to flag 1",obj.name)
                  else
                    _log("JSB V4 AI: Error in tasking land_func, data;\n%s",jsb.tbl2(obj))
                    obj.flag = 2
                  end
                elseif not obj.trg.perField or not vec or not vec.y then
                  obj.flag = 2
                end
              elseif obj.flag == 1 then
                -- TDOO check for threats on way and route around them?
                obj.flag = 2
              elseif obj.flag == 2 then
                -- TDOO check for threats on way and route around them?
                obj.alive = false
              end
            end)
            if not res then _log("ERROR in ai %s",tostring(fun)) obj.alive = false end
          end
          local fin = os.clock()-start
          if fin > 0.014 then _log("Land cor took %f",fin) end
          coroutine.yield()
        end
      end

     

×
×
  • Create New...