#!/bin/bash

unset WEAPONS
unset ITFLAGS

ITFLAGS[1]="IT_AXE"
ITFLAGS[2]="IT_SHOTGUN"
ITFLAGS[3]="IT_SUPER_SHOTGUN"
ITFLAGS[4]="IT_NAILGUN"
ITFLAGS[5]="IT_SUPER_NAILGUN"
ITFLAGS[6]="IT_GRENADE_LAUNCHER"
ITFLAGS[7]="IT_ROCKET_LAUNCHER"
ITFLAGS[8]="IT_LIGHTNING"

IFS='
'
OFS='
'

cat << EOF
/* This file was automatically generated by $0 */

#include "common.qh"
#include "weapon.qh"

EOF

#################################################

for i in "$@"; do
	for i in `grep '^/\* WEAPON ' "$i"`; do
		unset IMPULSE NAME
		IMPULSE=`echo "$i" | sed -e 's/^\/\* WEAPON \([0-9]\+\) \(.*\) \*\/$/\1/'`
		NAME=`echo "$i" | sed -e 's/^\/\* WEAPON \([0-9]\+\) \(.*\) \*\/$/\2/'`
		WEAPONS[$IMPULSE]="${WEAPONS[$IMPULSE]} $NAME"
		
cat << EOF
@extern float(float action) w_${NAME};
EOF
	done
done

unset IFS

##############################################

cat <<EOF

void() weapon_init_weapons = {
EOF

for i in `seq 0 8`; do
	for j in ${WEAPONS[$i]}; do
		echo "	w_${j}(WEAPON_INIT);"
	done
done

cat <<EOF
};
EOF

##############################################
cat <<EOF

void() weapon_select_best = {
	local float best_imp, best_idx, best_weight, wweight;
	local float bits, bitmask;
	local float(float action) best_func;

	best_func = self.w_func;
	best_imp = self.weapon_imp;
	best_weight = 0;

	bits = best_imp*3;
	bitmask = 7*best_imp;
	best_idx = (self.weapons & bitmask) / bits;

EOF

for i in `seq 0 8`; do
	COUNT=0
	for j in ${WEAPONS[$i]}; do
cat << EOF
	if (w_$j(WEAPON_SELECTABLE) > 0) {
		wweight = w_$j(WEAPON_WEIGHT);
		if (wweight > best_weight) {
			best_weight = wweight;
			best_imp = $i;
			best_idx = $COUNT;
			best_func = w_$j;
		}
	}

EOF
		COUNT=$((COUNT+1));
	done
done

cat <<EOF
	_weapon_select(best_imp, best_idx, best_func);
};
EOF

############################################

cat <<EOF

void(float imp) weapon_select_by_impulse = {
	local float i, bits, bitmask;

	if (imp == 0) { _weapon_select(imp, 0, w_void); return; }
	
	bits = imp*3;
	bitmask = 7*imp;
	i = (self.weapons & bitmask) / bits;

EOF

FIRST=1
for i in `seq 1 8`; do
	WEAPON_MAX=$((`echo "${WEAPONS[$i]}" | wc -w`-1))

	if [ "$WEAPON_MAX" -eq "-1" ]; then continue; fi

	if [ "$FIRST" -eq "1" ]; then
		echo "	if (imp == $i) {"
		FIRST=0
	else
		echo "	} else if (imp == $i) {"
	fi

cat <<EOF
		if (imp == self.weapon_imp) i++;

EOF
	COUNT=0
	for j in ${WEAPONS[$i]}; do
		if [ "$COUNT" -eq "0" ]; then continue; fi
cat <<EOF
		if (i == $COUNT && w_$j(WEAPON_SELECTABLE)) { _weapon_select(imp, i, w_$j); return; } i++;
EOF
		COUNT=$((COUNT+1))
	done

cat <<EOF
		i = 0;

EOF

	COUNT=0
	for j in ${WEAPONS[$i]}; do
cat <<EOF
		if (i == $COUNT && w_$j(WEAPON_SELECTABLE)) { _weapon_select(imp, i, w_$j); return; } i++;
EOF
		COUNT=$((COUNT+1))
	done
done

cat <<EOF
	}
};
EOF

#######################################################

cat <<EOF

void() update_weapon_flags = {
EOF

for i in `seq 1 8`; do
	WEAPON_MAX=$((`echo "${WEAPONS[$i]}" | wc -w`-1))

cat <<EOF
	self.items &= ~${ITFLAGS[$i]};
EOF
	if [ "$WEAPON_MAX" -eq "-1" ]; then continue; fi

	COUNT=0
	for j in ${WEAPONS[$i]}; do
cat <<EOF
	if (w_$j(WEAPON_SELECTABLE)) self.items |= ${ITFLAGS[$i]};
EOF
		COUNT=$((COUNT+1))
	done

	echo
done

cat <<EOF
};
EOF
