Vultraz

Untitled

Jan 16th, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.02 KB | None | 0 0
  1.         [take_item]
  2.             id=defring # wmllint: ignore
  3.             name= _ "Ring of Defense"
  4.             image=icons/ring_gold.png
  5.             description= _ "This ring will give its bearer +5% defense on all terrains."
  6.             effect_type="continuous"
  7.             [command]
  8.                 [object]
  9.                     silent=yes
  10.                     duration=forever
  11.                     {DT_RANCHORS_RING}
  12.                 [/object]
  13.             [/command]
  14.             [removal_command]
  15.                 [remove_object]
  16.                     [filter_wml]
  17.                         {DT_RANCHORS_RING}
  18.                     [/filter_wml]
  19.                 [/remove_object]
  20.             [/removal_command]
  21.         [/take_item]
  22.  
  23. --#textdomain wesnoth-Shadows_of_Deception
  24.  
  25. ---
  26. -- This brings up a dialog when a unit moves to pick up an item.
  27. -- There are 3 choices:
  28. -- * USE:   Apply the item's effect, and add to inventory if effect_type = continuous
  29. -- * TAKE:  Add item to inventory. No effect is applied
  30. -- * LEAVE: Do nothing
  31. --
  32. -- [take_item]
  33. --     id=must be unique
  34. --     name= _ "string"
  35. --     image=path/to/image.png
  36. --     description= _ "translatable string"
  37. --     effect_type=use either "single" or "continuous"
  38. --     event=the name of the event to fire if you USE or TAKE the item
  39. --     [usuable_by]
  40. --         ... SUF: The item will only be able to be used or taken if the primary
  41. --             unit matches this filter
  42. --     [/usuable_by]
  43. --     [usable_if]
  44. --         ... The item will only be usable if this condition is matched ...
  45. --     [/usable_if]
  46. --     [command]
  47. --         ... Code to execute when item is used
  48. --     [/command]
  49. --     [removal_command]
  50. --         ... Code to be executed if item is being unequiped.
  51. --             Only applies if effect_type = continuous
  52. --     [/removal_command]
  53. -- [/take_item]
  54. ---
  55.  
  56. local dialog = nxrequire "gui/dialogs/item_pickup"
  57. local buttons = dialog.buttons
  58. dialog = dialog.dialog
  59.  
  60. function wml_actions.take_item(cfg)
  61.     cfg = helper.shallow_parsed(cfg)
  62.  
  63.     local unit = wesnoth.get_units({x = wesnoth.current.event_context.x1, y = wesnoth.current.event_context.y1})[1].__cfg
  64.     local vars = helper.get_child(unit, "variables")
  65.  
  66.     local function item_preshow()
  67.         -- Set all widget starting values
  68.         wesnoth.set_dialog_value ( cfg.name, "item_name" )
  69.         wesnoth.set_dialog_value ( cfg.image or "", "image_name" )
  70.         wesnoth.set_dialog_value ( cfg.description, "item_description" )
  71.  
  72.         if cfg.effect_type == "continuous" then
  73.             wesnoth.set_dialog_value(_"Equip", "use_button")
  74.         end
  75.  
  76.         -- Disable the use button if necessary (the item cannot be uses on the specific loc or the wrong unit has it)
  77.         wesnoth.set_dialog_active(wesnoth.eval_conditional (helper.get_child(cfg, "usable_if") or {}), "use_button")
  78.  
  79.         local filter_block = helper.get_child(cfg, "usable_by")
  80.  
  81.         if not wesnoth.eval_conditional { { "have_unit", filter_block } } then
  82.             wesnoth.set_dialog_value(filter_block.cannot_use_message, "cannot_use_warning")
  83.  
  84.             wesnoth.set_dialog_active(false, "use_button")
  85.             wesnoth.set_dialog_active(false, "take_button")
  86.         end
  87.     end
  88.  
  89.     local button = wesnoth.show_dialog(dialog, item_preshow)
  90.  
  91.     local function set_item_vars(activate)
  92.         local item = helper.get_child(vars, "item", cfg.id)
  93.  
  94.         if item then
  95.             item.quantity = (item.quantity or 0) + 1
  96.         else
  97.             if activate then
  98.                 cfg.active = true
  99.             end
  100.             table.insert(vars, {"item", cfg})
  101.         end
  102.  
  103.         wesnoth.put_unit(unit)
  104.     end
  105.  
  106.     local function clean_up_item()
  107.         local event = cfg.event or cfg.id .. "_taken"
  108.         local loc_x = wesnoth.current.event_context.x1
  109.         local loc_y = wesnoth.current.event_context.y1
  110.  
  111.         wesnoth.fire_event(event, loc_x, loc_y)
  112.  
  113.         items.remove(loc_x, loc_y)
  114.     end
  115.  
  116.     if button == buttons.use or button == -1 then
  117.         if cfg.effect_type == "continuous" then
  118.             set_item_vars('and activate item')
  119.         end
  120.         wml_actions.command(helper.get_child(cfg, "command"))
  121.         clean_up_item()
  122.     end
  123.  
  124.     if button == buttons.take then
  125.         set_item_vars()
  126.         clean_up_item()
  127.     end
  128. end
Advertisement
Add Comment
Please, Sign In to add comment