]> git.pond.sub.org Git - empserver/blobdiff - src/lib/player/player.c
Update copyright notice
[empserver] / src / lib / player / player.c
index 1b56b142d35061f0a411b3e254a456306eb89d26..6108b264243f1dd9a34ae0fe216329b17da5eb2c 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
  *
  *  Known contributors to this file:
  *     Steve McClure, 2000
- *     Markus Armbruster, 2004-2008
- *     Ron Koenderink, 2004-2007
+ *     Markus Armbruster, 2004-2014
+ *     Ron Koenderink, 2004-2009
  */
 
 #include <config.h>
 
-#include "com.h"
+#include "chance.h"
 #include "empio.h"
 #include "empthread.h"
 #include "file.h"
-#include "journal.h"
 #include "misc.h"
 #include "nat.h"
 #include "optlist.h"
 #include "player.h"
-#include "proto.h"
 #include "prototypes.h"
-#include "tel.h"
 
 
 static int command(void);
@@ -60,11 +56,14 @@ player_main(struct player *p)
     struct natstr *natp;
     char buf[128];
 
-    p->state = PS_PLAYING;
     player = p;
     time(&player->curup);
     update_timeused_login(player->curup);
     show_motd();
+    if (running_test_suite)
+       pr("\n"
+          "***          Server configured for testing          ***\n"
+          "*** If you see this in a game, it is misconfigured! ***\n");
     if (init_nats() < 0) {
        pr("Server confused, try again later\n");
        return;
@@ -81,15 +80,12 @@ player_main(struct player *p)
           natp->nat_last_login <= natp->nat_last_logout
           ? ctime(&natp->nat_last_logout) : "?");
        pr("                  by: %s@%s\n",
-          natp->nat_userid,
-          *natp->nat_hostname ? natp->nat_hostname : natp->nat_hostaddr);
+          natp->nat_userid, natp->nat_hostaddr);
     }
     strcpy(natp->nat_userid, player->userid);
-    strcpy(natp->nat_hostname, player->hostname);
     strcpy(natp->nat_hostaddr, player->hostaddr);
     natp->nat_last_login = player->curup;
     putnat(natp);
-    journal_login();
     if (natp->nat_flags & NF_INFORM && natp->nat_tgms > 0) {
        if (natp->nat_tgms == 1)
            pr("You have a new telegram waiting ...\n");
@@ -100,7 +96,9 @@ player_main(struct player *p)
     }
 
     while (status() && command()) {
-       player->aborted = player->eof;
+       if (player->got_ctld)
+           io_set_eof(player->iop);
+       player->aborted = 0;
        empth_yield();
     }
     /* #*# I put the following line in to prevent server crash -KHS */
@@ -110,26 +108,26 @@ player_main(struct player *p)
     update_timeused(natp->nat_last_logout);
     enforce_minimum_session_time();
     pr("Bye-bye\n");
