mcmapを使って1hに1回マイクラのマップ画像を撮る

今回、mcmap使って定期的にマップ画像を更新するスクリプト書きましたので、覚書き。


とりあえずGithubから最新のものを持ってくる

$ mkdir -p /home/user/src/
$ git clone https://github.com/Zahl/mcmap.git /home/user/src/mcmap

On Linux/BSD it should be trivial to compile. All you need is g++, make, zlib and libpng.
(If they're missing and you're on debian or Ubuntu, try "apt-get install zlib1g-dev build-essential g++ libc6-dev libpng-dev",
on CentOS you need to do "yum install libpng-devel zlib-devel gcc-c++")
Just cd into the directory containing the source code and type "make".
"make turbo" would optimize the binary for your CPU.
Makes it a few microseconds faster.

http://www.minecraftforum.net/topic/37409-mcmap-isometric-renders-sspsmp-minecraft-131-support/


Scientific Linux6.5なので、

$ sudo yum install -y libpng-devel zlib-devel gcc-c++

ってやります。
全部すでに入ってましたけど!


次にコンパイルします

$ cd /home/user/src/mcmap/
$ make

これで「mcmap」は出来ます。
make turboってやると、今いる環境(CPUに対しての)に最適化してくれるらしいですけど好みです。(後述)


実行場所を/home/user/binにします。

$ mkdir -p /home/user/bin/mcmap/maps 
$ cd /home/user/bin/mcmap 
$ ln -s /home/user/src/mcmap/mcmap .


また、スクリプトで使う、pngcrushとconvertはそれぞれ、「pngcrush」「ImageMagick」を入れてください。

$ sudo yum info pngcrush 
Loaded plugins: security
Installed Packages
Name        : pngcrush
Arch        : x86_64
Version     : 1.7.66
Release     : 1.el6.rf
Size        : 489 
Repo        : installed
From repo   : rpmforge
Summary     : Optimizer for PNG (Portable Network Graphics) files
URL         : http://pmt.sourceforge.net/pngcrush/
License     : GPL
Description : pngcrush is an optimizer for PNG (Portable Network Graphics) files. It can be
            : run from a commandline in an MSDOS window, or from a UNIX or LINUX commandline.
            :
            : Its main purpose is to reduce the size of the PNG IDAT datastream by trying
            : various compression levels an PNG filter methods. It also can be used to
            : remove unwanted ancillary chunks, or to add certain chunks including gAMA,
            : tRNS, iCCP, and textual chunks.

$ sudo yum info ImageMagick
Loaded plugins: security
Installed Packages
Name        : ImageMagick
Arch        : x86_64
Version     : 6.5.4.7
Release     : 6.el6_2
Size        : 6.4 M
Repo        : installed
From repo   : sl
Summary     : An X application for displaying and manipulating images
URL         : http://www.imagemagick.org/
License     : ImageMagick
Description : ImageMagick is an image display and manipulation tool for the X
            : Window System. ImageMagick can read and write JPEG, TIFF, PNM, GIF,
            : and Photo CD image formats. It can resize, rotate, sharpen, color
            : reduce, or add special effects to an image, and when finished you can
            : either save the completed work in the original format or a different
            : one. ImageMagick also includes command line programs for creating
            : animated or transparent .gifs, creating composite images, creating
            : thumbnail images, and more.
            :
            : ImageMagick is one of your choices if you need a program to manipulate
            : and display images. If you want to develop your own applications
            : which use ImageMagick code or APIs, you need to install
            : ImageMagick-devel as well.

Available Packages
Name        : ImageMagick
Arch        : i686
Version     : 6.5.4.7
Release     : 6.el6_2
Size        : 1.7 M
Repo        : sl
Summary     : An X application for displaying and manipulating images
URL         : http://www.imagemagick.org/
License     : ImageMagick
Description : ImageMagick is an image display and manipulation tool for the X
            : Window System. ImageMagick can read and write JPEG, TIFF, PNM, GIF,
            : and Photo CD image formats. It can resize, rotate, sharpen, color
            : reduce, or add special effects to an image, and when finished you can
            : either save the completed work in the original format or a different
            : one. ImageMagick also includes command line programs for creating
            : animated or transparent .gifs, creating composite images, creating
            : thumbnail images, and more.
            :
            : ImageMagick is one of your choices if you need a program to manipulate
            : and display images. If you want to develop your own applications
            : which use ImageMagick code or APIs, you need to install
            : ImageMagick-devel as well.


次に実行スクリプトの作成です。
これもフォーラムにあるスクリプトをちょろっと改変したものです。
ファイル名は「w.sh」です。

#!/bin/bash
# http://www.minecraftforum.net/topic/37409-mcmap-isometric-renders-sspsmp-minecraft-131-support/

## full path to mcmap binary
cd /home/user/bin/mcmap
## path to your world
WORLD=/home/user/minecraft_server1.6.2_forge/world/
## path to write output image to
OUTPUT="./maps"
## see below to change the area to render (lines that start with ##)
################################

FILE=history-`date +%s`-w.png
NORMAL_F="w.png"

OLD5=none
if [ -e "$OUTPUT/$NORMAL_F" ]; then
        OLD5=`md5sum $OUTPUT/$NORMAL_F | cut -b 1-32`
fi

CHECK=`date +%k`
NIGHT="-skylight"
NIGHTSTATUS="0"

#./mcmap -png $NIGHT -from -40 -60 -to 20 20 "$WORLD" > /dev/null
./mcmap -png $NIGHT "$WORLD" > /dev/null

RET=$?
if [ "$RET" -ne "0" ]; then
        echo "Error creating image for $FILE"
        exit 1;
fi

NEW5=`md5sum output.png | cut -b 1-32`
if [ "$OLD5" != "$NEW5" ]; then
        mkdir -p $OUTPUT/history/
        pngcrush output.png $OUTPUT/history/$FILE > /dev/null
        convert -scale 600 -depth 5 output.png thumb.png
        pngcrush -brute thumb.png $OUTPUT/history/thumb.$FILE > /dev/null
        mv output.png $OUTPUT/$NORMAL_F
        mv thumb.png $OUTPUT/thumb.$NORMAL_F
fi


後は末尾にscpコマンドつけたり何だりしてWebから見れるようにすれば完成です。
ネザー、エンドのマップとか見たい場合には、

./mcmap -png -hell $NIGHT "$WORLD" > /dev/null
./mcmap -png -end  $NIGHT "$WORLD" > /dev/null

とかに変更すれば作成出来ます。
(一度行っていないとエラーが出るかもしれない)


これで現在のマップ画像が/home/user/bin/mcmap/maps/w.png に出力されます。
これを定期的に作成する場合はcronで設定します。

$ chmod +x /home/user/bin/mcmap/w.sh
$ crontab -e
$ crontab -l
58 * * * * /home/user/bin/mcmap/w.sh

って設定すると毎時58分に、先ほど書いたw.shを実行してくれます。

おまけ

先ほど、mcmapコンパイル時に「make」でも「make turbo」でも変わらないと言いました。
ただ、あくまでうちの環境下だけかもしれませんので各々で試してみてください。
以下、参考までに実行結果です。

$ cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 42
model name      : Intel(R) Xeon(R) CPU E31220 @ 3.10GHz
stepping        : 7
cpu MHz         : 3092.973
cache size      : 8192 KB
fpu             : yes
fpu_exception   : yes
cpuid level     : 13
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp
 lm constant_tsc arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq ssse3 cx16 sse4_1 sse4_2
popcnt aes xsave avx hypervisor lahf_lm ida arat epb xsaveopt pln pts dts
bogomips        : 6185.94
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

processor       : 1
vendor_id       : GenuineIntel
cpu family      : 6
model           : 42
model name      : Intel(R) Xeon(R) CPU E31220 @ 3.10GHz
stepping        : 7
cpu MHz         : 3092.973
cache size      : 8192 KB
fpu             : yes
fpu_exception   : yes
cpuid level     : 13
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp
 lm constant_tsc arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq ssse3 cx16 sse4_1 sse4_2
popcnt aes xsave avx hypervisor lahf_lm ida arat epb xsaveopt pln pts dts
bogomips        : 6185.94
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

processor       : 2
vendor_id       : GenuineIntel
cpu family      : 6
model           : 42
model name      : Intel(R) Xeon(R) CPU E31220 @ 3.10GHz
stepping        : 7
cpu MHz         : 3092.973
cache size      : 8192 KB
fpu             : yes
fpu_exception   : yes
cpuid level     : 13
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp
 lm constant_tsc arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq ssse3 cx16 sse4_1 sse4_2
popcnt aes xsave avx hypervisor lahf_lm ida arat epb xsaveopt pln pts dts
bogomips        : 6185.94
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

processor       : 3
vendor_id       : GenuineIntel
cpu family      : 6
model           : 42
model name      : Intel(R) Xeon(R) CPU E31220 @ 3.10GHz
stepping        : 7
cpu MHz         : 3092.973
cache size      : 8192 KB
fpu             : yes
fpu_exception   : yes
cpuid level     : 13
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq ssse3 cx16 sse4_1 sse4_2
popcnt aes xsave avx hypervisor lahf_lm ida arat epb xsaveopt pln pts dts
bogomips        : 6185.94
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

$ cat /proc/meminfo
MemTotal:        8061572 kB
MemFree:         1281284 kB
Buffers:          149188 kB
Cached:          1443884 kB
SwapCached:         7808 kB
Active:          4602660 kB
Inactive:        1786424 kB
Active(anon):    3677968 kB
Inactive(anon):  1118220 kB
Active(file):     924692 kB
Inactive(file):   668204 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:       8208376 kB
SwapFree:        8161992 kB
Dirty:              3536 kB
Writeback:             0 kB
AnonPages:       4789716 kB
Mapped:            25088 kB
Shmem:                 8 kB
Slab:             297840 kB
SReclaimable:     232212 kB
SUnreclaim:        65628 kB
KernelStack:        2288 kB
PageTables:        14808 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:    12239160 kB
Committed_AS:    4815524 kB
VmallocTotal:   34359738367 kB
VmallocUsed:      159032 kB
VmallocChunk:   34359574472 kB
HardwareCorrupted:     0 kB
AnonHugePages:   4681728 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:       10240 kB
DirectMap2M:     8378368 kB

$ time ./mcmap -png -skylight "/home/user/minecraft_server1.6.2_forge/world/" > /dev/null
./mcmap -png -skylight  > /dev/null  37.65s user 0.55s system 99% cpu 38.291 total

$ rm -f output.png

$ time ./mcmap.turbo -png -skylight "/home/user/minecraft_server1.6.2_forge/world/" > /dev/null
./mcmap.turbo -png -skylight  > /dev/null  37.69s user 0.47s system 99% cpu 38.196 total

というわけであんまり差がない気がするのでどちらでもいいんじゃないかなぁという、私の中での結論でした。