phantasmal_dgd_v1/
phantasmal_dgd_v1/bin/
phantasmal_dgd_v1/doc/
phantasmal_dgd_v1/mud/doc/
phantasmal_dgd_v1/mud/doc/api/
phantasmal_dgd_v1/mud/doc/kernel/
phantasmal_dgd_v1/mud/doc/kernel/hook/
phantasmal_dgd_v1/mud/doc/kernel/lfun/
phantasmal_dgd_v1/mud/include/
phantasmal_dgd_v1/mud/include/kernel/
phantasmal_dgd_v1/mud/kernel/lib/
phantasmal_dgd_v1/mud/kernel/lib/api/
phantasmal_dgd_v1/mud/kernel/obj/
phantasmal_dgd_v1/mud/kernel/sys/
phantasmal_dgd_v1/mud/tmp/
phantasmal_dgd_v1/mud/usr/System/
phantasmal_dgd_v1/mud/usr/System/keys/
phantasmal_dgd_v1/mud/usr/System/obj/
phantasmal_dgd_v1/mud/usr/System/open/lib/
phantasmal_dgd_v1/mud/usr/common/data/
phantasmal_dgd_v1/mud/usr/common/lib/parsed/
phantasmal_dgd_v1/mud/usr/common/obj/telopt/
phantasmal_dgd_v1/mud/usr/common/obj/ustate/
phantasmal_dgd_v1/mud/usr/game/
phantasmal_dgd_v1/mud/usr/game/include/
phantasmal_dgd_v1/mud/usr/game/obj/
phantasmal_dgd_v1/mud/usr/game/object/
phantasmal_dgd_v1/mud/usr/game/object/stuff/
phantasmal_dgd_v1/mud/usr/game/sys/
phantasmal_dgd_v1/mud/usr/game/text/
phantasmal_dgd_v1/mud/usr/game/users/
phantasmal_dgd_v1/src/host/
phantasmal_dgd_v1/src/host/beos/
phantasmal_dgd_v1/src/host/mac/
phantasmal_dgd_v1/src/host/unix/
phantasmal_dgd_v1/src/host/win32/res/
phantasmal_dgd_v1/src/kfun/
phantasmal_dgd_v1/src/lpc/
phantasmal_dgd_v1/src/parser/
#include <phantasmal/lpc_names.h>
#include <phantasmal/log.h>
#include <kernel/user.h>

inherit USER_STATE;

private mapping text;
private int     is_clone;
private int     active;

static void create(varargs int clone) {
  ::create();
  is_clone = clone;

  if(clone) {
    /* Not the current state (yet) */
    active = 0;
  } else {
    text = ([ ]);
  }
}

static void pop_state(void) {
  user->notify_done_scrolling();
  ::pop_state();
}

string get_text(void) {
  if(previous_program() != US_SCROLL_TEXT)
    error("Only scrollstates can call get_text!");

  if(is_clone) return nil;
  return text[previous_object()];
}

void set_text(string newtext) {
  if(previous_program() != US_SCROLL_TEXT)
    error("Only scrollstates can call set_text!");

  if(is_clone) error("Can't set text of a clone!");
  text[previous_object()] = newtext;
}

void append_text(string newtext) {
  string tmp;

  if(previous_program() != US_SCROLL_TEXT)
    error("Only scrollstates can call append_text!");

  tmp = text[previous_object()];
  if(!tmp) tmp = "";
  tmp += newtext;
  text[previous_object()] = tmp;
}

static void scroll_page(void) {
  mixed* lines, *firstlines;
  string text;
  int    num_lines;

  text = call_other(US_SCROLL_TEXT, "get_text");

  if(!text) {
    LOGD->write_syslog("No text found in US_SCROLL_TEXT:scroll_page!",
		       LOG_ERROR);
    return;
  }

  lines = explode("\n" + text + "\n", "\n");
  num_lines = user->get_num_lines() - 1;  /* -1 for status line */
  if(num_lines >= sizeof(lines)) {
    send_string(text);
    call_other(US_SCROLL_TEXT, "set_text", nil);
    pop_state();
    return;
  }

  firstlines = lines[..(num_lines-1)];
  lines = lines[num_lines..];

  text = implode(lines, "\n");
  call_other(US_SCROLL_TEXT, "set_text", text);

  text = implode(firstlines, "\r\n") + "\r\n";
  send_string(text);

  /* Print prompt at bottom */
  send_string("*** (enter to scroll forward, q to quit) ***\r\n");
}


/* USER_STATE functions */

void add_text(string str) {
  call_other(US_SCROLL_TEXT, "append_text", str);
}

int from_user(string input) {
  if(!input) return MODE_ECHO;

  input = STRINGD->trim_whitespace(input);
  if(input == "") {
    scroll_page();
    return MODE_ECHO;
  }
  if(input == "q" || input == "quit") {
    call_other(US_SCROLL_TEXT, "set_text", nil);
    pop_state();
    return MODE_ECHO;
  }

  send_string("*** Don't recognize command: " + input + "\r\n");
  return MODE_ECHO;
}

void to_user(string output) {
  call_other(US_SCROLL_TEXT, "append_text", output);

  if(!active) scroll_page();
}


void switch_to(int pushp) {
  string tmp;

  active = 1;

  /* If we pop back to an empty SCROLL_TEXT state, get rid of it */

  tmp = call_other(US_SCROLL_TEXT, "get_text");
  if(!tmp || tmp == "") {
    pop_state();
    return;
  }

  scroll_page();
}

void switch_from(int popp) {
  active = 0;

  /* call_other(US_SCROLL_TEXT, "set_text", nil); */
}