Teiresias Posted September 15, 2020 Report Share Posted September 15, 2020 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. 1 Quote Link to comment Share on other sites More sharing options...
Stan` Posted September 15, 2020 Report Share Posted September 15, 2020 CC @Angen Quote Link to comment Share on other sites More sharing options...
smiley Posted September 15, 2020 Report Share Posted September 15, 2020 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 { 1 Quote Link to comment Share on other sites More sharing options...
Teiresias Posted September 16, 2020 Author Report Share Posted September 16, 2020 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. Quote Link to comment Share on other sites More sharing options...
smiley Posted September 16, 2020 Report Share Posted September 16, 2020 Actually, costs are evidently `NonNegativeInteger` so the assumption was actually wrong. Quote Link to comment Share on other sites More sharing options...
Teiresias Posted September 26, 2020 Author Report Share Posted September 26, 2020 On 9/16/2020 at 10:22 PM, smiley said: Actually, costs are evidently `NonNegativeInteger` so the assumption was actually wrong. In my AI scripting experiments I also use negative values to indicate "this resource amount still has to be gathered to do that" (not compliant with a schema but still working fine). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.