Guest User

Untitled

a guest
Jul 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. --Worker action script
  2. --[[Possible worker actions:
  3. Bring food to queen
  4. Bring food to warehouse
  5. Get food from fungus plot
  6. Place new fungus plot
  7. --]]
  8.  
  9.  
  10. --Deliver food to queen objective--
  11.  
  12. local deliverFoodQueen_Condition = Condition:new()
  13.  
  14. function deliverFoodQueen_Condition:conditionMet(ant)
  15. return ant:GetFood() > 10 and factory:GetQueen():GetFood() < 10
  16. end
  17.  
  18. local deliverFoodQueen_c = deliverFoodQueen_Condition:new()
  19.  
  20.  
  21. --reuse the getQueen_Action action
  22. --reuse the moveToTarget_Action action
  23. --reuse the deliverFoodAnt_Action action
  24.  
  25. --End deliver food to queen objective--
  26.  
  27.  
  28. --Deliver food to warehouse objective--
  29.  
  30. local deliverFoodWarehouse_Condition = Condition:new()
  31.  
  32. function deliverFoodWarehouse_Condition:conditionMet(ant)
  33. return ant:GetFood() > 10
  34. end
  35.  
  36. local deliverFoodWarehouse_c = deliverFoodWarehouse_Condition:new()
  37.  
  38. --reuse getWarehouse_Action action
  39. --reuse moveToTarget_Action action
  40.  
  41. local deliverFoodWarehouse_Action = Action:new()
  42.  
  43. function deliverFoodWarehouse_Action:running(ant)
  44. --only takes a second
  45. return false
  46. end
  47.  
  48. function deliverFoodWarehouse_Action:run(ant)
  49. local withdrawal = ant:WithdrawFood(50)
  50. factory:GetWarehouse():DepositFood(withdrawal)
  51. end
  52.  
  53. local deliverFoodWarehouse_a = deliverFoodWarehouse_Action:new()
  54. --End Deliver food to warehouse objective--
  55.  
  56.  
  57. --Get food from fungus plot--
  58.  
  59. local getFoodWorker_Condition = Condition:new()
  60.  
  61. function getFoodWorker_Condition:conditionMet(ant)
  62. return ant:GetFood() <= 10 and ant.blackboard.plot ~= -1
  63. end
  64.  
  65. local getFoodWorker_c = getFoodWorker_Condition:new()
  66.  
  67.  
  68. local getPlot_Action = Action:new()
  69.  
  70. function getPlot_Action:running(ant)
  71. --instant
  72. return false
  73. end
  74.  
  75. function getPlot_Action:run(ant)
  76. local targetPlot = factory:GetPlotByID(ant.blackboard.plot)
  77.  
  78. ant.blackboard.target_x = targetPlot:GetX()
  79. ant.blackboard.target_y = targetPlot:GetY()
  80. ant.blackboard.target_ID = ant.blackboard.plot
  81. end
  82.  
  83. local getPlot_a = getPlot_Action:new()
  84.  
  85. --reuse moveToTarget_a
  86.  
  87. local harvestFood_Action = Action:new()
  88.  
  89. function harvestFood_Action:running(ant)
  90. return false
  91. end
  92.  
  93. function harvestFood_Action:run(ant)
  94. local retrievedPlot = factory:GetPlotByID(ant.blackboard.plot)
  95. if retrievedPlot ~= nil then
  96. local withdrawal = retrievedPlot:WithdrawFood(100)
  97. ant:AddFood(withdrawal)
  98.  
  99. if retrievedPlot:GetFood() == 0 then
  100. RemovePlot(ant.blackboard.plot)
  101. ant.blackboard.plot = -1
  102. end
  103. end
  104. end
  105.  
  106. local harvestFood_a = harvestFood_Action:new()
  107.  
  108. --End Get food from fungus plot--
  109.  
  110. --Place new plot--
  111.  
  112. local placePlot_Condition = Condition:new()
  113.  
  114. function placePlot_Condition:conditionMet(ant)
  115. return ant.blackboard.plot == -1
  116. end
  117.  
  118. local placePlot_c = placePlot_Condition:new()
  119.  
  120.  
  121. local getNewPlotLoc_Action = Action:new()
  122.  
  123. function getNewPlotLoc_Action:running(ant)
  124. return false
  125. end
  126.  
  127. function getNewPlotLoc_Action:run(ant)
  128. local seed = os.clock()
  129. math.randomseed(seed)
  130. math.random()
  131. math.random()
  132.  
  133. local x = math.random(ant:GetX() - 10, ant:GetX() + 10)
  134. local y = math.random(ant:GetY() - 10, ant:GetY() + 10)
  135. ant.blackboard.target_x = x
  136. ant.blackboard.target_y = y
  137. end
  138.  
  139. local getNewPlotLoc_a = getNewPlotLoc_Action:new()
  140.  
  141. --reuse moveToTarget_a
  142.  
  143. local placePlot_Action = Action:new()
  144.  
  145. function placePlot_Action:running(ant)
  146. return false
  147. end
  148.  
  149. function placePlot_Action:run(ant)
  150. local id = CreatePlot(ant.blackboard.target_x, ant.blackboard.target_y)
  151. if id ~= -1 then
  152. ant.blackboard.plot = id
  153. end
  154. end
  155.  
  156. local placePlot_a = placePlot_Action:new()
  157.  
  158. --End Place new plot--
  159.  
  160.  
  161. --Eat--
  162.  
  163. local eat_Condition = Condition:new()
  164.  
  165. function eat_Condition:conditionMet(ant)
  166. --worker eats every 8 seconds
  167. return ant:GetFood() > 0 and ant.blackboard.delta_sum >= 8
  168. end
  169.  
  170. local eat_c = eat_Condition:new()
  171.  
  172.  
  173. local eat_Action = Action:new()
  174.  
  175. function eat_Action:running(ant)
  176. --eating is...fast
  177. return false
  178. end
  179.  
  180. function eat_Action:run(ant)
  181. ant:Eat()
  182. ant.blackboard.delta_sum = -1
  183. end
  184.  
  185. local eat_a = eat_Action:new()
  186.  
  187. --end Eat--
  188.  
  189.  
  190. --Actual worker ant behavior tree--
  191. local DeliverFoodQueen = { condition = deliverFoodQueen_c, actions = { getQueen_a, moveToTarget_a, deliverFoodAnt_a } }
  192. local DeliverFoodWarehouse = { condition = deliverFoodWarehouse_c, actions = { getWarehouse_a, moveToTarget_a,
  193. deliverFoodWarehouse_a } }
  194. local GetFoodPlot = { condition = getFoodWorker_c, actions = { getPlot_a, moveToTarget_a, harvestFood_a } }
  195. local PlacePlot = { condition = placePlot_c, actions = { getNewPlotLoc_a, moveToTarget_a, placePlot_a } }
  196. local Eat = { condition = eat_c, actions = { eat_a } }
  197.  
  198. local WorkerBT = { Eat, DeliverFoodQueen, DeliverFoodWarehouse, GetFoodPlot, PlacePlot }
  199.  
  200.  
  201.  
  202. function WorkerRun(ID, dt)
  203. local ant = factory:GetAntByID(ID)
  204.  
  205. if ant.blackboard.curAction == 0 then
  206. for key, val in pairs(WorkerBT) do
  207. if WorkerBT[key].condition:conditionMet(ant) then
  208. ant.blackboard.behavior = key
  209. ant.blackboard.curAction = 1
  210. break;
  211. end
  212. end
  213. end
  214.  
  215. --run the current action
  216. if ant.blackboard.curAction ~= 0 and WorkerBT[ant.blackboard.behavior] ~= nil then
  217. WorkerBT[ant.blackboard.behavior].actions[ant.blackboard.curAction]:run(ant, dt)
  218.  
  219. --see if it's done and update current action and actions tree for ant
  220. local status = WorkerBT[ant.blackboard.behavior].actions[ant.blackboard.curAction]:running(ant)
  221. if status == false then
  222. if WorkerBT[ant.blackboard.behavior].actions[ant.blackboard.curAction + 1] ~= nil then
  223. ant.blackboard.curAction = ant.blackboard.curAction + 1
  224. else
  225. ant.blackboard.behavior = 0
  226. ant.blackboard.curAction = 0
  227. ant.blackboard.target_x = 0
  228. ant.blackboard.target_y = 0
  229. ant.blackboard.target_ID = 0
  230. end
  231. end
  232. end
  233.  
  234. ant.blackboard.delta_sum = ant.blackboard.delta_sum + dt
  235. end
Add Comment
Please, Sign In to add comment