Jump to content

Recommended Posts

I tried playing around with the AI API3 and set up my own entity template. Thereby I noticed an interesting behavior of the API3.Template.cost() function. I extracted a demonstrator for it: If you execute the following snippet

	(function (){
	        var rawTemplate = {
	            "Cost" : {
	                "Resources" : {
	                    "food" : 1,
		            //"wood" : 2,
	                    "stone" : 0,
	                    "metal" : 4
			}
		    }
		};
	        var fakeSharedAI = {
	            "_templatesModifications" : {}
		};
	        var testee = new API3.Template(
	            fakeSharedAI,
	            "someTemplate",
	            rawTemplate);
		warn("Costs are " + uneval(testee.cost()));
	})();

in the content of an AI script (e.g. inject it into petras start-up sequence), the 0AD log receives the following entry:

Quote

WARNING: Costs are ({food:1, stone:NaN, metal:4})

Note the NaN in the second property. So it seems template instantiation accepts skipped resource definitions gracefully, but I do not understand the behavior on "stone":0. Am I doing something wrong? I imagined something like "{food:1, stone:0, metal:4}" to build up.

  • Thanks 1
Link to post
Share on other sites
24 minutes ago, Teiresias said:

Note the NaN in the second property. So it seems template instantiation accepts skipped resource definitions gracefully, but I do not understand the behavior on "stone":0. Am I doing something wrong? I imagined something like "{food:1, stone:0, metal:4}" to build up.

I noticed this issue in the code sometime ago, but I assumed the Cost schema's doesn't allow 0 resource costs and didn't give it much thought. Turns out I was wrong.

You aren't really doing anything wrong. It's just a check being falsy when it shouldn't be. And casting undefined to a number gives a NaN. The following diff is all that's needed to change. One ought to care for edge cases, but it will fix the immediate cause. Diff generated from 0ad svn at rP24041.

diff --git a/binaries/data/mods/public/simulation/ai/common-api/entity.js b/binaries/data/mods/public/simulation/ai/common-api/entity.js
index 49d7636..2913650 100644
--- a/binaries/data/mods/public/simulation/ai/common-api/entity.js
+++ b/binaries/data/mods/public/simulation/ai/common-api/entity.js
@@ -33,7 +33,7 @@ var API3 = function(m)
 				let args = string.split("/");
 				for (let arg of args)
 				{
-					if (value[arg])
+					if (value[arg] != undefined)
 						value = value[arg];
 					else
 					{

 

  • Thanks 1
Link to post
Share on other sites

Thank you for the explanation. Your assumption

22 hours ago, smiley said:

I noticed this issue in the code sometime ago, but I assumed the Cost schema's doesn't allow 0 resource costs

may still hold, as I constructed the raw template data programatically (similar to the demonstrator in the first post) and this way bypassed the schema at all. I can live with it, I was just wondering if I am using the API3.Template class in wrong matter - before constructing a load of code doing so.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...