/*
No Copyright

The person who associated a work with this deed has dedicated the work to the
public domain by waiving all of his or her rights to the work worldwide under
copyright law, including all related and neighboring rights, to the extent
allowed by law.

You can copy, modify, distribute and perform the work, even for commercial
purposes, all without asking permission.

See https://creativecommons.org/share-your-work/public-domain/cc0/ for more
information on the license.


This file is part of the Zork inspired text adventure, made by Tom Blauwendraat.

More info on the project:
https://www.geekabit.nl/projects/some-gifts-for-me/#zork-inspired-text-adventure
*/


#include <stdio.h>
#include <string.h>
#include "vanalles.h"
#include "world.h"

//----------------------------------------------------------------------------
// WORLD DEFINITION
//----------------------------------------------------------------------------

world world1 =
  { 1,
  "Welcome to this world!\n\n"
  "You are Maarten Tromp, hero and saviour of mankind!\n"
  "Having pursued many ambitious projects, you are currently\n"
  "sitting in front of your computer writing a script.\n"
  "Your objective in this game is unknown. Well, to be more\n"
  "precise, it is unknown to you. You forgot. Enjoy the game!\n\n"
  "Oh yeah, you are also a lowly callcenter operator.\n"
  "Everyone needs money eh. So, don't be late for work.\n"
};

object objs[] = {
  { 0, "", "", "This object does not exist.\n", 0 },
  { 1, "a", "computer", "It runs Linux.\n", 0 },
  { 2, "some", "plants", "They look smokeable.\n", 0 },
  { 3, "a", "note", "HAPPY BIRTHDAY! And now die. ;-)\n"
      "<<programmer's note: i'll be fucking late for your party>>\n", 0 }
};

location locs[] = {
  { 0, "This location does not exist", 0, 0, 0, 0, 0, 0, 0, 0 },
  { 1, "You are in your room.\n", 0, 2, 0, 0, 0, 0, 1, 0 },
  { 2, "You are in the hallway.\n", 1, 3, 0, 0, 0, 4, 2, 0 },
  { 3, "You are in the room of Ruud.\n"
       "It smells funny here.\n", 2, 0, 0, 0, 0, 0, 0, 0 },
  { 4, "Oh no! You screwed up again! You fell down the stairs.\n"
       "You can't move and you're almost dead.. but wait..\n"
       "is that a note?\n", 0, 0, 0, 0, 0, 0, 3, 0 }
};

//----------------------------------------------------------------------------
// ATTRIBUTES
//----------------------------------------------------------------------------

int curloc;
char temp[1024];
int exitflag = 0;
char prompt[100] = ">";

//----------------------------------------------------------------------------
// ROUTINES
//----------------------------------------------------------------------------

void beginworld() {
  curloc = world1.startloc;
  printf("%s\n", world1.welcome);
  location_look(curloc);
  command_loop();
}

void command_loop() {
  while (!exitflag) {
    printf(prompt);
    fgets(temp, 256, stdin);
    do_command(temp);
  }
}

int do_command(char *cmd) {
  char ourtemp[1024];
  char ourtemp2[100];
  char *parm2;
  char *xtemp;

  ucase(cmd);
  removetrailingcrlf(cmd);
  strcpy(ourtemp, xthword(cmd, 0));
  strcpy(ourtemp2, xthword(cmd, 1));
  xtemp = trim(ourtemp);
  parm2 = trim(ourtemp2);

  if (emptystring(xtemp)) goto end_command;

  if (strcmp(xtemp, "LOOK")==0) {
    if (emptystring(parm2))
      location_look(curloc);
    else
      item_look(parm2);
  }
  else if (strcmp(xtemp, "NORTH")==0)
    go_north(curloc);
  else if (strcmp(xtemp, "N")==0)
    go_north(curloc);
  else if (strcmp(xtemp, "SOUTH")==0)
    go_south(curloc);
  else if (strcmp(xtemp, "S")==0)
    go_south(curloc);
  else if (strcmp(xtemp, "WEST")==0)
    go_west(curloc);
  else if (strcmp(xtemp, "W")==0)
    go_west(curloc);
  else if (strcmp(xtemp, "EAST")==0)
    go_east(curloc);
  else if (strcmp(xtemp, "E")==0)
    go_east(curloc);
  else if (strcmp(xtemp, "UP")==0)
    go_up(curloc);
  else if (strcmp(xtemp, "U")==0)
    go_up(curloc);
  else if (strcmp(xtemp, "DOWN")==0)
    go_down(curloc);
  else if (strcmp(xtemp, "D")==0)
    go_down(curloc);
  else if (strcmp(xtemp, "EXIT")==0)
    exit_game();
  else if (strcmp(xtemp, "QUIT")==0)
    exit_game();
  else if (strcmp(xtemp, "Q")==0)
    exit_game();
  else printf("Unrecognized command \"%s\".\n", xtemp);

  end_command:
    return 1;
}

