Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.73 KB | None | 0 0
  1. /// AI module for managing your economy. Building drones, vespen geysers, etc.
  2. let private getEconomyAI (mediator : GameMediator) =
  3.     async {
  4.  
  5.         while true do
  6.  
  7.             let currentState = mediator.CurrentGameState
  8.  
  9.             // Build workers with all available cash
  10.             if currentState.SupplyUsed < currentState.SupplyTotal &&
  11.                currentState.Minerals >= 50 &&
  12.                currentState.CanProduce.[ int (getWorkerType(g_GameMetadata.PlayerRace)) ] then
  13.                
  14.                 // BUG: Builds a worker at the first command center we find, this might not be the best one.
  15.                 // Ideally we'd have some higher level notion of a 'base' with a 'status' such as 'active' or 'out of resources'.
  16.                 let cmdCenter =
  17.                     currentState.Units
  18.                     |> Seq.filter (fun unit -> unit.Player = g_GameMetadata.PlayerID)
  19.                     |> Seq.tryFind (fun unit -> isCommandCenter (enum<UnitID> unit.TypeID))
  20.                    
  21.                 if Option.isSome cmdCenter then
  22.                     let cmdCenterID = cmdCenter |> Option.get |> (fun unit -> unit.ID)
  23.                     let cmd = TrainUnit(cmdCenterID, int (getWorkerType(g_GameMetadata.PlayerRace)))
  24.                     mediator.SendCommand(cmd)
  25.  
  26.             // Send idle workers to closest mineral patch
  27.             for scunit in currentState.Units do
  28.                 // If you own the unit, it's a worker, and it's sitting there then
  29.                 // send it to the closest mineral patch.
  30.                 if scunit.Player = mediator.GameMetadata.PlayerID &&
  31.                    isWorker scunit &&
  32.                    scunit.OrderID = int (UnitOrder.PlayerGuard) then
  33.                        
  34.                     let orderID = scunit.OrderID
  35.  
  36.                     let scunitLoc = { X = scunit.XPos; Y = scunit.YPos }
  37.  
  38.                     let mineralPatches =
  39.                         currentState.Units
  40.                         |> Seq.filter(fun unit -> unit.TypeID = int UnitID.MineralField)
  41.                         |> Seq.map(fun minPatch -> let patchLoc = { X = minPatch.XPos; Y = minPatch.YPos }
  42.                                                    scunitLoc.FlyingDistanceTo(patchLoc), minPatch)
  43.                         |> Seq.sortBy(fun (dist, minPatch) -> dist)
  44.                        
  45.                     if mineralPatches <> Seq.empty then
  46.                         let targetMinPatch = mineralPatches |> Seq.head |> snd
  47.                         let cmd = RightClickUnit(scunit.ID, targetMinPatch.ID)
  48.                         mediator.SendCommand(cmd)
  49.  
  50.             // Sleep and check again later. If this agent gets canceled it will be right here.
  51.             do! Async.Sleep(100)
  52.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement