shithub: pokecrystal

Download patch

ref: 3be81c1bf9cedbff4e0f081619883511433ef88b
parent: cf1c50555505f525ede5e2cd426f002312038de0
author: Jacob Moody <[email protected]>
date: Tue Feb 28 02:10:02 EST 2023

build on plan9

--- /dev/null
+++ b/mkfile
@@ -1,0 +1,175 @@
+
+</$cputype/mkfile
+TGFX=tools/$O.gfx
+TDIM=tools/$O.png_dimensions
+TANIM=tools/$O.pokemon_animation
+TANIMG=tools/$O.pokemon_animation_graphics
+TSTAD=tools/$O.stadium
+LZ=tools/lz/$O.out
+TOOLS=$TGFX $TDIM $TANIM $TANIMG $TSTAD
+
+AS=rgbasm
+AFLAGS=-hL -Q8 -P includes.asm -Weverything '-Wnumeric-string=2' '-Wtruncation=1'
+LD=rgblink
+LDFLAGS=-l layout.link
+FIX=rgbfix
+FIFLAGS=-Cjv -t PM_CRYSTAL -i BYTE -n 0 -k 01 -l 0x33 -m 0x10 -r 3 -p 0
+O=o
+
+TARG=pokecrystal.gbc
+
+OFILES=\
+	audio.$O\
+	home.$O\
+	main.$O\
+	ram.$O\
+	data/text/common.$O\
+	data/maps/map_data.$O\
+	data/pokemon/dex_entries.$O\
+	data/pokemon/egg_moves.$O\
+	data/pokemon/evos_attacks.$O\
+	engine/movie/credits.$O\
+	engine/overworld/events.$O\
+	gfx/misc.$O\
+	gfx/pics.$O\
+	gfx/sprites.$O\
+	gfx/tilesets.$O\
+	lib/mobile/main.$O\
+	lib/mobile/mail.$O\
+
+<inc.mk
+
+all:V:	$TARG
+
+include:VE:
+	{
+		for(i in gfx/pokemon/*/front.png){
+			which=`{echo $i | sed 's,gfx/pokemon/,,g;s,/front.png,,g'}
+			echo 'gfx/pokemon/'^$which^'/front.animated.2bpp:	gfx/pokemon/'^$which^'/front.2bpp gfx/pokemon/'^$which^'/front.dimensions'
+			echo '	$TANIMG -o $target $prereq'
+			echo 'gfx/pokemon/'^$which^'/front.animated.tilemap:	gfx/pokemon/'^$which^'/front.2bpp gfx/pokemon/'^$which^'/front.dimensions'
+			echo '	$TANIMG -t $target $prereq'
+			echo 'gfx/pokemon/'^$which^'/bitmask.asm:	gfx/pokemon/'^$which^'/front.animated.tilemap gfx/pokemon/'^$which^'/front.dimensions'
+			echo '	$TANIM -b $prereq >$target'
+			echo 'gfx/pokemon/'^$which^'/frames.asm:	gfx/pokemon/'^$which^'/front.animated.tilemap gfx/pokemon/'^$which^'/front.dimensions'
+			echo '	$TANIM -f $prereq >$target'
+			echo 'gfx/pokemon/'^$which^'/back.2bpp:	gfx/pokemon/'^$which^'/back.png'
+			echo '	rgbgfx -h -o $target $prereq'
+		}
+		for(i in gfx/trainers/*.png){
+			dst=`{echo $i | sed 's/\.png/\.2bpp/g'}
+			echo $dst^':	'^$i
+			echo '	rgbgfx -h -o $target $prereq'
+		}
+	} >anim.mk
+	{
+		echo 'BMFILES=\'
+		for(i in `{walk | grep asm}){
+			sed -n 's/.*(INCLUDE|INCBIN) "(gfx.*bitmask\.asm)".*/\2/gp' $i | sed 's/^/	/g;s/$/\\/g' | grep -v 'unown/bitmask.asm'
+		}
+		echo
+		echo 'FRFILES=\'
+		for(i in `{walk | grep asm}){
+			sed -n 's/.*(INCLUDE|INCBIN) "(gfx.*frames\.asm)".*/\2/gp' $i | sed 's/^/	/g;s/$/\\/g' | grep -v 'gfx/pokemon/unown/frames.asm|(kanto|johto)_frames.asm|gfx/pokemon/unown_frames.asm'
+		}
+		echo
+		echo 'B2FILES=\'
+		for(i in `{walk | grep asm}){
+			sed -n 's/.*(INCLUDE|INCBIN) "(gfx.*\.2bpp)".*/\2/gp' $i | sed 's/^/	/g;s/$/\\/g'
+		}
+		echo
+		echo 'B1FILES=\'
+		for(i in `{walk | grep asm}){
+			sed -n 's/.*(INCLUDE|INCBIN) "(gfx.*\.1bpp)".*/\2/gp' $i | sed 's/^/	/g;s/$/\\/g'
+		}
+		echo
+		echo 'DIMFILES=\'
+		for(i in `{walk | grep asm}){
+			sed -n 's/.*(INCLUDE|INCBIN) "(gfx.*\.dimensions)".*/\2/gp' $i | sed 's/^/	/g;s/$/\\/g'
+		}
+		echo
+		echo 'PALFILES=\'
+		for(i in `{walk | grep asm}){
+			sed -n 's/.*(INCLUDE|INCBIN) "(gfx.*\.gbcpal)".*/\2/gp' $i | sed 's/^/	/g;s/$/\\/g'
+		}
+		echo
+		echo 'LZFILES=\'
+		for(i in `{walk | grep asm}){
+			sed -n 's/.*(INCLUDE|INCBIN) "(gfx.*\.lz)".*/\2/gp' $i | sed 's/^/	/g;s/$/\\/g'
+		}
+		echo
+	} >inc.mk
+
+check:V:	$TARG
+	@{
+		rfork e;
+		RETAIL=`{awk '{ print $1 }' roms.sha1 | sed 1q}
+		OURS=`{sha1sum $TARG | awk '{ print $1 }'}
+		if(~ $RETAIL $OURS)
+			;
+	}
+
+$TOOLS:
+	@{ cd tools && mk all}
+
+dim:V:	$DIMFILES
+
+pal:V:	$PALFILES
+
+d1:V:	$B2FILES
+
+d2:V:	$B1FILES
+
+lz:V:	$LZFILES
+
+frame:V:	$FRFILES
+
+bitmask:V:	$BMFILES
+
+$OFILES:	dim pal d1 d2 lz frame bitmask
+
+gfx/trade/game_boy_cable.2bpp:	gfx/trade/game_boy.2bpp gfx/trade/link_cable.2bpp
+	cat $prereq > $target
+
+gfx/sgb/sgb_border.sgb.tilemap:	gfx/sgb/sgb_border.bin
+	tr < $prereq -d '\000' > $target
+
+<anim.mk
+<except.mk
+
+$TARG: $OFILES 
+	$LD $LDFLAGS -o $target $OFILES
+	$FIX $FIFLAGS $target
+	$TSTAD $target
+
+%.$O:	%.asm
+	$AS $AFLAGS -o $stem.$O $stem.asm
+
+%.2bpp:	%.png $TOOLS
+	rgbgfx -o $stem.2bpp $stem.png
+
+%.1bpp:	%.png $TOOLS
+	rgbgfx -d1 -o $stem.1bpp $stem.png
+
+%.dimensions:	%.png $TOOLS
+	$TDIM $stem.png $stem.dimensions
+
+%.gbcpal:	%.png
+	rgbgfx -p $target $stem.png
+
+%.lz:	%
+	$LZ -- $stem $target
+
+clean:VQ:
+	for(i in $B2FILES)
+		rm -f $i
+	for(i in $B1FILES)
+		rm -f $i
+	for(i in $DIMFILES)
+		rm -f $i
+	for(i in $LZFILES)
+		rm -f $i
+	for(i in $BMFILES)
+		rm -f $i
+	for(i in $FRFILES)
+		rm -f $i
--- a/tools/common.h
+++ b/tools/common.h
@@ -6,7 +6,6 @@
 #include <stdint.h>
 #include <stdbool.h>
 #include <stddef.h>
