-
Notifications
You must be signed in to change notification settings - Fork 1
/
getBiomeDistribution.sh
executable file
·113 lines (90 loc) · 2.79 KB
/
getBiomeDistribution.sh
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
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\t\n'
: "${F7D2D:?Please export F7D2D with 7D2D install folder}"
if [[ $# -ne 4 ]]; then
echo >&2 "$0 <prefabs.xml> <biomes.png> <size> <seed>"
exit 1
fi
coordsFor() {
declare -g tr bl
declare COORDS DIM ROT
COORDS="$1"
DIM="$2"
ROT="$3"
WIDTH="${DIM%%,*}"
HEIGHT="${DIM##*,}"
if [[ $((ROT % 2)) -eq 1 ]]; then
tmp="$WIDTH"
WIDTH="$HEIGHT"
HEIGHT="$tmp"
fi
X1=$((${COORDS%%,*} + CENTER))
Z2=$((-(${COORDS##,*}) + CENTER))
X2=$((X1 + WIDTH))
Z1=$((Z2 - HEIGHT))
tr="$((X2)),$((Z1))"
bl="$((X1)),$((Z2))"
}
BIN="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
XML="$1"
IMG="$2"
SIZE="$3"
SEED="$4"
IMGSIZE="${SIZE}x${SIZE}"
NAME="$SEED-$SIZE"
CENTER="$((SIZE / 2))"
COUNT_MASK="mask-count-$NAME.txt"
AREA_MASK="mask-area-$NAME.txt"
COUNT="$NAME-biome-count.txt"
declare -g -A DIM
declare -a MAPFILE
mapfile < <(xmlstarlet sel -t -m "/prefabs/decoration" -v "@name" -o ";" -v "@position" -o ";" -v "@rotation" -n - < "$XML")
rm -f "$COUNT_MASK" "$AREA_MASK"
for decoration in "${MAPFILE[@]}"; do
IFS=';' read -r prefab coords rotation <<<"$decoration"
if [[ -n ${DIM["$prefab"]+abc} ]]; then
dim="${DIM["$prefab"]}"
else
dim="$("${BIN}/prefabSize.sh" "${prefab}")"
DIM["$prefab"]="$dim"
fi
coordsFor "${coords}" "${dim}" "${rotation}"
echo "point ${bl}" >> "${COUNT_MASK}"
echo "rectangle ${tr} ${bl}" >> "${AREA_MASK}"
done
declare -A BIOMES
BIOMES[Forest]='#004000(FF)?'
BIOMES[Burnt Forest]='#BA00FF(FF)?'
BIOMES[Wasteland]='#FFA800(FF)?'
BIOMES[Desert]='#FFE477(FF)?'
BIOMES[Snow]='#FFFFFF(FF)?'
# Change from histogram data to "<color> <count>"
SUBST='s/^ *([0-9]*):.* (#[0-9A-Fa-f]*) .*$/\2,\1/'
# Replace colors with biome names
for biome in "${!BIOMES[@]}"; do
SUBST="$SUBST;s/${BIOMES[$biome]}/$biome/"
done
# Delete empty region
SUBST="$SUBST;/#000000(FF)?/d"
echo 'Biome,Prefab Count,Prefab Area,Area' > "${COUNT}"
listBiomes() {
printf "%s\n" "${!BIOMES[@]}" | sort
}
showPrefabs() {
join -t , \
<(convert "$IMG" \
\( -size "${IMGSIZE}" xc:black -fill white -draw "@${COUNT_MASK}" -transparent white \) \
-composite \
-format %c histogram:info:- | sed -Ee "$SUBST" | sort) \
<(convert "$IMG" \
\( -size "${IMGSIZE}" xc:black -fill white -draw "@${AREA_MASK}" -transparent white \) \
-composite \
-format %c histogram:info:- | sed -Ee "$SUBST" | sort)
}
showBiomes() {
convert "$IMG" -format %c histogram:info:- | sed -Ee "$SUBST" | sort
}
join -a 1 -e 0 -o 1.1,2.2,2.3,2.4 -t , <(listBiomes) \
<(join -a 2 -e 0 -o 2.1,1.2,1.3,2.2 -t , <(showPrefabs) <(showBiomes)) >> "${COUNT}"
echo "$COUNT"