<?php
// Based On: http://www.reddit.com/r/rpg/comments/1nqqjd/random_tavern_name_generator/

// Show source
if(isset($_GET['source']) AND $_GET['source'] == 1) { print "<pre>".htmlentities(file_get_contents("./tavern.php"))."</pre>"; die(); }

// Record starting time
$time_start = microtime(true);

// Flag inputs
$help = (isset($_GET['help']) AND $_GET['help'] == 1) ? true : false;								// Show help screen
$dark = (isset($_GET['dark']) AND $_GET['dark'] == 1) ? true : false;								// Display dark theme
$count = (isset($_GET['n']) AND $_GET['n'] > 0) ? min(1000, $_GET['n']) : 1;						// Get tavern count

// Temporary variables
$taverns = array();

// Misc functions
function url($url) { return "http://www.tabletopsociety.com/misc/tavern.php?{$url}"; }
function href($text, $url) { return "<a href='http://www.tabletopsociety.com/misc/tavern.php?{$url}'>{$text}</a>"; }
function array_mt_rand(&$arr) { return $arr[ roll(count($arr)) - 1 ]; }
function roll($faces) { return mt_rand(1,$faces); }

// Adjective1
function get_adjective($set = null)
{
	$adjectives = array(
		array('Black', 'Blue', 'Brass', 'Copper', 'Crystal', 'Golden', 'Green', 'Grey', 'Iron', 'Orange', 'Purple', 'Red', 'Shining', 'Silver', 'White', 'Yellow', 'Pink', 'Pearl', 'Rainbow'),
		array(
			'Angry', 'Ardent', 'Bawdy', 'Blind', 'Crazy', 'Crooked', 'Dark', 'Deaf', 'Dirty', 'Disfigured', 'Errant', 'Fancy', 'Fat', 'Fickle', 'Filthy', 'Foolish', 'Friendly', 'Frightened', 'Frigid', 'Graceful', 'Grumpy', 'Hairy',
			'Happy', 'Honest', 'Humble', 'Jovial', 'Lazy', 'Lonely', 'Loyal', 'Mad', 'Majestic', 'Muddy', 'Naughty', 'Noisy', 'Olde', 'Pale', 'Pretty', 'Proud', 'Queasy', 'Quick', 'Ragged', 'Regal', 'Rowdy', 'Royal', 'Rusty', 'Salty',
			'Skanky', 'Slippery', 'Sober', 'Soggy', 'Squeaky', 'Stingy', 'Straight', 'Strange', 'Striped', 'Studious', 'Swift', 'Thin', 'Toothless', 'Ugly', 'Vulgar',
			'Eager', 'Gloomy', 'Roasted', 'Pickled', 'Mangy', 'Curvy', 'Twisted',
		),
	);
	
	if($set == null) { $set = array_rand($adjectives); }
	return $adjectives[$set][array_rand($adjectives[$set])];
}

function get_adjective1() { return get_adjective(0); }
function get_adjective2() { return get_adjective(1); }

// Number
function get_number($plural = false)
{
	$numbers = array('One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve');
	
	$tmp = $numbers[array_rand($numbers)];
	
	if($tmp == 'One') { return 'Thirteen'; }
	
	return $tmp;
}

function get_plural_number()
{
	return get_number(true);
}

// Verb (not actually verbs)
function get_verb()
{
	$verbs = array(
		'Bleeding', 'Crying', 'Dancing', 'Drinking', 'Fainting', 'Falling', 'Fighting', 'Flying', 'Laughing', 'Leaping', 'Nesting', 'Over-Flowing', 'Prancing', 'Praying', 'Rhyming', 'Running', 'Skulking', 'Sleeping', 'Spitting', 'Wandering',
		'Stinking', 'Galloping', 'Throbbing', 'Inspiring',
	);
	return $verbs[array_rand($verbs)];
}

