-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.lua
416 lines (411 loc) · 15.2 KB
/
utils.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
local c_dirt_with_grass = minetest.get_content_id("default:dirt_with_grass")
local c_dirt_with_snow = minetest.get_content_id("default:dirt_with_snow")
local c_dirt_with_dry_grass = minetest.get_content_id("default:dirt_with_dry_grass")
local c_dirt_with_coniferous_litter = minetest.get_content_id("default:dirt_with_coniferous_litter")
local c_sand = minetest.get_content_id("default:sand")
local c_desert_sand = minetest.get_content_id("default:desert_sand")
local c_silver_sand = minetest.get_content_id("default:silver_sand")
--
local c_air = minetest.get_content_id("air")
local c_snow = minetest.get_content_id("default:snow")
local c_fern_1 = minetest.get_content_id("default:fern_1")
local c_fern_2 = minetest.get_content_id("default:fern_2")
local c_fern_3 = minetest.get_content_id("default:fern_3")
local c_rose = minetest.get_content_id("flowers:rose")
local c_viola = minetest.get_content_id("flowers:viola")
local c_geranium = minetest.get_content_id("flowers:geranium")
local c_tulip = minetest.get_content_id("flowers:tulip")
local c_dandelion_y = minetest.get_content_id("flowers:dandelion_yellow")
local c_dandelion_w = minetest.get_content_id("flowers:dandelion_white")
local c_bush_leaves = minetest.get_content_id("default:bush_leaves")
local c_bush_stem = minetest.get_content_id("default:bush_stem")
local c_a_bush_leaves = minetest.get_content_id("default:acacia_bush_leaves")
local c_a_bush_stem = minetest.get_content_id("default:acacia_bush_stem")
local c_water_source = minetest.get_content_id("default:water_source")
local c_water_flowing = minetest.get_content_id("default:water_flowing")
-------------------------------------------------------------------------------
-- function to copy tables
-------------------------------------------------------------------------------
function settlements.shallowCopy(original)
local copy = {}
for key, value in pairs(original) do
copy[key] = value
end
return copy
end
--
--
--
function settlements.round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
-------------------------------------------------------------------------------
-- function to find surface block y coordinate
-------------------------------------------------------------------------------
function settlements.find_surface_lvm(pos, minp)
--ab hier altes verfahren
local p6 = settlements.shallowCopy(pos)
local surface_mat = {
c_dirt_with_grass,
c_dirt_with_snow ,
c_dirt_with_dry_grass,
c_dirt_with_coniferous_litter,
c_sand,
c_desert_sand
}
local cnt = 0
local itter -- count up or down
local cnt_max = 200
-- starting point for looking for surface
local vi = va:index(p6.x, p6.y, p6.z)
if data[vi] == nil then return nil end
local tmp = minetest.get_name_from_content_id(data[vi])
if data[vi] == c_air then
itter = -1
else
itter = 1
end
while cnt < cnt_max do
cnt = cnt+1
local vi = va:index(p6.x, p6.y, p6.z)
-- local tmp = minetest.get_name_from_content_id(data[vi])
-- if vi == nil
-- then
-- return nil
-- end
for i, mats in ipairs(surface_mat) do
local node_check = va:index(p6.x, p6.y+1, p6.z)
if node_check and vi and data[vi] == mats and
(data[node_check] ~= c_water_source and
data[node_check] ~= c_water_flowing
)
then
local tmp = minetest.get_name_from_content_id(data[node_check])
return p6, mats
end
end
p6.y = p6.y + itter
if p6.y < 0 then return nil end
end
return nil --]]
end
-------------------------------------------------------------------------------
-- function to find surface block y coordinate
-- returns surface postion
-------------------------------------------------------------------------------
function settlements.find_surface(pos)
local p6 = settlements.shallowCopy(pos)
local cnt = 0
local itter -- count up or down
local cnt_max = 200
-- check, in which direction to look for surface
local surface_node = minetest.get_node_or_nil(p6)
if surface_node and string.find(surface_node.name,"air") then
itter = -1
else
itter = 1
end
-- go through nodes an find surface
while cnt < cnt_max do
cnt = cnt+1
minetest.forceload_block(p6)
surface_node = minetest.get_node_or_nil(p6)
if not surface_node then
-- Load the map at pos and try again
minetest.get_voxel_manip():read_from_map(p6, p6)
surface_node = minetest.get_node(p6)
if surface_node.name == "ignore" then
if settlements.debug == true then
minetest.chat_send_all("find_surface1: nil or ignore")
end
return nil
end
end
-- if surface_node == nil or surface_node.name == "ignore" then
-- --return nil
-- local fl = minetest.forceload_block(p6)
-- if not fl then
--
-- return nil
-- end
-- end
--
-- Check Surface_node and Node above
--
if settlements.surface_mat[surface_node.name] then
local surface_node_plus_1 = minetest.get_node_or_nil({ x=p6.x, y=p6.y+1, z=p6.z})
if surface_node_plus_1 and surface_node and
(string.find(surface_node_plus_1.name,"air") or
string.find(surface_node_plus_1.name,"snow") or
string.find(surface_node_plus_1.name,"fern") or
string.find(surface_node_plus_1.name,"flower") or
string.find(surface_node_plus_1.name,"bush") or
string.find(surface_node_plus_1.name,"tree") or
string.find(surface_node_plus_1.name,"grass"))
then
if settlements.debug == true then
minetest.chat_send_all("find_surface7: " ..surface_node.name.. " " .. surface_node_plus_1.name)
end
return p6, surface_node.name
else
if settlements.debug == true then
minetest.chat_send_all("find_surface2: wrong surface+1")
end
end
else
if settlements.debug == true then
if string.find(surface_node.name,"air") then
local a=1
else
minetest.chat_send_all("find_surface3: wrong surface "..surface_node.name)
end
end
end
-- end
p6.y = p6.y + itter
if p6.y < 0 then
if settlements.debug == true then
minetest.chat_send_all("find_surface4: y<0")
end
return nil
end
end
if settlements.debug == true then
minetest.chat_send_all("find_surface5: cnt_max overflow")
end
return nil
end
-------------------------------------------------------------------------------
-- check distance for new building
-------------------------------------------------------------------------------
function settlements.check_distance(building_pos, building_size)
local distance
for i, built_house in ipairs(settlement_info) do
distance = math.sqrt(
((building_pos.x - built_house["pos"].x)*(building_pos.x - built_house["pos"].x))+
((building_pos.z - built_house["pos"].z)*(building_pos.z - built_house["pos"].z)))
if distance < building_size or
distance < built_house["hsize"]
then
return false
end
end
return true
end
-------------------------------------------------------------------------------
-- save list of generated settlements
-------------------------------------------------------------------------------
function settlements.save()
local file = io.open(minetest.get_worldpath().."/settlements.txt", "w")
if file then
file:write(minetest.serialize(settlements_in_world))
file:close()
end
end
-------------------------------------------------------------------------------
-- load list of generated settlements
-------------------------------------------------------------------------------
function settlements.load()
local file = io.open(minetest.get_worldpath().."/settlements.txt", "r")
if file then
local table = minetest.deserialize(file:read("*all"))
if type(table) == "table" then
return table
end
end
return {}
end
-------------------------------------------------------------------------------
-- check distance to other settlements
-------------------------------------------------------------------------------
function settlements.check_distance_other_settlements(center_new_chunk)
-- local min_dist_settlements = 300
for i, pos in ipairs(settlements_in_world) do
local distance = vector.distance(center_new_chunk, pos)
-- minetest.chat_send_all("dist ".. distance)
if distance < settlements.min_dist_settlements then
return false
end
end
return true
end
-------------------------------------------------------------------------------
-- fill chests
-------------------------------------------------------------------------------
function settlements.fill_chest(pos)
-- find chests within radius
--local chestpos = minetest.find_node_near(pos, 6, {"default:chest"})
local chestpos = pos
-- initialize chest (mts chests don't have meta)
local meta = minetest.get_meta(chestpos)
if meta:get_string("infotext") ~= "Chest" then
minetest.registered_nodes["default:chest"].on_construct(chestpos)
end
-- fill chest
local inv = minetest.get_inventory( {type="node", pos=chestpos} )
-- always
inv:add_item("main", "default:apple "..math.random(1,3))
-- low value items
if math.random(0,1) < 1 then
inv:add_item("main", "farming:bread "..math.random(0,3))
inv:add_item("main", "default:steel_ingot "..math.random(0,3))
-- additional fillings when farmin mod enabled
if minetest.get_modpath("farming") ~= nil and farming.mod == "redo" then
if math.random(0,1) < 1 then
inv:add_item("main", "farming:melon_slice "..math.random(0,3))
inv:add_item("main", "farming:carrot "..math.random(0,3))
inv:add_item("main", "farming:corn "..math.random(0,3))
end
end
end
-- medium value items
if math.random(0,3) < 1 then
inv:add_item("main", "default:pick_steel "..math.random(0,1))
inv:add_item("main", "default:pick_bronze "..math.random(0,1))
inv:add_item("main", "fire:flint_and_steel "..math.random(0,1))
inv:add_item("main", "bucket:bucket_empty "..math.random(0,1))
inv:add_item("main", "default:sword_steel "..math.random(0,1))
end
end
-------------------------------------------------------------------------------
-- initialize furnace
-------------------------------------------------------------------------------
function settlements.initialize_furnace(pos)
-- find chests within radius
local furnacepos = minetest.find_node_near(pos,
7, --radius
{"default:furnace"})
-- initialize furnacepos (mts furnacepos don't have meta)
if furnacepos
then
local meta = minetest.get_meta(furnacepos)
if meta:get_string("infotext") ~= "furnace"
then
minetest.registered_nodes["default:furnace"].on_construct(furnacepos)
end
end
end
-------------------------------------------------------------------------------
-- initialize furnace, chests, bookshelves
-------------------------------------------------------------------------------
function settlements.initialize_nodes()
for i, built_house in ipairs(settlement_info) do
for j, schem in ipairs(schematic_table) do
if settlement_info[i]["name"] == schem["name"]
then
building_all_info = schem
break
end
end
local width = building_all_info["hwidth"]
local depth = building_all_info["hdepth"]
local height = building_all_info["hheight"]
local p = settlement_info[i]["pos"]
for yi = 1,height do
for xi = 0,width do
for zi = 0,depth do
local ptemp = {x=p.x+xi, y=p.y+yi, z=p.z+zi}
local node = minetest.get_node(ptemp)
if node.name == "default:furnace" or
node.name == "default:chest" or
node.name == "default:bookshelf"
then
minetest.registered_nodes[node.name].on_construct(ptemp)
end
-- when chest is found -> fill with stuff
if node.name == "default:chest" then
minetest.after(3,settlements.fill_chest,ptemp)
end
end
end
end
end
end
-------------------------------------------------------------------------------
-- randomize table
-------------------------------------------------------------------------------
function shuffle(tbl)
local table = settlements.shallowCopy(tbl)
local size = #table
for i = size, 1, -1 do
local rand = math.random(size)
table[i], table[rand] = table[rand], table[i]
end
return table
end
-------------------------------------------------------------------------------
-- evaluate heightmap
-------------------------------------------------------------------------------
function settlements.evaluate_heightmap()
-- max height and min height, initialize with impossible values for easier first time setting
local max_y = -50000
local min_y = 50000
-- only evaluate the center square of heightmap 40 x 40
local square_start = 1621
local square_end = 1661
for j = 1 , 40, 1 do
for i = square_start, square_end, 1 do
-- skip buggy heightmaps, return high value
if heightmap[i] == -31000 or
heightmap[i] == 31000
then
return max_height_difference + 1
end
if heightmap[i] < min_y
then
min_y = heightmap[i]
end
if heightmap[i] > max_y
then
max_y = heightmap[i]
end
end
-- set next line
square_start = square_start + 80
square_end = square_end + 80
end
-- return the difference between highest and lowest pos in chunk
local height_diff = max_y - min_y
-- filter buggy heightmaps
if height_diff <= 1
then
return max_height_difference + 1
end
-- debug info
if settlements.debug == true
then
minetest.chat_send_all("heightdiff ".. height_diff)
end
return height_diff
end
-------------------------------------------------------------------------------
-- get LVM of current chunk
-------------------------------------------------------------------------------
function settlements.getlvm(minp, maxp)
local vm = minetest.get_voxel_manip()
local emin, emax = vm:read_from_map(minp, maxp)
local va = VoxelArea:new{
MinEdge = emin,
MaxEdge = emax
}
local data = vm:get_data()
return vm, data, va, emin, emax
end
-------------------------------------------------------------------------------
-- get LVM of current chunk
-------------------------------------------------------------------------------
function settlements.setlvm(vm, data)
-- Write data
vm:set_data(data)
vm:write_to_map(true)
end
-------------------------------------------------------------------------------
-- Set array to list
-- https://stackoverflow.com/questions/656199/search-for-an-item-in-a-lua-list
-------------------------------------------------------------------------------
function settlements.Set (list)
local set = {}
for _, l in ipairs(list) do set[l] = true end
return set
end