forked from theFox6/working_villages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomes.lua
214 lines (200 loc) · 7.06 KB
/
homes.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
-- smart_villages.homes represents a table that contains the villagers homes.
-- This table's keys are inventory names, and values are home objects.
smart_villages.homes = (function()
local file_name = minetest.get_worldpath() .. "/smart_villages_homes"
minetest.register_on_shutdown(function()
local save_data = {}
for k,v in pairs(smart_villages.homes) do
save_data[k]={marker=v.marker}
end
local file = io.open(file_name, "w")
file:write(minetest.serialize(save_data))
file:close()
end)
local file = io.open(file_name, "r")
if file ~= nil then
local data = file:read("*a")
file:close()
return minetest.deserialize(data)
end
return {}
end) ()
local function out_of_limit(pos)
if (pos.x>30927 or pos.x<-30912
or pos.y>30927 or pos.y<-30912
or pos.z>30927 or pos.z<-30912) then
return false
end
return true
end
minetest.register_node("smart_villages:home_marker", {
description = "home marker for smart_villages",
drawtype = "nodebox",
tiles = {"default_sign_wall_wood.png"},
inventory_image = "default_sign_wood.png",
wield_image = "default_sign_wood.png",
paramtype = "light",
paramtype2 = "wallmounted",
sunlight_propagates = true,
is_ground_content = false,
walkable = false,
node_box = {
type = "wallmounted",
wall_top = {-0.4375, 0.4375, -0.3125, 0.4375, 0.5, 0.3125},
wall_bottom = {-0.4375, -0.5, -0.3125, 0.4375, -0.4375, 0.3125},
wall_side = {-0.5, -0.3125, -0.4375, -0.4375, 0.3125, 0.4375},
},
groups = {choppy = 2, dig_immediate = 2, attached_node = 1},
legacy_wallmounted = true,
sounds = default.node_sound_defaults(),
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
local owner = placer:get_player_name()
meta:set_string("owner", owner)
end,
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string(
"formspec",
"size[5,5]"..
"field[0.5,1;4,1;name;house label;${name}]"..
"field[0.5,2;4,1;bed_pos;bed position;${bed_pos}]"..
"field[0.5,3;4,1;door_pos;position outside the house;${door_pos}]"..
"button_exit[1,4;2,1;ok;Write]")
end,
on_receive_fields = function(pos, _, fields, sender)
local meta = minetest.get_meta(pos)
local sender_name = sender:get_player_name()
local failed = false
if minetest.is_protected(pos, sender_name) then
minetest.record_protection_violation(pos, sender_name)
return
end
if (meta:get_string("bed")~="" and meta:get_string("door")~="")
or (fields.bed_pos == nil and fields.door_pos == nil) then
return
end
local coords = minetest.string_to_pos(fields.bed_pos)
if coords == nil then
-- fail on illegal input of coordinates
minetest.chat_send_player(sender_name, 'You failed to provide correct coordinates for the bed position. '..
'Please enter the X, Y, and Z coordinates of the desired destination in a comma seperated list. '..
'Example: The input "10,20,30" means the destination at the coordinates X=10, Y=20 and Z=30.')
failed = true
elseif out_of_limit(coords) then
minetest.chat_send_player(sender_name, 'The coordinates of your bed position '..
'do not exist in our coordinate system. Correct coordinates range from -30912 to 30927 in all axes.')
failed = true
else
meta:set_string("bed", fields.bed_pos)
end
coords = minetest.string_to_pos(fields.door_pos)
if coords == nil then
-- fail on illegal input of coordinates
minetest.chat_send_player(sender_name, 'You failed to provide correct coordinates for the door position. '..
'Please enter the X, Y, and Z coordinates of the desired destination in a comma seperated list. '..
'Example: The input "10,20,30" means the destination at the coordinates X=10, Y=20 and Z=30.')
failed = true
elseif out_of_limit(coords) then
minetest.chat_send_player(sender_name, 'The coordinates of your bed position '..
'do not exist in our coordinate system. Correct coordinates range from -30912 to 30927 in all axes.')
failed = true
else
meta:set_string("door", fields.door_pos)
end
if not failed then
meta:set_string("infotext", fields.name)
meta:set_string("formspec",
"size[5,4]"..
"label[0.5,0.5;house label: ".. fields.name .."]"..
"label[0.5,1;bed position:".. fields.bed_pos .."]"..
"label[0.5,1.5;position outside:".. fields.door_pos .."]"..
"label[0.5,2;position of this marker:" .. pos.x .. "," .. pos.y .. "," .. pos.z .. "]"..
"button_exit[1,2.5;2,1;ok;exit]")
end
end,
can_dig = function(pos, player)
local meta = minetest.get_meta(pos)
local owner = meta:get_string("owner")
local pname = player:get_player_name()
return pname == owner or pname == minetest.setting_get("name")
end,
})
-- home is a prototype home object
smart_villages.home = {version = 1}
-- get the home of a villager
function smart_villages.get_home(self)
return smart_villages.homes[self.inventory_name]
end
-- check whether a villager has a home
function smart_villages.is_valid_home(self)
local home = smart_villages.get_home(self)
if home == nil then
return false
end
if not home.version~=1 then --update home
for k, v in pairs(smart_villages.home) do
home[k] = v
end
end
return true
end
-- get the position of the home_marker
function smart_villages.home:get_marker()
return self.marker
end
function smart_villages.home:get_marker_meta()
local home_marker_pos = self:get_marker()
if minetest.get_node(home_marker_pos).name == "ignore" then
minetest.get_voxel_manip():read_from_map(home_marker_pos, home_marker_pos)
end
if minetest.get_node(home_marker_pos).name ~= "smart_villages:home_marker" then
if smart_villages.debug_logging and not(vector.equals(home_marker_pos,{x=0,y=0,z=0})) then
minetest.log("warning", "The door position of an invalid home was requested.")
minetest.log("warning", "Given home position:" .. minetest.pos_to_string(home_marker_pos))
end
return false
end
return minetest.get_meta(home_marker_pos)
end
-- get the position that marks "outside"
function smart_villages.home:get_door()
if self.door~=nil then
return self.door
end
local meta = self:get_marker_meta()
local door_pos = meta:get_string("door")
if not door_pos then
if smart_villages.debug_logging then
local home_marker_pos = self:get_marker()
minetest.log("warning", "The position outside the house was not entered for the home at:" ..
minetest.pos_to_string(home_marker_pos))
end
return false
end
self.door = minetest.string_to_pos(door_pos)
return self.door
end
-- get the bed of a villager
function smart_villages.home:get_bed()
if self.bed~=nil then
return self.bed
end
local meta = self:get_marker_meta()
local bed_pos = meta:get_string("bed")
if not bed_pos then
if smart_villages.debug_logging then
local home_marker_pos = self:get_marker()
minetest.log("warning", "The position of the bed was not entered for the home at:" ..
minetest.pos_to_string(home_marker_pos))
end
return false
end
self.bed = minetest.string_to_pos(bed_pos)
return self.bed
end
-- set the home of a villager
function smart_villages.set_home(inv_name,marker_pos)
smart_villages.homes[inv_name] = table.copy(smart_villages.home)
smart_villages.homes[inv_name].marker = marker_pos
end