AI won't build cottages (for food)

User avatar
Mikko Merikivi
Posts: 18
Joined: Fri Sep 13, 2013 9:04 pm

AI won't build cottages (for food)

Postby Mikko Merikivi » Tue Oct 08, 2013 4:03 am

I already squashed some bugs to get to even understand the situation but I'm pretty sure I have a problem that for whatever reason the AI won't build cottages (they provide supply) when food/supply limit is reached. Sometimes the game even crashes (not always, really strange, again) with this information extracted with gdb: http://paste.dy.fi/JF/plain

Furthermore, when I put a command "AiSet("unit-dwarves-cottage", 1)" in the AI script, it does build it. It's only when the AI is out of food that it won't build them.

So, what do I have done scripting-wise that would be relevant to know? The load order of relevant scripts in stratagus.lua goes like this: units (inside it are also the animations and buttons), upgrades, AiHelper, land_attack-dwarves (the AI script used in my tests).
https://sourceforge.net/p/battle4mandic ... atagus.lua


The unit-dwarves-worker is the one supposed to build the cottages.
https://sourceforge.net/p/battle4mandic ... worker.lua


In the file for unit-dwarves-cottage we have these relevant lines:

Code: Select all

Supply = 6,

DefineButton( { Pos = 3, Level = 2, Icon = "icon-dwarves-cottage",
Action = "build", Value = "unit-dwarves-cottage",
Key = "c", Hint = _("BUILD ~!COTTAGE"),
ForUnit = {"unit-dwarves-worker", "unit-dwarves-advanced-worker", "unit-dwarves-advanced-worker2"} } )

DefineAllow("unit-dwarves-cottage", "AAAAAAAAAAAAAAAA") 
https://sourceforge.net/p/battle4mandic ... ottage.lua


In the upgrade.lua for dwarves there aren't any dependencies for cottages.
https://sourceforge.net/p/battle4mandic ... pgrade.lua


In the ai.lua (contains the DefineAiHelper) I tried the game with just this:

Code: Select all

race1 = "dwarves"
race2 = "darkness"


DefineAiHelper(
	{"build", "unit-dwarves-worker",
		"unit-dwarves-town-center", "unit-dwarves-cottage", "unit-dwarves-guard-tower",
		"unit-dwarves-market",
		"unit-dwarves-blacksmith", "unit-dwarves-lumber-mill"},
	{"unit-limit", "unit-dwarves-cottage", "food"}
)


--
--  Some functions used by Ai
--

-- Create some counters used by ai
local function CreateAiGameData()
	if stratagus == nil then
		stratagus = {}
	end
	if stratagus.gameData == nil then
		stratagus.gameData = {}
	end
	if stratagus.gameData.AIState == nil then
		stratagus.gameData.AIState = {}
		stratagus.gameData.AIState.index = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
		stratagus.gameData.AIState.loop_index = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
	end
end

local function CleanAiGameData()
	if stratagus ~= nil and stratagus.gameData ~= nil then
		stratagus.gameData.AIState = nil
	end
end

function ReInitAiGameData()
	CleanAiGameData()
	CreateAiGameData()
end

--
--  Load the actual individual scripts.
--
ReInitAiGameData()
Load("scripts/ai/passive.lua")
Load("scripts/ai/land_attack-dwarves.lua")
Load("scripts/ai/land_attack-darkness.lua")
Load("scripts/ai/luhdaga.lua")
The ReInitAiGameData() is from Aleona's Tales (I don't actually know what it does) but I have tried even without it and that doesn't help either.

https://sourceforge.net/p/battle4mandic ... pts/ai.lua


The land_attack-dwarves script uses the new layout suggested to me that doesn't put every order inside "function() return" and "end,". The layout was pretty much copied from Aleona's Tales' adaptive AI. The land_attack-dwarves will train workers until it has enough money to do other things. However, I've tried even with a stripped down version in which it will only train workers. It does utilize my self-made CheckDependency function but I doubt that's the problem since I've tested this with the unpatched Stratagus and it doesn't work either.

https://sourceforge.net/p/battle4mandic ... warves.lua



