-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake-functions.pl
More file actions
executable file
·378 lines (293 loc) · 10.1 KB
/
make-functions.pl
File metadata and controls
executable file
·378 lines (293 loc) · 10.1 KB
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
#!/usr/bin/perl
#
# Includes
use strict;
use warnings;
#
# Constants
# Help screen.
my ($help_screen);
$help_screen = << 'Endofblock';
Usage: make-functions.pl (target directory)
Endofblock
#
# Testing switches.
# "Smoke test" switch, generating the fewest test cases.
my ($smoke_test);
$smoke_test = 0;
# "Version" switch, forcing older versions.
# Use '' or 'latest' for the current version.
my ($force_version);
$force_version = '';
#$force_version = '1.15';
#
# Configuration variables.
# Version.
my ($version);
if ( '1.15' eq $force_version )
{ $version = '20200622'; }
else
{ $version = '20240107'; }
# Ticks to wait before doing anything.
my ($tickdelay);
$tickdelay = 20;
# Movement distances.
# These need to be multiples or factors of 16, and within chunk-loading
# distance. Ones smaller than the spaceship size are dropped.
my (@distlist);
# Baseline version.
#@distlist = ( 16, 32, 64 );
# Extended version. Cap at 64, even though 96 should work.
@distlist = ( 4, 8, 16, 32, 48, 64 );
# Smoke test version.
if ($smoke_test)
{ @distlist = ( 32 ); }
# Space Bunny configuration.
my ($bunnyreportname, $bunnyrealname, $bunnytype);
$bunnyreportname = 'Warp Bunny';
# This should be something that nobody would accidentally duplicate.
# Or deliberately duplicate without reading this script or the functions.
$bunnyrealname = 'SpaceBunnyEntity';
# This was originally a rabbit, but that resulted in spaceships raining
# rabbit bits down underneath them, causing some players distress.
# With markers, check all other scripts for interactions in case one of them
# decides to affect all markers in the world while a spaceship is active.
#$bunnytype = 'rabbit';
$bunnytype = 'marker';
# NBT tag and NBT selector for Space Bunny entities.
# This also includes an "everything except space bunnies" selector.
# NOTE - 1.20.4 changed the way text selection works.
my ($bunnytag, $bunnyselector, $bunnynonselector);
if ( '1.15' eq $force_version )
{
$bunnytag = 'CustomName:\'{"text":"' . $bunnyrealname . '"}\'';
$bunnyselector = 'type=' . $bunnytype . ',nbt={' . $bunnytag . '}';
$bunnynonselector = 'type=!' . $bunnytype;
}
else
{
$bunnytag = 'CustomName:\'"' . $bunnyrealname . '"\'';
$bunnyselector = 'type=' . $bunnytype . ',nbt={' . $bunnytag . '}';
$bunnynonselector = 'nbt=!{' . $bunnytag . '}';
}
#
# Functions
# Writes the specified string to a file.
# Arg 0 is the filename to write to.
# Arg 1 is the text to write.
# No return value.
sub WriteFile
{
my ($oname, $text);
$oname = $_[0];
$text = $_[1];
if (!( (defined $oname) && (defined $text) ))
{
print "### [WriteFile] Bad arguments.\n";
}
elsif (!open(OFILE, ">$oname"))
{
print "### [WriteFile] Couldn't write to \"$oname\".\n";
}
else
{
print OFILE $text;
close(OFILE);
}
}
# This generates scripts for one specific motion of a ship of one size.
# Arg 0 is the base directory to put scripts in.
# Arg 1 is the subdirectory for this ship size's scripts.
# Arg 2 is the ship width (X).
# Arg 3 is the ship height (Y).
# Arg 4 is the ship depth (Z).
# Arg 5 is the X motion offset ('' or 0 for none).
# Arg 6 is the Y motion offset ('' or 0 for none).
# Arg 7 is the Z motion offset ('' or 0 for none).
# Arg 8 is a unique label to use for this motion.
# No return value.
sub GenerateMovement
{
my ($outdir, $casedir, $xsize, $ysize, $zsize, $dx, $dy, $dz, $label);
my ($prefix);
my ($xmid, $ymid, $zmid, $radius);
$outdir = $_[0];
$casedir = $_[1];
$xsize = $_[2];
$ysize = $_[3];
$zsize = $_[4];
$dx = $_[5];
$dy = $_[6];
$dz = $_[7];
$label = $_[8];
if (!( (defined $outdir) && (defined $casedir)
&& (defined $xsize) && (defined $ysize) && (defined $zsize)
&& (defined $dx) && (defined $dy) && (defined $dz)
&& (defined $label) ))
{
print "### [GenerateMovement] Bad arguments.\n";
}
else
{
$prefix = "$casedir/$label";
# Entry point.
# This is a mutex wrapper for the real entry point.
WriteFile( "$outdir/$prefix" . '.mcfunction',
"execute if entity \@e[$bunnyselector] run"
. " say $bunnyreportname is busy!\n"
. "execute unless entity \@e[$bunnyselector] run"
. " function cjt_ship:$prefix" . "_real\n" );
# Real entry point.
WriteFile( "$outdir/$prefix" . '_real.mcfunction',
"function cjt_ship:makebunny\n"
. 'schedule function cjt_ship:' . $prefix . 'copy ' . $tickdelay . "\n"
. 'schedule function cjt_ship:' . $prefix . 'tport '
. ($tickdelay + 2) . "\n"
. 'schedule function cjt_ship:' . $casedir . '/erase '
. ($tickdelay + 4) . "\n"
. 'schedule function cjt_ship:killbunny ' . ($tickdelay + 6) . "\n"
);
# Copy helper.
# NOTE - Do not use "replace move". That leaves the passengers falling,
# and the displacement will carry over during the teleport.
WriteFile( "$outdir/$prefix" . 'copy.mcfunction',
"execute at \@e[$bunnyselector] run clone ~ ~ ~ ~" . ($xsize - 1)
. " ~" . ($ysize - 1) . " ~" . ($zsize - 1)
. " ~$dx ~$dy ~$dz\n" );
# Teleport helper.
# NOTE - Don't move the SpaceBunny. SpaceBunny has to stay where it is.
# Do move all other critters, in case horses or dogs are on board.
# Remember "at @s"! Otherwise everything moves to the same location.
# FIXME - Bedrock Edition can use tilde notation in a volume selector.
# Java Edition can't, so we have to do radius from a midpoint.
# This will pull along entities that are near but not in the ship.
$xmid = ($xsize - 1) * 0.5;
$ymid = ($ysize - 1) * 0.5;
$zmid = ($zsize - 1) * 0.5;
$radius = 0.5 * sqrt($xsize * $xsize + $ysize * $ysize + $zsize * $zsize);
$radius = sprintf('%.2f', $radius);
WriteFile( "$outdir/$prefix" . 'tport.mcfunction',
"execute at \@e[$bunnyselector] positioned ~$xmid ~$ymid ~$zmid"
. ' as @e[' . $bunnynonselector . ',distance=..' . $radius . ']'
. ' at @s run teleport @s' . " ~$dx ~$dy ~$dz\n" );
}
}
# Entry point for generating scripts for a given ship size.
# NOTE - This supports different X and Z even though our test cases have
# them the same.
# Arg 0 is the directory to put scripts in.
# Arg 1 is a prefix label for the scripts (identifying the ship size).
# Arg 2 is the width (X).
# Arg 3 is the height (Y).
# Arg 4 is the depth (Z). (If undef, defaults to width.)
# No return value.
sub GenerateScripts
{
my ($outdir, $label, $xsize, $ysize, $zsize);
my ($cmd, $result);
my ($thisdist);
my ($xmid, $ymid, $zmid, $radius);
$outdir = $_[0];
$label = $_[1];
$xsize = $_[2];
$ysize = $_[3];
$zsize = $_[4];
if (!(defined $zsize))
{ $zsize = $xsize; }
if (!( (defined $outdir) && (defined $label)
&& (defined $xsize) && (defined $ysize) && (defined $zsize) ))
{
print "### [GenerateScripts] Bad arguments.\n";
}
else
{
# Progress banner, since this takes a while (maybe due to shell calls?).
print ".. Generating \"$label\"...\n";
# NOTE - Minecraft does not like more than one underscore in a name.
# The approved way of grouping functions is subfolders.
# Make a folder for this set of scripts.
$cmd = "mkdir $outdir/$label";
$result = `$cmd`;
# Calculate dimensions for the entity selection volume.
$xmid = ($xsize - 1) * 0.5;
$ymid = ($ysize - 1) * 0.5;
$zmid = ($zsize - 1) * 0.5;
$radius = 0.5 * sqrt($xsize * $xsize + $ysize * $ysize + $zsize * $zsize);
$radius = sprintf('%.2f', $radius);
# The "erase" script is common to all.
WriteFile( "$outdir/$label/erase.mcfunction",
# First, destroy blocks in the target volume.
"execute at \@e[$bunnyselector] run fill ~ ~ ~ ~" . ($xsize - 1)
. " ~" . ($ysize - 1) . " ~" . ($zsize - 1) . " air\n"
# Next, destroy all item entities in the target volume.
# Borrow the teleport volume specifier, since we can't use ~ notation.
. "execute at \@e[$bunnyselector] positioned ~$xmid ~$ymid ~$zmid"
. ' run kill @e[type=item,distance=..' . $radius . "]\n"
);
# Per-direction, per-distance scripts.
foreach $thisdist (@distlist)
{
if ($thisdist >= $xsize)
{
GenerateMovement($outdir, $label, $xsize, $ysize, $zsize,
-$thisdist, '', '', "west$thisdist");
GenerateMovement($outdir, $label, $xsize, $ysize, $zsize,
$thisdist, '', '', "east$thisdist");
}
if ($thisdist >= $ysize)
{
GenerateMovement($outdir, $label, $xsize, $ysize, $zsize,
'', -$thisdist, '', "down$thisdist");
GenerateMovement($outdir, $label, $xsize, $ysize, $zsize,
'', $thisdist, '', "up$thisdist");
}
if ($thisdist >= $zsize)
{
GenerateMovement($outdir, $label, $xsize, $ysize, $zsize,
'', '', -$thisdist, "north$thisdist");
GenerateMovement($outdir, $label, $xsize, $ysize, $zsize,
'', '', $thisdist, "south$thisdist");
}
}
}
}
#
# Main Program
my ($outdir);
$outdir = $ARGV[0];
if (!(defined $outdir))
{
print $help_screen;
}
else
{
WriteFile("$outdir/version.mcfunction",
"say CJT spaceship scripts version $version.\n");
# FIXME - This should also be invisible, but that's tricky to get working.
# NOTE - The rabbit needs to be invulnerable. Otherwise operations fail
# when the rabbit dies mid-transport.
# Markers default to being invisible and invulnerable, but critters need
# specific tags. Add them even if we're using a marker.
WriteFile("$outdir/makebunny.mcfunction",
"summon $bunnytype ~ ~ ~ {NoAI:1,Invulnerable:1,$bunnytag}\n");
WriteFile("$outdir/killbunny.mcfunction", "kill \@e[$bunnyselector]\n");
if ($smoke_test)
{
# Smoke test.
GenerateScripts($outdir, "disc8", 8, 4);
}
else
{
# Full version.
GenerateScripts($outdir, "cube8", 8, 8);
GenerateScripts($outdir, "cube16", 16, 16);
GenerateScripts($outdir, "cube32", 32, 32);
GenerateScripts($outdir, "tower16", 8, 16);
GenerateScripts($outdir, "tower32", 16, 32);
GenerateScripts($outdir, "disc8", 8, 4);
GenerateScripts($outdir, "disc16", 16, 8);
GenerateScripts($outdir, "disc32", 32, 16);
}
}
#
# This is the end of the file.