game.title = "Bloodfields";

global
{
	command: "quit"
	{
		exit();
	}

	command: "exit"
	{
		exit();
	}
}

situation <<start>>
{
	setup
	{
		global.enemyTypes = {
			1: {
				'name': 'snail',
				'action': 'oozes on',
				'hp': 3,
				'attack': 2
			},
			2: {
				'name': 'wolf',
				'action': 'bites',
				'hp': 7,
				'attack': 3
			},
			3: {
				'name': 'snake',
				'action': 'strikes at',
				'hp': 3,
				'attack': 3
			}
		};

		global.enemyMods = {
			1: {
				'name': 'pathetic',
				'hp': 0,
				'attack': 0
			},
			2: {
				'name': 'sickly',
				'hp': 3,
				'attack': 1
			},
			3: {
				'name': 'wimpy',
				'hp': 5,
				'attack': 2
			}
		};

		player.hpMax = 10;
		player.hpCur = player.hpMax;
		player.xp = 0;
		player.level = 1;
		player.attack = 3;
		player.potions = 1;

		global.bJustTravelled = false;

		goto( <<field>> );
	}
}

function status()
{
	display('You have ' + (100-player.xp) + ' xp until your next level.');
	display('You have ' + player.hpCur + ' of ' + player.hpMax + ' hp.');
	display('You are carrying ' + player.potions + ' potions.');
}

function look()
{
	display('''You are standing in a field of wheat.''');
	if exists(global.enemy) then
	{
		display('You see a ' + global.enemy['name'] + ' in front of you.');
	}
	else if exists( global.treasure ) then
	{
		display('You find a ' + global.treasure['name'] + ' lying here!');
	}
}

function mkEnemy()
{
	eid = random( 1, 3 );
	global.enemy = global.enemyTypes[eid];

	mod = player.level;
	if mod > 3 then
	{
		mod = 3;
	}
	global.enemy['name'] = global.enemyMods[mod]['name'] + ' ' +
		global.enemy['name'];
	global.enemy['attack'] += global.enemyMods[mod]['attack'];
	global.enemy['hp'] += global.enemyMods[mod]['hp'];
	global.enemy['level'] = mod;
}

function mkTreasure()
{
	global.treasure = {
		'name': 'potion'
	};
}

function rollAttack( attack )
{
	return( random( attack-attack/3, attack+attack/3 ) );
}

function playerAttack()
{
	damage = rollAttack( player.attack );
	global.enemy['hp'] = global.enemy['hp'] - damage;
	display('You strike the ' + global.enemy['name'] + ' for ' + damage +
		' damage.');
	if global.enemy['hp'] <= 0 then
	{
		xp = 10;
		display('You killed the ' + global.enemy['name'] + '!  You gained ' +
			xp + ' experience points.');
		player.xp += xp;
		while player.xp >= 100 do
		{
			player.xp -= 100;
			player.level += 1;
			player.hpMax += integer(random(0.25,0.75)*player.hpMax);
			player.hpCur = player.hpMax;
			display("You have leveled!  Welcome to level " + player.level );
		}

		select = random(1, 3);
		if select == 1 then
		{
			display('It looks like the ' + global.enemy['name'] +
				' dropped something...');
			mkTreasure();
		}
		delete( global.enemy );
	}
}

function enemyAttack()
{
	damage = rollAttack( global.enemy['attack'] );
	player.hpCur -= damage;
	display("The " + global.enemy['name'] + ' ' + global.enemy['action'] +
		' you for ' + damage + ' damage.');
	if player.hpCur <= 0 then
	{
		display('The ' + global.enemy['name'] + ' killed you!');
		exit();
	}
	else
	{
		display('''You have ''' + player.hpCur + ''' hp left.''');
	}
}

situation <<travel>>
{
	enter
	{
		delete( global.enemy );
		delete( global.treasure );
		
		display('You wander aimlessly through the endless field of wheat.');

		select = random(1,6);
		if select <= 4 then // 66% of the time, enemy!
		{
			mkEnemy();
			display('''There's a ''' + global.enemy['name'] + ''' here!''');
		}
		else if select == 6 then // 16% of the time, treasure!
		{
			mkTreasure();
			display('''You find a ''' + global.treasure['name'] +
				''' lying here!''');
		}

		global.bJustTravelled = true;

		goto( <<field>> );
	}
}

situation <<field>>
{
	command: "attack"
	{
		if exists(global.enemy) then
		{
			playerAttack();
			goto( <<field>> );
		}
		else
		{
			display('''There's nothing here to attack...''');
		}
	}

	command: "take"
	{
		if exists(global.treasure) then
		{
			if global.treasure['name'] == 'potion' then
			{
				display('''You pickup the ''' + global.treasure['name'] +
					''' and put it in your pocket.''');
				player.potions += 1;
				delete( global.treasure );
			}
		}
		else
		{
			display('''There's nothing here to take...''');
		}
	}

	command: 'drink'
	{
		if player.potions == 0 then
		{
			display('''You don't have anything to drink!''');
		}
		else
		{
			if player.hpCur == player.hpMax then
			{
				display('''You're already as healthy as can be!
					Save the potions for when you're more injured.''');
			}
			else
			{
				player.potions -= 1;
				gain = integer( random( 0.25, 0.5 ) * player.hpMax );
				player.hpCur += gain;
				if player.hpCur > player.hpMax then
				{
					gain -= player.hpCur - player.hpMax;
					player.hpCur = player.hpMax;
				}
				display('''That really hit the spot!  The potion restored ''' +
					gain + ''' hp!''');
				goto( <<field>> );
			}
		}
	}

	command: "look"
	{
		look();
	}

	command: "status"
	{
		status();
	}

	command: "walk"
	{
		if not exists(global.enemy) then
		{
			goto( <<travel>> );
		}
		else
		{
			display("You can't leave, the " + global.enemy['name'] +
				' is blocking your path.');
		}
	}

	command: "help"
	{
		display("Available commands are: walk, status, look, attack, take,
			drink");
	}

	setup
	{
		look();
	}

	enter
	{
		if not global.bJustTravelled then
		{
			if exists( global.enemy ) then
			{
				enemyAttack();
			}
		}
		global.bJustTravelled = false;
	}
}