-    journal_logout();
 }
 
 static int
 command(void)
 {
+    struct natstr *natp = getnatp(player->cnum);
     char *redir;               /* UTF-8 */
-    char scanspace[1024];
     time_t now;
 
+    prprompt(natp->nat_timeused / 60, natp->nat_btu);
     if (getcommand(player->combuf) < 0)
-       return 0;
+       return player->aborted;
 
     now = time(NULL);
     update_timeused(now);
-    if (!player->god && !may_play_now(getnatp(player->cnum), now))
+    if (!player->god && !may_play_now(natp, now))
        return 0;
 
-    if (parse(player->combuf, scanspace, player->argp, player->comtail,
-             &player->condarg, &redir) < 0) {
+    if (parse(player->combuf, player->argbuf, player->argp,
+             player->comtail, &player->condarg, &redir) < 0) {
        pr("See \"info Syntax\"?\n");
     } else {
        if (dispatch(player->combuf, redir) < 0)
@@ -145,8 +143,6 @@ status(void)
     int old_nstat;
     char buf[128];
 
-    if (player->eof || player->state == PS_SHUTDOWN)
-       return 0;
     natp = getnatp(player->cnum);
     if (player->dolcost > 100.0)
        pr("That just cost you $%.2f\n", player->dolcost);
@@ -173,8 +169,10 @@ status(void)
 
     time(&player->curup);
     update_timeused(player->curup);
-    if (!may_play_now(natp, player->curup))
+    if (io_error(player->iop) || io_eof(player->iop)
+       || !may_play_now(natp, player->curup))
        return 0;
+
     if (player->btused) {
        natp->nat_btu -= player->btused;
        player->btused = 0;
@@ -203,6 +201,24 @@ status(void)
     return 1;
 }
 
+/*
+ * Make all objects stale if @arg is one of the player's command arguments.
+ * See ef_make_stale() for what "making stale" means.
+ * Useful for functions that prompt for missing arguments.
+ * These can yield the processor, so we'd like to call ef_make_stale()
+ * there.  Except that leads to false positives when the caller passes
+ * an argument that is never null, and relies on the fact that the
+ * function doesn't yield then.  We can't know that in general.  But
+ * we do know in the common special case of command arguments.
+ */
+void
+make_stale_if_command_arg(char *arg)
+{
+    if (player->argbuf <= arg
+       && arg <= player->argbuf + sizeof(player->argbuf))
+       ef_make_stale();
+}
+
 /*
  * XXX This whole mess should be redone; execute block should
  * start with "exec start", and should end with "exec end".
@@ -211,44 +227,46 @@ status(void)
 int
 execute(void)
 {
-    char buf[1024];
+    char buf[1024];            /* UTF-8 */
     int failed;
-    char *p;
+    char *p;                   /* UTF-8 */
     char *redir;               /* UTF-8 */
-    char scanspace[1024];
 
     failed = 0;
 
-    if (player->comtail[1])
-       p = player->comtail[1];
-    else
-       p = getstring("File? ", buf);
+    p = player->comtail[1];
+    if (!p)
+       p = ugetstring("File? ", buf);
     if (p == NULL || *p == '\0')
        return RET_SYN;
     prexec(p);
 
-    while (!failed && status()) {
+    while (!failed && status() && !player->got_ctld) {
        player->nstat &= ~EXEC;
-       if (recvclient(buf, sizeof(buf)) < 0)
+       if (getcommand(player->combuf) < 0)
            break;
-       if (parse(buf, scanspace, player->argp, player->comtail,
-                 &player->condarg, &redir) < 0) {
+       if (parse(player->combuf, player->argbuf, player->argp,
+                 player->comtail, &player->condarg, &redir) < 0) {
            failed = 1;
            continue;
        }
-       pr("\nExecute : %s\n", buf);
+       pr("\nExecute : ");
+       uprnf(player->combuf);
+       pr("\n");
        if (redir) {
            pr("Execute : redirection not supported\n");
            failed = 1;
-       } else if (dispatch(buf, NULL) < 0)
+       } else if (dispatch(player->combuf, NULL) < 0)
            failed = 1;
+       /* player->aborted not reset; makes next getcommand() fail */
+       empth_yield();
     }
     if (failed) {
        while (recvclient(buf, sizeof(buf)) >= 0) ;
     }
 
     pr("Execute : %s\n", failed ? "aborted" : "terminated");
-    player->eof = 0;
+    player->got_ctld = 0;
     return RET_OK;
 }
 
@@ -262,13 +280,12 @@ show_motd(void)
 int
 quit(void)
 {
-    player->state = PS_SHUTDOWN;
+    io_set_eof(player->iop);
     return RET_OK;
 }
 
 char *
 praddr(struct player *p)
 {
-    return prbuf("%s@%s", p->userid,
-                *p->hostname ? p->hostname : p->hostaddr);
+    return prbuf("%s@%s", p->userid, p->hostaddr);
 }