[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[bos-dev] Developing a defensive Ai



I am working on a defensive Ai.
It works... a little bit.

Some of the code (to determine if something is building, and so we are
waiting for it) is very ugly. Mostly because trying more normal ways
did not work as expected.

Here is the code so far:

--     ____                _       __
--    / __ )____  _____   | |     / /___ ___________
--   / __  / __ \/ ___/   | | /| / / __ `/ ___/ ___/
--  / /_/ / /_/ (__  )    | |/ |/ / /_/ / /  (__  )
-- /_____/\____/____/     |__/|__/\__,_/_/  /____/
--
--       A futuristic real-time strategy game.
--          This file is part of Bos Wars.
--
--	defensive.lua	- Define the AI.
--
--	(c) Copyright 2012 by Paul Dufresne
--
--      This program is free software; you can redistribute it and/or modify
--      it under the terms of the GNU General Public License as published by
--      the Free Software Foundation; either version 2 of the License, or
--      (at your option) any later version.
--
--      This program is distributed in the hope that it will be useful,
--      but WITHOUT ANY WARRANTY; without even the implied warranty of
--      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--      GNU General Public License for more details.
--
--      You should have received a copy of the GNU General Public License
--      along with this program; if not, write to the Free Software
--      Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301  USA
--

--Created May 30, 2012 by Paul Dufresne (dufresnep@xxxxxxxxx)


--To experiment, you could comment out the 'RegisterAi' line in
default.lua file ... this one, whatever the filename, will become the
default AI
-- because of RegisterAi("default", "default",... at the end of *this* file

local need_waiting, wait_for

function InitAiScripts_default()
  need_waiting = { false, false, false, false, false, false, false, false}
  wait_for={nil,nil,nil,nil,nil,nil,nil,nil}
  was_having = {0, 0, 0, 0, 0, 0, 0, 0}
end

function NumberUnits(unit)
  local the_unit = UnitTypeByIdent(unit)
  --Hum, doc says "building" for next line, but it is "buildings"
(with the final 's')
  --local building = GetNumUnitsAt(AiPlayer(), "buildings", {0, 0},
{99999, 99999})
  local count = Players[AiPlayer()].UnitTypesCount[the_unit.Slot]
  --print("building="..building)
  return count --  +building
end

function dostuff()
  local action,howmany,what=nil
  local want={
    -- "H" stands for "Have at least"
    {"H",1,"vault"};
	{"H",1,"engineer"};
	{"H",1,"powerplant"};
	{"H",1,"magmapump"};
	{"H",3,"engineer"};
	{"H",3,"powerplant"};
	{"H",2,"magmapump"};
	{"H",1,"camp"};
	{"H",1,"assault"};
	{"H",1,"grenadier"};
	{"H",1,"bazoo"};
	{"H",3,"gturret"};
	{"H",1,"cannon"};
	{"H",1,"msilo"};
	{"H",2,"cannon"};
	{"H",2,"msilo"};
	{"H",10,"cannon"}
	}
	
	--if need_waiting[AiPlayer()] then
	--  print("waiting for "..wait_for[AiPlayer()])
	--else print("not waiting")
    --end
	
    if need_waiting[AiPlayer()] then
	  print("Waiting for "..wait_for[AiPlayer()])
	  if  NumberUnits(wait_for[AiPlayer()]) == was_having[AiPlayer()] then
	    return -- Quantity unchanged, let's wait is is built... or lost
[hope we will not lost it the same time it is build!!!]
	  --Wait(wait_for[AiPlayer()])
	  else
	    print("ok, was having "..was_having[AiPlayer()].." and now have
"..NumberUnits(wait_for[AiPlayer()]).."of "..wait_for[AiPlayer()])
	    need_waiting[AiPlayer()]=false
	    wait_for[AiPlayer()]=nil
	  end
	end
	
	--local building = GetNumUnitsAt(AiPlayer(), "buildings", {0, 0},
{99999, 99999})
	--local any=GetNumUnitsAt(AiPlayer(), "any", {0, 0}, {99999, 99999})
	--print("building="..building)
	--print("any="..any)
	--if building > 1 then return end -- it is likely to be the building
I just ask, wait for it

	
	for index,cmd in ipairs(want) do
	  -- in dostuff now: local action, howmany, what = nil
	  --print("index:"..index)
	  --print("cmd:"..cmd)
	  if cmd then
	    --print("element:"..element)
	    if cmd[1] then action=cmd[1] end
		if cmd[2] then howmany=cmd[2] end
		if cmd[3] then what="unit-"..cmd[3] end
		--print("Action:"..action.." howmany:"..howmany.." what:"..what)
		--print("NumberUnits(what)="..NumberUnits(what))
		local number=NumberUnits(what)
		print("I have "..number.." "..what)
		if action=="H" and NumberUnits(what) < howmany then
		  need_waiting[AiPlayer()]=true
		  wait_for[AiPlayer()]=what
		  was_having[AiPlayer()]=number
		  AiNeed(what)
		  print("requested a "..what)
		  break
	    end
	  else print("Command is nil!") end
	end
	print ("returned action is "..action.." howmany:"..howmany.." what:"..what)
	return true
end


function AiDefensive()
	print("Player "..AiPlayer().." playing.")
	return dostuff()
end

RegisterAi("default", "default", AiDefensive, InitAiScripts_default)

Attachment: defensive.lua
Description: Binary data