towers.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. local function tower_overview(pos)
  2. local meta = core.get_meta(pos)
  3. local health = meta:get_int('health')
  4. local scale = (health/200) * 3
  5. local formspec =
  6. kobo.building_inv..
  7. 'textarea[3.25,0;7.5,1;;;Achery Tower]'..
  8. 'image[0,0;3,3;kobo_tower_snap.png]'..
  9. 'textarea[3.25,.75;7.5,2.25;;;Slaughter the incoming hordes of puny goblins with this death machine.]'..
  10. 'image[0,3.75;'..scale..',.25;kobo_health_fg.png]'
  11. return formspec
  12. end
  13. local function tower_overview_wrapper(player, pos)
  14. local refresh = kobo.refresh[player]
  15. if refresh then
  16. core.show_formspec(player, 'kobo:tower', tower_overview(pos))
  17. core.after(.25, function()
  18. tower_overview_wrapper(player, pos)
  19. end)
  20. end
  21. end
  22. local tower_col = {
  23. type = 'fixed',
  24. fixed = {{-.4375, -.5, -.4375, .4375, -.3125, .4375}}
  25. }
  26. local tower_sel = {
  27. type = 'fixed',
  28. fixed = {{-.4375, -.5, -.4375, .4375, 1.25, .4375}}
  29. }
  30. core.register_node('kobo:tower', {
  31. description = 'Archery Tower',
  32. drawtype = 'mesh',
  33. mesh = 'kobo_tower.obj',
  34. tiles = {'kobo_tower.png'},
  35. paramtype = 'light',
  36. range = 15,
  37. selection_box = tower_sel,
  38. collision_box = tower_col,
  39. groups = {player_building=1},
  40. _snapshot = 'kobo_tower_snap.png',
  41. _cost = {0, 50, 0, 100},
  42. _health = 200,
  43. _max_inv = 0,
  44. _long_desc = 'Slaughter the incoming hordes of puny goblins with this death machine.',
  45. after_place_node = function(pos, placer)
  46. local pos_check = vector.subtract(pos, {x=0, y=1, z=0})
  47. local node_check = core.get_node(pos_check)
  48. local player_name = placer:get_player_name()
  49. if core.get_item_group(node_check.name, 'ground') > 0 then
  50. local timer = core.get_node_timer(pos)
  51. local meta = core.get_meta(pos)
  52. local resources = kobo.resources[player_name]
  53. meta:set_string('owner', player_name)
  54. meta:set_int('health', 200)
  55. kobo.place_lights(pos)
  56. resources.xp = resources.xp + 1
  57. timer:start(1)
  58. else
  59. core.chat_send_player(player_name, 'You can\'t place this here.')
  60. core.remove_node(pos)
  61. return true
  62. end
  63. end,
  64. after_destruct = function(pos)
  65. kobo.remove_lights(pos)
  66. end,
  67. on_rightclick = function(pos, node, clicker)
  68. local player_name = clicker:get_player_name()
  69. local meta = core.get_meta(pos)
  70. local owner = meta:get_string('owner')
  71. if owner == player_name or core.check_player_privs(clicker, {server = true}) then
  72. kobo.player_pos[player_name] = pos
  73. kobo.refresh[player_name] = true
  74. tower_overview_wrapper(player_name, pos)
  75. kobo.save()
  76. end
  77. end,
  78. on_timer = function(pos)
  79. local timer = core.get_node_timer(pos)
  80. local meta = core.get_meta(pos)
  81. local owner = meta:get_string('owner')
  82. local count = 0
  83. for obj in core.objects_inside_radius(pos, 5) do
  84. if count >= 5 then break end
  85. local entity = obj:get_luaentity()
  86. if entity then
  87. if not core.is_player(obj) then
  88. if entity.name ~= 'kobo:goblin_tombstone' then
  89. if entity.owner ~= owner then
  90. entity.health = entity.health - 1 or 24
  91. count = count + 1
  92. if entity.health <= 0 then
  93. local resources = kobo.resources[owner]
  94. if entity.name == 'kobo:goblin_rogue' then
  95. resources.xp = resources.xp + 5
  96. resources.metal = resources.metal + 1
  97. entity.object:remove()
  98. else
  99. entity.object:remove()
  100. resources.xp = resources.xp + 1
  101. end
  102. end
  103. end
  104. end
  105. end
  106. end
  107. end
  108. --object:punch(puncher, time_from_last_punch, tool_capabilities, direction)
  109. timer:start(1)
  110. end,
  111. })
  112. core.register_on_player_receive_fields(function(player, formname, fields)
  113. if formname == 'kobo:tower' then
  114. local name = player:get_player_name()
  115. if fields.remove then
  116. local resources = kobo.resources[name] or {}
  117. resources.lumber = resources.lumber + 37
  118. resources.stone = resources.stone + 75
  119. local pos = kobo.player_pos[name]
  120. core.remove_node(pos)
  121. kobo.refresh[name] = false
  122. kobo.hud_refresh(player)
  123. core.close_formspec(name, 'kobo:tower')
  124. elseif fields.repair then
  125. local pos = kobo.player_pos[name]
  126. local new_pos = vector.add(pos, {x=0, y=1, z=0})
  127. core.set_node(new_pos, {name='kobo:repair'})
  128. core.chat_send_player(name, 'Summonend a repairman.')
  129. end
  130. end
  131. end)