So, have you got any ideas what the problem might be? Also, another strange problem I'm having which I haven't yet had time to properly address but I'll say it anyway in case someone has had a similar problem: When I or the AI is building something, when the building is finished the worker won't stop automatically "building" it. It will just stays there hacking the air with its axe (the building animation).
User avatar
Andrettin
Posts: 433
Joined: Sun Jun 30, 2013 9:58 pm
Location: Vienna, Austria
Contact:

Re: AI won't build cottages (for food)

Postby Andrettin » Tue Oct 08, 2013 4:35 am

Maybe putting something like the following code in the Dwarves' land attack AI will solve your issue:

Code: Select all

if (GetPlayerData(AiPlayer(), "Demand") >= GetPlayerData(AiPlayer(), "Supply")) then
	AiSet("unit-dwarves-cottage", (GetPlayerData(AiPlayer(), "UnitTypesCount", "unit-dwarves-cottage") + 1))
end
User avatar
Mikko Merikivi
Posts: 18
Joined: Fri Sep 13, 2013 9:04 pm

Re: AI won't build cottages (for food)

Postby Mikko Merikivi » Tue Oct 08, 2013 5:32 am

Andrettin wrote:Maybe putting something like the following code in the Dwarves' land attack AI will solve your issue:

Code: Select all

if (GetPlayerData(AiPlayer(), "Demand") >= GetPlayerData(AiPlayer(), "Supply")) then
	AiSet("unit-dwarves-cottage", (GetPlayerData(AiPlayer(), "UnitTypesCount", "unit-dwarves-cottage") + 1))
end
Yes, it did. Maybe it's actually even better that way since now the moment the AI will start building a cottage or other supply providing stucture won't be hardcoded. Still, I can't help but wonder why the hardcoded way isn't working. Anyway, it's not important.
User avatar
Mikko Merikivi
Posts: 18
Joined: Fri Sep 13, 2013 9:04 pm

Re: AI won't build cottages (for food)

Postby Mikko Merikivi » Tue Oct 08, 2013 6:04 am

As for the units not finishing up what they started to build, it was apparently a bug with the BuildFromOutside tag that was just recently fixed. Here's the fix:

Code: Select all

diff -c -r stratagus_2.2.7.veryorig/src/action/action_build.cpp stratagus_2.2.7.patched/src/action/action_build.cpp
*** stratagus_2.2.7.veryorig/src/action/action_build.cpp	2013-10-03 19:56:26.453550587 +0300
--- stratagus_2.2.7.patched/src/action/action_build.cpp	2013-10-07 23:01:12.159922447 +0300
***************
*** 452,458 ****
  		return ;
  	}
  	if (this->State == State_BuildFromOutside) {
! 		this->BuildFromOutside(unit);
  	}
  }
  
--- 452,460 ----
  		return ;
  	}
  	if (this->State == State_BuildFromOutside) {
! 		if (this->BuildFromOutside(unit)) {
! 			this->Finished = true;
! 		}
  	}
  }
User avatar
Andrettin
Posts: 433
Joined: Sun Jun 30, 2013 9:58 pm
Location: Vienna, Austria
Contact:

Re: AI won't build cottages (for food)

Postby Andrettin » Tue Oct 08, 2013 6:07 am

Mikko Merikivi wrote:Yes, it did.
Cool :)

By the way, since you have a Dwarven civilization in Battle for Mandicore, you may be interested in Jinn's Dwarven Town Hall and b_o's Dwarven Miner graphics which I have posted in this thread:

viewtopic.php?f=33&t=3418&p=4566&sid=76 ... ef11#p4566
User avatar
Mikko Merikivi
Posts: 18
Joined: Fri Sep 13, 2013 9:04 pm

Re: AI won't build cottages (for food)

Postby Mikko Merikivi » Tue Oct 08, 2013 8:02 am

It seems I spoke too soon. The AI does build the cottages now but it will still crash sometimes, however, with a different error. This one: http://paste.dy.fi/rZY/plain

I've seen that error before and I thought it was because cottages could hold foodresource which wasn't on the map. However, it seems to be back so that couldn't have been it.

EDIT: Don't hold your breath, but I think I might have fixed the issues. The documentation for AiSetCollect was obsolete and when I switched to the new function, it didn't crash on my first run.
Edit 2: Nope, that wasn't it either. I was just lucky it didn't crash that time.

Who is online

Users browsing this forum: No registered users and 1 guest