#!/usr/bin/perl
use strict;
use warnings;
use lib "./";
use Roguelike;

# Setup
my $fast = 1;
foreach my $opt (@ARGV) {
  $fast = 1 if $opt eq "--fast";
  $fast = 0 if $opt eq "--intro";
  $Game->{Centered} = 1 if $opt eq "--center";
  $Game->{Testing} = 1 if $opt eq "--test";
}
$Game->{MaxDepth} = 20;

# Load stuff.
$Game->loaditems("Equipment");
$Game->loadmons("Monsters");
$Game->loaditems("Items");
$Game->loadkeymap("Keymap");

# Set up the opening room.
$Game->addlevel("<opening");

# Add stairs
$Game->{Levels}[0]{Map}[22][21]{_} = "<";
render($Game->{Levels}[0]{Map}[22][21]);
$Game->{Levels}[0]{StairsUp}[0] = [22, 21];
$Game->{Levels}[0]{Map}[1][21]{_} = ">";
render($Game->{Levels}[0]{Map}[1][21]);
$Game->{Levels}[0]{StairsDown}[0] = [1, 21];

# Now Player. We need the map in place.
$Game->loadmons("Player");

# And items!
if ($fast) {
  $Game->{Silence} = 1;
  $Player->{Inv}->add(
    $Game->{Content}{incense}->new(Mod => 0, Qty => 4)
  );
  $Player->{Inv}->add(
    $Game->{Content}{tarotcard}->new(Qty => 2)
  );
  $Player->{Inv}->add(
    $Game->{Content}{trenchcoat}->new
  );
  $Player->{Inv}->add(
    $Game->{Content}{ratflail}->new
  );
  $Player->switchweapon;
  $Player->wear( $Player->{Inv}->get("d") );
  $Player->go(1, 21);
  push @{ $Game->{DelayedInit} }, sub {
    $Player->{Base}->eventoff("READYMSG");
  };
  $Game->{Silence} = 0;
  #Roguelike::Interface::Curses::ungetch(">");
} else {
  $Game->{Levels}[0]->inv(20, 9, "add",
                          $Game->{Content}{incense}->new(Mod => 0, Qty => 3)
  );
  render($Game->{Levels}[0]{Map}[20][9]);
  $Game->{Levels}[0]->inv(15, 9, "add",
                          $Game->{Content}{tarotcard}->new(Qty => 2)
  );
  render($Game->{Levels}[0]{Map}[15][9]);
  $Game->{Levels}[0]->inv(22, 33, "add",
                          $Game->{Content}{incense}->new(Mod => 0)
  );
  render($Game->{Levels}[0]{Map}[22][33]);
  $Game->{Levels}[0]->inv(15, 33, "add",
                          $Game->{Content}{trenchcoat}->new
  );
  render($Game->{Levels}[0]{Map}[15][33]);
  $Game->{Levels}[0]->inv(19, 27, "add",
                          $Game->{Content}{ratflail}->new
  );
  render($Game->{Levels}[0]{Map}[19][27]);

  # Set the mood... introduction scene!
  $Game->{UI}->msgbox(0);
  $Game->{UI}->msgbox(1,
  "Hazy summer days, endless, just drifting. Constant work on your ambitious
  Perl roguelike project. Coffee, insomnia, incense. Night and day clashing in
  one titanic headache. Away from friends, just stranded on this surreal island 
  between two lives. And that constant, throbbing feeling of <Blue>Isometric
  Boredom<black>..."
  );
  $Game->{UI}->msgbox(1,
  "The monotony is nothing more than a distraction. You've thrown your heart
  into the water many times before, but the river of junior high was always so
  shallow that it never had far to sink. But at the very end, somehow, it sank
  into the depths of the ocean. The bubble of blissful beauty beneath the
  distorted surface soon popped, leaving you to face the terrors of the depths. 
  Your heart washed up on shore, bruised and beaten."
  );
  $Game->{UI}->msgbox(1,
  "The feverish dreams of agony began, curiously coinciding with the arrival of
  your new authentic Native American dreamcatcher. Somewhere deep in the depths
  of your own personal hell, you dropped your heart. This pitiful escape would
  sicken you, if you could still feel. You need to recover yourself."
  );
  $Game->{UI}->msgbox(1,
  "You waver gently on the stream of conciousness, treading calmly. The serene
  scene of phospherance above you swirls, emptying into an endless void. You've
  drifted way past the Lover's Reef, into the dreadful place where you cannot
  help but to sink into the place where you must face your worst..."
  );

  # Main menu!
  my $load = 0;
  while (1) {
    my $in = $Game->{UI}->msgbox(5,
      "EmotionRL",
      "By Da-Breegster",
      "",
      "Press space to begin the game.",
      "Or another key to choose another option.",
      "<purple>I<black>nstructions",
      "<purple>L<black>oad game"
    );
    if ($in eq " ") {
      last;
    } elsif ($in eq "i" or $in eq "I" or $in eq "?") {
      $Game->{UI}->msgbox(1,
      "<purple>Instructions<black>",
      "Welcome to EmotionRL, the first demo game made with the PerlRL engine!",
      "EmotionRL should be pretty easy; I wanted to show off my engine more than
      I wanted to create a balanced game.",
      "I'm assuming you're familiar with roguelikes. The summary of the keymap
       only holds ture if you haven't changed the Keymap file.",
      "<Green>I highly recommend that you examine all items and monsters, read
       all messages, and try out all items and attacks."
      );
      $Player->help;
    } elsif ($in eq "l" or $in eq "L") {
      if (-e "game.tgz") {
        $load = 1;
        last;
      } else {
        $Game->{UI}->msgbox(1,
          "There is no saved game in your current directory!"
        );
      }
    }
  }
  $Game->{UI}->msgbox(2);
  Roguelike::Interface::Curses::ungetch("L") if $load;
}

$Game->start;
