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