]> git.lizzy.rs Git - Crafter.git/blob - mods/minecart/init.lua
Add in high speed minecart axis locking
[Crafter.git] / mods / minecart / init.lua
1 local pool = {}
2
3 local dirs = {
4         {x= 1,y= 0,z= 0},
5         {x=-1,y= 0,z= 0},
6
7         {x= 1,y= 1,z= 0}, 
8         {x=-1,y= 1,z= 0},
9
10         {x= 1,y=-1,z= 0},
11         {x=-1,y=-1,z= 0},
12
13         {x= 0,y= 0,z= 1},
14         {x= 0,y= 0,z=-1},
15
16         {x= 0,y= 1,z= 1},
17         {x= 0,y= 1,z=-1},
18
19         {x= 0,y=-1,z= 1},
20         {x= 0,y=-1,z=-1},
21 }
22
23 local axis_order = {
24
25 }
26 local function data_injection(pos,data)
27         if data then
28                 pool[minetest.hash_node_position(pos)] = true
29         else
30                 pool[minetest.hash_node_position(pos)] = nil
31         end
32 end
33
34
35 local function create_axis(pos)
36         local possible_dirs = {}
37         for _,dir in pairs(dirs) do
38                 local pos2 = vector.add(pos,dir)
39                 if pool[minetest.hash_node_position(pos2)] then
40                         table.insert(possible_dirs,dir)
41                 end
42         end
43         return(possible_dirs)
44 end
45
46
47
48 local minecart = {}
49
50 minecart.on_step = function(self,dtime)
51         local pos = vector.round(self.object:get_pos())
52         if not self.axis_lock then
53                 local possible_dirs = create_axis(pos)
54                 for _,dir in pairs(possible_dirs) do
55                         if dir.x ~=0 then
56                                 self.axis_lock = "x"
57                                 break
58                         elseif dir.z ~= 0 then
59                                 self.axis_lock = "z"
60                                 break
61                         end
62                 end
63         else
64                 print(self.axis_lock)
65         end
66 end
67
68 minecart.on_rightclick = function(self,clicker)
69 end
70
71 --get old data
72 minecart.on_activate = function(self,staticdata, dtime_s)
73         self.object:set_armor_groups({immortal=1})
74         if string.sub(staticdata, 1, string.len("return")) ~= "return" then
75                 return
76         end
77         local data = minetest.deserialize(staticdata)
78         if type(data) ~= "table" then
79                 return
80         end
81
82 end
83
84 minecart.get_staticdata = function(self)
85         return minetest.serialize({
86         })
87 end
88
89
90
91 minecart.initial_properties = {
92         physical = false, -- otherwise going uphill breaks
93         collisionbox = {-0.4, -0.5, -0.4, 0.4, 0.45, 0.4},--{-0.5, -0.4, -0.5, 0.5, 0.25, 0.5},
94         visual = "mesh",
95         mesh = "minecart.x",
96         visual_size = {x=1, y=1},
97         textures = {"minecart.png"},
98 }
99
100
101 minecart.on_punch = function(self,puncher, time_from_last_punch, tool_capabilities, dir, damage)
102         --local obj = minetest.add_item(self.object:getpos(), "minecart:minecart")
103         --self.object:remove()
104 end
105
106         
107
108 minetest.register_entity("minecart:minecart", minecart)
109
110
111
112
113
114
115
116
117
118
119
120
121 minetest.register_craftitem("minecart:minecart", {
122         description = "Minecart",
123         inventory_image = "minecartitem.png",
124         wield_image = "minecartitem.png",
125         on_place = function(itemstack, placer, pointed_thing)
126                 if not pointed_thing.type == "node" then
127                         return
128                 end
129                 
130                 local sneak = placer:get_player_control().sneak
131                 local noddef = minetest.registered_nodes[minetest.get_node(pointed_thing.under).name]
132                 if not sneak and noddef.on_rightclick then
133                         minetest.item_place(itemstack, placer, pointed_thing)
134                         return
135                 end
136                 
137                 if minetest.get_item_group(minetest.get_node(pointed_thing.under).name, "rail")>0 then
138                         minetest.add_entity(pointed_thing.under, "minecart:minecart")
139                 else
140                         return
141                 end
142
143                 itemstack:take_item()
144
145                 return itemstack
146         end,
147 })
148
149 minetest.register_craft({
150         output = "minecart:minecart",
151         recipe = {
152                 {"main:iron", "", "main:iron"},
153                 {"main:iron", "main:iron", "main:iron"},
154         },
155 })
156
157
158
159
160
161 minetest.register_node("minecart:rail",{
162         description = "Rail",
163         wield_image = "rail.png",
164         tiles = {
165                 "rail.png", "railcurve.png",
166                 "railt.png", "railcross.png"
167         },
168         drawtype = "raillike",
169         paramtype = "light",
170         sunlight_propagates = true,
171         is_ground_content = false,
172         walkable = false,
173         node_placement_prediction = "",
174         selection_box = {
175                 type = "fixed",
176                 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
177         },
178         sounds = main.stoneSound(),
179         after_place_node = function(pos)
180                 data_injection(pos,true)
181         end,
182         after_destruct = function(pos)
183                 data_injection(pos)
184         end,
185         groups={stone=1,wood=1,rail=1,attached_node=1},
186 })
187
188
189 minetest.register_lbm({
190         name = "minecart:rail",
191         nodenames = {"minecart:rail"},
192         run_at_every_load = true,
193         action = function(pos)
194                 data_injection(pos,true)
195                 print("buildin dat cash")
196         end,
197 })
198
199 minetest.register_craft({
200         output = "minecart:rail 32",
201         recipe = {
202                 {"main:iron","","main:iron"},
203                 {"main:iron","main:stick","main:iron"},
204                 {"main:iron","","main:iron"}
205         }
206 })