kpoppel
2013-07-02 053fa59739f4b772174bf0a090969b3395ab3f98
commit | author | age
ee5c6c 1 -- Th coal driven EU generator.
K 2 -- A simple device to get started on the electric machines.
3 -- Inefficient and expensive in coal (200EU 16 ticks)
4 -- Also only allows for LV machinery to run.
82cba9 5 minetest.register_alias("generator", "technic:generator")
R 6 minetest.register_alias("generator", "technic:generator_active")
7
8 minetest.register_craft({
9     output = 'technic:generator',
10     recipe = {
11         {'default:stone', 'default:stone', 'default:stone'},
12         {'default:stone', '', 'default:stone'},
13         {'default:stone', 'moreores:copper_ingot', 'default:stone'},
14     }
15 })
16
17 minetest.register_craftitem("technic:generator", {
18     description = "Coal Driven Generator",
19     stack_max = 99,
20 }) 
21
ee5c6c 22 local generator_formspec =
82cba9 23     "invsize[8,9;]"..
R 24     "image[0,0;5,5;technic_generator_menu.png]"..
25     "image[1,1;1,2;technic_power_meter_bg.png]"..
26 --    "label[0,0;Generator]"..
27     "label[1,3;Power level]"..
28     "list[current_name;src;3,1;1,1;]"..
29     "image[4,1;1,1;default_furnace_fire_bg.png]"..
30     "list[current_player;main;0,5;8,4;]"
31     
32
ee5c6c 33 minetest.register_node(
K 34    "technic:generator",
35    {
36       description = "Coal Driven Generator",
37       tiles = {"technic_generator_top.png", "technic_machine_bottom.png", "technic_generator_side.png",
38            "technic_generator_side.png", "technic_generator_side.png", "technic_generator_front.png"},
39       paramtype2 = "facedir",
40       groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
41       legacy_facedir_simple = true,
42       sounds = default.node_sound_wood_defaults(),
43       on_construct = function(pos)
44             local meta = minetest.env:get_meta(pos)
45             meta:set_string("infotext", "Coal Electric Generator")
46             meta:set_float("technic_power_machine", 1)
47             meta:set_int("LV_EU_supply", 0)
48             meta:set_int("LV_EU_from_fuel", 1) -- Signal to the switching station that this device burns some sort of fuel and needs special handling
49             meta:set_int("burn_time", 0)
50             meta:set_string("formspec", generator_formspec)
51             local inv = meta:get_inventory()
52             inv:set_size("src", 1)
82cba9 53         end,    
ee5c6c 54       can_dig = function(pos,player)
K 55            local meta = minetest.env:get_meta(pos);
56            local inv = meta:get_inventory()
57            if not inv:is_empty("src") then
58               minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty");
59               return false
60            else
61               return true
62            end
82cba9 63         end,
ee5c6c 64    })
82cba9 65
ee5c6c 66 minetest.register_node(
K 67    "technic:generator_active",
68    {
69       description = "Coal Driven Generator",
70       tiles = {"technic_generator_top.png", "technic_machine_bottom.png", "technic_generator_side.png",
71            "technic_generator_side.png", "technic_generator_side.png", "technic_generator_front_active.png"},
72       paramtype2 = "facedir",
73       groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,not_in_creative_inventory=1},
74       legacy_facedir_simple = true,
75       sounds = default.node_sound_wood_defaults(),
76       drop="technic:generator",
77       can_dig = function(pos,player)
78            local meta = minetest.env:get_meta(pos);
79            local inv = meta:get_inventory()
80            if not inv:is_empty("src") then
81               minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty");
82               return false
83            else
84               return true
85            end
82cba9 86         end,
ee5c6c 87    })
82cba9 88
ee5c6c 89 minetest.register_abm(
K 90    {
91       nodenames = {"technic:generator","technic:generator_active"},
92       interval = 1,
93       chance   = 1,
94       action = function(pos, node, active_object_count, active_object_count_wider)
95           local meta = minetest.env:get_meta(pos)
96           local burn_time= meta:get_int("burn_time")
82cba9 97
ee5c6c 98           -- If more to burn and the energy produced was used: produce some more
K 99           if burn_time>0 then
100              if meta:get_int("LV_EU_supply") == 0 then
101             -- We did not use the power
102             meta:set_int("LV_EU_supply", 200) -- Give 200EUs
103              else
104             burn_time = burn_time - 1
105             meta:set_int("burn_time",burn_time)
106              end
107           end
82cba9 108
ee5c6c 109           -- Burn another piece of coal
K 110           if burn_time==0 then
111              local inv = meta:get_inventory()
112              if inv:is_empty("src") == false  then 
113             local srcstack = inv:get_stack("src", 1)
114             src_item=srcstack:to_table()
115             if src_item["name"] == "default:coal_lump" then
116                srcstack:take_item()
117                inv:set_stack("src", 1, srcstack)
118                burn_time=16
119                meta:set_int("burn_time",burn_time)
120                hacky_swap_node (pos,"technic:generator_active") 
121                meta:set_int("LV_EU_supply", 200) -- Give 200EUs
122             end
123              end
124           end
82cba9 125
ee5c6c 126           local load = 8 -- math.floor((charge/max_charge)*100)
K 127           local percent = math.floor((burn_time/16)*100)
128           meta:set_string("formspec",
129                   "invsize[8,9;]"..
130                      "image[1,1;1,2;technic_power_meter_bg.png^[lowpart:"..
131                      (load)..":technic_power_meter_fg.png]"..
132                   "label[0,0;Generator]"..
133                   "label[1,3;Power level]"..
134                   "list[current_name;src;3,1;1,1;]"..
135                   "image[4,1;1,1;default_furnace_fire_bg.png^[lowpart:"..
136                   (percent)..":default_furnace_fire_fg.png]"..
137                    "list[current_player;main;0,5;8,4;]"
138              )
612349 139
ee5c6c 140           if burn_time==0 then
K 141              hacky_swap_node (pos,"technic:generator")
142           end
143            end
144    })
82cba9 145
ee5c6c 146 technic.register_LV_machine ("technic:generator","PR")
K 147 technic.register_LV_machine ("technic:generator_active","PR")