Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement aspects #280

Merged
merged 7 commits into from
Apr 17, 2021
Merged

Implement aspects #280

merged 7 commits into from
Apr 17, 2021

Conversation

Ruin0x11
Copy link
Owner

@Ruin0x11 Ruin0x11 commented Apr 17, 2021

Purpose of this PR

  • Bug fix
  • Enhancement
  • New feature

Related issues

Close #15.

Description

Implements an "aspect" system, which is synonymous with the capability system described in previous issues. It allows you to add new functionality to instanced map objects.

local aspect = cargo:get_aspect_or_default(IItemCargo)
aspect.cargo_weight = 25000

cargo:set_aspect(IItemCargo, ItemCargoAspect:new(cargo, { cargo_quality = 5 })

Aspects support temporary values, to allow for using :calc() as before. Using aspects, you'd be able to override the behavior of an interface that encompasses IAspect, even for things like property accesses through temporary values. From the unit tests:

local AspectHolder_ITestAspect = require("test.api.AspectHolder_ITestAspect")
local IAspectModdable = require("api.IAspectModdable")

local AspectHolder_TestAspectModdable = class.class("TestAspectModdable", AspectHolder_ITestAspect)

function AspectHolder_TestAspectModdable:init(target, params)
   self.foo = params.my_foo or 0
end

function AspectHolder_TestAspectModdable:calc(target, prop)
   local v = IAspectModdable.calc(self, target, "foo")

   if prop == "foo" then
      return v * 100
   end

   return v
end

function AspectHolder_TestAspectModdable:mod(target, prop, v, params)
   if prop == "foo" then
      v = v + 42
   end

   return IAspectModdable.mod(self, target, prop, v, params)
end

return AspectHolder_TestAspectModdable

Finally, a new _ext table is now supported on all prototypes for map objects, which lets you specify which aspects should get instantiated on object creation, and their parameters. The parameters will get passed to the constructor along with the object instance.

data:add {
   _type = "base.item",
   _id = "aspected",

   _ext = {
      AspectHolder_ITestAspect
   }
}

data:add {
   _type = "base.item",
   _id = "cargo_art",
   elona_id = 669,
   image = "elona.item_painting_of_landscape",
   value = 3800,
   rarity = 150000,
   coefficient = 100,

   categories = {
      "elona.cargo"
   },

   _ext = {
      [IItemCargo] = {
         cargo_quality = 7,
         cargo_weight = 35000
      }
   }
}

The end goal is to replace the usages of item:has_category() and similar for determining if an item "acts like" equipment, cargo, and so on. Item categories are still useful for things like item generation, so they will stay for the time being.

The parts of the cargo system that were implemented have been updated to use aspects.

@Ruin0x11 Ruin0x11 added enhancement New feature or request modding Concerns new modding features beyond the scope of porting vanilla's codebase. refactoring This requires refactoring existing code labels Apr 17, 2021
@Ruin0x11 Ruin0x11 merged commit 311f169 into develop Apr 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request modding Concerns new modding features beyond the scope of porting vanilla's codebase. refactoring This requires refactoring existing code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Namespace custom game object fields by the mod that adds them
1 participant