// Noun
function get_noun($type = null)
{
	$nouns = array(
		'person' => array(
			'Archer', 'Artisan', 'Baker', 'Bandit', 'Beggar', 'Bishop', 'Blacksmith', 'Brewer', 'Butcher', 'Butler', 'Cardinal', 'Cobbler', 'Cook', 'Cooper', 'Damsel', 'Drummer', 'Duke', 'Duchess', 'Executioner', 'Farmer', 'Ferrier', 'Fisherman', 
			'Fishmonger', 'Fool', 'Gatekeeper', 'Gravedigger', 'Groom', 'Guildsman', 'Hatter', 'Hayward', 'Herald', 'Hunter', 'Jailer', 'Jester', 'King', 'Knight', 'Lady', 'Lord', 'Maid', 'Maiden', 'Mason', 'Mayor', 'Mercenary', 'Merchant', 'Miller',
			'Miner', 'Minstrel', 'Monk', 'Noble', 'Pilgrim', 'Pirate', 'Potter', 'Priest', 'Prince', 'Princess', 'Privateer', 'Queen', 'Ratcatcher', 'Sailor', 'Scholar', 'Scribe', 'Scullery', 'Servant', 'Sheriff', 'Slattern', 'Slave', 'Smuggler',
			'Soldier', 'Spy', 'Squire', 'Steward', 'Tailor', 'Tax Collector', 'Thief', 'Tinker', 'Torturer', 'Vagabond', 'Weaver', 'Wench', 'Wizard',
			'Hobbit', 'Dwarf', 'Elf',
			'Wife', 'Husband', 'Father', 'Mother',
		),
		'animal' => array(
			'Ape', 'Honey Badger', 'Bat', 'Bear', 'Beaver', 'Bee', 'Bison', 'Boar', 'Camel', 'Cat', 'Cockerel', 'Crow', 'Doe', 'Dog', 'Dolphin', 'Dove', 'Duck', 'Eagle', 'Elephant', 'Emu', 'Ferret', 'Fish', 'Flounder', 'Fox', 'Frog', 'Goat', 'Goose',
			'Hawk', 'Heron', 'Hippo', 'Hornet', 'Horse', 'Hound', 'Kingfisher', 'Ladybug', 'Lion', 'Lizard', 'Magpie', 'Mantis', 'Mink', 'Monkey', 'Mouse', 'Ostrich', 'Otter', 'Owl', 'Ox', 'Parrot', 'Pig', 'Pony', 'Raccoon', 'Rat', 'Raven', 'Rhino',
			'Rooster', 'Seahorse', 'Seal', 'Shark', 'Skunk', 'Snake', 'Song Bird', 'Sparrow', 'Spider', 'Squirrel', 'Stag', 'Stork', 'Swan', 'Thrush', 'Tiger', 'Toad', 'Tortoise', 'Turkey', 'Turtle', 'Vulture', 'Walrus', 'Warthog', 'Weasel', 'Whale',
			'Wolf', 'Wolverine', 'Woodchuck', 'Zebra'
		),
		'monster' => array(
			'Angel', 'Beholder', 'Brownie', 'Bugbear', 'Centaur', 'Chimera', 'Devil', 'Dragon', 'Faerie', 'Giant', 'Goblin', 'Golem', 'Greenman', 'Griffin', 'Hellhound', 'Hobgoblin', 'Jackalope', 'Kobold', 'Mimic', 'Minotaur', 'Ogre', 'Orc', 'Otyugh',
			'Owlbear', 'Peryton', 'Phoenix', 'Rust Monster', 'Scarecrow', 'Skeleton', 'Sphinx', 'Stirge', 'Troll', 'Unicorn', 'Werewolf', 'Wyvern'
		),
		'object' => array(
			'Anchor', 'Anvil', 'Barrel', 'Beef', 'Bell', 'Blessing', 'Boot', 'Bottle', 'Bowl', 'Bucket', 'Cactus', 'Caltrop', 'Candle', 'Cap', 'Castle', 'Chain', 'Chair', 'Cheese', 'Cloak', 'Crown', 'Cup', 'Dagger', 'Door', 'Fire', 'Flag', 'Flagon',
			'Flail', 'Folly', 'Goblet', 'Hammer', 'Hart', 'Head', 'Hoe', 'Hook', 'Hop Pole', 'Jug', 'Kettle', 'Key', 'Ladder', 'Lantern', 'Lock', 'Mace', 'Map', 'Mill', 'Moon', 'Mug', 'Mutton', 'Net', 'Noose', 'Oak', 'Post', 'Pot', 'Quill', 'Quiver',
			'Ring', 'Road', 'Rose', 'Sausage', 'Sceptre', 'Scythe ', 'Shoe', 'Spade', 'Spigot', 'Spoon', 'Staff', 'Stars', 'Stone', 'Sun', 'Sundial', 'Sword', 'Table', 'Tankard', 'Tap', 'Thorn', 'Torch', 'Tree', 'Wagonwheel', 'Water', 'Wheel', 'Whip',
			'Wind', 'Windmill',
			'Cloud', 'Sack', 'Ingot',
		),
	);
	
	if($type == null) { $type = array_rand($nouns); }
	return $nouns[$type][array_rand($nouns[$type])];
}

