Shard 0.4/dev - Page 40

Shard 0.4/dev

Here is where ideas can be collected for the skirmish AI in development

Moderators: hoijui, Moderators

User avatar
Beherith
Posts: 5145
Joined: 26 Oct 2007, 16:21

Re: Shard 0.4/dev

Post by Beherith »

prandipadaro wrote:Any idea on how to update the /BA/unittable.lua
anyone have a script?
thanks in advance

Here is a script that does 99% of the work, BUT, i know for fact that a few things are wrong, (airRange is one of them, bigexplosions is probably incomplete)

There are also multipliers for radarrange etc, youre gonna have to cross-check those.

The widget writes the info into the infolog.

You can also use the to_string(table or anything) to pretty print tables for fixing.

EDIT: crap, i missed techlevel too

Code: Select all

function widget:GetInfo()
    return {
        name      = "Shard Unittable exporter", --(v0.5)
        desc      = "Exports the unittable for shard",
        author    = "Beherith",
        date      = "2016-03-22",
        license   = "GPL V2",
        layer     = -10000,
        enabled   = true,
    }
end

--name	radarRadius	techLevel	isBuilding	needsWater	extractsMetal	bigExplosion	airLosRadius	losRadius	sonarRadius	metalCost	xsize	totalEnergyOut	jammerRadius	wreckName	zsize	mtype	airRange	submergedRange	stealth	groundRange	isWeapon	factoriesCanBuild	"armaap"	"armplat"	"armlab"	"armalab"	"armsy"	"armasy"	"asubpen"	"armavp"	"armvp"	"armap"	"armhp"	"armfhp"	"coraap"	"corplat"	"corlab"	"coralab"	"corsy"	"corasy"	"csubpen"	"coravp"	"corvp"	"corap"	"corhp"	"corfhp"

--hard ones: mtype,factoriesCanBuild
function widget:Initialize()
	bigExplosions = {ATOMIC_BLAST = true, COMMANDER_BLAST = true,}
	for id,unitDef in pairs(UnitDefs) do
		str= "unitTable[\"" .. unitDef.name..'\"] = {'
		
		str = str .. 'radarRadius = ' .. tostring(unitDef.radarRadius * 0.1 ) .. ','
		str = str .. 'isBuilding = ' .. tostring(unitDef.isBuilding) .. ','
		str = str .. 'needsWater = ' .. tostring(unitDef.minWaterDepth >0 ) .. ','
		str = str .. 'extractsMetal = ' .. tostring(unitDef.extractsMetal) .. ','
		
		if bigExplosions[unitDef.explodeas ] == true then
			str = str .. 'bigExplosion = ' .. tostring(true ) .. ','
		else
			str = str .. 'bigExplosion = ' .. tostring(false) .. ','
		end
		str = str .. 'airLosRadius = ' .. tostring(unitDef.airLosRadius  * 0.1 ) .. ','
		str = str .. 'losRadius = ' .. tostring(unitDef.losRadius * 0.1 ) .. ','
		str = str .. 'sonarRadius = ' .. tostring(unitDef.sonarRadius * 0.1 ) .. ','
		str = str .. 'metalCost = ' .. tostring(unitDef.metalCost) .. ','
		str = str .. 'xsize = ' .. tostring(unitDef.xsize ) .. ','
		str = str .. 'totalEnergyOut = ' .. tostring(unitDef.totalEnergyOut) .. ','
		str = str .. 'jammerRadius = ' .. tostring(unitDef.jammerRadius * 0.1 ) .. ','
		if unitDef.wreckName ~= '' then str = str .. 'wreckName = \"' .. tostring(unitDef.wreckName) .. '\",' end
		
		
		groundRange = 0
		submergedRange = 0
		airRange = 0
		isWeapon = false
		for weaponindex, weaponInfo in ipairs(unitDef.weapons) do
			isWeapon = true
			weaponDefID = weaponInfo.weaponDef
			--Spring.Echo(unitDef.name,'onlytargets:',to_string(weaponInfo.onlyTargets))
			if weaponInfo.onlyTargets["vtol"] then
				airRange = math.max(airRange,WeaponDefs[weaponDefID].range)
			elseif WeaponDefs[weaponDefID].type == "TorpedoLauncher" then
				submergedRange = math.max(submergedRange,WeaponDefs[weaponDefID].range)
			else
				groundRange = math.max(groundRange,WeaponDefs[weaponDefID].range)
			end
		
		str = str .. 'airRange = ' .. tostring(airRange) .. ','
		str = str .. 'groundRange = ' .. tostring(groundRange) .. ','
		str = str .. 'submergedRange = ' .. tostring(submergedRange) .. ','
		str = str .. 'isWeapon = ' .. tostring(isWeapon) .. ','
		
		
		str = str .. 'buildOptions = ' .. tostring(unitDef.buildOptions ~= nil) .. ','
		
		factoriesCanBuild = ''
		for id2,unitDef2 in pairs(UnitDefs) do
			if unitDef2.buildOptions and unitDef2.buildOptions[unitDef] then 
			factoriesCanBuild = factoriesCanBuild .. unitDef2.name .. ','
			end
		end
		if factoriesCanBuild ~= '' then
			str = str .. 'factoriesCanBuild = {' .. tostring(factoriesCanBuild) .. '},'
		end
		
		mtype = 'veh' -- buildings are veh, possible types are veh,bot,shp,hov,sub,air,amp
		
		if unitDef.moveDef and unitDef.moveDef.name then
			-- Spring.Echo(unitDef.name,"unitDef.moveDef",to_string(unitDef.moveDef))
			if unitDef.moveDef.name:find('uboat') ~= nil then 
				mtype = 'sub'
			elseif  unitDef.moveDef.name:find('boat') ~= nil then 
				mtype = 'shp'
			elseif  unitDef.moveDef.name:find('hover') ~= nil then 
				mtype = 'hov'
			elseif  unitDef.moveDef.name:find('akbot') ~= nil then 
				mtype = 'amp'
			elseif  unitDef.moveDef.name:find('atank') ~= nil then 
				mtype = 'amp'
			end
		end
		if unitDef.canfly then
			mtype = 'air'
		end
		str = str .. 'mtype = ' .. tostring(mtype) .. ','
		
		str = str .. '}'
		Spring.Echo(str)
		str = ""
		end
	end


