Guest User

Untitled

a guest
May 10th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.59 KB | None | 0 0
  1. ACTIONVAR_USER = 1
  2. ACTIONVAR_OBJ1 = 2
  3. ACTIONVAR_OBJ2 = 3
  4. ACTIONBAR_TEMP = 4
  5. ACTIONVAR_USERPOS = 5
  6. ACTIONVAR_OBJ1POS = 6
  7. ACTIONVAR_OBJ2POS = 7
  8. ACTIONVAR_OBJ2TYPE = 8
  9. ACTIONVAR_OBJ1ID = 9
  10.  
  11. ActionSystem = {
  12.     actionsId = {
  13.         [ACTIONVAR_OBJ1] = {},
  14.     },
  15.     actionsAId = {
  16.         [ACTIONVAR_OBJ1] = {},
  17.     },
  18.     actionsUid = {
  19.         [ACTIONVAR_OBJ1] = {},
  20.     },
  21.     b = 100,
  22. }
  23.  
  24. function ActionSystem:executeConditions(conditionList, vars, fromPosition, isHotkey)
  25.     if #conditionList == 0 then
  26.         return true
  27.     end
  28.    
  29.     for _, func in ipairs(conditionList) do
  30.         if not func(vars, fromPosition, isHotkey) then
  31.             return false
  32.         end
  33.     end
  34.    
  35.     return true
  36. end
  37.  
  38. function ActionSystem:executeActions(actionList, vars, fromPosition, isHotkey)
  39.     for _, func in ipairs(actionList) do
  40.         if not func(vars, fromPosition, isHotkey) then
  41.             return false
  42.         end
  43.     end
  44.    
  45.     return true
  46. end
  47.  
  48. function ActionSystem:doActionList(list, vars, fromPosition, isHotkey)
  49.     for _, data in ipairs(list) do
  50.         if self:executeConditions(data.conditions, vars, fromPosition, isHotkey) then
  51.             if self:executeActions(data.actions, vars, fromPosition, isHotkey) then
  52.                 return true
  53.             end
  54.         end    
  55.     end
  56.    
  57.     return false
  58. end
  59.  
  60. function ActionSystem:getActions(whichObjectId, list, id)
  61.     return list[whichObjectId][id]
  62. end
  63.  
  64. function ActionSystem:onUseEventCallback(player, item, playerPosition, itemPosition, fromPosition, target, toPosition, isHotkey)
  65.     local vars = {
  66.         [ACTIONVAR_USER] = player,
  67.         [ACTIONVAR_OBJ1] = item,
  68.         [ACTIONVAR_OBJ1POS] = itemPosition,
  69.     }
  70.    
  71.     local itemId = item:getId()
  72.     local actionId = item:getActionId()
  73.     local uniqueId = item:getAttribute(ITEM_ATTRIBUTE_UNIQUEID)
  74.    
  75.     vars[ACTIONVAR_OBJ1ID] = itemId
  76.    
  77.     if uniqueId > 0 then
  78.         local list = self:getActions(ACTIONVAR_OBJ1, self.ActionsUid, uniqueId)
  79.         if list then
  80.             if self:doActionList(list, vars, fromPosition, isHotkey) then
  81.                 return true
  82.             end
  83.         end
  84.     end
  85.    
  86.     if actionId > 0 then
  87.         local list = self:getActions(ACTIONVAR_OBJ1, self.actionsAId, actionId)
  88.         if list then
  89.             if self:doActionList(list, vars, fromPosition, isHotkey) then
  90.                 return true
  91.             end
  92.         end
  93.     end
  94.    
  95.     local list = self:getActions(ACTIONVAR_OBJ1, self.actionsId, itemId)
  96.     if list then
  97.         if self:doActionList(list, vars, fromPosition, isHotkey) then
  98.             return true
  99.         end
  100.     end
  101.    
  102.     return false
  103. end
  104.  
  105. function IsType(var, id)
  106.     return function()
  107.         return var, id
  108.     end
  109. end
  110.  
  111. function IsPosition(target, x, y, z)
  112.     local p = Position(x, y, z)
  113.     return function(vars, fromPosition, isHotkey)
  114.         local posVar
  115.         if target == ACTIONVAR_OBJ1 then
  116.             posVar = ACTIONVAR_OBJ1POS
  117.         end
  118.  
  119.         return (vars[ACTIONVAR_OBJ1POS] == p)
  120.     end
  121. end
  122.  
  123. function Effect(target, effect)
  124.     return function(vars, fromPosition, isHotkey)
  125.         vars[ACTIONVAR_OBJ1POS]:sendMagicEffect(effect)
  126.         return true
  127.     end
  128. end
  129.  
  130.  
  131. function Use(...)
  132.     local parameters = {...}
  133.    
  134.     --first condition must be the function that defines item id/unique id/action id define
  135.     local firstCondition = parameters[1]
  136.     table.remove(parameters, 1)
  137.    
  138.     local var, choiceId = firstCondition()
  139.    
  140.     local foundActions = false
  141.    
  142.     local conditions = {}
  143.     local actions = {}
  144.    
  145.     for _, param in ipairs(parameters) do
  146.         if param == "->" then
  147.             foundActions = true
  148.         else
  149.             if foundActions then
  150.                 table.insert(actions, param)
  151.             else
  152.                 table.insert(conditions, param)
  153.             end
  154.         end
  155.     end
  156.    
  157.     local data = {conditions = conditions, actions = actions}
  158.    
  159.     if not ActionSystem.actionsId[var][choiceId] then
  160.         ActionSystem.actionsId[var][choiceId] = {}
  161.     end
  162.    
  163.     table.insert(ActionSystem.actionsId[var][choiceId], data)
  164. end
Add Comment
Please, Sign In to add comment