// Helper functions (mainly to avoid more regex shenanigans)
function get_object() { return get_noun('object'); }
function get_monster() { return get_noun('monster'); }
function get_animal() { return get_noun('animal'); }
function get_person() { return get_noun('person'); }

// Locations (separate from nouns to avoid weirdness)
function get_location()
{
	$locations = array(
		'Bay', 'Sea', 'River', 'Mountain', 'Hill', 'Shore', 'Lake', 'Pond', 'Stream', 'City', 'Tower', 'Harbor', 'Pier', 'Market', 'Square'
	);
	
	return $locations[array_rand($locations)];
}

// Relations
function get_relation()
{
	$relations = array(
		'of', 'by', 'above', 'under', 'at', 'upon',
	);
	
	return $relations[array_rand($relations)];
}

// Names
function get_name()
{
	$names = array(
	);
	
	return $names[array_rand($names)];
}

// Prefixes
function get_prefix()
{
	$prefixes = array('The ', 'The ', 'The ', 'The ', 'The ', 'The ', 'The ', 'The ', 'Ye Olde ', '');
	
	return $prefixes[array_rand($prefixes)];
}

// Build random tavern(s)
function generate()
{
	global $taverns;
	global $count;
	
	$formats = array(
		'simple' => array(
			"[prefix][noun]",
			"[prefix][noun] &amp; [noun]",
			"[prefix][adjective] [noun]",
			"[prefix][verb] [noun]",
			"[prefix][verb] [monster]",
		),
		'medium' => array(
			"[prefix][object] of the [noun]",
			"[prefix][noun] by the [location]",
			"[prefix][noun] of the [location]",
			"[prefix][plural_number] [noun]s",
		),
		'complex' => array(
			"[prefix][animal]'s [adjective] [object]",
			"[prefix][monster]'s [adjective] [object]",
			"[prefix][noun] [relation] the [location]",
			"[prefix][noun] &amp; [adjective] [noun]",
			"[prefix][adjective] [noun]'s [object]",
			"[prefix][adjective1] [adjective2] [noun]",
			"[prefix][plural_number] [adjective] [noun]s",
		),
	);
	
	for($j = 0; $j < $count; $j++)
	{
		$complexity = 'simple';
		$r = rand(0, 100);
		if($r > 80) { $complexity = 'complex'; }
		else if($r > 50) { $complexity = 'medium'; }
		
		$format = $formats[$complexity][array_rand($formats[$complexity])];
		$tavern = $format;

		// Get replacements
		$matches = array();
		preg_match_all("/\[(.*?)\]/", $format, $matches);

		for($i = 0; $i < count($matches[0]); $i++)
		{
			// Using strpos instead of str_replace to avoid replacing multiple instances with same result
			$pos = strpos($tavern,$matches[0][$i]);
			if ($pos !== false) {
				$tavern = substr_replace($tavern, call_user_func("get_".$matches[1][$i]), $pos, strlen($matches[0][$i]));
			}
		}

		$taverns[] = $tavern;
	}
}

