goblin_base.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. local function base()
  2. local resources = kobo.goblins
  3. local formspec =
  4. 'formspec_version[7]'..
  5. 'size[12,8]'..
  6. 'background[-1,-1;14,10;lobby_button_bg.png]'..
  7. 'label[4.5,1;This is a Goblin base.]'..
  8. 'label[1,1;Lumber: '..resources.lumber..']'..
  9. 'label[1,1.5;Food: '..resources.food..']'..
  10. 'label[1,2;Stone: '..resources.stone..']'..
  11. 'label[1,2.5;Metal: '..resources.metal..']'..
  12. 'label[1,3;XP: '..resources.xp..']'..
  13. 'label[1,3.5;Wave: '..resources.wave..']'..
  14. 'button[4,5;3,1;rogue;Spawn Rogue]'..
  15. 'button_exit[4,4;4,1;exit;Okay]'
  16. return formspec
  17. end
  18. local base_box = {
  19. type = 'fixed',
  20. fixed = {{-.5, -.5, -.5, 1.5, -.4375, 1.5}}
  21. }
  22. core.register_node('kobo:goblin_base', {
  23. description = 'Goblin Base',
  24. drawtype = 'mesh',
  25. mesh = 'kobo_goblin_base.obj',
  26. tiles = {'kobo_goblin_base.png'},
  27. paramtype = 'light',
  28. paramtype2 = 'facedir',
  29. selection_box = base_box,
  30. collision_box = base_box,
  31. groups = {goblin=1},
  32. on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  33. if core.check_player_privs(clicker, {server = true}) then
  34. local resources = kobo.goblins
  35. local player_name = clicker:get_player_name()
  36. core.show_formspec(player_name, 'kobo:goblin_base', base())
  37. kobo.player_pos[player_name] = pos
  38. end
  39. end,
  40. on_timer = function(pos)
  41. local timer = core.get_node_timer(pos)
  42. local resources = kobo.goblins
  43. local food = resources.food / 25
  44. local lumber = resources.lumber / 5
  45. local spawn_count = 1
  46. if food > lumber then
  47. spawn_count = lumber
  48. else
  49. spawn_count = food
  50. end
  51. spawn_count = math.max(math.floor(spawn_count), 1)
  52. local home_pos = vector.add(pos, {x=0, y=1, z=0})
  53. for i = 1, spawn_count do
  54. local goblin_scout = minetest.add_entity(pos, 'kobo:goblin_scout')
  55. local goblin_scout_entity = goblin_scout:get_luaentity()
  56. goblin_scout_entity.state = 'idle'
  57. goblin_scout_entity.inv = 0
  58. goblin_scout_entity.home = home_pos
  59. goblin_scout_entity.health = 25
  60. end
  61. resources.xp = resources.xp + spawn_count
  62. resources.wave = resources.wave + 1
  63. resources.food = resources.food - (25 * spawn_count)
  64. resources.lumber = resources.lumber - (5 * spawn_count)
  65. if resources.wave % 9 == 0 then --Every nth goblins send a wave.
  66. local count = resources.wave / 9
  67. for i = 1, count do
  68. local goblin_rogue = minetest.add_entity(pos, 'kobo:goblin_rogue')
  69. local goblin_rogue_entity = goblin_rogue:get_luaentity()
  70. goblin_rogue_entity.state = 'idle'
  71. goblin_rogue_entity.last_job = pos
  72. goblin_rogue_entity.health = 40
  73. end
  74. end
  75. timer:start(60)
  76. end,
  77. })
  78. core.register_on_player_receive_fields(function(player, formname, fields)
  79. if formname == 'kobo:goblin_base' then
  80. if fields.rogue then
  81. local name = player:get_player_name()
  82. local pos = kobo.player_pos[name]
  83. local goblin_rogue = minetest.add_entity(pos, 'kobo:goblin_rogue')
  84. local goblin_rogue_entity = goblin_rogue:get_luaentity()
  85. goblin_rogue_entity.state = 'idle'
  86. goblin_rogue_entity.last_job = pos
  87. goblin_rogue_entity.health = 40
  88. end
  89. end
  90. end)
  91. core.register_decoration({
  92. deco_type = 'simple',
  93. place_on = {'kobo:dirth'},
  94. sidelen = 16,
  95. fill_ratio = 0.00005,
  96. y_min = -1,
  97. y_max = 2,
  98. decoration = 'kobo:goblin_base',
  99. param2 = 0,
  100. param2_max = 3,
  101. biomes = {'wasteland'},
  102. spawn_by = 'kobo:dirth',
  103. num_spawn_by = 8
  104. })
  105. core.register_lbm({
  106. label = 'Start Goblin timer',
  107. name = 'kobo:goblin',
  108. nodenames = {'kobo:goblin_base'},
  109. run_at_every_load = true,
  110. action = function(pos, node)
  111. local timer = core.get_node_timer(pos)
  112. timer:start(60)
  113. end
  114. })
  115. core.register_abm({ --Random Goblins that run around and cause mischief.
  116. label = 'Rogue Goblin Spawning',
  117. name = 'kobo:goblin_spawn',
  118. nodenames = {'kobo:dirth'},
  119. interval = 300,
  120. chance = 250,
  121. action = function(pos)
  122. local spawn_pos = vector.add(pos, {x=0, y=1, z=0})
  123. if core.get_node(spawn_pos).name == 'air' then
  124. if core.get_node_light(spawn_pos) < 5 then
  125. local goblin_rogue = minetest.add_entity(spawn_pos, 'kobo:goblin_rogue')
  126. local goblin_rogue_entity = goblin_rogue:get_luaentity()
  127. goblin_rogue_entity.state = 'idle'
  128. goblin_rogue_entity.last_job = spawn_pos
  129. goblin_rogue_entity.health = 40
  130. end
  131. end
  132. end
  133. })