end

function to_string(data, indent)
	local str = ""

	if(indent == nil) then
		indent = 0
	end

	-- Check the type
	if(type(data) == "string") then
		str = str .. ("    "):rep(indent) .. data .. "\n"
	elseif(type(data) == "number") then
		str = str .. ("    "):rep(indent) .. data .. "\n"
	elseif(type(data) == "boolean") then
		if(data == true) then
			str = str .. "true"
		else
			str = str .. "false"
		end
	elseif(type(data) == "table") then
		local i, v
		for i, v in pairs(data) do
			-- Check for a table in a table
			if(type(v) == "table") then
				str = str .. ("    "):rep(indent) .. i .. ":\n"
				str = str .. to_string(v, indent + 2)
			else
				str = str .. ("    "):rep(indent) .. i .. ": " .. to_string(v, 0)
			end
		end
	elseif (data ==nil) then
		str=str..'nil'
	else
		--print_debug(1, "Error: unknown data type: %s", type(data))
		str=str.. "Error: unknown data type:" .. type(data)
		Spring.Echo('X data type')
	end

	return str
end
User avatar
prandipadaro
Posts: 98
Joined: 19 Oct 2011, 22:38

Re: Shard 0.4/dev

Post by prandipadaro »

thanks beherith for your work, i will use it fast and fill the small gaps that you left.
User avatar
Beherith
Posts: 5145
Joined: 26 Oct 2007, 16:21

Re: Shard 0.4/dev

Post by Beherith »

Also, you can probably leave out all the chickens, since they are unbuildable.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Re: Shard 0.4/dev

Post by AF »

There was no VFS API when Shard was built. It could be added, but I would need to build the necessary parts to bridge between the C++ OO API and the lower level C APIs.

But I would bare in mind that the end result would be a kludge of great magnitude, if additional information needs adding to the unit type then it should be added, and I still think someone with greater familiarity with the Spring lua APIs can help port Shard so it runs in gadget land rather than native AI land, at which point all of this is a non-issue

Personally I advise against writing out configs this way, but I didn't write it, and what's done is done, it does the job
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: Shard 0.4/dev

Post by Forboding Angel »

Couldn't it be done without porting? or does vfs simply not work like that?
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Re: Shard 0.4/dev

Post by AF »

It's not a flag that needs switching, it's work that would take a while to do. I'd estimate around a day of my own time to do it, maybe a bit less for someone else who's done similar things. I forget who it was that mentioned it was possible and indicated where to begin though

If I was going to spend that kind of continuous chunk of time on Spring related activities though, VFS in Shard wouldn't be at the top of my list.

There is an alternative though, AIs can send messages to games, and if I remember correctly, sending responses back is now working. A gadget in BA could provide all the necessary information at runtime, and a Shard Module could request the information on creation then hold on to it throughout the game. The BA gadget would be rather simple, with the most difficult part being to serialise a lua table.
User avatar
Beherith
Posts: 5145
Joined: 26 Oct 2007, 16:21

Re: Shard 0.4/dev

Post by Beherith »

