Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 9.86 KB | None | 0 0
  1. ---
  2. game/engines/default/engine/Object.lua       |  7 ++
  3.  game/engines/default/engine/Zone.lua         |  8 +--
  4.  game/engines/default/engine/utils.lua        | 96 ++++++++++++++++++++++++++++
  5.  game/modules/tome/class/Object.lua           | 73 ++++++++++++++++-----
  6.  game/modules/tome/class/interface/Combat.lua | 12 +++-
  7.  5 files changed, 172 insertions(+), 24 deletions(-)
  8.  
  9. diff --git a/game/engines/default/engine/Object.lua b/game/engines/default/engine/Object.lua
  10. index 4dff64c..594c6e2 100644
  11. --- a/game/engines/default/engine/Object.lua
  12. +++ b/game/engines/default/engine/Object.lua
  13. @@ -244,3 +244,10 @@ end
  14.  function _M:getEntityKind()
  15.     return "object"
  16.  end
  17. +
  18. +--- List of ego rules to be applied.
  19. +_M.ego_rules = {}
  20. +
  21. +function _M:addEgoRule(rule)
  22. +  table.insert(self.ego_rules, rule)
  23. +end
  24. diff --git a/game/engines/default/engine/Zone.lua b/game/engines/default/engine/Zone.lua
  25. index 8f612c0..005c48d 100644
  26. --- a/game/engines/default/engine/Zone.lua
  27. +++ b/game/engines/default/engine/Zone.lua
  28. @@ -475,8 +475,8 @@ function _M:finishEntity(level, type, e, ego_filter)
  29.                 ego.instant_resolve = nil
  30.                 -- Void the uid, we dont want to erase the base entity's one
  31.                 ego.uid = nil
  32. -               -- Merge additively but with array appending, so that nameless resolvers are not lost
  33. -               table.mergeAddAppendArray(e, ego, true)
  34. +               -- Merge with Object's ego merge rules.
  35. +        table.ruleMergeAppendAdd(e, ego, self.object_class.ego_rules)
  36.                 e.name = newname
  37.                 e.egoed = true
  38.             end
  39. @@ -563,8 +563,8 @@ function _M:finishEntity(level, type, e, ego_filter)
  40.                 ego.instant_resolve = nil
  41.                 -- Void the uid, we dont want to erase the base entity's one
  42.                 ego.uid = nil
  43. -               -- Merge additively but with array appending, so that nameless resolvers are not lost
  44. -               table.mergeAddAppendArray(e, ego, true)
  45. +               -- Merge with Object's ego merge rules.
  46. +        table.ruleMergeAppendAdd(e, ego, self.object_class.ego_rules)
  47.                 e.name = newname
  48.                 e.egoed = true
  49.             end
  50. diff --git a/game/engines/default/engine/utils.lua b/game/engines/default/engine/utils.lua
  51. index 4b633ab..dbaeca9 100644
  52. --- a/game/engines/default/engine/utils.lua
  53. +++ b/game/engines/default/engine/utils.lua
  54. @@ -173,6 +173,102 @@ function table.merge(dst, src, deep, k_skip, k_skip_deep, addnumbers)
  55.     return dst
  56.  end
  57.  
  58. +
  59. +table.rules = {}
  60. +
  61. +-- Turns any rule names into the actual rules. rules should be an
  62. +-- integer-key only list of functions and rule names. rule_defs is a
  63. +-- lookup table for rule definitions, defaulting to table.rules.
  64. +function table.parseRules(rules, rule_defs)
  65. +  if rules.__parsed then return end
  66. +  rule_defs = rule_defs or table.rules
  67. +  for i, r in ipairs(rules) do
  68. +    if type(r) == 'string' then
  69. +      rules[i] = rule_defs[r]
  70. +    end
  71. +  end
  72. +  rules.__parsed = true
  73. +end
  74. +
  75. +--[[
  76. +Applies a series of rules to a pair of tables. rules should be a list of functions
  77. +to be applied, in order, until one returns true.
  78. +
  79. +All keys in the table src are looped through, starting with array
  80. +indices, and then the rest of the keys. The rules are given the
  81. +following arguments:
  82. +The dst table's value for the current key.
  83. +The src table's value for the current key.
  84. +The current key.
  85. +The dst table.
  86. +The src table.
  87. +The list of rules.
  88. +A state table which the rules are free to modify.
  89. +--]]
  90. +function table.applyRules(dst, src, rules, state)
  91. +  if not dst or not src then return end
  92. +  state = state or {}
  93. +  local used_keys = {}
  94. +  -- First loop through with ipairs so we get the numbers in order.
  95. +  for k, v in ipairs(src) do
  96. +    for _, rule in ipairs(rules) do
  97. +      if rule(dst[k], src[k], k, dst, src, rules, state) then
  98. +        used_keys[k] = true
  99. +        break
  100. +      end
  101. +    end
  102. +  end
  103. +  -- Then loop through with pairs, skipping the ones we got with ipairs.
  104. +  for k, v in pairs(src) do
  105. +    if not used_keys[k] then
  106. +      for _, rule in ipairs(rules) do
  107. +        if rule(dst[k], src[k], k, dst, src, rules, state) then
  108. +          break
  109. +        end
  110. +      end
  111. +    end
  112. +  end
  113. +end
  114. +
  115. +-- Simply overwrites the value.
  116. +table.rules.overwrite = function(dvalue, svalue, key, dst)
  117. +  dst[key] = svalue
  118. +  return true
  119. +end
  120. +-- Does the recursion.
  121. +table.rules.recurse = function(dvalue, svalue, key, dst, src, rules, state)
  122. +  if type(dvalue) ~= 'table' or type(svalue) ~= 'table' then return end
  123. +  state = table.clone(state)
  124. +  state.path = table.clone(state.path) or {}
  125. +  table.insert(state.path, key)
  126. +  table.applyRules(dvalue, svalue, rules, state)
  127. +  return true
  128. +end
  129. +-- Appends indices.
  130. +table.rules.append = function(dvalue, svalue, key, dst, src, rules, state)
  131. +  if type(key) ~= 'number' then return end
  132. +  table.insert(dst, svalue)
  133. +  return true
  134. +end
  135. +-- Adds numbers
  136. +table.rules.add = function(dvalue, svalue, key, dst)
  137. +  if type(dvalue) ~= 'number' or type(svalue) ~= 'number' then return end
  138. +  dst[key] = dvalue + svalue
  139. +  return true
  140. +end
  141. +
  142. +--[[
  143. +A convenience method for merging tables, appending numeric indices,
  144. +and adding number values in addition to other rules.
  145. +--]]
  146. +function table.ruleMergeAppendAdd(dst, src, rules)
  147. +  rules = table.clone(rules)
  148. +  for _, rule in pairs {'append', 'recurse', 'add', 'overwrite'} do
  149. +    table.insert(rules, table.rules[rule])
  150. +  end
  151. +  table.applyRules(dst, src, rules)
  152. +end
  153. +
  154.  function table.mergeAppendArray(dst, src, deep, k_skip, k_skip_deep, addnumbers)
  155.     -- Append the array part
  156.     k_skip = k_skip or {}
  157. diff --git a/game/modules/tome/class/Object.lua b/game/modules/tome/class/Object.lua
  158. index ea4fc1d..9dc4562 100644
  159. --- a/game/modules/tome/class/Object.lua
  160. +++ b/game/modules/tome/class/Object.lua
  161. @@ -605,23 +605,43 @@ function _M:getTextualDesc(compare_with, use_actor)
  162.             desc:add(found and {"color","WHITE"} or {"color","GREEN"}, "Special effect when this weapon hits: "..special, {"color","LAST"}, true)
  163.         end
  164.  
  165. -       special = ""
  166. -       if combat.special_on_crit then
  167. -           special = combat.special_on_crit.desc
  168. -       end
  169. -       found = false
  170. -       for i, v in ipairs(compare_with or {}) do
  171. -           if v[field] and v[field].special_on_crit then
  172. -               if special ~= v[field].special_on_crit.desc then
  173. -                   desc:add({"color","RED"}, "Special effect when this weapon crits: "..v[field].special_on_crit.desc, {"color","LAST"}, true)
  174. -               else
  175. -                   found = true
  176. -               end
  177. -           end
  178. -       end
  179. -       if special ~= "" then
  180. -           desc:add(found and {"color","WHITE"} or {"color","GREEN"}, "Special effect when this weapon crits: "..special, {"color","LAST"}, true)
  181. -       end
  182. +    -- Special on crit
  183. +    local have_list = combat.special_on_crit or {}
  184. +    if have_list and have_list.desc then have_list = {have_list} end
  185. +    local lose_list = {}
  186. +    for i, v in pairs(compare_with or {}) do
  187. +      if v[field] and v[field].special_on_crit then
  188. +        lose_list = v[field].special_on_crit
  189. +        break
  190. +      end
  191. +    end
  192. +    if lose_list and lose_list.desc then lose_list = {lose_list} end
  193. +    local have_descs, lose_descs = {}, {}
  194. +    for i, v in ipairs(have_list) do
  195. +      have_descs[v.desc] = true
  196. +    end
  197. +    for i, v in ipairs(lose_list) do
  198. +      lose_descs[v.desc] = true
  199. +    end
  200. +    -- kept specials
  201. +    for i, v in ipairs(lose_list) do
  202. +      if have_descs[v.desc] then
  203. +        desc:add({"color","WHITE"}, "Special effect when this weapon crits: "..v.desc, {"color","LAST"}, true)
  204. +      end
  205. +    end
  206. +    -- gained specials
  207. +    for i, v in ipairs(have_list) do
  208. +      if not lose_descs[v.desc] then
  209. +        desc:add({"color","GREEN"}, "Special effect when this weapon crits: "..v.desc, {"color","LAST"}, true)
  210. +      end
  211. +    end
  212. +    -- lost specials
  213. +    for i, v in ipairs(lose_list) do
  214. +      if not have_descs[v.desc] then
  215. +        desc:add({"color","RED"}, "Special effect when this weapon crits: "..v.desc, {"color","LAST"}, true)
  216. +      end
  217. +    end
  218. +
  219.  
  220.         local special = ""
  221.         if combat.special_on_kill then
  222. @@ -1672,3 +1692,22 @@ function _M:addedToLevel(level, x, y)
  223.         self.__price_level_mod = util.getval(level.data.objects_cost_modifier, self)
  224.     end
  225.  end
  226. +
  227. +-- Merge special_on_crit values.
  228. +_M:addEgoRule(
  229. +  function(dvalue, svalue, key, dst, src, rules, state)
  230. +    -- Only work on the special_on_crit key.
  231. +    if key ~= 'special_on_crit' then return end
  232. +    -- If the special isn't a table, make it an empty one.
  233. +    if type(dvalue) ~= 'table' then dvalue = {} end
  234. +    if type(svalue) ~= 'table' then svalue = {} end
  235. +    -- If the special is a single special, wrap it to allow multiple.
  236. +    if dvalue.fct then dvalue = {dvalue} end
  237. +    if svalue.fct then svalue = {svalue} end
  238. +    -- Save changes to the specials.
  239. +    dst[key] = dvalue
  240. +    src[key] = svalue
  241. +    -- Return false so we can let the normal table merge take care of
  242. +    -- the rest.
  243. +    return false
  244. +end)
  245. diff --git a/game/modules/tome/class/interface/Combat.lua b/game/modules/tome/class/interface/Combat.lua
  246. index d0ee9fa..36b4a83 100644
  247. --- a/game/modules/tome/class/interface/Combat.lua
  248. +++ b/game/modules/tome/class/interface/Combat.lua
  249. @@ -751,9 +751,15 @@ function _M:attackTargetWith(target, weapon, damtype, mult, force_dam)
  250.         weapon.special_on_hit.fct(weapon, self, target, dam)
  251.     end
  252.  
  253. -   if hitted and crit and weapon and weapon.special_on_crit and weapon.special_on_crit.fct and (not target.dead or weapon.special_on_crit.on_kill) then
  254. -       weapon.special_on_crit.fct(weapon, self, target, dam)
  255. -   end
  256. +   if hitted and crit and weapon and weapon.special_on_crit then
  257. +    local specials = weapon.special_on_crit
  258. +    if specials.fct then specials = {specials} end
  259. +    for _, special in ipairs(specials) do
  260. +      if special.fct and (not target.dead or special.on_kill) then
  261. +        special.fct(weapon, self, target, dam)
  262. +      end
  263. +    end
  264. +  end
  265.  
  266.     if hitted and weapon and weapon.special_on_kill and weapon.special_on_kill.fct and target.dead then
  267.         weapon.special_on_kill.fct(weapon, self, target, dam)
  268. --
  269. 1.8.3.1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement