miniufo.lua 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. ufos = {}
  2. local floor_pos = function(pos)
  3. return {x=math.floor(pos.x),y=math.floor(pos.y),z=math.floor(pos.z)}
  4. end
  5. local UFO_SPEED = 1
  6. local UFO_TURN_SPEED = 2
  7. local UFO_MAX_SPEED = 10
  8. local UFO_FUEL_USE = .01
  9. local fuel_from_wear = function(wear)
  10. local fuel
  11. if wear == 0 then
  12. fuel = 0
  13. else
  14. fuel = (65535-(wear-1))*100/65535
  15. end
  16. return fuel
  17. end
  18. local wear_from_fuel = function(fuel)
  19. local wear = (100-(fuel))*65535/100+1
  20. if wear > 65535 then wear = 0 end
  21. return wear
  22. end
  23. local get_fuel = function(self)
  24. return self.fuel
  25. end
  26. local set_fuel = function(self,fuel,object)
  27. self.fuel = fuel
  28. end
  29. local ufo_from_item = function(itemstack,placer,pointed_thing)
  30. -- restore the fuel inside the item
  31. local wear = itemstack:get_wear()
  32. -- add the entity
  33. e = minetest.add_entity(pointed_thing.above, "ufowreck:miniufo")
  34. local miniufo = e:get_luaentity()
  35. -- set high hp to not destroy it with guns etc
  36. e:set_hp(1000000)
  37. set_fuel(miniufo,fuel_from_wear(wear))
  38. -- remove the item
  39. itemstack:take_item()
  40. end
  41. local miniufo = {
  42. physical = true,
  43. collisionbox = {-1.5,0,-1.5, 1.5,1.2,1.5},
  44. visual = "mesh",
  45. mesh = "ufo.x",
  46. textures = {"ufo_0.png"},
  47. groups = {immortal=1},
  48. glow = 5,
  49. driver = nil,
  50. v = 0,
  51. fuel = 0,
  52. fueli = 0
  53. }
  54. function miniufo.on_rightclick(self, clicker)
  55. --minetest.chat_send_all("right_clicked!")
  56. if not clicker or not clicker:is_player() then
  57. return
  58. end
  59. local name = clicker:get_player_name()
  60. local inv = clicker:get_inventory()
  61. local wielded = clicker:get_wielded_item()
  62. if wielded:get_name() == 'default:mese_crystal' then
  63. minetest.chat_send_all('line 116')
  64. local fuel_lev = get_fuel(self)
  65. if fuel_lev < 100 then
  66. inv:remove_item('main',{name="default:mese_crystal", count=1, wear=0, metadata=""})
  67. fuel_lev = fuel_lev + 20
  68. if fuel_lev > 100 then
  69. fuel_lev = 100
  70. end
  71. set_fuel(self, fuel_lev)
  72. minetest.chat_send_all('line 124')
  73. return
  74. end
  75. end
  76. if self.driver and name == self.driver then
  77. self.driver = nil
  78. self.auto = false
  79. clicker:set_detach()
  80. player_api.player_attached[name] = false
  81. player_api.set_animation(clicker, "stand" , 30)
  82. local pos = clicker:get_pos()
  83. pos = {x = pos.x, y = pos.y + 0.2, z = pos.z}
  84. minetest.after(0.1, function()
  85. clicker:set_pos(pos)
  86. end)
  87. elseif not self.driver then
  88. local attach = clicker:get_attach()
  89. if attach and attach:get_luaentity() then
  90. local luaentity = attach:get_luaentity()
  91. if luaentity.driver then
  92. luaentity.driver = nil
  93. end
  94. clicker:set_detach()
  95. end
  96. self.driver = name
  97. clicker:set_attach(self.object, "", {x=0,y=1.0,z=0}, {x=0,y=0,z=0})
  98. player_api.player_attached[name] = true
  99. -- minetest.after(0.2, function()
  100. -- player_api.set_animation(clicker, "sit" , 30)
  101. -- end)
  102. clicker:set_look_horizontal(self.object:get_yaw())
  103. end
  104. end
  105. function miniufo:on_activate (staticdata, dtime_s)
  106. local data = staticdata:split(';')
  107. if data and data[1] and data[2] then
  108. -- self.owner_name = data[1]
  109. self.name = data[1]
  110. self.fuel = tonumber(data[2])
  111. end
  112. self.object:set_armor_groups({immortal=1})
  113. end
  114. function miniufo:on_punch(puncher, time_from_last_punch, tool_capabilities, direction)
  115. if puncher and puncher:is_player() then
  116. local inv = puncher:get_inventory()
  117. local wielded = puncher:get_wielded_item()
  118. --minetest.chat_send_all(dump(wielded))
  119. local wear = wear_from_fuel(get_fuel(self))
  120. local leftover = inv:add_item("main", {name="ufowreck:miniufo", wear = wear})
  121. if not leftover:is_empty() then
  122. minetest.add_item(self.object:get_pos(), leftover)
  123. end
  124. self.object:remove()
  125. end
  126. end
  127. function miniufo:on_step (dtime)
  128. local fuel = get_fuel(self)
  129. if self.driver then
  130. local driver_objref = minetest.get_player_by_name(self.driver)
  131. if driver_objref then
  132. local ctrl = driver_objref:get_player_control()
  133. -- local ctrl = self.driver:get_player_control()
  134. local vel = self.object:get_velocity()
  135. if fuel == nil then fuel = 0 end
  136. --if ctrl.up then fuel = 20 end
  137. if fuel > 0 and ctrl.up then
  138. vel.x = vel.x + math.cos(self.object:getyaw()+math.pi/2)*UFO_SPEED
  139. vel.z = vel.z + math.sin(self.object:getyaw()+math.pi/2)*UFO_SPEED
  140. fuel = fuel - UFO_FUEL_USE
  141. else
  142. vel.x = vel.x*.99
  143. vel.z = vel.z*.99
  144. end
  145. if ctrl.down then
  146. vel.x = vel.x*.9
  147. vel.z = vel.z*.9
  148. end
  149. if fuel > 0 and ctrl.jump then
  150. vel.y = vel.y+UFO_SPEED
  151. fuel = fuel - UFO_FUEL_USE
  152. elseif fuel > 0 and ctrl.sneak then
  153. vel.y = vel.y-UFO_SPEED
  154. fuel = fuel - UFO_FUEL_USE
  155. else
  156. vel.y = vel.y*.9
  157. end
  158. if vel.x > UFO_MAX_SPEED then vel.x = UFO_MAX_SPEED end
  159. if vel.x < -UFO_MAX_SPEED then vel.x = -UFO_MAX_SPEED end
  160. if vel.y > UFO_MAX_SPEED then vel.y = UFO_MAX_SPEED end
  161. if vel.y < -UFO_MAX_SPEED then vel.y = -UFO_MAX_SPEED end
  162. if vel.z > UFO_MAX_SPEED then vel.z = UFO_MAX_SPEED end
  163. if vel.z < -UFO_MAX_SPEED then vel.z = -UFO_MAX_SPEED end
  164. self.object:set_velocity(vel)
  165. if ctrl.left then
  166. self.object:setyaw(self.object:getyaw()+math.pi/120*UFO_TURN_SPEED)
  167. end
  168. if ctrl.right then
  169. self.object:setyaw(self.object:getyaw()-math.pi/120*UFO_TURN_SPEED)
  170. end
  171. end
  172. else
  173. self.object:set_velocity({x=0, y=0, z=0})
  174. end
  175. if fuel < 0 then fuel = 0 end
  176. if fuel > 100 then fuel = 100 end
  177. if self.fueli ~= math.floor(fuel*8/100) then
  178. self.fueli = math.floor(fuel*8/100)
  179. print(self.fueli)
  180. self.textures = {"ufo_"..self.fueli..".png"}
  181. self.object:set_properties(self)
  182. end
  183. set_fuel(self,fuel)
  184. end
  185. function miniufo:get_staticdata()
  186. return self.name..";"..tostring(self.fuel)
  187. end
  188. minetest.register_entity("ufowreck:miniufo", miniufo)
  189. --register tool (with/without technic mod)
  190. local tooldef = {
  191. description = minetest.colorize("#0E0", "Alien ")..minetest.colorize("#A0F", "Mini UFO"),
  192. inventory_image = "ufos_inventory.png",
  193. wield_image = "ufos_inventory.png",
  194. tool_capabilities = {load=0,max_drop_level=0, groupcaps={fleshy={times={}, uses=100, maxlevel=0}}},
  195. on_place = function(itemstack, placer, pointed_thing)
  196. if pointed_thing.type ~= "node" then
  197. return
  198. end
  199. -- Call on_rightclick if the pointed node defines it
  200. if placer and not placer:get_player_control().sneak then
  201. local n = minetest.get_node(pointed_thing.under)
  202. local nn = n.name
  203. if minetest.registered_nodes[nn] and minetest.registered_nodes[nn].on_rightclick then
  204. return minetest.registered_nodes[nn].on_rightclick(pointed_thing.under, n, placer, itemstack) or itemstack
  205. end
  206. end
  207. ufo_from_item(itemstack,placer,pointed_thing)
  208. return itemstack
  209. end,
  210. }
  211. -- if technic then
  212. -- tooldef.on_refill = technic.refill_RE_charge
  213. -- tooldef.wear_represents = "technic_RE_charge"
  214. -- technic.register_power_tool("ufowreck:miniufo", 2000000)
  215. -- end
  216. minetest.register_tool("ufowreck:miniufo", tooldef)
  217. --[[minetest.register_craft( {
  218. output = 'ufowreck:miniufo',
  219. recipe = {
  220. { "ufowreck:alien_control", "xdecor:cushion", "ufowreck:alien_metal"},
  221. { "ufowreck:alien_metal", "ufowreck:alien_engine", "ufowreck:alien_metal"},
  222. { "default:meselamp", "tubelib:lamp", "default:meselamp"},
  223. },
  224. })]]--