Advertisement
Guest User

PL_kolek's mirrorAI

a guest
May 2nd, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.25 KB | None | 0 0
  1. --PL_kolek's MicroAI for mirroring recruitment
  2. return {
  3.     init = function(ai)
  4.  
  5.     local AH = wesnoth.require("~add-ons/AI-demos/lua/ai_helper.lua")
  6.  
  7.     local recruit_cas = {}
  8.     local internal_recruit_cas = {}
  9.     local internal_params = {}
  10.     wesnoth.require("~add-ons/AI-demos/lua/generic-recruit_engine.lua").init(ai, internal_recruit_cas, internal_params)
  11.  
  12.     local recruit
  13.  
  14.     function recruit_cas:recruit_test_eval(cfg)
  15.         -- Check if leader is on keep
  16.         local leader = wesnoth.get_units { side = wesnoth.current.side, canrecruit = 'yes' }[1]
  17.         if (not leader) or (not wesnoth.get_terrain_info(wesnoth.get_terrain(leader.x, leader.y)).keep) then
  18.             return 0
  19.         end
  20.  
  21.         -- Check if there is space left for recruiting
  22.         local width, height, border = wesnoth.get_map_size()
  23.         local castle = {
  24.             locs = wesnoth.get_locations {
  25.                 x = "1-"..width, y = "1-"..height,
  26.                 { "and", {
  27.                 x = leader.x, y = leader.y, radius = 200,
  28.                 { "filter_radius", { terrain = 'C*^*,K*^*,*^Kov,*^Cov' } }
  29.                 }}
  30.             }
  31.         }
  32.         local no_space = true
  33.         for i,c in ipairs(castle.locs) do
  34.             local unit = wesnoth.get_unit(c[1], c[2])
  35.             if (not unit) then
  36.                 no_space = false
  37.                 break
  38.             end
  39.         end
  40.         if no_space then return 0 end
  41.  
  42.         --get other player units
  43.         local other_side
  44.         if(wesnoth.current.side == 1) then
  45.             other_side =2
  46.         else
  47.             other_side=1
  48.         end
  49.         local other_units=wesnoth.get_units{ side = other_side, canrecruit = 'no'}
  50.         local my_units=wesnoth.get_units{ side = wesnoth.current.side, canrecruit = 'no'}
  51.         local found_units={}
  52.         for i, other_unit in ipairs(other_units) do
  53.             local found=false
  54.             for j, my_unit in ipairs(my_units) do
  55.                 if my_unit.type == other_unit.type and not found_units[my_unit.id] then
  56.                     found_units[my_unit.id] = true
  57.                     found = true
  58.                     break
  59.                 end
  60.             end
  61.             if not found then
  62.                 for k,recruit_type in ipairs(wesnoth.sides[wesnoth.current.side].recruit) do
  63.                     if (recruit_type == other_unit.type) then
  64.                         recruit=recruit_type
  65.                         return 180000
  66.                     end
  67.                 end
  68.             end
  69.         end
  70.         return 0            
  71.     end
  72.  
  73.     function recruit_cas:recruit_test_exec(cfg)
  74.         if wesnoth.unit_types[recruit].cost <= wesnoth.sides[wesnoth.current.side].gold then
  75.             ai.recruit(recruit)
  76.         end
  77.     end
  78.  
  79.     return recruit_cas
  80.     end
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement