1{ stdenv, fetchurl, pkgconfig, unzip, zlib, libpng, bzip2, SDL, SDL_mixer
2, buildEnv, config
3}:
4
5let
6 # Choose your "paksets" of objects, images, text, music, etc.
7 paksets = config.simutrans.paksets or "pak64 pak128";
8
9 result = with stdenv.lib; withPaks (
10 if paksets == "*" then attrValues pakSpec # taking all
11 else map (name: pakSpec.${name}) (splitString " " paksets)
12 );
13
14 ver1 = "120";
15 ver2 = "0";
16 ver3 = "1";
17 version = "${ver1}.${ver2}.${ver3}";
18 ver_dash = "${ver1}-${ver2}-${ver3}";
19 ver2_dash = "${ver1}-${ver2}";
20
21 binary_src = fetchurl {
22 url = "mirror://sourceforge/simutrans/simutrans/${ver_dash}/simutrans-src-${ver_dash}.zip";
23 sha256 = "10rn259nxq2hhfpar8zwgxi1p4djvyygcm2f6qhih7l9clvnw2h1";
24 };
25
26
27 # As of 2015/03, many packsets still didn't have a release for version 120.
28 pakSpec = stdenv.lib.mapAttrs
29 (pakName: attrs: mkPak (attrs // {inherit pakName;}))
30 {
31 pak64 = {
32 srcPath = "${ver2_dash}/simupak64-${ver_dash}";
33 sha256 = "0y5v1ncpjyhjkkznqmk13kg5d0slhjbbvg1y8q5jxhmhlkghk9q2";
34 };
35 "pak64.japan" = {
36 srcPath = "${ver2_dash}/simupak64.japan-${ver_dash}";
37 sha256 = "14swy3h4ij74bgaw7scyvmivfb5fmp21nixmhlpk3mav3wr3167i";
38 };
39
40 pak128 = {
41 srcPath = "pak128%20for%20ST%20120%20%282.5.2%2B%20nightly%20r1560%2C%20bugfixes%29/pak128-r1560--ST120";
42 sha256 = "1wd51brc4aglqi3w7s8fxgxrw0k7f653w4wbnmk83k07fwfdyf24";
43 };
44 "pak128.britain" = {
45 srcPath = "pak128.Britain%20for%20${ver2_dash}/pak128.Britain.1.16-${ver2_dash}";
46 sha256 = "1rww9rnpk22l2z3s1d7y2gmd6iwhv72s7pff8krnh7z0q386waak";
47 };
48 "pak128.cs" = { # note: it needs pak128 to work
49 url = "mirror://sourceforge/simutrans/Pak128.CS/pak128.cz_v.0.2.1.zip";
50 sha256 = "008d8x1s0vxsq78rkczlnf57pv1n5hi1v5nbd1l5w3yls7lk11sc";
51 };
52 "pak128.german" = {
53 url = "mirror://sourceforge/simutrans/PAK128.german/"
54 + "PAK128.german_0.7_${ver1}.x/PAK128.german_0.7.0.1_${ver1}.x.zip";
55 sha256 = "1575akms18raxaijy2kfyqm07wdx6y5q85n7wgvq2fqydrnx33w8";
56 };
57
58 /* This release contains accented filenames that prevent unzipping.
59 "pak192.comic" = {
60 srcPath = "pak192comic%20for%20${ver2_dash}/pak192comic-0.4-${ver2_dash}up";
61 sha256 = throw "";
62 };
63 */
64 };
65
66
67 mkPak = {
68 sha256, pakName, srcPath ? null
69 , url ? "mirror://sourceforge/simutrans/${pakName}/${srcPath}.zip"
70 }:
71 stdenv.mkDerivation {
72 name = "simutrans-${pakName}";
73 unpackPhase = "true";
74 installPhase = let src = fetchurl { inherit url sha256; };
75 in ''
76 mkdir -p "$out/share/simutrans/${pakName}"
77 cd "$out/share/simutrans/${pakName}"
78 "${unzip}/bin/unzip" "${src}"
79 chmod -R +w . # some zipfiles need that
80
81 set +o pipefail # no idea why it's needed
82 toStrip=`find . -iname '*.pak' | head -n 1 | sed 's|\./\(.*\)/[^/]*$|\1|'`
83 echo "Detected path '$toStrip' to strip"
84 mv ./"$toStrip"/* .
85 rmdir -p "$toStrip"
86 '';
87 };
88
89 /* The binaries need all data in one directory; the default is directory
90 of the executable, and another option is the current directory :-/ */
91 withPaks = paks: buildEnv {
92 inherit (binaries) name;
93 paths = [binaries] ++ paks;
94 postBuild = ''
95 rm "$out/bin" && mkdir "$out/bin"
96 cat > "$out/bin/simutrans" <<EOF
97 #!${stdenv.shell}
98 cd "$out"/share/simutrans
99 exec "${binaries}/bin/simutrans" -use_workdir "\''${extraFlagsArray[@]}" "\$@"
100 EOF
101 chmod +x "$out/bin/simutrans"
102 '';
103
104 passthru.meta = binaries.meta // { hydraPlatforms = []; };
105 passthru.binaries = binaries;
106 };
107
108 binaries = stdenv.mkDerivation rec {
109 name = "simutrans-${version}";
110
111 src = binary_src;
112
113 sourceRoot = ".";
114
115 buildInputs = [ pkgconfig zlib libpng bzip2 SDL SDL_mixer unzip ];
116
117 configurePhase = let
118 # Configuration as per the readme.txt and config.template
119 platform =
120 if stdenv.isLinux then "linux" else
121 if stdenv.isDarwin then "mac" else throw "add your platform";
122 config = ''
123 BACKEND = mixer_sdl
124 COLOUR_DEPTH = 16
125 OSTYPE = ${platform}
126 VERBOSE = 1
127 '';
128 #TODO: MULTI_THREAD = 1 is "highly recommended",
129 # but it's roughly doubling CPU usage for me
130 in ''
131 echo "${config}" > config.default
132
133 # Use ~/.simutrans instead of ~/simutrans
134 substituteInPlace simsys.cc --replace '%s/simutrans' '%s/.simutrans'
135
136 # use -O2 optimization (defaults are -O or -O3)
137 sed -i -e '/CFLAGS += -O/d' Makefile
138 export CFLAGS+=-O2
139 '';
140
141 enableParallelBuilding = true;
142
143 installPhase = ''
144 mkdir -p $out/share/
145 mv simutrans $out/share/
146
147 mkdir -p $out/bin/
148 mv build/default/sim $out/bin/simutrans
149 '';
150
151 meta = with stdenv.lib; {
152 description = "A simulation game in which the player strives to run a successful transport system";
153 longDescription = ''
154 Simutrans is a cross-platform simulation game in which the
155 player strives to run a successful transport system by
156 transporting goods, passengers, and mail between
157 places. Simutrans is an open source remake of Transport Tycoon.
158 '';
159
160 homepage = http://www.simutrans.com/;
161 license = with licenses; [ artistic1 gpl1Plus ];
162 maintainers = with maintainers; [ kkallio vcunat ];
163 platforms = with platforms; linux ++ darwin;
164 };
165 };
166
167in result
168