]> git.lizzy.rs Git - Crafter.git/blob - mods/minecart/init.lua
108fe7cecff0fc4daf07cd6b5fcf6409fb73e91f
[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 local function collision_detect(self)
47         if not self.axis_lock then return end
48         local pos = self.object:get_pos()
49
50         for _,object in ipairs(minetest.get_objects_inside_radius(pos, 1)) do
51                 if object:is_player() then
52                         local pos2 = object:get_pos()
53                         if self.axis_lock == "x" then
54                                 local velocity = 1-vector.distance(vector.new(pos.x,0,0),vector.new(pos2.x,0,0))
55                                 local dir = vector.direction(vector.new(pos2.x,0,0),vector.new(pos.x,0,0))
56                                 self.object:add_velocity(dir)
57                         elseif self.axis_lock == "z" then
58                                 local velocity = 1-vector.distance(vector.new(0,0,pos.z),vector.new(0,0,pos2.z))
59                                 local dir = vector.direction(vector.new(0,0,pos2.z),vector.new(0,0,pos.z))
60                                 self.object:add_velocity(dir)
61                         end
62                         return
63                 end
64         end
65 end
66
67 local minecart = {}
68
69 minecart.on_step = function(self,dtime)
70         local pos = vector.round(self.object:get_pos())
71         if not self.axis_lock then
72                 local possible_dirs = create_axis(pos)
73                 for _,dir in pairs(possible_dirs) do
74                         if dir.x ~=0 then
75                                 self.axis_lock = "x"
76                                 break
77                         elseif dir.z ~= 0 then
78                                 self.axis_lock = "z"
79                                 break
80                         end
81                 end
82         else
83                 collision_detect(self)
84         end
85 end
86
87 minecart.on_rightclick = function(self,clicker)
88 end
89
90 --get old data
91 minecart.on_activate = function(self,staticdata, dtime_s)
92         self.object:set_armor_groups({immortal=1})
93         if string.sub(staticdata, 1, string.len("return")) ~= "return" then
94                 return
95         end
96         local data = minetest.deserialize(staticdata)
97         if type(data) ~= "table" then
98                 return
99         end
100
101 end
102
103 minecart.get_staticdata = function(self)
104         return minetest.serialize({
105         })
106 end
107
108
109
110 minecart.initial_properties = {
111         physical = false, -- otherwise going uphill breaks
112         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},
113         visual = "mesh",
114         mesh = "minecart.x",
115         visual_size = {x=1, y=1},
116         textures = {"minecart.png"},
117 }
118
119
120 minecart.on_punch = function(self,puncher, time_from_last_punch, tool_capabilities, dir, damage)
121         --local obj = minetest.add_item(self.object:getpos(), "minecart:minecart")
122         --self.object:remove()
123 end
124
125         
126
127 minetest.register_entity("minecart:minecart", minecart)
128
129
130
131
132
133
134
135
136
137
138
139
140 minetest.register_craftitem("minecart:minecart", {
141         description = "Minecart",
142         inventory_image = "minecartitem.png",
143         wield_image = "minecartitem.png",
144         on_place = function(itemstack, placer, pointed_thing)
145                 if not pointed_thing.type == "node" then
146                         return
147                 end
148                 
149                 local sneak = placer:get_player_control().sneak
150                 local noddef = minetest.registered_nodes[minetest.get_node(pointed_thing.under).name]
151                 if not sneak and noddef.on_rightclick then
152                         minetest.item_place(itemstack, placer, pointed_thing)
153                         return
154                 end
155                 
156                 if minetest.get_item_group(minetest.get_node(pointed_thing.under).name, "rail")>0 then
157                         minetest.add_entity(pointed_thing.under, "minecart:minecart")
158                 else
159                         return
160                 end
161
162                 itemstack:take_item()
163
164                 return itemstack
165         end,
166 })
167
168 minetest.register_craft({
169         output = "minecart:minecart",
170         recipe = {
171                 {"main:iron", "", "main:iron"},
172                 {"main:iron", "main:iron", "main:iron"},
173         },
174 })
175
176
177
178
179
180 minetest.register_node("minecart:rail",{
181         description = "Rail",
182         wield_image = "rail.png",
183         tiles = {
184                 "rail.png", "railcurve.png",
185                 "railt.png", "railcross.png"
186         },
187         drawtype = "raillike",
188         paramtype = "light",
189         sunlight_propagates = true,
190         is_ground_content = false,
191         walkable = false,
192         node_placement_prediction = "",
193         selection_box = {
194                 type = "fixed",
195                 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
196         },
197         sounds = main.stoneSound(),
198         after_place_node = function(pos)
199                 data_injection(pos,true)
200         end,
201         after_destruct = function(pos)
202                 data_injection(pos)
203         end,
204         groups={stone=1,wood=1,rail=1,attached_node=1},
205 })
206
207
208 minetest.register_lbm({
209         name = "minecart:rail",
210         nodenames = {"minecart:rail"},
211         run_at_every_load = true,
212         action = function(pos)
213                 data_injection(pos,true)
214                 print("buildin dat cash")
215         end,
216 })
217
218 minetest.register_craft({
219         output = "minecart:rail 32",
220         recipe = {
221                 {"main:iron","","main:iron"},
222                 {"main:iron","main:stick","main:iron"},
223                 {"main:iron","","main:iron"}
224         }
225 })