walls.lua 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. local function wall_overview(pos)
  2. local meta = core.get_meta(pos)
  3. local health = meta:get_int('health')
  4. local scale = (health/150) * 3
  5. local formspec =
  6. kobo.building_inv..
  7. 'textarea[3.25,0;7.5,1;;;Wooden Wall]'..
  8. 'image[0,0;3,3;kobo_wall_1_snap.png]'..
  9. 'textarea[3.25,.75;7.5,2.25;;;Basic defense to keep the goblins out.]'..
  10. 'image[0,3.75;'..scale..',.25;kobo_health_fg.png]'
  11. return formspec
  12. end
  13. local function wall_overview_wrapper(player, pos)
  14. local refresh = kobo.refresh[player]
  15. if refresh then
  16. core.show_formspec(player, 'kobo:wall_1', wall_overview(pos))
  17. core.after(.25, function()
  18. wall_overview_wrapper(player, pos)
  19. end)
  20. end
  21. end
  22. local wall_box_straight = {
  23. type = 'fixed',
  24. fixed = {{-.5, -.5, -.125, .5, 0, .125}}
  25. }
  26. local wall_box_corner = {
  27. type = 'fixed',
  28. fixed = {{-.125, -.5, -.125, .5, 0, .125},
  29. {-.125, -.5, -.5, .125, 0, -.125}}
  30. }
  31. local wall_box_t = {
  32. type = 'fixed',
  33. fixed = {{-.5, -.5, -.125, .5, 0, .125},
  34. {-.125, -.5, -.125, .125, 0, .5}}
  35. }
  36. local wall_box_x = {
  37. type = 'fixed',
  38. fixed = {{-.5, -.5, -.125, .5, 0, .125},
  39. {-.125, -.5, -.5, .125, 0, .5}}
  40. }
  41. local function update_node(pos, check_surrounding, placing)
  42. local shape = 0
  43. local pos1 = vector.add(pos, {x=1, y=0, z=0})
  44. local node1 = core.get_node(pos1)
  45. local pos2 = vector.add(pos, {x=0, y=0, z=1})
  46. local node2 = core.get_node(pos2)
  47. local pos3 = vector.add(pos, {x=-1, y=0, z=0})
  48. local node3 = core.get_node(pos3)
  49. local pos4 = vector.add(pos, {x=0, y=0, z=-1})
  50. local node4 = core.get_node(pos4)
  51. if core.get_item_group(node1.name, 'player_walls') > 0 then
  52. shape = shape + 1000
  53. if check_surrounding then
  54. update_node(pos1, false, true)
  55. end
  56. end
  57. if core.get_item_group(node2.name, 'player_walls') > 0 then
  58. shape = shape + 100
  59. if check_surrounding then
  60. update_node(pos2, false, true)
  61. end
  62. end
  63. if core.get_item_group(node3.name, 'player_walls') > 0 then
  64. shape = shape + 10
  65. if check_surrounding then
  66. update_node(pos3, false, true)
  67. end
  68. end
  69. if core.get_item_group(node4.name, 'player_walls') > 0 then
  70. shape = shape + 1
  71. if check_surrounding then
  72. update_node(pos4, false, true)
  73. end
  74. end
  75. if placing then
  76. if shape == 1001 then
  77. core.swap_node(pos, {name = 'kobo:wall_1_corner', param2 = 0})
  78. elseif shape == 11 then
  79. core.swap_node(pos, {name = 'kobo:wall_1_corner', param2 = 1})
  80. elseif shape == 1100 then
  81. core.swap_node(pos, {name = 'kobo:wall_1_corner', param2 = 3})
  82. elseif shape == 110 then
  83. core.swap_node(pos, {name = 'kobo:wall_1_corner', param2 = 2})
  84. elseif shape == 1 then
  85. core.swap_node(pos, {name = 'kobo:wall_1_straight', param2 = 1})
  86. elseif shape == 100 then
  87. core.swap_node(pos, {name = 'kobo:wall_1_straight', param2 = 1})
  88. elseif shape == 101 then
  89. core.swap_node(pos, {name = 'kobo:wall_1_straight', param2 = 1})
  90. elseif shape == 1110 then
  91. core.swap_node(pos, {name = 'kobo:wall_1_t', param2 = 0})
  92. elseif shape == 1010 then
  93. core.swap_node(pos, {name = 'kobo:wall_1_straight', param2 = 2})
  94. elseif shape == 1000 then
  95. core.swap_node(pos, {name = 'kobo:wall_1_straight', param2 = 2})
  96. elseif shape == 10 then
  97. core.swap_node(pos, {name = 'kobo:wall_1_straight', param2 = 2})
  98. elseif shape == 1101 then
  99. core.swap_node(pos, {name = 'kobo:wall_1_t', param2 = 1})
  100. elseif shape == 111 then
  101. core.swap_node(pos, {name = 'kobo:wall_1_t', param2 = 3})
  102. elseif shape == 1011 then
  103. core.swap_node(pos, {name = 'kobo:wall_1_t', param2 = 2})
  104. elseif shape == 1111 then
  105. core.swap_node(pos, {name = 'kobo:wall_1_x', param2 = 0})
  106. end
  107. end
  108. end
  109. core.register_node('kobo:wall_1_straight', {
  110. description = 'Wooden Wall',
  111. drawtype = 'mesh',
  112. mesh = 'kobo_wall_1_straight.obj',
  113. tiles = {'kobo_wall_1_straight.png'},
  114. paramtype = 'light',
  115. paramtype2 = 'facedir',
  116. range = 15,
  117. light_source = 4,
  118. selection_box = wall_box_straight,
  119. collision_box = wall_box_straight,
  120. groups = {player_building=1, player_walls=1},
  121. _snapshot = 'kobo_wall_1_snap.png',
  122. _cost = {0, 100, 0 ,0},
  123. _health = 150,
  124. _max_inv = 0,
  125. _long_desc = 'Basic defense to keep the goblins out.',
  126. after_place_node = function(pos, placer)
  127. local pos_check = vector.subtract(pos, {x=0, y=1, z=0})
  128. local node_check = core.get_node(pos_check)
  129. local player_name = placer:get_player_name()
  130. if core.get_item_group(node_check.name, 'ground') > 0 then
  131. local meta = core.get_meta(pos)
  132. meta:set_string('owner', player_name)
  133. meta:set_int('health', 150)
  134. update_node(pos, true, true)
  135. end
  136. end,
  137. on_rightclick = function(pos, node, clicker)
  138. local player_name = clicker:get_player_name()
  139. local meta = core.get_meta(pos)
  140. local owner = meta:get_string('owner')
  141. if owner == player_name or core.check_player_privs(clicker, {server = true}) then
  142. kobo.player_pos[player_name] = pos
  143. kobo.refresh[player_name] = true
  144. wall_overview_wrapper(player_name, pos)
  145. kobo.save()
  146. end
  147. end,
  148. after_destruct = function(pos)
  149. update_node(pos, true, false)
  150. core.remove_node(vector.add(pos, {x=0, y=1, z=0}))
  151. end,
  152. })
  153. core.register_node('kobo:wall_1_corner', {
  154. description = 'Wooden Wall',
  155. drawtype = 'mesh',
  156. mesh = 'kobo_wall_1_corner.obj',
  157. tiles = {'kobo_wall_1_corner.png'},
  158. paramtype = 'light',
  159. paramtype2 = 'facedir',
  160. range = 15,
  161. light_source = 4,
  162. selection_box = wall_box_corner,
  163. collision_box = wall_box_corner,
  164. groups = {player_building=1, player_walls=1},
  165. _snapshot = 'kobo_wall_1_snap.png',
  166. _cost = {0, 100, 0 ,0},
  167. _health = 150,
  168. _max_inv = 0,
  169. _long_desc = 'Basic defense to keep the goblins out.',
  170. on_rightclick = function(pos, node, clicker)
  171. local player_name = clicker:get_player_name()
  172. local meta = core.get_meta(pos)
  173. local owner = meta:get_string('owner')
  174. if owner == player_name or core.check_player_privs(clicker, {server = true}) then
  175. kobo.player_pos[player_name] = pos
  176. kobo.refresh[player_name] = true
  177. wall_overview_wrapper(player_name, pos)
  178. kobo.save()
  179. end
  180. end,
  181. after_destruct = function(pos)
  182. update_node(pos, true)
  183. end,
  184. })
  185. core.register_node('kobo:wall_1_t', {
  186. description = 'Wooden Wall',
  187. drawtype = 'mesh',
  188. mesh = 'kobo_wall_1_t.obj',
  189. tiles = {'kobo_wall_1_t.png'},
  190. paramtype = 'light',
  191. paramtype2 = 'facedir',
  192. range = 15,
  193. light_source = 4,
  194. selection_box = wall_box_t,
  195. collision_box = wall_box_t,
  196. groups = {player_building=1, player_walls=1},
  197. _snapshot = 'kobo_wall_1_snap.png',
  198. _cost = {0, 100, 0 ,0},
  199. _health = 150,
  200. _max_inv = 0,
  201. _long_desc = 'Basic defense to keep the goblins out.',
  202. on_rightclick = function(pos, node, clicker)
  203. local player_name = clicker:get_player_name()
  204. local meta = core.get_meta(pos)
  205. local owner = meta:get_string('owner')
  206. if owner == player_name or core.check_player_privs(clicker, {server = true}) then
  207. kobo.player_pos[player_name] = pos
  208. kobo.refresh[player_name] = true
  209. wall_overview_wrapper(player_name, pos)
  210. kobo.save()
  211. end
  212. end,
  213. after_destruct = function(pos)
  214. update_node(pos, true)
  215. end,
  216. })
  217. core.register_node('kobo:wall_1_x', {
  218. description = 'Wooden Wall',
  219. drawtype = 'mesh',
  220. mesh = 'kobo_wall_1_x.obj',
  221. tiles = {'kobo_wall_1_x.png'},
  222. paramtype = 'light',
  223. paramtype2 = 'facedir',
  224. range = 15,
  225. light_source = 4,
  226. selection_box = wall_box_x,
  227. collision_box = wall_box_x,
  228. groups = {player_building=1, player_walls=1},
  229. _snapshot = 'kobo_wall_1_snap.png',
  230. _cost = {0, 100, 0 ,0},
  231. _health = 150,
  232. _max_inv = 0,
  233. _long_desc = 'Basic defense to keep the goblins out.',
  234. on_rightclick = function(pos, node, clicker)
  235. local player_name = clicker:get_player_name()
  236. local meta = core.get_meta(pos)
  237. local owner = meta:get_string('owner')
  238. if owner == player_name or core.check_player_privs(clicker, {server = true}) then
  239. kobo.player_pos[player_name] = pos
  240. kobo.refresh[player_name] = true
  241. wall_overview_wrapper(player_name, pos)
  242. kobo.save()
  243. end
  244. end,
  245. after_destruct = function(pos)
  246. update_node(pos, true)
  247. end,
  248. })
  249. core.register_on_player_receive_fields(function(player, formname, fields)
  250. if formname == 'kobo:wall_1' then
  251. local name = player:get_player_name()
  252. if fields.remove then
  253. local resources = kobo.resources[name] or {}
  254. resources.lumber = resources.lumber + 75
  255. local pos = kobo.player_pos[name]
  256. core.remove_node(pos)
  257. kobo.refresh[name] = false
  258. kobo.hud_refresh(player)
  259. core.close_formspec(name, 'kobo:wall_1')
  260. elseif fields.repair then
  261. local pos = kobo.player_pos[name]
  262. local new_pos = vector.add(pos, {x=0, y=1, z=0})
  263. core.set_node(new_pos, {name='kobo:repair'})
  264. core.chat_send_player(name, 'Summonend a repairman.')
  265. end
  266. end
  267. end)