#!/usr/bin/perl

use warnings;
use strict;

my $dowhat="\nWhat will you do?\n";
my $bridge = "\nYou are on the bridge. There is a red light flashing on the main console.\nThere are doorways to the north, south, and west.\n";
my $bridge2 = "\nYou are on the bridge. You can see out the window that the ship has landed on an unknown planet.\nThere are doorways to the north, south and west\n\n";
my $engine = "\nYou are in the engineering room. There seems to be some smoke coming from\nan open panel.\nThere is a wall panel on the floor. The bridge is to the north.\n\n";
my $engine2 = "\nYou are in the engineering room. There seems to be smoke coming from an open panel.\nThe bridge is to the north\n\n";
my $engine3 = "\nYou are in engineering. The bridge is to the north\n\n";
my $crew = "\nYou enter the crew quarters. Of course, since the crew evacuated, you have them all to yourself. The bridge is to the south\n\n";
my $light = "\nThe light is labeled \"Smoke alert in engineering\"\n\n";
my $plate = 0;
my $lightbulb = 0;
my $airlock2 = "\nYou are in the airlock. The ship has landed on a habitable planet, and\nyou may leave if you want to. The bridge is to the east\n";
my $airlock = "\nYou are in the airlock. You are currently travelling through hyperspace,\nand cannot leave the ship. The bridge is to the east\n";

print qq(Welcome to "Spaceship"\n);
print "What is your name? \n\n";
print "My name is:";
my $name=<>;
chomp $name;
	print "Welcome to the bridge, $name\n\n", "Uh Oh, it looks like something is wrong. You will need to make an emergency\nlanding... If you can fix the ship first.\n\n";
	

BRIDGE: {
	if ($lightbulb == 0) {
		print "$bridge";
		}
	else {
		print "$bridge2";
		}
	BRIDGE2: {
	print "$dowhat";
	my $do = <>;
	chomp $do;
	if ($do eq "examine light") {
		print $light;
		redo BRIDGE2;
		}
		elsif ($do eq "north") {
		goto CREW;
		}
		if ($do eq "west") {
		goto AIRLOCK;
		}
		elsif ($do eq "look") {
		goto BRIDGE;
		}
		elsif ($do eq "south") {
		goto ENGINE;
			}
		goto BRIDGE2;
		}			
}

ENGINE: {
	if ($plate == 0) {
		print "$engine";
		}
	elsif ($plate == 1) {
		print "$engine2";
		}
	elsif ($plate == 2) {
		print "$engine3";
			}
		ENGINE2: {
			print "$dowhat";
			my $do = <>;
			chomp $do;
			if ($do eq "take panel") {
				print "You take the metal panel";
				$plate=1;
				}
			elsif ($do eq "north") {
			goto BRIDGE;
			}
			elsif ($do eq "use panel") {
					if ($plate == 1) {
						print "You replace the plate over the hole in the wall, and\nsinglehandedly save yourself from smoke inhalation.\n";
						$plate = 2;
						$lightbulb = 1; 
							}
					else {
						print "\nYou have no plate to use\n";
							}
						}
			elsif ($do eq "look") {
				goto ENGINE;
				}
			}
		goto ENGINE2;
		}

CREW: {
	print $crew;
		CREW2: {
			print "$dowhat";
			my $do = <>;
			chomp $do;
			if ($do eq "south") {
				goto BRIDGE;
					}
			elsif ($do eq "look") {
			goto CREW;
				}
			goto CREW2;
			}
}

AIRLOCK: {
	if ($lightbulb == 1) {
		print "$airlock2";
		}
		else {
		print "$airlock";
			}
		AIRLOCK2: {
			print "$dowhat";
			my $do = <>;
			chomp $do;
			if ($lightbulb == 1) {
				if ($do eq "leave ship") {
					goto WIN;
						}
			else {
				goto AIRLOCK2
				}
					}
			if ($lightbulb == 0) {
				if ($do eq "leave ship") {
					 print "\nYou can't leave yet, you have to fix the ship first so it can land\n";
						}
					}
			if ($do eq "east") {
				goto BRIDGE;
				}
			else {
				goto AIRLOCK2;
				}
				}
		}

WIN: {
	print "Congratulations! You fixed the ship. You have left to explore the alien\nlandscape. To continue this storyline, wait for me to get bored and make a\nsequal.\n";
	goto PLAY;
}


		
PLAY: {
print 'Play again? [Y/N]';
my $playagain=<>;
chomp $playagain;
if ($playagain eq "y") {
	$plate=0;
	$lightbulb=0;
	print "\n\n\n\n\n";
	goto BRIDGE;
	}
if ($playagain eq "n") {
	print "Exiting...";
	exit 0;
	}
redo PLAY
}	

