forked from Sokomine/handle_schematics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplace_buildings.lua
1445 lines (1252 loc) · 55.7 KB
/
place_buildings.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
handle_schematics.get_content_id_replaced = function( node_name, replacements )
if( not( node_name )) then
return minetest.get_content_id( 'ignore' );
end
local new_name = node_name
if( replacements and replacements.table and replacements.table[ node_name ]) then
new_name = replacements.table[ node_name ];
end
if(minetest.registered_aliases[ new_name ]) then
new_name = minetest.registered_aliases[ new_name ]
end
-- try the original name if the new one doesn't exist
if(not(minetest.registered_nodes[ new_name ])
and minetest.registered_nodes[ node_name ]) then
new_name = node_name
end
if(minetest.registered_aliases[ new_name ]) then
new_name = minetest.registered_aliases[ new_name ];
end
if(not(minetest.registered_nodes[ new_name ])) then
new_name = 'air';
end
return minetest.get_content_id( new_name );
end
-- shortcut (we need this function often)
local GCIR = handle_schematics.get_content_id_replaced
-- either uses get_node_or_nil(..) or the data from voxelmanip
-- the function might as well be local (only used by *.mg_drop_moresnow)
handle_schematics.get_node_somehow = function( x, y, z, a, data, param2_data )
if( a and data and param2_data ) then
return { content = data[a:index(x, y, z)], param2 = param2_data[a:index(x, y, z)] };
end
-- no voxelmanip; get the node the normal way
local node = minetest.get_node_or_nil( {x=x, y=y, z=z} );
if( not( node ) ) then
return { content = moresnow.c_ignore, param2 = 0 };
end
return { content = minetest.get_content_id( node.name ), param2 = node.param2, name = node.name };
end
-- "drop" moresnow snow on diffrent shapes; works for voxelmanip and node-based setting
handle_schematics.mg_drop_moresnow = function( x, z, y_top, y_bottom, a, data, param2_data, cid)
-- this only works if moresnow is installed
if( not( handle_schematics.moresnow_installed )) then
return;
end
local y = y_top;
local node_above = handle_schematics.get_node_somehow( x, y+1, z, a, data, param2_data );
local node_below = nil;
while( y >= y_bottom ) do
node_below = handle_schematics.get_node_somehow( x, y, z, a, data, param2_data );
if( node_above.content == moresnow.c_air
and node_below.content
and node_below.content ~= moresnow.c_ignore
and node_below.content ~= moresnow.c_air ) then
-- turn water into ice, but don't drop snow on it
if( node_below.content == cid.c_water_source
or node_below.content == cid.c_river_water_source) then
return { height = y, suggested = {new_id = cid.c_ice, param2 = 0 }};
end
-- if the node below drops snow when digged (i.e. is either snow or a moresnow node), we're finished
local get_drop = minetest.get_name_from_content_id( node_below.content );
if( get_drop ) then
get_drop = handle_schematics.node_defined( get_drop );
if( get_drop and get_drop.drop and type( get_drop.drop )=='string' and get_drop.drop == 'default:snow') then
return;
end
end
if( not(node_below.content)
or node_below.content == moresnow.c_snow ) then
return;
end
local suggested = moresnow.suggest_snow_type( node_below.content, node_below.param2 );
-- c_snow_top and c_snow_fence can only exist when the node 2 below is a solid one
if( suggested.new_id == moresnow.c_snow_top
or suggested.new_id == moresnow.c_snow_fence) then
local node_below2 = handle_schematics.get_node_somehow( x, y-1, z, a, data, param2_data);
if( node_below2.content ~= moresnow.c_ignore
and node_below2.content ~= moresnow.c_air ) then
local suggested2 = moresnow.suggest_snow_type( node_below2.content, node_below2.param2 );
if( suggested2.new_id == moresnow.c_snow ) then
return { height = y+1, suggested = suggested };
end
end
-- it is possible that this is not the right shape; if so, the snow will continue to fall down
elseif( suggested.new_id ~= moresnow.c_ignore ) then
return { height = y+1, suggested = suggested };
end
-- TODO return; -- abort; there is no fitting moresnow shape for the node below
end
y = y-1;
node_above = node_below;
end
end
-- helper function for generate_building
-- places a marker that allows players to buy plots with houses on them (in order to modify the buildings)
local function generate_building_plotmarker( pos, minp, maxp, data, param2_data, a, cid, building_nr_in_bpos, village_id, filename)
-- position the plot marker so that players can later buy this plot + building in order to modify it
-- pos.o contains the original orientation (determined by the road and the side the building is
local p = {x=pos.x, y=pos.y+1, z=pos.z};
-- vector for placing mg_villages:mob_spawner
local v = {x=0,z=0};
if( pos.o == 0 ) then
p.x = p.x - 1;
p.z = p.z + pos.bsizez - 1;
v.z = -1;
p.yaw = 90;
elseif( pos.o == 2 ) then
p.x = p.x + pos.bsizex;
v.z = 1;
p.yaw = 270;
elseif( pos.o == 1 ) then
p.z = p.z + pos.bsizez;
p.x = p.x + pos.bsizex - 1;
v.x = -1;
p.yaw = 0;
elseif( pos.o == 3 ) then
p.z = p.z - 1;
v.x = 1;
p.yaw = 180;
end
-- actually position the marker
if( p.x >= minp.x and p.x <= maxp.x and p.z >= minp.z and p.z <= maxp.z and p.y >= minp.y and p.y <= maxp.y) then
if( handle_schematics.moresnow_installed
and data[ a:index(p.x, p.y, p.z)] == cid.c_snow
and p.y<maxp.y
and moresnow and moresnow.c_snow_top and cid.c_snow_top ~= cid.c_ignore) then
data[ a:index(p.x, p.y+1, p.z)] = moresnow.c_snow_top;
end
data[ a:index(p.x, p.y, p.z)] = cid.c_plotmarker;
param2_data[a:index(p.x, p.y, p.z)] = pos.brotate;
-- store the necessary information in the marker so that it knows for which building it is responsible
local meta = minetest.get_meta( p );
meta:set_string('village_id', village_id );
meta:set_int( 'plot_nr', building_nr_in_bpos );
meta:set_string('infotext', 'Plot No. '..tostring( building_nr_in_bpos ).. ' with '..tostring( filename ));
-- information about the direction the mob ought to look at
meta:set_int('yaw', p.yaw );
end
-- place a mob spawner in front of the house for each bed
-- we do it here so that the roads will not overwrite it
local binfo = mg_villages.BUILDINGS[pos.btype];
if( not( binfo ) or not( binfo.bed_count ) or binfo.bed_count<1 or not( minetest.registered_nodes["mg_villages:mob_spawner"])) then
return;
end
cid.c_mob_spawner = minetest.get_content_id("mg_villages:mob_spawner");
p.y = p.y - 2; -- hide the spawner from view
for i=1,binfo.bed_count do
p.x = p.x + v.x;
p.z = p.z + v.z;
if( p.x >= minp.x and p.x <= maxp.x and p.z >= minp.z
and p.z <= maxp.z and p.y >= minp.y and p.y <= maxp.y) then
-- place the mob spawner
data[ a:index(p.x, p.y, p.z)] = cid.c_mob_spawner;
-- not really necessary but can't hurt
param2_data[a:index(p.x, p.y, p.z)] = pos.brotate;
-- store where to find information about the mob this spawner is responsible for
local meta = minetest.get_meta( p );
meta:set_string('village_id', village_id );
meta:set_int( 'plot_nr', building_nr_in_bpos );
meta:set_int( 'bed_nr', i );
meta:set_string('infotext', 'MOB SPAWNER for bed nr '..tostring(i)..' on plot nr '..tostring( building_nr_in_bpos )..' in village '..tostring( village_id ));
end
end
end
-- pos has to contain information about the building - o (orientation), bsizex and bsizez
-- this function returns a position in front of the building, with the building stretching equally to the right and left
-- (useful for mobs who want to leave/enter the plot)
-- bed_nr can be used to assign diffrent spawn points to mobs in diffrent beds
handle_schematics.get_pos_in_front_of_house = function( pos, bed_nr )
if( not( bed_nr )) then
bed_nr = 0;
end
local p = {x=pos.x, y=pos.y+1, z=pos.z, yaw = 0};
if( pos.o == 0 ) then
p.x = p.x - 1;
p.z = p.z + pos.bsizez - 1 - bed_nr;
-- p.z = p.z - math.floor(pos.bsizex/2+0.5) - bed_nr;
p.yaw = 90;
elseif( pos.o == 2 ) then
p.x = p.x + pos.bsizex;
-- p.z = p.z + math.floor(pos.bsizex/2+0.5) + bed_nr;
p.z = p.z + bed_nr;
p.yaw = 270;
elseif( pos.o == 1 ) then
p.z = p.z + pos.bsizez;
p.x = p.x + pos.bsizex - 1 - bed_nr;
-- p.x = p.x - math.floor(pos.bsizez/2+0.5) - bed_nr;
p.yaw = 0;
elseif( pos.o == 3 ) then
p.z = p.z - 1;
-- p.x = p.x + math.floor(pos.bsizez/2+0.5) + bed_nr;
p.x = p.x + bed_nr;
p.yaw = 180;
end
return p;
end
-- we do have a list of all nodenames the building contains (the .mts file provided it);
-- we can thus apply all replacements to these nodenames;
-- this also checks param2 and sets some other variables to indicate that it's i.e. a tree or a chest
-- (which both need special handling later on)
local function generate_building_translate_nodenames( nodenames, replacements, cid, binfo_scm, mirror_x, mirror_z )
if( not( nodenames )) then
return;
end
local i;
local v;
local new_nodes = {};
for i,node_name in ipairs( nodenames ) do
new_nodes[ i ] = {}; -- array for collecting information about the new content id for nodes with number "i" in their .mts savefile
-- some nodes may be called differently when mirrored; needed for doors
local new_node_name = node_name;
if( new_node_name and ( mirror_x or mirror_z ) and handle_schematics.mirrored_node[ new_node_name ] ) then
new_node_name = handle_schematics.mirrored_node[ node_name ];
new_nodes[ i ].is_mirrored = 1; -- currently unused
end
-- apply the replacements
if( new_node_name and replacements.table[ new_node_name ] ) then
new_node_name = replacements.table[ new_node_name ];
new_nodes[ i ].is_replaced = 1; -- currently unused
end
-- those chests do not exist as regular nodes; they're just placeholders
if( node_name == 'cottages:chest_private'
or node_name == 'cottages:chest_work'
or node_name == 'cottages:chest_storage' ) then
new_nodes[ i ].is_replaced = 1; -- currently unused
new_nodes[ i ].special_chest = node_name;
-- TODO: perhaps use a locked chest owned by the mob living there?
-- place a normal chest here
new_nodes[ i ].new_content = cid.c_chest;
new_nodes[ i ].special_chest = node_name;
new_node_name = 'default:chest';
elseif(new_node_name == 'cottages:chest_private'
or new_node_name == 'cottages:chest_work'
or new_node_name == 'cottages:chest_storage' ) then
new_nodes[ i ].is_replaced = 1; -- currently unused
new_nodes[ i ].special_chest = new_node_name;
-- TODO: perhaps use a locked chest owned by the mob living there?
-- place a normal chest here
new_nodes[ i ].new_content = cid.c_chest;
elseif( node_name == 'default:chest'
or new_node_name == 'default:chest' ) then
new_nodes[ i ].is_replaced = 1; -- currently unused
new_nodes[ i ].special_chest = 'default:chest';
new_nodes[ i ].new_content = cid.c_chest;
elseif( node_name == 'default:bookshelf'
or new_node_name == 'default:bookshelf' ) then
new_nodes[ i ].is_replaced = 1; -- currently unused
new_nodes[ i ].special_chest = 'default:bookshelf';
new_nodes[ i ].new_content = cid.c_bookshelf;
-- recognize beds
elseif( handle_schematics.bed_node_names[ node_name ]
or handle_schematics.bed_node_names[ new_node_name ]) then
new_nodes[ i ].is_bed = 1;
elseif( node_name == "mg_villages:mob_workplace_marker"
or new_node_name == "mg_villages:mob_workplace_marker" ) then
new_nodes[ i ].is_workplace_marker = 1;
end
-- only existing nodes can be placed
local regnode = handle_schematics.node_defined( new_node_name );
if( new_node_name and regnode) then
-- apply global replacements
if( handle_schematics.global_replacement_table[ new_node_name ]) then
new_node_name = handle_schematics.global_replacement_table[ new_node_name ];
end
new_nodes[ i ].new_node_name = new_node_name;
new_nodes[ i ].new_content = minetest.get_content_id( new_node_name );
if( regnode.on_construct ) then
new_nodes[ i ].on_construct = 1;
end
local new_content = new_nodes[ i ].new_content;
if( new_content == cid.c_dirt_with_grass ) then
new_nodes[ i ].is_grass = 1;
-- dirt acts as a general placeholder
elseif( new_content == cid.c_dirt ) then
new_nodes[ i ].is_dirt = 1;
elseif( new_content == cid.c_sapling
or new_content == cid.c_jsapling
or new_content == cid.c_psapling
or new_content == cid.c_asapling
or new_content == cid.c_aspsapling
or new_content == cid.c_savannasapling
or new_content == cid.c_pinesapling ) then
-- store that a tree is to be grown there
new_nodes[ i ].is_tree = 1;
elseif( new_content == cid.c_chest
or new_content == cid.c_bookshelf
or new_content == cid.c_chest_locked
or new_content == cid.c_chest_shelf
or new_content == cid.c_chest_ash
or new_content == cid.c_chest_aspen
or new_content == cid.c_chest_birch
or new_content == cid.c_chest_maple
or new_content == cid.c_chest_chestnut
or new_content == cid.c_chest_pine
or new_content == cid.c_chest_spruce) then
-- we're dealing with a chest that might need filling
new_nodes[ i ].is_chestlike = 1;
elseif( new_content == cid.c_sign ) then
-- the sign may require some text to be written on it
new_nodes[ i ].is_sign = 1;
-- doors need special treatment as they changed from 2 to 1 node
elseif( string.sub( node_name, 1, 6)=="doors:"
and string.sub( new_node_name, 1, 6)=="doors:" ) then
if( string.sub( new_node_name, -2 ) =="_a") then
new_nodes[ i ].is_door_a = 1;
elseif( string.sub( new_node_name, -2 ) =="_b") then
new_nodes[ i ].is_door_b = 1;
end
-- if we are placing a glass node, param2 needs to be set to 0
elseif( regnode and regnode.drawtype
and (regnode.drawtype=="glasslike" or regnode.drawtype=="glasslike_framed" or regnode.drawtype=="glasslike_framed_optional")) then
new_nodes[ i ].set_param2_to_0 = 1;
-- torches can be 3d now; for that, they use diffrent nodes
elseif( new_node_name == 'mg_villages:torch' or new_node_name == 'default:torch' ) then
new_nodes[ i ].is_torch = 1;
end
-- handle_schematics.get_param2_rotated( 'facedir', param2 ) needs to be called for nodes
-- which use either facedir or wallmounted;
-- realtest rotates some nodes diffrently and does not come with default:ladder
if( node_name == 'default:ladder' and handle_schematics.is_realtest) then
new_nodes[ i ].change_param2 = {}; --{ 2->1, 5->2, 3->3, 4->0 }
new_nodes[ i ].change_param2[2] = 1;
new_nodes[ i ].change_param2[5] = 2;
new_nodes[ i ].change_param2[3] = 3;
new_nodes[ i ].change_param2[4] = 0;
new_nodes[ i ].paramtype2 = 'facedir';
-- ..except if they are stairs or ladders
elseif( string.sub( node_name, 1, 7 ) == 'stairs:' or string.sub( node_name, 1, 6 ) == 'doors:') then
new_nodes[ i ].paramtype2 = 'facedir';
-- normal nodes
elseif( regnode and regnode.paramtype2 and (regnode.paramtype2=='facedir' or regnode.paramtype2=='wallmounted')) then
new_nodes[ i ].paramtype2 = regnode.paramtype2;
end
-- we tried our best, but the replacement node is not defined
elseif( new_node_name ~= 'mg:ignore' ) then
local msg = 'ERROR: Did not find a suitable replacement for '..tostring( node_name )..' (suggested but inexistant: '..
tostring( new_node_name )..'). Building: '..tostring( binfo_scm )..'.';
if( mg_villages and mg_villages.print ) then
mg_villages.print( mg_villages.DEBUG_LEVEL_WARNING, msg );
else
print( msg );
end
msg = nil;
new_nodes[ i ].ignore = 1; -- keep the old content
else -- handle mg:ignore
new_nodes[ i ].ignore = 1;
end
end
return new_nodes;
end
local function generate_building_what_to_place_here_and_how(t, node_content, new_nodes, cid, keep_ground, ground_type, mirror_x, mirror_z, pos )
local new_content = node_content;
if( not( t )) then
if( node_content ~= cid.c_plotmarker
and (not(handle_schematics.moresnow_installed) or not(moresnow) or node_content ~= moresnow.c_snow_top )) then
-- place nothing/air
return { new_content = cid.c_air, new_param2 = 0, n = {} };
end
end
-- TODO: there ought to be no error here....
if( not( t) or not( t[1] ) or not( new_nodes[ t[1]])
-- set air only if the new node is not of type ignore
or (not( new_nodes[ t[1]].new_content) and not(new_nodes[ t[1] ].ignore))) then
return { new_content = cid.c_air, new_param2 = 0, n = {} };
end
-- take care of replacements
local n = new_nodes[ t[1] ]; -- t[1]: id of the old node
if( not( n.ignore )) then
new_content = n.new_content;
else
new_content = node_content;
end
-- replace all dirt and dirt with grass at that x,z coordinate with the stored ground grass node;
if( n.is_grass and keep_ground and ground_type) then
new_content = ground_type;
elseif( n.is_dirt and node_content ~= cid.c_air ) then
new_content = node_content;
end
-- do not overwrite plotmarkers
if( node_content == cid.c_plotmarker or node_content == cid.c_mob_spawner) then
-- keep the old content
new_content = node_content;
end
-- remove misplaced scaffolding nodes
if( new_content == cid.c_air and (node_content == cid.c_scaffolding or node_content == cid.c_scaffolding_empty)) then
new_content = cid.c_air;
end
-- quite often we just need some kind of floor/ground - and do not depend on a particular node
if( handle_schematics.also_acceptable
and new_content ~= cid.c_ignore and node_content ~= cid.c_ignore
and handle_schematics.also_acceptable[ new_content ]
and handle_schematics.also_acceptable[ new_content ].is_ok
and handle_schematics.also_acceptable[ new_content ].is_ok[ node_content ]) then
new_content = node_content;
end
-- the old torch is split up into three new types
if( n.is_torch ) then
if( t[2]==0 ) then
new_content = cid.c_torch_ceiling;
elseif( t[2]==1 ) then
new_content = cid.c_torch;
else
new_content = cid.c_torch_wall;
end
end
local param2 = t[2];
-- handle rotation
if( n.paramtype2 ) then
if( n.change_param2 and n.change_param2[ t[2] ]) then
param2 = n.change_param2[ param2 ];
end
if( mirror_x and param2<24) then
param2 = handle_schematics.rotation_table[ n.paramtype2 ][ param2+1 ][ pos.brotate+1 ][ 2 ];
elseif( mirror_z and param2<24) then
param2 = handle_schematics.rotation_table[ n.paramtype2 ][ param2+1 ][ pos.brotate+1 ][ 3 ];
elseif( param2 < 24) then
param2 = handle_schematics.rotation_table[ n.paramtype2 ][ param2+1 ][ pos.brotate+1 ][ 1 ];
end
end
-- glasslike nodes need to have param2 set to 0 (else they get a strange fill state)
if( n.set_param2_to_0 ) then
param2 = 0;
end
return { new_content = new_content, new_param2 = param2, n = n };
end
-- if scaffolding_only is set, a statistic of missing_nodes will be returned
handle_schematics.generate_building = function(pos, minp, maxp, data, param2_data, a, extranodes, replacements, cid, extra_calls, building_nr_in_bpos, village_id, binfo_extra, road_node, keep_ground, scaffolding_only)
local binfo = binfo_extra;
if( not( binfo ) and mg_villages) then
binfo = mg_villages.BUILDINGS[pos.btype]
end
local scm
-- the building got removed from mg_villages.BUILDINGS in the meantime
if( not( binfo )) then
return;
end
-- schematics of .mts type are not handled here; they need to be placed using place_schematics
if( binfo.is_mts and binfo.is_mts == 1 ) then
return;
end
-- roads are very simple structures that are not stored as schematics
if( pos.btype == 'road' ) then
handle_schematics.place_road( minp, maxp, data, param2_data, a, road_node, pos, cid.c_air );
return;
end
if( not( pos.no_plotmarker )) then
generate_building_plotmarker( pos, minp, maxp, data, param2_data, a, cid, building_nr_in_bpos, village_id, binfo.scm );
end
-- skip building if it is not located at least partly in the area that is currently beeing generated
if( pos.x > maxp.x or pos.x + pos.bsizex < minp.x
or pos.z > maxp.z or pos.z + pos.bsizez < minp.z ) then
return;
end
if( pos.btype and
(( binfo.sizex ~= pos.bsizex and binfo.sizex ~= pos.bsizez )
or ( binfo.sizez ~= pos.bsizex and binfo.sizez ~= pos.bsizez )
or not( binfo.scm_data_cache ))) then
if( mg_villages and mg_villages.print ) then
mg_villages.print( mg_villages.DEBUG_LEVEL_WARNING,
'ERROR: This village was created using diffrent buildings than those known know. Cannot place unknown building.');
else
print( 'ERROR: Size information about this building differs. Cannot place building.');
end
return;
end
if( binfo.scm_data_cache )then
scm = binfo.scm_data_cache;
else
scm = binfo.scm
end
-- the fruit is set per building, not per village as the other replacements
if( binfo.farming_plus and binfo.farming_plus == 1 and pos.fruit and mg_villages) then
mg_villages.get_fruit_replacements( replacements, pos.fruit);
end
-- statistic containing information about nodes that still need to be placed (only of intrest if scaffolding_only is set)
local missing_nodes = {};
local c_ignore = minetest.get_content_id("ignore")
local c_air = minetest.get_content_id("air")
local c_snow = GCIR( "default:snow", replacements);
local c_dirt = GCIR( "default:dirt", replacements );
local c_dirt_with_grass = GCIR( "default:dirt_with_grass", replacements );
local c_dirt_with_snow = GCIR( "default:dirt_with_snow", replacements );
local c_scaffolding_empty = GCIR( "handle_schematics:support", replacements );
local c_scaffolding = GCIR( "handle_schematics:support_setup", replacements );
local c_dig_here = GCIR( "handle_schematics:dig_here", replacements );
-- freeze water if there is moresnow on top
cid.c_water_source = GCIR( "default:water_source", replacements);
cid.c_river_water_source = GCIR( "default:river_water_source", replacements);
cid.c_ice = GCIR( "default:ice", replacements);
local scm_x = 0;
local scm_z = 0;
local step_x = 1;
local step_z = 1;
local scm_z_start = 0;
if( pos.brotate == 2 ) then
scm_x = pos.bsizex+1;
step_x = -1;
end
if( pos.brotate == 1 ) then
scm_z = pos.bsizez+1;
step_z = -1;
scm_z_start = scm_z;
end
local mirror_x = false;
local mirror_z = false;
if( pos.mirror ) then
if( binfo.axis and binfo.axis == 1 ) then
mirror_x = true;
mirror_z = false;
-- used for "restore original landscape"
elseif( binfo.axis and binfo.axis == 3 ) then
mirror_z = true;
mirror_x = true;
else
mirror_x = false;
mirror_z = true;
end
end
-- translate all nodenames and apply the replacements
local new_nodes = generate_building_translate_nodenames( binfo.nodenames, replacements, cid, binfo.scm, mirror_x, mirror_z );
for x = 0, pos.bsizex-1 do
scm_x = scm_x + step_x;
scm_z = scm_z_start;
for z = 0, pos.bsizez-1 do
scm_z = scm_z + step_z;
local xoff = scm_x;
local zoff = scm_z;
if( pos.brotate == 2 ) then
if( mirror_x ) then
xoff = pos.bsizex - scm_x + 1;
end
if( mirror_z ) then
zoff = scm_z;
else
zoff = pos.bsizez - scm_z + 1;
end
elseif( pos.brotate == 1 ) then
if( mirror_x ) then
xoff = pos.bsizez - scm_z + 1;
else
xoff = scm_z;
end
if( mirror_z ) then
zoff = pos.bsizex - scm_x + 1;
else
zoff = scm_x;
end
elseif( pos.brotate == 3 ) then
if( mirror_x ) then
xoff = pos.bsizez - scm_z + 1;
else
xoff = scm_z;
end
if( mirror_z ) then
zoff = scm_x;
else
zoff = pos.bsizex - scm_x + 1;
end
elseif( pos.brotate == 0 ) then
if( mirror_x ) then
xoff = pos.bsizex - scm_x + 1;
end
if( mirror_z ) then
zoff = pos.bsizez - scm_z + 1;
end
end
local has_snow = false;
local ground_type = c_dirt_with_grass;
-- remove all dig_here-indicators
if( scaffolding_only ) then
local ax = pos.x+x;
local az = pos.z+z;
for y = 0, binfo.ysize-1 do
local ay = pos.y+y+binfo.yoff;
if (ax >= minp.x and ax <= maxp.x) and (ay >= minp.y and ay <= maxp.y) and (az >= minp.z and az <= maxp.z) then
if( data[a:index(ax, ay, az)] == c_dig_here ) then
data[a:index(ax, ay, az)] = cid.c_air;
-- remove old and obsolete metadata
table.insert( extra_calls.clear_meta, {x=ax, y=ay, z=az});
end
end
end
end
for y = 0, binfo.ysize-1 do
local ax = pos.x+x;
local ay = pos.y+y+binfo.yoff;
local az = pos.z+z;
if (ax >= minp.x and ax <= maxp.x) and (ay >= minp.y and ay <= maxp.y) and (az >= minp.z and az <= maxp.z) then
local new_content = c_air;
local t = cid.c_ignore;
if( scm and scm[y+1] and scm[y+1][xoff] ) then
t = scm[y+1][xoff][zoff];
end
local node_content = data[a:index(ax, ay, az)];
if( binfo.yoff+y == 0 ) then
-- no snow on the gravel roads
if( node_content == c_dirt_with_snow or data[a:index(ax, ay+1, az)]==c_snow) then
has_snow = true;
end
ground_type = node_content;
end
-- -- we have the wrong node there
-- elseif( ((not(t) and current_content ~= cid.c_air)
-- or (t and new_nodes[t[1]] and not(new_nodes[t[1]].ignore) and current_content ~= new_nodes[ t[1]].new_content))
-- -- TODO: detect wrong rotation (param2)
--
-- and current_content ~= c_scaffolding
-- and current_content ~= c_dig_here
-- and ay<maxp.y) then
-- -- there is air above; we can place a digging indicator
-- if( data[a:index(ax, ay+1, az)] == cid.c_air or data[a:index(ax, ay+1, az)]==c_scaffolding) then
-- data[ a:index(ax, ay+1, az)] = c_dig_here;
-- end
-- end
-- which node (and which rotation) do we need here?
local res = generate_building_what_to_place_here_and_how(t, node_content, new_nodes, cid, keep_ground, ground_type, mirror_x, mirror_z, pos )
local new_content = res.new_content;
local param2 = res.new_param2;
local n = res.n;
-- scaffolding nodes are only placed when there is air now and there ought to be a node from the building
if( scaffolding_only ) then
-- new_content is adjusted later on, so store here what might be missing so that we can create the missing_nodes statistic
local new_content_wanted = new_content;
-- a node is to be placed here AND it is diffrent from the existing one AND
-- the existing node is not air, scaffolding, special scaffolding or a dig-here-indicator
-- -> the existing node needs to be digged
if(new_content and new_content ~= node_content
and node_content ~= cid.c_air and node_content ~= c_scaffolding and node_content ~= c_scaffolding_empty
-- if this is just a plant in a diffrent growth stage: ignore it
and not( handle_schematics.player_can_provide[new_content] and
handle_schematics.player_can_provide[new_content] ==
handle_schematics.player_can_provide[node_content])) then
local h;
-- search upward for the first empty (air) node or dig-here-indicator and place a dig_here-indicator
for h=ay, maxp.y do
local node_here = data[ a:index(ax, h, az)];
if( node_here == cid.c_air or node_here == c_dig_here or node_here == c_scaffolding or node_here == c_scaffolding_empty ) then
if( data[ a:index(ax, h, az)] ~= c_dig_here ) then
table.insert( extra_calls.scaffolding, {x=ax, y=h, z=az, dig_down = h-ay, what_where = {{ ay, new_content, param2}}});
else
-- store which node needs to be placed at which height
for i,v in ipairs( extra_calls.scaffolding ) do
if( v.x==ax and v.y==h and v.z==az ) then
table.insert( v.what_where, { ay, new_content, param2});
end
end
end
data[ a:index(ax, h, az)] = c_dig_here;
-- how much is to be digged is counted later on (when evaluating extra_calls.scaffolding)
break;
end
end
-- keep the old content
new_content = node_content;
param2 = param2_data[a:index(ax, ay, az)];
n = {};
-- a new node is to be placed here AND it is not air or ignore AND
-- the existing node is either air, scaffolding or special scaffolding
-- -> place special scaffolding to indicate which node is wanted here
elseif(new_content and new_content ~= cid.c_air and new_content ~= cid.c_ignore
and (node_content == cid.c_air or node_content == c_scaffolding or node_content == c_scaffolding_empty)) then
-- store what we expect/want at this place
table.insert( extra_calls.scaffolding, {x=ax, y=ay, z=az, node_wanted=new_content, param2_wanted=param2});
-- place scaffolding instead of the wanted node
-- TODO: count how many scaffolding nodes where placed
new_content = c_scaffolding;
param2 = 0;
n = {};
-- a new node is to be placed here AND it is air AND
-- there is a scaffolding node already
-- -> remove this misplaced scaffolding node
elseif( new_content and new_content == cid.c_air
and (node_content == c_scaffolding or node_content == c_scaffolding_empty)) then
-- we need to remove metadata at this position
table.insert( extra_calls.clear_meta, {x=ax, y=ay, z=az});
new_content = cid.c_air;
param2 = 0;
n = {};
-- let the old node continue to exist
else
new_content = node_content;
param2 = param2_data[a:index(ax, ay, az)];
n = {};
end
-- create a statistic of all missing nodes
if( node_content ~= new_content_wanted and node_content and new_content_wanted
and new_content_wanted ~= cid.c_air and new_content_wanted ~= cid.c_ignore
and not( handle_schematics.player_can_provide[new_content] and
handle_schematics.player_can_provide[new_content] ==
handle_schematics.player_can_provide[node_content])) then
if( not( missing_nodes[ new_content_wanted ])) then
missing_nodes[ new_content_wanted ] = 1;
else
missing_nodes[ new_content_wanted ] = missing_nodes[ new_content_wanted ] + 1;
end
end
end
-- actually change the node
data[ a:index(ax, ay, az)] = new_content;
param2_data[a:index(ax, ay, az)] = param2;
-- some nodes need on_construct to be called in order to be set up properly
if( n.on_construct ) then
if( not( extra_calls.on_constr[ new_content ] )) then
extra_calls.on_constr[ new_content ] = { {x=ax, y=ay, z=az}};
else
table.insert( extra_calls.on_constr[ new_content ], {x=ax, y=ay, z=az});
end
end
-- store that a tree is to be grown there
if( n.is_tree ) then
table.insert( extra_calls.trees, {x=ax, y=ay, z=az, typ=new_content, snow=has_snow});
-- we're dealing with a chest that might need filling
elseif( n.is_chestlike ) then
table.insert( extra_calls.chests, {x=ax, y=ay, z=az, typ=new_content, bpos_i=building_nr_in_bpos, typ_name=n.special_chest});
-- the sign may require some text to be written on it
elseif( n.is_sign ) then
table.insert( extra_calls.signs, {x=ax, y=ay, z=az, typ=new_content, bpos_i=building_nr_in_bpos});
-- doors need the state param to be set (which depends on param2)
elseif( n.is_door_a ) then
table.insert( extra_calls.door_a, {x=ax, y=ay, z=az, typ=new_content, p2=param2_data[a:index(ax, ay, az)]});
elseif( n.is_door_b ) then
table.insert( extra_calls.door_b, {x=ax, y=ay, z=az, typ=new_content, p2=param2_data[a:index(ax, ay, az)]});
-- beds are of special intrest to npc
elseif( n.is_bed ) then
local found = false;
for i,bed in ipairs(extra_calls.beds ) do
if( bed and bed.x == ax and bed.y == ay and bed.z == az ) then
found = true;
end
end
if( not( found)) then
table.insert( extra_calls.beds, {x=ax, y=ay, z=az, typ=new_content, p2=param2_data[a:index(ax, ay, az)]});
end
-- workplace markers need to know their village_id, plot_nr etc. as well
elseif( n.is_workplace_marker ) then
local found = false;
for i,w in ipairs(extra_calls.workplaces ) do
if( w and w.x == ax and w.y == ay and w.z == az ) then
found = true;
end
end
if( not( found)) then
table.insert( extra_calls.workplaces, {x=ax, y=ay, z=az, typ=new_content, p2=param2_data[a:index(ax, ay, az)]});
end
end
end
end
local ax = pos.x + x;
local az = pos.z + z;
local y_top = pos.y+binfo.yoff+binfo.ysize;
if( y_top+1 > maxp.y ) then
y_top = maxp.y-1;
end
local y_bottom = pos.y+binfo.yoff;
if( y_bottom < minp.y ) then
y_bottom = minp.y;
end
if( has_snow and ax >= minp.x and ax <= maxp.x and az >= minp.z and az <= maxp.z ) then
local res = handle_schematics.mg_drop_moresnow( ax, az, y_top, y_bottom-1, a, data, param2_data, cid);
if( res and (data[ a:index(ax, res.height, az)]==cid.c_air
or data[ a:index(ax, res.height, az)]==cid.c_water )) then
data[ a:index(ax, res.height, az)] = res.suggested.new_id;
param2_data[a:index(ax, res.height, az)] = res.suggested.param2;
has_snow = false;
end
end
end
end
if( scaffolding_only ) then
return missing_nodes;
end
end
-- actually place the buildings (at least those which came as .we files; .mts files are handled later on)
-- this code was also responsible for tree placement;
-- place_buildings is used by mg_villages exclusively. It calls the local function generate_building and
-- therefore resides in this file.
handle_schematics.place_buildings = function(village, minp, maxp, data, param2_data, a, cid, village_id)
-- this function is only relevant for mg_villages
if( not( mg_villages )) then
return;
end
local vx, vz, vs, vh = village.vx, village.vz, village.vs, village.vh
local village_type = village.village_type;
local bpos = village.to_add_data.bpos;
local replacements = mg_villages.get_replacement_table( village.village_type, nil, village.to_add_data.replacements );
cid.c_chest = GCIR( 'default:chest', replacements );
cid.c_bookshelf = GCIR( 'default:bookshelf', replacements );
cid.c_chest_locked = GCIR( 'default:chest_locked', replacements );
cid.c_chest_shelf = GCIR( 'cottages:shelf', replacements );
cid.c_chest_ash = GCIR( 'trees:chest_ash', replacements );
cid.c_chest_aspen = GCIR( 'trees:chest_aspen', replacements );
cid.c_chest_birch = GCIR( 'trees:chest_birch', replacements );
cid.c_chest_maple = GCIR( 'trees:chest_maple', replacements );
cid.c_chest_chestnut = GCIR( 'trees:chest_chestnut', replacements );
cid.c_chest_pine = GCIR( 'trees:chest_pine', replacements );
cid.c_chest_spruce = GCIR( 'trees:chest_spruce', replacements );
cid.c_sign = GCIR( 'default:sign_wall', replacements );
cid.c_torch = GCIR( 'default:torch', replacements );
cid.c_torch_ceiling = GCIR( 'default:torch_ceiling', replacements );
cid.c_torch_wall = GCIR( 'default:torch_wall', replacements );
--print('REPLACEMENTS: '..minetest.serialize( replacements.table )..' CHEST: '..tostring( minetest.get_name_from_content_id( cid.c_chest ))); -- TODO
local extranodes = {}
local extra_calls = { on_constr = {}, trees = {}, chests = {}, signs = {}, traders = {}, door_a = {}, door_b = {}, scaffolding = {}, clear_meta = {}, beds = {}, workplaces = {} };
for i, pos in ipairs(bpos) do
-- roads are only placed if there are at least mg_villages.MINIMAL_BUILDUNGS_FOR_ROAD_PLACEMENT buildings in the village
if( not(pos.btype) or pos.btype ~= 'road' or village.anz_buildings > mg_villages.MINIMAL_BUILDUNGS_FOR_ROAD_PLACEMENT )then
-- replacements are in table format for mapgen-based building spawning
local road_material = mg_villages.road_node;
if( pos.road_material ) then
road_material = pos.road_material;
end
-- we need to collect the positions of all beds...even if they are placed in diffrent mapchunks
if( not( pos.beds )) then
pos.beds = {};
end
extra_calls.beds = pos.beds;
if( not( pos.workplaces )) then
pos.workplaces = {};
end
extra_calls.workplaces = pos.workplaces;
-- do not use scaffolding here; place the building directly
handle_schematics.generate_building(pos, minp, maxp, data, param2_data, a, extranodes, replacements, cid, extra_calls, i, village_id, nil, road_material, true, false )
-- the bed positions are of intrest later on
pos.beds = extra_calls.beds;
pos.workplaces = extra_calls.workplaces;
end
end
-- we store the beds per building; not per village
extra_calls.beds = nil;
extra_calls.workplaces = nil;
-- replacements are in list format for minetest.place_schematic(..) type spawning
return { extranodes = extranodes, bpos = bpos, replacements = replacements.list, dirt_roads = village.to_add_data.dirt_roads,
plantlist = village.to_add_data.plantlist, extra_calls = extra_calls };
end
-- get content ids replaced
handle_schematics.get_cid_table = function( replacements )
-- only very few nodes are actually used from the cid table (content ids)
local cid = {};
cid.c_air = minetest.get_content_id( 'air' );
cid.c_dirt = GCIR( 'default:dirt', replacements );
cid.c_dirt_with_grass = GCIR( 'default:dirt_with_grass',replacements );
cid.c_sapling = GCIR( 'default:sapling', replacements );
cid.c_jsapling = GCIR( 'default:junglesapling', replacements );
cid.c_psapling = GCIR( 'default:pine_sapling', replacements );
cid.c_asapling = GCIR( 'default:acacia_sapling', replacements );
cid.c_aspsapling = GCIR( 'default:aspen_sapling', replacements );
cid.c_savannasapling = GCIR( 'mg:savannasapling', replacements );
cid.c_pinesapling = GCIR( 'mg:pinesapling', replacements );
cid.c_plotmarker = GCIR( 'mg_villages:plotmarker', replacements );
cid.c_mob_spawner = GCIR( 'mg_villages:mob_spawner',replacements );
cid.c_chest = GCIR( 'default:chest', replacements );
cid.c_chest_locked = GCIR( 'default:chest_locked', replacements );
cid.c_chest_shelf = GCIR( 'cottages:shelf', replacements );
cid.c_chest_ash = GCIR( 'trees:chest_ash', replacements );
cid.c_chest_aspen = GCIR( 'trees:chest_aspen', replacements );
cid.c_chest_birch = GCIR( 'trees:chest_birch', replacements );
cid.c_chest_maple = GCIR( 'trees:chest_maple', replacements );
cid.c_chest_chestnut = GCIR( 'trees:chest_chestnut', replacements );
cid.c_chest_pine = GCIR( 'trees:chest_pine', replacements );
cid.c_chest_spruce = GCIR( 'trees:chest_spruce', replacements );
cid.c_sign = GCIR( 'default:sign_wall', replacements );
cid.c_torch = GCIR( 'default:torch', replacements );
cid.c_torch_ceiling = GCIR( 'default:torch_ceiling', replacements );
cid.c_torch_wall = GCIR( 'default:torch_wall', replacements );
-- for roads
cid.c_sign = GCIR( 'default:gravel', replacements );
-- additional entries from mg_villages
cid.c_ignore = minetest.get_content_id( 'ignore' );
cid.c_stone = GCIR( 'default:stone', replacements);
cid.c_snow = GCIR( 'default:snow', replacements);
cid.c_snowblock = GCIR( 'default:snowblock', replacements);