-#include <stdnoreturn.h>
 #include <inttypes.h>
 #include <string.h>
 #include <errno.h>
@@ -23,13 +22,10 @@
 
 #define error_exit(...) exit((fprintf(stderr, PROGRAM_NAME ": " __VA_ARGS__), 1))
 
-noreturn void usage_exit(int status) {
+void usage_exit(int status) {
 	fprintf(stderr, "Usage: " PROGRAM_NAME " " USAGE_OPTS "\n");
 	exit(status);
 }
-
-int getopt_long_index;
-#define getopt_long(argc, argv, optstring, longopts) getopt_long(argc, argv, optstring, longopts, &getopt_long_index)
 
 void *xmalloc(size_t size) {
 	errno = 0;
--- /dev/null
+++ b/tools/getopt.c
@@ -1,0 +1,287 @@
+/*
+ * Copyright © 2005-2020 Rich Felker, et al.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/* This implementation was taken from musl and modified for RGBDS */
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <stdio.h>
+#include <string.h>
+#include <wchar.h>
+
+#include "getopt.h"
+
+char *optarg;
+int optind = 1, musl_opterr = 1, musl_optopt;
+int musl_optreset = 0;
+static int musl_optpos;
+
+static void musl_getopt_msg(char const *a, char const *b, char const *c, size_t l)
+{
+	FILE *f = stderr;
+
+	if (fputs(a, f) >= 0 &&
+	    fwrite(b, strlen(b), 1, f) &&
+	    fwrite(c, 1, l, f) == l)
+		putc('\n', f);
+}
+
+static int getopt(int argc, char *argv[], char const *optstring)
+{
+	int i;
+	wchar_t c, d;
+	int k, l;
+	char *optchar;
+
+	if (!optind || musl_optreset) {
+		musl_optreset = 0;
+		musl_optpos = 0;
+		optind = 1;
+	}
+
+	if (optind >= argc || !argv[optind])
+		return -1;
+
+	if (argv[optind][0] != '-') {
+		if (optstring[0] == '-') {
+			optarg = argv[optind++];
+			return 1;
+		}
+		return -1;
+	}
+
+	if (!argv[optind][1])
+		return -1;
+
+	if (argv[optind][1] == '-' && !argv[optind][2])
+		return optind++, -1;
+
+	if (!musl_optpos)
+		musl_optpos++;
+	k = mbtowc(&c, argv[optind] + musl_optpos, MB_LEN_MAX);
+	if (k < 0) {
+		k = 1;
+		c = 0xfffd; /* replacement char */
+	}
+	optchar = argv[optind] + musl_optpos;
+	musl_optpos += k;
+
+	if (!argv[optind][musl_optpos]) {
+		optind++;
+		musl_optpos = 0;
+	}
+
+	if (optstring[0] == '-' || optstring[0] == '+')
+		optstring++;
+
+	i = 0;
+	d = 0;
+	do {
+		l = mbtowc(&d, optstring+i, MB_LEN_MAX);
+		if (l > 0)
+			i += l;
+		else
+			i++;
+	} while (l && d != c);
+
+	if (d != c || c == ':') {
+		musl_optopt = c;
+		if (optstring[0] != ':' && musl_opterr)
+			musl_getopt_msg(argv[0], ": unrecognized option: ", optchar, k);
+		return '?';
+	}
+	if (optstring[i] == ':') {
+		optarg = 0;
+		if (optstring[i + 1] != ':' || musl_optpos) {
+			optarg = argv[optind++] + musl_optpos;
+			musl_optpos = 0;
+		}
+		if (optind > argc) {
+			musl_optopt = c;
+			if (optstring[0] == ':')
+				return ':';
+			if (musl_opterr)
+				musl_getopt_msg(argv[0], ": option requires an argument: ",
+						optchar, k);
+			return '?';
+		}
+	}
+	return c;
+}
+
+static void permute(char **argv, int dest, int src)
+{
+	char *tmp = argv[src];
+	int i;
+
+	for (i = src; i > dest; i--)
+		argv[i] = argv[i-1];
+	argv[dest] = tmp;
+}
+
+static int getopt_long_core(int argc, char **argv, char const *optstring,
+				 const struct option *longopts, int *idx, int longonly);
+
+int getopt_long(int argc, char **argv, char const *optstring,
+			    const struct option *longopts, int *idx, int longonly)
+{
+	int ret, skipped, resumed;
+
+	if (!optind || musl_optreset) {
+		musl_optreset = 0;
+		musl_optpos = 0;
+		optind = 1;
+	}
+
+	if (optind >= argc || !argv[optind])
+		return -1;
+
+	skipped = optind;
+	if (optstring[0] != '+' && optstring[0] != '-') {
+		int i;
+		for (i = optind; ; i++) {
+			if (i >= argc || !argv[i])
+				return -1;
+			if (argv[i][0] == '-' && argv[i][1])
+				break;
+		}
+		optind = i;
+	}
+	resumed = optind;
+	ret = getopt_long_core(argc, argv, optstring, longopts, idx, longonly);
+	if (resumed > skipped) {
+		int i, cnt = optind - resumed;
+
+		for (i = 0; i < cnt; i++)
+			permute(argv, skipped, optind - 1);
+		optind = skipped + cnt;
+	}
+	return ret;
+}
+
+static int getopt_long_core(int argc, char **argv, char const *optstring,
+				 const struct option *longopts, int *idx, int longonly)
+{
+	optarg = 0;
+	if (longopts && argv[optind][0] == '-' &&
+	    ((longonly && argv[optind][1] && argv[optind][1] != '-') ||
+	     (argv[optind][1] == '-' && argv[optind][2]))) {
+		int colon = optstring[optstring[0] == '+' || optstring[0] == '-'] == ':';
+		int i, cnt, match = 0;
+		char *arg = 0, *opt, *start = argv[optind] + 1;
+
+		for (cnt = i = 0; longopts[i].name; i++) {
+			char const *name = longopts[i].name;
+
+			opt = start;
+			if (*opt == '-')
+				opt++;
+			while (*opt && *opt != '=' && *opt == *name) {
+				name++;
+				opt++;
+			}
+			if (*opt && *opt != '=')
+				continue;
+			arg = opt;
+			match = i;
+			if (!*name) {
+				cnt = 1;
+				break;
+			}
+			cnt++;
+		}
+		if (cnt == 1 && longonly && arg - start == mblen(start, MB_LEN_MAX)) {
+			int l = arg - start;
+
+			for (i = 0; optstring[i]; i++) {
+				int j = 0;
+
+				while (j < l && start[j] == optstring[i + j])
+					j++;
+				if (j == l) {
+					cnt++;
+					break;
+				}
+			}
+		}
+		if (cnt == 1) {
+			i = match;
+			opt = arg;
+			optind++;
+			if (*opt == '=') {
+				if (!longopts[i].has_arg) {
+					musl_optopt = longopts[i].val;
+					if (colon || !musl_opterr)
+						return '?';
+					musl_getopt_msg(argv[0],
+						": option does not take an argument: ",
+						longopts[i].name,
+						strlen(longopts[i].name));
+					return '?';
+				}
+				optarg = opt + 1;
+			} else if (longopts[i].has_arg == required_argument) {
+				optarg = argv[optind];
+				if (!optarg) {
+					musl_optopt = longopts[i].val;
+					if (colon)
+						return ':';
+					if (!musl_opterr)
+						return '?';
+					musl_getopt_msg(argv[0],
+						": option requires an argument: ",
+						longopts[i].name,
+						strlen(longopts[i].name));
+					return '?';
+				}
+				optind++;
+			}
+			if (idx)
+				*idx = i;
+			if (longopts[i].flag) {
+				*longopts[i].flag = longopts[i].val;
+				return 0;
+			}
+			return longopts[i].val;
+		}
+		if (argv[optind][1] == '-') {
+			musl_optopt = 0;
+			if (!colon && musl_opterr)
+				musl_getopt_msg(argv[0], cnt ?
+					": option is ambiguous: " :
+					": unrecognized option: ",
+					argv[optind] + 2,
+					strlen(argv[optind] + 2));
+			optind++;
+			return '?';
+		}
+	}
+	return getopt(argc, argv, optstring);
+}
+
+int getopt_long_only(int argc, char **argv, char const *optstring,
+			  const struct option *longopts, int *idx)
+{
+	return getopt_long(argc, argv, optstring, longopts, idx, 1);
+}
--- /dev/null
+++ b/tools/getopt.h
@@ -1,0 +1,57 @@
+/*
+ * Copyright © 2005-2020 Rich Felker, et al.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/* This implementation was taken from musl and modified for RGBDS */
+
+#ifndef RGBDS_EXTERN_GETOPT_H
+#define RGBDS_EXTERN_GETOPT_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern char *optarg;
+extern int optind, musl_opterr, musl_optopt, musl_optreset;
+
+struct option {
+	char const *name;
+	int has_arg;
+	int *flag;
+	int val;
+};
+
+int getopt_long(int argc, char **argv, char const *optstring,
+			  const struct option *longopts, int *idx, int longonly);
+
+int getopt_long_only(int argc, char **argv, char const *optstring,
+			  const struct option *longopts, int *idx);
+
+#define no_argument        0
+#define required_argument  1
+#define optional_argument  2
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
+#endif
--- a/tools/gfx.c
+++ b/tools/gfx.c
@@ -36,7 +36,7 @@
 		{"help", no_argument, 0, 'h'},
 		{0}
 	};
-	for (int opt; (opt = getopt_long(argc, argv, "d:o:p:h", long_options)) != -1;) {
+	for (int opt; (opt = getopt_long(argc, argv, "d:o:p:h", long_options, NULL, 0)) != -1;) {
 		switch (opt) {
 		case 'R':
 			options.remove_whitespace = true;
@@ -207,14 +207,18 @@
 };
 
 bool flip_exists(const uint8_t *tile, const uint8_t *tiles, int tile_size, int num_tiles, bool xflip, bool yflip) {
-	uint8_t flip[tile_size]; // VLA
-	memset(flip, 0, tile_size);
-	int half_size = tile_size / 2;
-	for (int i = 0; i < tile_size; i++) {
-		int j = yflip ? (options.interleave && i < half_size ? half_size : tile_size) - 1 - (i ^ 1) : i;
-		flip[j] = xflip ? flipped[tile[i]] : tile[i];
-	}
-	return tile_exists(flip, tiles, tile_size, num_tiles);
+	uint8_t *flip;
+	bool ret;
+
+	flip = mallocz(tile_size, 1);
+ 	int half_size = tile_size / 2;
+ 	for (int i = 0; i < tile_size; i++) {
+ 		int j = yflip ? (options.interleave && i < half_size ? half_size : tile_size) - 1 - (i ^ 1) : i;
+ 		flip[j] = xflip ? flipped[tile[i]] : tile[i];
+ 	}
+	ret = tile_exists(flip, tiles, tile_size, num_tiles);
+	free(flip);
+	return ret;
 }
 
 void remove_flip(struct Graphic *graphic, bool xflip, bool yflip) {
--- /dev/null
+++ b/tools/lz/mkfile
@@ -1,0 +1,22 @@
+</$objtype/mkfile
+
+OFILES=\
+	global.$O\
+	main.$O\
+	merging.$O\
+	mpcomp.$O\
+	nullcomp.$O\
+	options.$O\
+	output.$O\
+	packing.$O\
+	repcomp.$O\
+	spcomp.$O\
+	uncomp.$O\
+	util.$O\
+
+# ThIs MaKeS It PoRtAbLe
+POSIX=-D PRIu32="ud" -DPRId32="d" -DPRIx32="x" -DPRIX32="X" -DPRIo32="o" -DSTDOUT_FILENO=1 -DSTDIN_FILENO=0 -DPRIu8="ud" -DPRIu16="ud" -DPRId16="d" -DPRIx16="x" -DPRIX16="X" -DMB_LEN_MAX=4 -DUINT32_C='(uint32_t)'
+
+CFLAGS=$CFLAGS -p -I/sys/include/npe -D__plan9__ -D__${objtype}__ $POSIX
+
+</sys/src/cmd/mkone
--- a/tools/lz/mpcomp.c
+++ b/tools/lz/mpcomp.c
@@ -13,6 +13,7 @@
 */
 
 struct command * try_compress_multi_pass (const unsigned char * data, const unsigned char * flipped, unsigned short * size, unsigned flags) {
+  struct command _tmp;
   struct command * result = calloc(*size, sizeof(struct command));
   unsigned char * reversed = malloc(*size);
   short * sources = malloc(*size * sizeof(short));
@@ -32,12 +33,17 @@
       result[pos].count = current;
       if ((result[pos].command == 2) && (current & 1) && !(flags & 2)) result[pos].count --;
     }
-    if (result[pos].count <= command_size(result[pos])) result[pos] = (struct command) {.command = 0, .count = 0};
+    _tmp.command = 0;
+    _tmp.count = 0;
+    _tmp.value = 0;
+    if (result[pos].count <= command_size(result[pos])) result[pos] = _tmp;
   }
   for (pos = 0; pos < *size; pos ++)
     if (!result[pos].command) {
       for (current = 1; (current < MAX_COMMAND_COUNT) && ((pos + current) < *size); current ++) if (result[pos + current].command) break;
-      result[pos] = (struct command) {.command = 0, .count = current, .value = pos};
+      result[pos].command = 0;
+      result[pos].count = current;
+      result[pos].value = pos;
     } else if (result[pos].count > MAX_COMMAND_COUNT) {
       result[pos + MAX_COMMAND_COUNT] = result[pos];
       result[pos + MAX_COMMAND_COUNT].count -= MAX_COMMAND_COUNT;
@@ -57,10 +63,12 @@
 struct command pick_command_for_pass (const unsigned char * data, const unsigned char * flipped, const unsigned char * reversed, const short * sources,
                                       unsigned short length, unsigned short position, unsigned flags) {
   struct command result = pick_repetition_for_pass(data, length, position, flags);
+  unsigned char* _arr[] = {data, flipped, reversed};
   if (result.count >= MULTIPASS_SKIP_THRESHOLD) return result;
   unsigned char p;
   for (p = 0; p < 3; p ++) {
-    struct command temp = pick_copy_for_pass(data, p[(const unsigned char * []) {data, flipped, reversed}], sources, p + 4, length, position, flags);
+    struct command temp;
+    temp = pick_copy_for_pass(data, _arr[p], sources, p + 4, length, position, flags);
     if (temp.command == 7) continue;
     if (temp.count > result.count) result = temp;
   }
@@ -70,25 +78,42 @@
 
 struct command pick_repetition_for_pass (const unsigned char * data, unsigned short length, unsigned short position, unsigned flags) {
   unsigned short p;
+  struct command _tmp;
   if (data[position]) {
-    if ((position + 1) >= length) return (struct command) {.command = 1, .count = 1, .value = data[position]};
+    if ((position + 1) >= length){
+        _tmp.command = 1;
+        _tmp.count = 1;
+        _tmp.value = data[position];
+        return _tmp;
+    }
     struct command result;
-    if (!(flags & 8) && (data[position] == data[position + 1]))
-      result = (struct command) {.command = 1, .value = data[position]};
-    else
-      result = (struct command) {.command = 2, .value = data[position] | (data[position + 1] << 8)};
+    if (!(flags & 8) && (data[position] == data[position + 1])){
+      result.command = 1;
+      result.count = 0;
+      result.value = data[position];
+    } else {
+      result.command = 2;
+      result.count = 0;
+      result.value = data[position] | (data[position + 1] << 8);
+    }
     for (p = 1; ((position + p) < length) && (p < LOOKAHEAD_LIMIT); p ++) if (data[position + p] != data[position + (p & 1)]) break;
     result.count = p;
     return result;
   } else {
     for (p = position + 1; (p < length) && (p < (position + LOOKAHEAD_LIMIT)); p ++) if (data[p]) break;
-    return (struct command) {.command = 3, .count = p - position};
+    _tmp.command = 3;
+    _tmp.count = p - position;
+    _tmp.value = 0;
+    return _tmp;
   }
 }
 
 struct command pick_copy_for_pass (const unsigned char * data, const unsigned char * reference, const short * sources, unsigned char command_type,
                                    unsigned short length, unsigned short position, unsigned flags) {
-  struct command result = {.command = 7, .count = (flags & 4) ? 4 : 3};
+  struct command result;
+  result.command = 7;
+  result.count = (flags & 4) ? 4 : 3;
+  result.value = 0;
   if (length < 3) return result;
   unsigned refpos, count;
   const unsigned char * current;
@@ -106,7 +131,9 @@
     if (count > (length - refpos)) count = length - refpos;
     if (count > (length - position)) count = length - position;
     if (result.count > count) continue;
-    result = (struct command) {.command = command_type, .count = count, .value = sources[-1]};
+    result.command = command_type;
+    result.count = count;
+    result.value = sources[-1];
   }
   return result;
 }
--- a/tools/lz/nullcomp.c
+++ b/tools/lz/nullcomp.c
@@ -8,6 +8,7 @@
 
 struct command * store_uncompressed (__attribute__((unused)) const unsigned char * data, __attribute__((unused)) const unsigned char * bitflipped, unsigned short * size, unsigned flags) {
   unsigned short position, block, remainder = *size;
+  struct command _tmp;
   struct command * result = NULL;
   *size = 0;
   for (position = 0; remainder; position += block, remainder -= block) {
@@ -14,7 +15,10 @@
     block = (remainder > MAX_COMMAND_COUNT) ? MAX_COMMAND_COUNT : remainder;
     if (!(flags & 1) && (block <= (2 * SHORT_COMMAND_COUNT)) && (block > SHORT_COMMAND_COUNT)) block = SHORT_COMMAND_COUNT;
     result = realloc(result, sizeof(struct command) * (1 + *size));
-    result[(*size) ++] = (struct command) {.command = 0, .count = block, .value = position};
+    _tmp.command = 0;
+    _tmp.count = block;
+    _tmp.value = position;
+    result[(*size) ++] = _tmp;
   }
   return result;
 }
--- a/tools/lz/repcomp.c
+++ b/tools/lz/repcomp.c
@@ -9,6 +9,7 @@
 
 struct command * try_compress_repetitions (const unsigned char * data, __attribute__((unused)) const unsigned char * bitflipped, unsigned short * size, unsigned flags) {
   unsigned short pos = 0, skipped = 0;
+  struct command _tmp;
   struct command * result = malloc(*size * sizeof(struct command));
   struct command * current = result;
   struct command candidate;
@@ -24,7 +25,10 @@
       candidate.value |= candidate.value << 8;
     }
     if ((flags & (1 << candidate.command)) && (command_size(candidate) <= candidate.count)) {
-      if (skipped) *(current ++) = (struct command) {.command = 0, .count = skipped, .value = pos - skipped};
+      _tmp.command = 0;
+      _tmp.count = skipped;
+      _tmp.value = pos - skipped;
+      if (skipped) *(current ++) = _tmp;
       skipped = 0;
       *(current ++) = candidate;
       pos += candidate.count;
@@ -31,12 +35,18 @@
     } else {
       pos ++;
       if ((++ skipped) == MAX_COMMAND_COUNT) {
-        *(current ++) = (struct command) {.command = 0, .count = MAX_COMMAND_COUNT, .value = pos - MAX_COMMAND_COUNT};
+        _tmp.command = 0;
+        _tmp.count = MAX_COMMAND_COUNT;
+        _tmp.value = pos - MAX_COMMAND_COUNT;
+        *(current ++) = _tmp;
         skipped = 0;
       }
     }
   }
-  if (skipped) *(current ++) = (struct command) {.command = 0, .count = skipped, .value = pos - skipped};
+  _tmp.command = 0;
+  _tmp.count = skipped;
+  _tmp.value = pos - skipped;
+  if (skipped) *(current ++) = _tmp;
   *size = current - result;
   result = realloc(result, *size * sizeof(struct command));
   return result;
@@ -43,7 +53,19 @@
 }
 
 struct command find_repetition_at_position (const unsigned char * data, unsigned short position, unsigned short length) {
-  if ((position + 1) >= length) return data[position] ? ((struct command) {.command = 7}) : ((struct command) {.command = 3, .count = 1});
+  struct command ret;
+  if ((position + 1) >= length){
+      if(data[position]){
+      ret.command = 7;
+      ret.count = 0;
+      ret.value = 0;
+      return ret;
+    }
+    ret.command = 3;
+    ret.count = 1;
+    ret.value = 0;
+    return ret;
+  }
   unsigned char value[2] = {data[position], data[position + 1]};
   unsigned repcount, limit = length - position;
   if (limit > MAX_COMMAND_COUNT) limit = MAX_COMMAND_COUNT;
@@ -51,7 +73,12 @@
   struct command result;
   result.count = repcount;
   if (*value != value[1]) {
-    if (!*value && (repcount < 3)) return (struct command) {.command = 3, .count = 1};
+    if (!*value && (repcount < 3)){
+        ret.command = 3;
+        ret.count = 1;
+        ret.value = 0;
+        return ret;
+    }
     result.command = 2;
     result.value = ((unsigned) (*value)) | (((unsigned) (value[1])) << 8);
   } else if (*value) {
--- a/tools/lz/spcomp.c
+++ b/tools/lz/spcomp.c
@@ -22,6 +22,7 @@
 */
 
 struct command * try_compress_single_pass (const unsigned char * data, const unsigned char * bitflipped, unsigned short * length, unsigned flags) {
+  struct command _tmp;
   struct command * commands = malloc(sizeof(struct command) * *length);
   memset(commands, -1, sizeof(struct command) * *length);
   struct command * current_command = commands;
@@ -35,16 +36,19 @@
       *current_command = pick_best_command(2, repetition, copy);
     else
       *current_command = pick_best_command(2, copy, repetition);
-    *current_command = pick_best_command(2, (struct command) {.command = 0, .count = 1, .value = position}, *current_command);
+    _tmp.command = 0;
+    _tmp.count = 1;
+    _tmp.value = position;
+    *current_command = pick_best_command(2, _tmp, *current_command);
     if ((flags & 2) && (command_size(*current_command) == current_command -> count))
       if (previous_data && (previous_data != SHORT_COMMAND_COUNT) && (previous_data != MAX_COMMAND_COUNT))
-        *current_command = (struct command) {.command = 0, .count = 1, .value = position};
+        *current_command = _tmp;
     if (scan_delay_flag) {
       if (scan_delay >= scan_delay_flag)
         scan_delay = 0;
       else if (current_command -> command) {
         scan_delay ++;
-        *current_command = (struct command) {.command = 0, .count = 1, .value = position};
+        *current_command = _tmp;
       }
     }
     if (current_command -> command)
@@ -59,15 +63,27 @@
 }
 
 struct command find_best_copy (const unsigned char * data, unsigned short position, unsigned short length, const unsigned char * bitflipped, unsigned flags) {
-  struct command simple = {.command = 7};
+  struct command simple;
+  simple.command = 7;
+  simple.count = 0;
+  simple.value = 0;
   struct command flipped = simple, backwards = simple;
   short count, offset;
-  if ((count = scan_forwards(data + position, length - position, data, position, &offset)))
-    simple = (struct command) {.command = 4, .count = count, .value = offset};
-  if ((count = scan_forwards(data + position, length - position, bitflipped, position, &offset)))
-    flipped = (struct command) {.command = 5, .count = count, .value = offset};
-  if ((count = scan_backwards(data, length - position, position, &offset)))
-    backwards = (struct command) {.command = 6, .count = count, .value = offset};
+  if ((count = scan_forwards(data + position, length - position, data, position, &offset))){
+    simple.command = 4;
+    simple.count = count;
+    simple.value = offset;
+  }
+  if ((count = scan_forwards(data + position, length - position, bitflipped, position, &offset))){
+    flipped.command = 5;
+    flipped.count = count;
+    flipped.value = offset;
+  }
+  if ((count = scan_backwards(data, length - position, position, &offset))){
+    backwards.command = 6;
+    backwards.count = count;
+    backwards.value = offset;
+  }
   struct command command;
   switch (flags / 24) {
     case 0: command = pick_best_command(3, simple, backwards, flipped); break;
@@ -121,7 +137,19 @@
 }
 
 struct command find_best_repetition (const unsigned char * data, unsigned short position, unsigned short length) {
-  if ((position + 1) >= length) return data[position] ? ((struct command) {.command = 7}) : ((struct command) {.command = 3, .count = 1});
+  struct command out;
+  if ((position + 1) >= length){
+    if(data[position]){
+      out.command = 7;
+      out.count = 0;
+      out.value = 0;
+    } else {
+      out.command = 3;
+      out.count = 1;
+      out.value = 0;
+    }
+    return out;
+  }
   unsigned char value[2] = {data[position], data[position + 1]};
   unsigned repcount, limit = length - position;
   if (limit > MAX_COMMAND_COUNT) limit = MAX_COMMAND_COUNT;
@@ -129,7 +157,12 @@
   struct command result;
   result.count = repcount;
   if (*value != value[1]) {
-    if (!*value && (repcount < 3)) return (struct command) {.command = 3, .count = 1};
+    if (!*value && (repcount < 3)){
+      out.command = 3;
+      out.count = 1;
+      out.value = 0;
+      return out;
+    }
     result.command = 2;
     result.value = ((unsigned) (*value)) | (((unsigned) (value[1])) << 8);
   } else if (*value) {
--- a/tools/lz/util.c
+++ b/tools/lz/util.c
@@ -42,9 +42,10 @@
 }
 
 short command_size (struct command command) {
+  short _arr[] = {command.count, 1, 2, 0};
   short header_size = 1 + (command.count > SHORT_COMMAND_COUNT);
   if (command.command & 4) return header_size + 1 + (command.value >= 0);
-  return header_size + command.command[(short []) {command.count, 1, 2, 0}];
+  return header_size + _arr[command.command];
 }
 
 unsigned short compressed_length (const struct command * commands, unsigned short count) {
--- /dev/null
+++ b/tools/mkfile
@@ -1,0 +1,18 @@
+</$objtype/mkfile
+
+OFILES=\
+	getopt.$O\
+
+TARG=\
+	gfx\
+	png_dimensions\
+	pokemon_animation\
+	pokemon_animation_graphics\
+	stadium\
+
+# ThIs MaKeS It PoRtAbLe
+POSIX=-D PRIu32="ud" -DPRId32="d" -DPRIx32="x" -DPRIX32="X" -DPRIo32="o" -DSTDOUT_FILENO=1 -DSTDIN_FILENO=0 -DPRIu8="ud" -DPRIu16="ud" -DPRId16="d" -DPRIx16="x" -DPRIX16="X" -DMB_LEN_MAX=4 -DUINT32_C='(uint32_t)'
+
+CFLAGS=$CFLAGS -I/sys/include/npe -D__plan9__ -D__${objtype}__ $POSIX
+
+</sys/src/cmd/mkmany
--- a/tools/pokemon_animation.c
+++ b/tools/pokemon_animation.c
@@ -15,7 +15,7 @@
 		{"help", no_argument, 0, 'h'},
 		{0}
 	};
-	for (int opt; (opt = getopt_long(argc, argv, "bfh", long_options)) != -1;) {
+	for (int opt; (opt = getopt_long(argc, argv, "bfh", long_options, NULL, 0)) != -1;) {
 		switch (opt) {
 		case 'b':
 			options->use_bitmasks = true;
--- a/tools/pokemon_animation_graphics.c
+++ b/tools/pokemon_animation_graphics.c
@@ -17,7 +17,7 @@
 		{"help", no_argument, 0, 'h'},
 		{0}
 	};
-	for (int opt; (opt = getopt_long(argc, argv, "o:t:h", long_options)) != -1;) {
+	for (int opt; (opt = getopt_long(argc, argv, "o:t:h", long_options, NULL, 0)) != -1;) {
 		switch (opt) {
 		case 'o':
 			options->out_filename = optarg;
--- a/tools/stadium.c
+++ b/tools/stadium.c
@@ -9,7 +9,7 @@
 		{"help", no_argument, 0, 'h'},
 		{0}
 	};
-	for (int opt; (opt = getopt_long(argc, argv, "eh", long_options)) != -1;) {
+	for (int opt; (opt = getopt_long(argc, argv, "eh", long_options, NULL, 0)) != -1;) {
 		switch (opt) {
 		case 'e':
 			*european = true;