Advertisement
Guest User

Simple Stat Item Framework

a guest
Jan 18th, 2017
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.39 KB | None | 0 0
  1. local mod = RegisterMod("ExampleFrameworkMod", 1)
  2.  
  3. --[==[
  4. KubeRoot's simple stat item framework
  5.  
  6. Free to use for any purpose as long as this notice is preserved. (No need to keep the "How to use")
  7. --]==]
  8.  
  9.  
  10. --[==[
  11. How to use:
  12. Put everything from 'local items' to the last 'end' at the top of your mod just below your RegisterMod.
  13. Read any comments in the code and adjust the code as written
  14. Put your item declarations into the items table, with your item's name as the table key.
  15.  
  16. For example:
  17.  
  18. items["Mysterious fabric"] = {
  19.     CanFly = true,
  20.     TearFlags = 3,
  21.     Damage = 1,
  22.     TearColor = Color(0.9,0.9,1,0.6,0,0,0),
  23. }
  24.  
  25. items["Pegasus Boots"] = {
  26.     MoveSpeed = 2,
  27.     TearFlags = 8,
  28.     ShotSpeed = 1,
  29.     Damage = -1,
  30.     TearColor = Color(0.3, 1, 0.4, 1, 0, 50, 0)
  31. }
  32.  
  33. Make sure all the items are included in a valid items.xml for your mod.
  34.  
  35.  
  36. NOTE:
  37. At the current time (18.01.2017) MaxFireDelay appears to have a broken cache evaluation - the callback
  38. triggers, but the stat changes aren't preserved. I am not including any fixes for that for futureproofing
  39. reasons, as well as to raise awareness of the issue.
  40. --]==]
  41.  
  42. local items
  43. do
  44.     local mod = mod -- !!! CHANGE THIS IF YOUR RegisterMod VARIABLE ISN'T mod !!!
  45.  
  46.     local mt, mt2 = {}, {}
  47.     items = setmetatable({}, mt)
  48.     local nextCache = 0
  49.     local game = Game()
  50.     local stats = {
  51.         [CacheFlag.CACHE_DAMAGE] = {"Damage"},
  52.         [CacheFlag.CACHE_FIREDELAY] = {"MaxFireDelay"},
  53.         [CacheFlag.CACHE_SHOTSPEED] = {"ShotSpeed"},
  54.         [CacheFlag.CACHE_RANGE] = { "TearHeight", "TearFallingSpeed", "TearFallingAcceleration" },
  55.         [CacheFlag.CACHE_SPEED] = {"MoveSpeed"},
  56.         [CacheFlag.CACHE_TEARFLAG] = {"TearFlags"},
  57.         [CacheFlag.CACHE_TEARCOLOR] = {"TearColor", "LaserColor"},
  58.         [CacheFlag.CACHE_FLYING] = {"CanFly"},
  59.         [CacheFlag.CACHE_LUCK] = {"Luck"},
  60.         --[CacheFlag.CACHE_WEAPON] = ,
  61.         --[CacheFlag.CACHE_FAMILIARS] = ,
  62.     }
  63.     local statLookup = {}
  64.     for k, v in pairs(stats) do
  65.         for _, v2 in pairs(v) do
  66.             statLookup[v2] = k
  67.         end
  68.     end
  69.  
  70.     local bitwiseStats = {
  71.         TearFlags = true
  72.     }
  73.  
  74.     local function __players(n, i)
  75.         i=i+1
  76.         if i<n then
  77.             return i, game:GetPlayer(i)
  78.         end
  79.     end
  80.  
  81.     local function players()
  82.         return __players, game:GetNumPlayers(), -1
  83.     end
  84.  
  85.     function mt:__newindex(name, item)
  86.         local t = {}
  87.         t.id = Isaac.GetItemIdByName(name)
  88.         t.name = name
  89.         t.__orig = item
  90.  
  91.         local itemflags = 0
  92.         for flag, flagtab in pairs(stats) do
  93.             for _, flagname in pairs(flagtab) do
  94.                 if item[flagname] then
  95.                     itemflags = itemflags | flag
  96.                     break
  97.                 end
  98.             end
  99.         end
  100.         t.cacheflags = itemflags
  101.  
  102.         rawset(self, name, setmetatable(t, mt2))
  103.     end
  104.  
  105.     function mt2:__index(k)
  106.         return self.__orig[k]
  107.     end
  108.  
  109.     function mt2:__newindex(k, v)
  110.         local stat = statLookup[k]
  111.         if stat then
  112.             nextCache = nextCache | stat
  113.             self.cacheflags = self.cacheflags | stat
  114.         end
  115.  
  116.         self.__orig[k] = v
  117.     end
  118.  
  119.     local isColor
  120.     do
  121.         local function colorTest(c)
  122.             local _ = (c.R and c.G and c.B and c.A and c.RO and c.__mul) or error()
  123.         end
  124.  
  125.         isColor = function(c)
  126.             return type(c)=="userdata" and pcall(colorTest, c)
  127.         end
  128.     end
  129.  
  130.     local cwhite = Color(1,1,1,1,0,0,0)
  131.     local function cloneColor(c)
  132.         return Color.Lerp(c, cwhite, 0)
  133.     end
  134.  
  135.     local function onEvaluateCache(_mod, ply, flag)
  136.         for name, item in pairs(items) do
  137.             if ply:HasCollectible(item.id) and (flag & item.cacheflags) ~= 0 then
  138.                 local n = ply:GetCollectibleNum(item.id)
  139.                 for k, v in pairs(item.__orig) do
  140.                     if (flag & statLookup[k]) ~= 0 then
  141.                         if type(v) == "number" then
  142.                             if bitwiseStats[k] then
  143.                                 ply[k] = ply[k] | v
  144.                             else
  145.                                 ply[k] = ply[k] + v * n
  146.                             end
  147.                         elseif isColor(v) then
  148.                             ply[k] = cloneColor(v) * ply[k]
  149.                         else
  150.                             ply[k] = v
  151.                         end
  152.                     end
  153.                 end
  154.             end
  155.         end
  156.     end
  157.  
  158.     local function onUpdate(_mod)
  159.         if nextCache ~= 0 then
  160.             for k, ply in players() do
  161.                 ply:AddCacheFlags(nextCache)
  162.                 ply:EvaluateItems()
  163.             end
  164.             nextCache = 0
  165.         end
  166.     end
  167.  
  168.     mod:AddCallback(ModCallbacks.MC_EVALUATE_CACHE, onEvaluateCache)
  169.     mod:AddCallback(ModCallbacks.MC_POST_UPDATE, onUpdate)
  170. end
  171.  
  172. items["Mysterious fabric"] = {
  173.     CanFly = true,
  174.     TearFlags = 3,
  175.     Damage = 1,
  176.     TearColor = Color(0.9,0.9,1,0.6,0,0,0),
  177. }
  178.  
  179. items["Pegasus Boots"] = {
  180.     MoveSpeed = 2,
  181.     TearFlags = 8,
  182.     ShotSpeed = 1,
  183.     Damage = -1,
  184.     TearColor = Color(0.3, 1, 0.4, 1, 0, 50, 0)
  185. }
  186.  
  187. items["Pegasus Boots"].Damage = -2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement