mission_ember_combat_ai_test.lua 1.9 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package.path = "scripts/?.lua;" .. package.path
  2. local AI = require("mission_ember.combat_ai")
  3. local vars, calls = {}, {}
  4. local state = {variable = function(_, key) return vars[key] end}
  5. local objective = {}
  6. local squad = {assign_combat_objective = function(_, args) calls[#calls+1] = args end}
  7. local context = {
  8. set_variable = function(_,key,value) vars[key]=value end,
  9. slot = function(_,name) if name == "objective" then return objective else assert(name=="squad"); return squad end end,
  10. }
  11. local function update(revision, costs)
  12. AI.update(context,state,"ai","squad","objective",3,{objective_revision=revision, task_costs=costs})
  13. end
  14. AI.assign(context,"squad","objective",-1)
  15. vars.ai=0
  16. update(0,{100,20,30}); assert(#calls==1) -- stale computation
  17. update(1,{2040,2040,2040,0}); assert(#calls==1) -- unreachable; ignore unauthored lane
  18. update(1,{100,20,30}); assert(#calls==2 and calls[2].task_group==1 and calls[2].objective==objective)
  19. update(1,{20,20,30}); assert(#calls==2) -- stable tie
  20. update(1,{10,20,30}); assert(#calls==3 and calls[3].task_group==0)
  21. update(1,{2040,2040,2040}); assert(#calls==4 and calls[4].task_group==-1)
  22. update(1,{nil,16,2040}); assert(#calls==5 and calls[5].task_group==1)
  23. print("combat objective cost selection tests passed")
  24. -- Different squads share a word; updating lane 12 must preserve lane 1 exactly.
  25. local before = vars.ai
  26. AI.update(context,state,"ai","squad","objective",3,{objective_revision=1,task_costs={30,20,10}},12)
  27. assert(vars.ai & 31 == before & 31)
  28. assert((vars.ai >> 55) & 31 == 3)
  29. assert(math.type(vars.ai)=="integer")
  30. -- Cargo stays reserved when costs change after native passenger creation.
  31. AI.assign(context,"squad","objective",-1,true)
  32. assert(calls[#calls].reserved)
  33. AI.update(context,state,"cargo","squad","objective",3,{objective_revision=1,task_costs={30,20,10}},1,true)
  34. assert(calls[#calls].reserved and calls[#calls].task_group == 2)