//----------------------------------------------------------------------------
// COMMANDS
//----------------------------------------------------------------------------

void location_look(int loc) {
  printf("%s\n", locs[loc].text);
  printf("You see: ");
  spell_items(loc);
  printf("\nExits: ");
  spell_directions(loc);
  printf("\n");
}

int spell_items(int loc) {
  int firstone = 1;
  if (locs[loc].obj1) {
    if (!firstone) printf(", ");
    printf("%s %s", objs[locs[loc].obj1].prefix,
      objs[locs[loc].obj1].name);
    firstone = 0;
  }
  if (locs[loc].obj2) {
    if (!firstone) printf(", ");
    printf("%s %s", objs[locs[loc].obj2].prefix,
      objs[locs[loc].obj2].name);
    firstone = 0;
  }
  if (firstone)
    printf("nothing.");
  else
    printf(".");
  return 0;
}

void item_look(char *itemstr) {
  char utemp[200];

  if (locs[curloc].obj1==0) goto nofound;
  else {
    strcpy(utemp, objs[locs[curloc].obj1].name);
    ucase(utemp);
    if ((strcmp(utemp, itemstr))==0)
      printf(objs[locs[curloc].obj1].appearance);
    else goto nofound;
  }

  // todo: obj2

  return;
  nofound:
    printf("You don't see that here.\n");
}

void spell_directions(int loc) {
  int firstone = 1;
  if (locs[loc].north) {
    if (!firstone) printf(", ");
    printf("north");
    firstone = 0;
  }
  if (locs[loc].south) {
    if (!firstone) printf(", ");
    printf("south");
    firstone = 0;
  }
  if (locs[loc].east) {
    if (!firstone) printf(", ");
    printf("east");
    firstone = 0;
  }
  if (locs[loc].west) {
    if (!firstone) printf(", ");
    printf("west");
    firstone = 0;
  }
  if (locs[loc].up) {
    if (!firstone) printf(", ");
    printf("up");
    firstone = 0;
  }
  if (locs[loc].down) {
    if (!firstone) printf(", ");
    printf("down");
    firstone = 0;
  }

  if (firstone)
    printf("none");
  printf(".");
}

void go_north(int loc) {
  if (locs[loc].north) go_location(locs[loc].north);
    else printf("You can't go there.\n");
}

void go_south(int loc) {
  if (locs[loc].south) go_location(locs[loc].south);
    else printf("You can't go there.\n"); 
}

void go_west(int loc) {
  if (locs[loc].west) go_location(locs[loc].west);
    else printf("You can't go there.\n");
}

void go_east(int loc) {
  if (locs[loc].east) go_location(locs[loc].east);
    else printf("You can't go there.\n");
}

void go_up(int loc) {
  if (locs[loc].up) go_location(locs[loc].up);
    else printf("You can't go there.\n");
}

void go_down(int loc) {
  if (locs[loc].down) go_location(locs[loc].down);
    else printf("You can't go there.\n");
}

void go_location(int loc) {
  curloc = loc;
  location_look(loc);
}

void exit_game(void) {
  printf("Goodbye.");
  exitflag = 1;
}


// end of file
