Skip to content

Commit

Permalink
Added NIL alternative for Builder generation, as nil value are optimi…
Browse files Browse the repository at this point in the history
…zed away on declaration
  • Loading branch information
schwiti6190 committed Oct 13, 2023
1 parent cc3df08 commit 51ab3ea
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 7 additions & 1 deletion scripts/CpObject.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,21 @@ function CpObject(base, init)
end

---@class CpObjectUtil
CpObjectUtil = {}
CpObjectUtil = {
BUILDER_API_NIL = "nil"
}

--- Registers a builder api for a class.
--- The attributes are set as private variables with "_" before the variable name
--- and the builder functions are named like the attribute.
--- Nil values have to be replaced with CpObjectUtil.BUILDER_API_NIL !!
---@param class table
---@param attributesToDefault table<attributeName, any>
function CpObjectUtil.registerBuilderAPI(class, attributesToDefault)
for attributeName, default in pairs(attributesToDefault) do
if default == CpObjectUtil.BUILDER_API_NIL then
default = nil
end
--- Applies the default value to the private variable
class["_" .. attributeName] = default
--- Creates the builder functions/ setters with the public variable name
Expand Down
6 changes: 3 additions & 3 deletions scripts/ai/PathfinderController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,16 @@ PathfinderControllerContext.attributesToDefaultValue = {
["offFieldPenalty"] = 7.5,
["useFieldNum"] = false,
-- Pathfinder nodes in this area have a prohibitive penalty (2000)
["areaToAvoid"] = nil,
["areaToAvoid"] = CpObjectUtil.BUILDER_API_NIL,
["allowReverse"] = false,
["vehiclesToIgnore"] = nil,
["vehiclesToIgnore"] = CpObjectUtil.BUILDER_API_NIL,
-- If false, as we reach the maximum iterations, we relax our criteria to reach the goal: allow for arriving at
-- bigger angle differences, trading off accuracy for speed. This usually results in a direction at the goal
-- being less then 30º off which in many cases isn't a problem.
-- Otherwise, for example when a combine self unloading must accurately find the trailer, set this to true.
["mustBeAccurate"] = false,
-- No fruit penalty in this area (e.g. when we know the goal is in fruit but want to avoid fruit all the way there)
["areaToIgnoreFruit"] = nil
["areaToIgnoreFruit"] = CpObjectUtil.BUILDER_API_NIL
}

function PathfinderControllerContext:init(vehicle, numRetries)
Expand Down

0 comments on commit 51ab3ea

Please sign in to comment.