#include <curses.h>
#include <err.h>
#include <math.h>
#include <pwd.h>
#include <stdlib.h>
#include <string.h>
#ifdef TERMIOS
#include <termios.h>
#endif
#include <time.h>
#include <unistd.h>
#include "macros.h"
#include "pathnames.h"
#include "phantdefs.h"
#include "phantglobs.h"
int
main(int argc, char **argv)
{
bool noheader = FALSE;
bool headeronly = FALSE;
bool examine = FALSE;
time_t seconds;
double dtemp;
initialstate();
while (--argc && (*++argv)[0] == '-')
switch ((*argv)[1]) {
case 's':
noheader = TRUE;
break;
case 'H':
headeronly = TRUE;
break;
case 'a':
activelist();
cleanup(TRUE);
case 'p':
purgeoldplayers();
cleanup(TRUE);
case 'S':
Wizard = !getuid();
break;
case 'x':
examine = TRUE;
break;
case 'm':
monstlist();
cleanup(TRUE);
case 'b':
scorelist();
cleanup(TRUE);
}
if (!isatty(0))
cleanup(TRUE);
playinit();
if (examine) {
changestats(FALSE);
cleanup(TRUE);
}
if (!noheader) {
titlelist();
purgeoldplayers();
}
if (headeronly)
cleanup(TRUE);
do
{
Fileloc = -1L;
mvaddstr(22, 17, "Do you have a character to run [Q = Quit] ? ");
switch (getanswer("NYQ", FALSE)) {
case 'Y':
Fileloc = recallplayer();
break;
case 'Q':
cleanup(TRUE);
default:
Fileloc = rollnewplayer();
break;
}
clear();
}
while (Fileloc < 0L);
if (Player.p_level > 5.0)
Timeout = TRUE;
strlcpy(Player.p_login, Login, LOGIN_NAME_MAX);
time(&seconds);
Player.p_lastused = localtime(&seconds)->tm_yday;
Player.p_status = S_PLAYING;
writerecord(&Player, Fileloc);
Statptr = &Stattable[Player.p_type];
altercoordinates(Player.p_x, Player.p_y, A_FORCED);
clear();
for (;;)
{
adjuststats();
if (Throne && Player.p_crowns == 0 && Player.p_specialtype != SC_KING)
{
mvaddstr(5, 0, "You're not allowed in the Lord's Chamber without a crown.\n");
altercoordinates(0.0, 0.0, A_NEAR);
}
checktampered();
if (Player.p_status != S_CLOAKED
&& (dtemp = fabs(Player.p_x)) == fabs(Player.p_y)
&& !Throne)
{
dtemp = sqrt(dtemp / 100.0);
if (floor(dtemp) == dtemp)
{
tradingpost();
clear();
}
}
checkbattle();
neatstuff();
if (Player.p_status == S_CLOAKED) {
if (Player.p_mana > 3.0)
Player.p_mana -= 3.0;
else
{
Player.p_status = S_PLAYING;
Changed = TRUE;
}
}
if (Player.p_status != S_PLAYING && Player.p_status != S_CLOAKED)
{
Player.p_status = S_PLAYING;
Changed = TRUE;
}
if (Changed)
{
writerecord(&Player, Fileloc);
Changed = FALSE;
continue;
}
readmessage();
displaystats();
move(6, 0);
if (Throne)
throneroom();
addstr("1:Move 2:Players 3:Talk 4:Stats 5:Quit ");
if (Player.p_level >= MEL_CLOAK && Player.p_magiclvl >= ML_CLOAK)
addstr("6:Cloak ");
if (Player.p_level >= MEL_TELEPORT && Player.p_magiclvl >= ML_TELEPORT)
addstr("7:Teleport ");
if (Player.p_specialtype >= SC_COUNCIL || Wizard)
addstr("8:Intervene ");
procmain();
}
}
void
initialstate(void)
{
#ifdef TERMIOS
struct termios tty;
#endif
Beyond = FALSE;
Marsh = FALSE;
Throne = FALSE;
Changed = FALSE;
Wizard = FALSE;
Timeout = FALSE;
Users = 0;
Windows = FALSE;
Echo = TRUE;
if ((Login = getlogin()) == NULL) {
struct passwd *gpwd;
gpwd = getpwuid(getuid());
if (gpwd != NULL)
Login = gpwd->pw_name;
else
errx(1, "Who are you?");
}
#ifdef TERMIOS
if (tcgetattr(0, &tty) == 0) {
Ch_Erase = tty.c_cc[VERASE];
Ch_Kill = tty.c_cc[VKILL];
} else {
Ch_Erase = CH_ERASE;
Ch_Kill = CH_KILL;
}
#else
Ch_Erase = CH_ERASE;
Ch_Kill = CH_KILL;
#endif
if ((Playersfp = fopen(_PATH_PEOPLE, "r+")) == NULL)
error(_PATH_PEOPLE);
if ((Monstfp = fopen(_PATH_MONST, "r+")) == NULL)
error(_PATH_MONST);
if ((Messagefp = fopen(_PATH_MESS, "r")) == NULL)
error(_PATH_MESS);
if ((Energyvoidfp = fopen(_PATH_VOID, "r+")) == NULL)
error(_PATH_VOID);
}
long
rollnewplayer(void)
{
int chartype;
int ch;
initplayer(&Player);
clear();
mvaddstr(4, 21, "Which type of character do you want:");
mvaddstr(8, 4,
"1:Magic User 2:Fighter 3:Elf 4:Dwarf 5:Halfling 6:Experimento ");
if (Wizard) {
addstr("7:Super ? ");
chartype = getanswer("1234567", FALSE);
} else {
addstr("? ");
chartype = getanswer("123456", FALSE);
}
do {
genchar(chartype);
mvprintw(12, 14,
"Strength : %2.0f Quickness: %2.0f Mana : %2.0f\n",
Player.p_strength, Player.p_quickness, Player.p_mana);
mvprintw(13, 14,
"Energy Level: %2.0f Brains : %2.0f Magic Level: %2.0f\n",
Player.p_energy, Player.p_brains, Player.p_magiclvl);
if (Player.p_type == C_EXPER || Player.p_type == C_SUPER)
break;
mvaddstr(14, 14, "Type '1' to keep >");
ch = getanswer(" ", TRUE);
}
while (ch != '1');
if (Player.p_type == C_EXPER || Player.p_type == C_SUPER)
for (;;) {
mvaddstr(16, 0, "Enter the X Y coordinates of your experimento ? ");
getstring(Databuf, SZ_DATABUF);
sscanf(Databuf, "%lf %lf", &Player.p_x, &Player.p_y);
if (fabs(Player.p_x) > D_EXPER || fabs(Player.p_y) > D_EXPER)
mvaddstr(17, 0, "Invalid coordinates. Try again.\n");
else
break;
}
for (;;)
{
mvprintw(18, 0,
"Give your character a name [up to %d characters] ? ", SZ_NAME - 1);
getstring(Player.p_name, SZ_NAME);
truncstring(Player.p_name);
if (Player.p_name[0] == '\0')
mvaddstr(19, 0, "Invalid name.");
else
if (findname(Player.p_name, &Other) >= 0L)
mvaddstr(19, 0, "Name already in use.");
else
break;
addstr(" Pick another.\n");
}
Echo = FALSE;
do {
mvaddstr(20, 0, "Give your character a password [up to 8 characters] ? ");
getstring(Player.p_password, SZ_PASSWORD);
mvaddstr(21, 0, "One more time to verify ? ");
getstring(Databuf, SZ_PASSWORD);
}
while (strcmp(Player.p_password, Databuf) != 0);
Echo = TRUE;
return (allocrecord());
}
void
procmain(void)
{
int ch;
double x;
double y;
double temp;
FILE *fp;
int loop;
bool hasmoved = FALSE;
ch = inputoption();
mvaddstr(4, 0, "\n\n");
move(7, 0);
clrtobot();
if (Player.p_specialtype == SC_VALAR && (ch == '1' || ch == '7'))
ch = ' ';
switch (ch) {
case 'K':
case 'N':
x = Player.p_x;
y = Player.p_y + MAXMOVE();
hasmoved = TRUE;
break;
case 'J':
case 'S':
x = Player.p_x;
y = Player.p_y - MAXMOVE();
hasmoved = TRUE;
break;
case 'L':
case 'E':
x = Player.p_x + MAXMOVE();
y = Player.p_y;
hasmoved = TRUE;
break;
case 'H':
case 'W':
x = Player.p_x - MAXMOVE();
y = Player.p_y;
hasmoved = TRUE;
break;
default:
Player.p_energy += (Player.p_maxenergy + Player.p_shield) / 15.0
+ Player.p_level / 3.0 + 2.0;
Player.p_energy =
MIN(Player.p_energy, Player.p_maxenergy + Player.p_shield);
if (Player.p_status != S_CLOAKED)
{
Player.p_mana += (Circle + Player.p_level) / 4.0;
if (drandom() < 0.2 && Player.p_status == S_PLAYING && !Throne)
encounter(-1);
}
break;
case 'X':
changestats(TRUE);
break;
case '1':
for (loop = 3; loop; --loop) {
mvaddstr(4, 0, "X Y Coordinates ? ");
getstring(Databuf, SZ_DATABUF);
if (sscanf(Databuf, "%lf %lf", &x, &y) != 2)
mvaddstr(5, 0, "Try again\n");
else
if (distance(Player.p_x, x, Player.p_y, y) > MAXMOVE())
ILLMOVE();
else {
hasmoved = TRUE;
break;
}
}
break;
case '2':
userlist(TRUE);
break;
case '3':
mvaddstr(4, 0, "Message ? ");
getstring(Databuf, SZ_DATABUF);
fp = fopen(_PATH_MESS, "w");
if (Databuf[0] != '\0')
fprintf(fp, "%s: %s", Player.p_name, Databuf);
fclose(fp);
break;
case '4':
allstatslist();
break;
case '5':
leavegame();
case '6':
if (Player.p_level < MEL_CLOAK || Player.p_magiclvl < ML_CLOAK)
ILLCMD();
else
if (Player.p_status == S_CLOAKED)
Player.p_status = S_PLAYING;
else
if (Player.p_mana < MM_CLOAK)
mvaddstr(5, 0, "No mana left.\n");
else {
Changed = TRUE;
Player.p_mana -= MM_CLOAK;
Player.p_status = S_CLOAKED;
}
break;
case '7':
if (Player.p_level < MEL_TELEPORT || Player.p_magiclvl < ML_TELEPORT)
ILLCMD();
else
for (loop = 3; loop; --loop) {
mvaddstr(4, 0, "X Y Coordinates ? ");
getstring(Databuf, SZ_DATABUF);
if (sscanf(Databuf, "%lf %lf", &x, &y) == 2) {
temp = distance(Player.p_x, x, Player.p_y, y);
if (!Throne
&& Player.p_specialtype <= SC_COUNCIL
&& temp > (Player.p_level + Player.p_magiclvl) * 20.0)
ILLMOVE();
else {
temp = (temp / 75.0 + 1.0) * 20.0;
if (!Throne && temp > Player.p_mana)
mvaddstr(5, 0, "Not enough power for that distance.\n");
else {
if (!Throne)
Player.p_mana -= temp;
hasmoved = TRUE;
break;
}
}
}
}
break;
case 'C':
case '9':
if (Throne)
mvaddstr(5, 0, "No monsters in the chamber!\n");
else
if (Player.p_specialtype != SC_VALAR)
{
Player.p_sin += 1e-6;
encounter(-1);
}
break;
case '0':
if (Wizard || (Player.p_specialtype == SC_KING && Throne))
dotampered();
else
ILLCMD();
break;
case '8':
if (Wizard || Player.p_specialtype >= SC_COUNCIL)
dotampered();
else
ILLCMD();
break;
}
if (hasmoved)
{
altercoordinates(x, y, A_SPECIFIC);
if (drandom() < 0.2 && Player.p_status == S_PLAYING && !Throne)
encounter(-1);
}
}
void
titlelist(void)
{
FILE *fp;
bool councilfound = FALSE;
bool kingfound = FALSE;
double hiexp, nxtexp;
double hilvl, nxtlvl;
char hiname[21], nxtname[21];
nxtexp = 0;
mvaddstr(0, 14,
"W e l c o m e t o P h a n t a s i a (vers. 3.3.2)!");
if ((fp = fopen(_PATH_MOTD, "r")) != NULL
&& fgets(Databuf, SZ_DATABUF, fp) != NULL) {
mvaddstr(2, 40 - strlen(Databuf) / 2, Databuf);
fclose(fp);
}
fseek(Playersfp, 0L, SEEK_SET);
while (fread(&Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
if (Other.p_specialtype == SC_KING &&
Other.p_status != S_NOTUSED)
{
snprintf(Databuf, sizeof Databuf,
"The present ruler is %s Level:%.0f",
Other.p_name, Other.p_level);
mvaddstr(4, 40 - strlen(Databuf) / 2, Databuf);
kingfound = TRUE;
break;
}
if (!kingfound)
mvaddstr(4, 24, "There is no ruler at this time.");
fseek(Playersfp, 0L, SEEK_SET);
while (fread(&Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
if (Other.p_specialtype == SC_VALAR && Other.p_status != S_NOTUSED)
{
snprintf(Databuf, sizeof Databuf,
"The Valar is %s Login: %s", Other.p_name,
Other.p_login);
mvaddstr(6, 40 - strlen(Databuf) / 2, Databuf);
break;
}
fseek(Playersfp, 0L, SEEK_SET);
Lines = 10;
while (fread(&Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
if (Other.p_specialtype == SC_COUNCIL && Other.p_status != S_NOTUSED)
{
if (!councilfound) {
mvaddstr(8, 30, "Council of the Wise:");
councilfound = TRUE;
}
snprintf(Databuf, sizeof Databuf,
"%s Login: %s", Other.p_name, Other.p_login);
mvaddstr(Lines++, 40 - strlen(Databuf) / 2, Databuf);
}
nxtname[0] = hiname[0] = '\0';
hiexp = 0.0;
nxtlvl = hilvl = 0;
fseek(Playersfp, 0L, SEEK_SET);
while (fread(&Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
if (Other.p_experience > hiexp && Other.p_specialtype <= SC_KING && Other.p_status != S_NOTUSED)
{
nxtexp = hiexp;
hiexp = Other.p_experience;
nxtlvl = hilvl;
hilvl = Other.p_level;
strlcpy(nxtname, hiname, sizeof nxtname);
strlcpy(hiname, Other.p_name, sizeof hiname);
} else
if (Other.p_experience > nxtexp
&& Other.p_specialtype <= SC_KING
&& Other.p_status != S_NOTUSED)
{
nxtexp = Other.p_experience;
nxtlvl = Other.p_level;
strlcpy(nxtname, Other.p_name, sizeof nxtname);
}
mvaddstr(15, 28, "Highest characters are:");
snprintf(Databuf, sizeof Databuf,
"%s Level:%.0f and %s Level:%.0f",
hiname, hilvl, nxtname, nxtlvl);
mvaddstr(17, 40 - strlen(Databuf) / 2, Databuf);
if ((fp = fopen(_PATH_LASTDEAD, "r")) != NULL
&& fgets(Databuf, SZ_DATABUF, fp) != NULL) {
mvaddstr(19, 25, "The last character to die was:");
mvaddstr(20, 40 - strlen(Databuf) / 2, Databuf);
fclose(fp);
}
refresh();
}
long
recallplayer(void)
{
long loc = 0L;
int loop;
int ch;
clear();
mvprintw(10, 0, "What was your character's name ? ");
getstring(Databuf, SZ_NAME);
truncstring(Databuf);
if ((loc = findname(Databuf, &Player)) >= 0L)
{
Echo = FALSE;
for (loop = 0; loop < 2; ++loop) {
mvaddstr(11, 0, "Password ? ");
getstring(Databuf, SZ_PASSWORD);
if (strcmp(Databuf, Player.p_password) == 0)
{
Echo = TRUE;
if (Player.p_status != S_OFF)
{
clear();
addstr("Your character did not exit normally last time.\n");
addstr("If you think you have good cause to have your character saved,\n");
printw("you may quit and mail your reason to 'root'.\n");
addstr("Otherwise, continuing spells certain death.\n");
addstr("Do you want to quit ? ");
ch = getanswer("YN", FALSE);
if (ch == 'Y') {
Player.p_status = S_HUNGUP;
writerecord(&Player, loc);
cleanup(TRUE);
}
death("Stupidity");
}
return (loc);
} else
mvaddstr(12, 0, "No good.\n");
}
Echo = TRUE;
} else
mvaddstr(11, 0, "Not found.\n");
more(13);
return (-1L);
}
void
neatstuff(void)
{
double temp;
int ch;
switch ((int) ROLL(0.0, 100.0)) {
case 1:
case 2:
if (Player.p_poison > 0.0) {
mvaddstr(4, 0, "You've found a medic! How much will you offer to be cured ? ");
temp = floor(infloat());
if (temp < 0.0 || temp > Player.p_gold)
{
mvaddstr(6, 0, "He was not amused, and made you worse.\n");
Player.p_poison += 1.0;
} else if (drandom() / 2.0 > (temp + 1.0) / MAX(Player.p_gold, 1))
mvaddstr(5, 0, "Sorry, he wasn't interested.\n");
else {
mvaddstr(5, 0, "He accepted.");
Player.p_poison = MAX(0.0, Player.p_poison - 1.0);
Player.p_gold -= temp;
}
}
break;
case 3:
mvaddstr(4, 0, "You've been caught raping and pillaging!\n");
Player.p_experience += 4000.0;
Player.p_sin += 0.5;
break;
case 4:
temp = ROLL(10.0, 75.0);
mvprintw(4, 0, "You've found %.0f gold pieces, want them ? ", temp);
ch = getanswer("NY", FALSE);
if (ch == 'Y')
collecttaxes(temp, 0.0);
break;
case 5:
if (Player.p_sin > 1.0) {
mvaddstr(4, 0, "You've found a Holy Orb!\n");
Player.p_sin -= 0.25;
}
break;
case 6:
if (Player.p_poison < 1.0) {
mvaddstr(4, 0, "You've been hit with a plague!\n");
Player.p_poison += 1.0;
}
break;
case 7:
mvaddstr(4, 0, "You've found some holy water.\n");
++Player.p_holywater;
break;
case 8:
mvaddstr(4, 0, "You've met a Guru. . .");
if (drandom() * Player.p_sin > 1.0)
addstr("You disgusted him with your sins!\n");
else if (Player.p_poison > 0.0) {
addstr("He looked kindly upon you, and cured you.\n");
Player.p_poison = 0.0;
} else {
addstr("He rewarded you for your virtue.\n");
Player.p_mana += 50.0;
Player.p_shield += 2.0;
}
break;
case 9:
mvaddstr(4, 0, "You've found an amulet.\n");
++Player.p_amulets;
break;
case 10:
if (Player.p_blindness) {
mvaddstr(4, 0, "You've regained your sight!\n");
Player.p_blindness = FALSE;
}
break;
default:
if (Player.p_poison > 0.0) {
temp = Player.p_poison * Statptr->c_weakness
* Player.p_maxenergy / 600.0;
if (Player.p_energy > Player.p_maxenergy / 10.0
&& temp + 5.0 < Player.p_energy)
Player.p_energy -= temp;
}
break;
}
}
void
genchar(int type)
{
int subscript;
struct charstats *statptr;
subscript = type - '1';
if (subscript < C_MAGIC || subscript > C_EXPER)
if (subscript != C_SUPER || !Wizard)
subscript = C_FIGHTER;
statptr = &Stattable[subscript];
Player.p_quickness =
ROLL(statptr->c_quickness.base, statptr->c_quickness.interval);
Player.p_strength =
ROLL(statptr->c_strength.base, statptr->c_strength.interval);
Player.p_mana =
ROLL(statptr->c_mana.base, statptr->c_mana.interval);
Player.p_maxenergy =
Player.p_energy =
ROLL(statptr->c_energy.base, statptr->c_energy.interval);
Player.p_brains =
ROLL(statptr->c_brains.base, statptr->c_brains.interval);
Player.p_magiclvl =
ROLL(statptr->c_magiclvl.base, statptr->c_magiclvl.interval);
Player.p_type = subscript;
if (Player.p_type == C_HALFLING)
Player.p_experience = ROLL(600.0, 200.0);
}
void
playinit(void)
{
initscr();
noecho();
cbreak();
clear();
refresh();
Windows = TRUE;
}
void
cleanup(int doexit)
{
if (Windows) {
move(LINES - 2, 0);
refresh();
nocbreak();
endwin();
}
if (Playersfp) {
fclose(Playersfp);
Playersfp = NULL;
}
if (Monstfp) {
fclose(Monstfp);
Monstfp = NULL;
}
if (Messagefp) {
fclose(Messagefp);
Messagefp = NULL;
}
if (Energyvoidfp) {
fclose(Energyvoidfp);
Energyvoidfp = NULL;
}
if (doexit)
exit(0);
}