If you specify a specific format I should serialize to, ill make the relevant gadget. Also, should I post any errors Shard throws into this thread or do you prefer another form of ticket?
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Re: Shard 0.4/dev

Post by AF »

A github issue is better, posting here works but things get lost, especially if it's in code I didn't write
User avatar
eronoobos
Posts: 73
Joined: 27 Mar 2015, 20:25

Re: Shard 0.4/dev

Post by eronoobos »

i just remembered i wrote the thing that makes the unittable.lua
Attachments
shard_help_unit_table.lua
(5.41 KiB) Downloaded 26 times
User avatar
eronoobos
Posts: 73
Joined: 27 Mar 2015, 20:25

Re: Shard 0.4/dev

Post by eronoobos »

prandipadaro wrote:I try to improve the TaskQueueBehaviour:BestFactory() function inside BA configuration.
thank the lord, a.k.a. you
prandipadaro wrote:I'm searching a way to determinate in which mobNetworks area the builder is.
Try this widget. First you need to set DebugEnabled = true at the top of maphandler.lua Press 1 to view the mobNetworks, I think. There's other information in keys 2 (los simulator), 3 (targets), 4 ("NOBUILD"? i forget), 5 (defense clusters, aka 'turtles'), all of which need the relevant modules DebugEnabled
Attachments
shard_help_debug_visualizer.lua
(5.87 KiB) Downloaded 23 times
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Re: Shard 0.4/dev

Post by AF »

Those scripts should probably be in the git repo in a subfolder or elsewhere so they're under version control
User avatar
prandipadaro
Posts: 98
Joined: 19 Oct 2011, 22:38

Re: Shard 0.4/dev

Post by prandipadaro »

oh gui! how can i thank you?
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: Shard 0.4/dev

Post by Forboding Angel »

Behe, do me a favor and let me know when/if you do that cause Evo would benefit from this as well. As something that has to be standalone, shard isn't that useful, but the game changes completely if you can store the relevant configs in the game itself.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Re: Shard 0.4/dev

Post by AF »

Note that this wouldn't let you put the logic in the game, the files may as well be json/xml files
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: Shard 0.4/dev

Post by Forboding Angel »

AF wrote:Note that this wouldn't let you put the logic in the game, the files may as well be json/xml files
Define "the logic". Do you mean things other than taskqueues, or do you specifically mean taskqueues?
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Re: Shard 0.4/dev

Post by AF »

I don't like the idea of executable lua code being sent across the divide, it encourages a whole new class of issues, and overcomplicates things. Perhaps you could override the new shard_include function to load files by using the AI <-> Game API combined with the VFS, but the performance would suffer, and the ability to debug would be significantly more difficult as there would no longer be file names or references, stack traces would be almost useless.

What Beherith wants to implement on the other hand should be used for transferring static data, with no functions or logic. What you want is best done by finishing the port from the Shard C++ API to the Spring gadget API, rather than using workarounds and kludges. Keeping in mind that doing that is also significantly easier than writing a whole new lua AI from scratch
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Re: Shard 0.4/dev

Post by AF »

I've made some major updates to https://shard.tomjn.com

As well as the various blog posts taking existing documentation, I've created a main documentation area, with a page for every file and object in the AI. These will explain what each one does, what they're intended to do, what they interact with, and their purpose.

I've put placeholders in place, and filled a great number of them with information, e.g.:

Image
Attachments
Screen Shot 2016-04-06 at 19.43.41.png
(287.1 KiB) Not downloaded yet
User avatar
prandipadaro
Posts: 98
Joined: 19 Oct 2011, 22:38

Re: Shard 0.4/dev

Post by prandipadaro »

I've made some major updates to https://shard.tomjn.com
very nice thank you!
User avatar
eronoobos
Posts: 73
Joined: 27 Mar 2015, 20:25

Re: Shard 0.4/dev

Post by eronoobos »

I decided to try converting Shard to a Lua AI. It's not as bad as I thought, but also worse. I'm getting confused about environments. Every instance of the Lua AI needs to be its own class (which is for most purposes a table, i think?). Which is fine, but it would be nice if modules within each instance could have a shared environment. (In Shard, I used to store global variables in the ai table.) Each module could have something like self.ai, which would be its own parent, but this would require a lot more work for me or anyone else trying to convert their game config. Every module would need to have a local ai and an ai = self.ai in its :Init(). The same problem for the more important matter of all the api functions within map, game, unit, etc. I'm thinking of Spring's LUS localization. In my case I would rather have for example game:GetEnemies() than self.ai.game:GetEnemies() within a module. Is this a pipe dream? Should I accept massive table nesting?

Alternatively, I could work on converting the config to using Spring lua functions instead of converting the Shard proxy functions to using them.

https://github.com/eronoobos/ShardGoneNative
Post Reply

Return to “AI”