/*
	wsc2wormnet - converts Worms Armageddon scheme files to WormNET scheme format
	Copyright (C) 2005  Simon Arlott

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <stdio.h>
#include <unistd.h>

#define WA 2
#define WWP 1

int main() {
	unsigned char c;
	int mode=0;
	int i=0;

	while (read(0,&c,1)>0) {
		i++;

		if ((i == 1 && c != 'S')
			|| (i == 2 && c != 'C')
			|| (i == 3 && c != 'H')
			|| (i == 4 && c != 'M')) {
			fprintf(stderr, "Invalid WSC file\n");
			return 1;
		} else if (i == 5) {
			if (c != WA) {
				fprintf(stderr, "Invalid WSC version\n");
				return 1;
			}
			mode = c;
			continue;
		} else if (i < 6) { continue; }

		if (mode == WA) {
			if (i == 6) { // hot-seat time
				if (c == 0) c=0;
				else if (c <= 5) c=5;
				else if (c <= 10) c=10;
				else c=15;
			} else if (i == 7 || i == 8) { // retreat time
				if (c == 0) c=0;
				else if (c <= 3) c=3;
				else if (c <= 5) c=5;
				else c=10;
			} else if (i == 18 || i == 20  || i == 22) { // crates - weapon, health, utility
				if (c < 251) c+=5;
				c/=10;
				if (c > 10) c=10;
			} else if (i == 21) { // health crates
				if (c <= 25) c=1;
				else if (c <= 50) c=2;
				else if (c <= 75) c=3;
				else c=4;
			} else if (i == 27) { // health
				if (c <= 100) c=2;
				else if (c <= 150) c=3;
				else c=4;
			} else if (i == 28) { // turn time
				if (c <= 15) c=0;
				else if (c <= 20) c=1;
				else if (c <= 30) c=2;
				else if (c <= 45) c=3;
				else if (c <= 60) c=4;
				else c=5;
			} else if (i == 29) { // round time
				if (c == 0) c=0;
				else if (c <= 5) c=1;
				else if (c <= 10) c=2;
				else if (c <= 15) c=3;
				else if (c <= 20) c=4;
				else if (c <= 25) c=5;
				else c=6;
			} else if (i == 30) { // no. of rounds
			} else if (i >= 42) { // weapon settings
				int t=(i-42) % 4;
				if (t == 0 && c > 10) c=10; // ammo 0-10 (a-k)
				else if (t == 1 && c > 4) c=4; // power 1-5 (a-e)
				else if (t == 2 && c > 9) c=9; // delay 0-9 (a-j)
				else if (t == 3 && c > 5) c=5; // crate 0-5 (a-f)
			}
		}

		if (c <= 15) c+=97;
		else c='?';

		write(1,&c,1);
	}

	c=10;
	write(1,&c,1);

	return 0;
}