// Go Go Go
if($help != 1)
{
	// Execute Tavern creation
	generate();
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<title>Instant Tavern Generator</title>
	<style type="text/css">
	html { height: 100%; }
	body {
		height: 100%;
		margin: 0px;
		font-family: Georgia, serif;
		/* Internet Explorer 10 */
		display:-ms-flexbox;
		-ms-flex-pack:center;
		-ms-flex-align:center;

		/* Firefox */
		display:-moz-box;
		-moz-box-pack:center;
		-moz-box-align:center;

		/* Safari, Opera, and Chrome */
		display:-webkit-box;
		-webkit-box-pack:center;
		-webkit-box-align:center;

		/* W3C */
		display:box;
		box-pack:center;
		box-align:center;
	}
	a.tavern { color: #000; text-decoration: none; text-align: center; display: block; width: 100%; }
	a.toolbar { display: block; font-weight: bold; color: #a0a0a0; line-height: 20px; padding: 2px; position: fixed; right: 1em; top: 1em; text-decoration: none; }
	
	div.help { margin: auto; width: 900px; padding: 10px 0px; font-size: 1.25em; }
	div.tavern { margin: auto; width: 100%; text-align: center; }
	
	body.dark { color: #fff !important; background-color: #101010; }
	.dark a { color: #fff; }
	</style>
</head>
<body<?php if($dark) { echo " class='dark'"; } ?>>
	<a href="http://www.tabletopsociety.com/misc/tavern.php?help=1&dark=<?php echo $dark; ?>" class="toolbar">?</a>
	
<?php if($help != 1) { ?>
	<div>
	<?php foreach($taverns as $tavern) { ?>
	<div class="tavern" style="padding: <?php echo (int)(max(20, (50 / $count))); ?>px 0px; font-size: <?php echo min(4, max(2, (4 / ($count/4)))); ?>em;">
		<a href="#" onclick="javascript:location.reload(true);" class="tavern"><?php echo $tavern; ?></a>
	</div>
	<?php } ?>
	</div>
<?php } else { ?>

	<div class="help">
		<p><strong>Instant Tavern Generator: Usage</strong></p>
		<p>TODO</p>
		<p>
			<em>Handy Form:</em>
		</p>
		<form method="get" style="font-size: 0.7em;">
			<strong>Number: </strong><input size="2" name="n" value="1" /> 
			<strong>Visual Style: </strong><select name="dark"><option value="0"<?php echo (!$dark) ? " SELECTED" : ""; ?>>Light</option><option value="1"<?php echo ($dark) ? " SELECTED" : ""; ?>>Dark</option></select><br />
			<input type="submit" value="Go" />
		</form>
		<p>
			<em>Examples:</em>
			<ul>
				<li><?php echo href("Random (Default)", ""); ?></li>
				<li><?php echo href("10 results [n=10]", "n=10"); ?></li>
			</ul>
		</p>
		<p>
			<em>Notes:</em>
			<ul>
				<li>Bugs / Suggestions / Feedback: <a href="mailto:timberjaw@gmail.com">timberjaw@gmail.com</a></li>
				<li><?php echo href("View Source Code", "source=1"); ?></li>
			</ul>
		</p>
	</div>
<?php } ?>
<?php $time_end = microtime(true); $time = $time_end - $time_start; echo "<!-- Page Built in  {$time}s -->"; ?>
</body>
</html>