base.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. local function base_overview(pos)
  2. local meta = core.get_meta(pos)
  3. local health = meta:get_int('health')
  4. local scale = (health/500) * 3
  5. local formspec =
  6. kobo.building_inv..
  7. 'textarea[3.25,0;7.5,1;;;Town Center]'..
  8. 'image[0,0;3,3;kobo_base_snap.png]'..
  9. 'image_button[3.25,2;1,1;kobo_lumberjack_ico.png;lumberjack;;true;false]'..
  10. 'tooltip[lumberjack;Create a lumberjack (25 lumber, 25 food);black;white]'..
  11. 'image_button[4.5,2;1,1;kobo_forager_ico.png;forager;;true;false]'..
  12. 'tooltip[forager;Create a forager (5 lumber, 25 food);black;white]'..
  13. 'image_button[5.75,2;1,1;kobo_miner_ico.png;miner;;true;false]'..
  14. 'tooltip[miner;Create a miner (15 lumber, 15 stone, 50 food);black;white]'..
  15. 'image_button[7,2;1,1;kobo_courier_ico.png;courier;;true;false]'..
  16. 'tooltip[courier;Create a courier (15 lumber, 5 metal, 50 food);black;white]'..
  17. 'image_button[8.25,2;1,1;kobo_repairman_ico.png;repairman;;true;false]'..
  18. 'tooltip[repairman;Create a repairman (15 lumber, 15 metal, 25 food);black;white]'..
  19. 'image[0,3.75;'..scale..',.25;kobo_health_fg.png]'
  20. return formspec
  21. end
  22. local function base_overview_wrapper(player, pos)
  23. local refresh = kobo.refresh[player]
  24. if refresh then
  25. core.show_formspec(player, 'kobo:base', base_overview(pos))
  26. core.after(.25, function()
  27. base_overview_wrapper(player, pos)
  28. end)
  29. end
  30. end
  31. local function base(player)
  32. local formspec =
  33. kobo.building_inv..
  34. 'textarea[3.25,0;7.5,1;;;'..player..'\'s Town Center]'..
  35. 'image[0,0;3,3;kobo_base_snap.png]'
  36. return formspec
  37. end
  38. local function game_over()
  39. local resources = kobo.goblins
  40. local formspec =
  41. 'formspec_version[7]'..
  42. 'size[12,8]'..
  43. 'background[-1,-1;14,10;kobo_defeat_screen.png]'..
  44. 'style_type[textarea;textcolor=black;font_size=+10]'..
  45. 'textarea[0,.125;12,8;;; Aww, too bad, you lose. The goal is to NOT let the goblins destroy your base, maybe that wasn\'t made clear in the instructions. If so my apologies.\n'..
  46. 'You survived '..resources.wave..' waves of goblins, doubtless you\'ll do better next time.]'
  47. return formspec
  48. end
  49. local base_col = {
  50. type = 'fixed',
  51. fixed = {{-.5, -.5, -.5, 1.5, -.4375, 1.5}}
  52. }
  53. local base_sel = {
  54. type = 'fixed',
  55. fixed = {{-.5, -.5, -.5, 1.5, -.4375, 1.5},
  56. {.5, -.5, .5, 1.5, 1, 1.5}}
  57. }
  58. core.register_node('kobo:base', {
  59. description = 'Town Center',
  60. drawtype = 'mesh',
  61. mesh = 'kobo_base.obj',
  62. tiles = {'kobo_base.png'},
  63. paramtype = 'light',
  64. paramtype2 = 'facedir',
  65. range = 15,
  66. selection_box = base_sel,
  67. collision_box = base_col,
  68. groups = {storage=1, player_building=1},
  69. _snapshot = 'kobo_base_snap.png',
  70. _cost = {1000, 1000, 1000, 1000},
  71. _health = 500,
  72. _max_inv = 'No Max',
  73. _long_desc = 'Your central storage building. You can create villagers here, and they will drop off resources here.',
  74. after_place_node = function(pos, placer, itemstack)
  75. local player_name = placer:get_player_name()
  76. if kobo.space_for_building(pos) then
  77. local pos_check = vector.subtract(pos, {x=0, y=1, z=0})
  78. local node_check = core.get_node(pos_check)
  79. if core.get_item_group(node_check.name, 'ground') > 1 then
  80. local meta = core.get_meta(pos)
  81. meta:set_string('owner', player_name)
  82. meta:set_string('infotext', player_name..'\'s base')
  83. meta:set_int('health', 500)
  84. kobo.place_lights(pos)
  85. kobo.bases[player_name] = core.pos_to_string(pos)
  86. else
  87. core.chat_send_player(player_name, 'Place your base on grassy ground.')
  88. core.remove_node(pos)
  89. return true
  90. end
  91. else
  92. core.chat_send_player(player_name, 'There is something in the way.')
  93. core.remove_node(pos)
  94. return itemstack
  95. end
  96. end,
  97. on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  98. local player_name = clicker:get_player_name()
  99. local meta = core.get_meta(pos)
  100. local owner = meta:get_string('owner')
  101. if owner == player_name or core.check_player_privs(clicker, {server = true}) then
  102. local player_inv = clicker:get_inventory()
  103. local items = player_inv:get_stack('hand', 1)
  104. local item_name = items:get_name()
  105. local item_count = items:get_count()
  106. local resources = kobo.resources[player_name] or {}
  107. if item_name == 'kobo:lumber' then
  108. resources.lumber = resources.lumber + item_count
  109. elseif item_name == 'kobo:stone' then
  110. resources.stone = resources.stone + item_count
  111. elseif item_name == 'kobo:food' then
  112. resources.food = resources.food + item_count
  113. elseif item_name == 'kobo:metal' then
  114. resources.metal = resources.metal + item_count
  115. end
  116. player_inv:set_list('hand', {})
  117. kobo.refresh[player_name] = true
  118. base_overview_wrapper(player_name, pos)
  119. kobo.hud_refresh(clicker)
  120. kobo.player_pos[player_name] = pos
  121. kobo.save()
  122. else
  123. core.show_formspec(player_name, 'kobo:base', base(player_name))
  124. end
  125. end,
  126. on_destruct = function(pos)
  127. local meta = core.get_meta(pos)
  128. local owner = meta:get_string('owner')
  129. core.show_formspec(owner, 'kobo:game_over', game_over())
  130. end,
  131. after_destruct = function(pos, oldnode)
  132. kobo.remove_lights(pos)
  133. kobo.remove_building_spaces(pos, oldnode)
  134. end,
  135. })
  136. core.register_on_player_receive_fields(function(player, formname, fields)
  137. local name = player:get_player_name()
  138. if fields.quit then
  139. kobo.refresh[name] = false
  140. end
  141. if formname == 'kobo:base' then
  142. if fields.lumberjack then
  143. local resources = kobo.resources[name] or {}
  144. if resources.lumber >= 25 and resources.food >= 25 then
  145. local pos = kobo.player_pos[name]
  146. local home_pos = vector.add(pos, {x=0, y=1, z=0})
  147. local lumberjack = minetest.add_entity(pos, 'kobo:lumberjack')
  148. local lumberjack_entity = lumberjack:get_luaentity()
  149. lumberjack_entity.owner = name
  150. lumberjack_entity.home = home_pos
  151. lumberjack_entity.state = 'idle'
  152. lumberjack_entity.inv = 0
  153. resources.lumber = resources.lumber - 25
  154. resources.food = resources.food - 25
  155. kobo.hud_refresh(player)
  156. else
  157. core.chat_send_player(name, 'You need more resources.')
  158. end
  159. elseif fields.forager then
  160. local resources = kobo.resources[name] or {}
  161. if resources.lumber >= 25 and resources.food >= 25 then
  162. local pos = kobo.player_pos[name]
  163. local home_pos = vector.add(pos, {x=0, y=1, z=0})
  164. local forager = minetest.add_entity(pos, 'kobo:forager')
  165. local forager_entity = forager:get_luaentity()
  166. forager_entity.owner = name
  167. forager_entity.home = home_pos
  168. forager_entity.state = 'idle'
  169. forager_entity.inv = 0
  170. resources.lumber = resources.lumber - 5
  171. resources.food = resources.food - 25
  172. kobo.hud_refresh(player)
  173. else
  174. core.chat_send_player(name, 'You need more resources.')
  175. end
  176. elseif fields.miner then
  177. local resources = kobo.resources[name] or {}
  178. if resources.lumber >= 15 and resources.food >= 50 and resources.stone >= 15 then
  179. local pos = kobo.player_pos[name]
  180. local home_pos = vector.add(pos, {x=0, y=1, z=0})
  181. local miner = minetest.add_entity(pos, 'kobo:miner')
  182. local miner_entity = miner:get_luaentity()
  183. miner_entity.owner = name
  184. miner_entity.home = home_pos
  185. miner_entity.state = 'idle'
  186. miner_entity.inv = 0
  187. miner_entity.mat = ''
  188. resources.lumber = resources.lumber - 15
  189. resources.food = resources.food - 50
  190. resources.stone = resources.stone - 15
  191. kobo.hud_refresh(player)
  192. else
  193. core.chat_send_player(name, 'You need more resources.')
  194. end
  195. elseif fields.courier then
  196. local resources = kobo.resources[name] or {}
  197. if resources.lumber >= 15 and resources.food >= 50 and resources.metal >= 5 then
  198. local pos = kobo.player_pos[name]
  199. local home_pos = vector.add(pos, {x=0, y=1, z=0})
  200. local courier = minetest.add_entity(pos, 'kobo:courier')
  201. local courier_entity = courier:get_luaentity()
  202. courier_entity.owner = name
  203. courier_entity.home = home_pos
  204. courier_entity.state = 'idle'
  205. courier_entity.inv = 0
  206. courier_entity.mat = ''
  207. resources.lumber = resources.lumber - 15
  208. resources.food = resources.food - 50
  209. resources.metal = resources.metal - 5
  210. kobo.hud_refresh(player)
  211. else
  212. core.chat_send_player(name, 'You need more resources.')
  213. end
  214. elseif fields.repairman then
  215. local resources = kobo.resources[name] or {}
  216. if resources.lumber >= 15 and resources.food >= 25 and resources.metal >= 15 then
  217. local pos = kobo.player_pos[name]
  218. local home_pos = vector.add(pos, {x=0, y=1, z=0})
  219. local repairman = minetest.add_entity(pos, 'kobo:repairman')
  220. local repairman_entity = repairman:get_luaentity()
  221. repairman_entity.owner = name
  222. repairman_entity.home = home_pos
  223. repairman_entity.state = 'idle'
  224. resources.lumber = resources.lumber - 15
  225. resources.food = resources.food - 25
  226. resources.metal = resources.metal - 15
  227. kobo.hud_refresh(player)
  228. else
  229. core.chat_send_player(name, 'You need more resources.')
  230. end
  231. elseif fields.remove then
  232. local pos = kobo.player_pos[name]
  233. -- core.remove_node(pos)
  234. core.chat_send_player(name, 'You can\'t do that right now.')
  235. kobo.refresh[name] = false
  236. core.close_formspec(name, 'kobo:base')
  237. elseif fields.repair then
  238. local pos = kobo.player_pos[name]
  239. local new_pos = vector.add(pos, {x=0, y=1, z=0})
  240. core.set_node(new_pos, {name='kobo:repair'})
  241. core.chat_send_player(name, 'Summonend a repairman.')
  242. elseif fields.quit then
  243. kobo.refresh[name] = false
  244. end
  245. end
  246. end)