home *** CD-ROM | disk | FTP | other *** search
/ Informática Multimedia: Special Games (Alt) / INFESPGAMES.iso / os2 / backgam / source / world.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-18  |  7.2 KB  |  277 lines

  1. /*************************************************************
  2.  *    ______                                                 *
  3.  *   /     /\  TinyFugue was derived from a client initially *
  4.  *  /   __/  \ written by Anton Rang (Tarrant) and later     *
  5.  *  |  / /\  | modified by Leo Plotkin (Grod).  The early    *
  6.  *  |  |/    | versions of TinyFugue written by Greg Hudson  *
  7.  *  |  X__/  | (Explorer_Bob).  The current version is       *
  8.  *  \ /      / written and maintained by Ken Keys (Hawkeye), *
  9.  *   \______/  who can be reached at kkeys@ucsd.edu.         *
  10.  *                                                           *
  11.  *             No copyright 1992, no rights reserved.        *
  12.  *             Fugue is in the public domain.                *
  13.  *************************************************************/
  14.  
  15. /********************************************************
  16.  * Fugue world routines.                                *
  17.  ********************************************************/
  18.  
  19. #include <stdio.h>
  20. #include <ctype.h>
  21. #include "tf.h"
  22. #include "dstring.h"
  23. #include "util.h"
  24. #include "history.h"
  25. #include "world.h"
  26. #include "output.h"
  27. #include "macro.h"
  28. #include "process.h"
  29.  
  30. static void FDECL(free_fields,(World *w));
  31. static void FDECL(replace_world,(World *old, World *new));
  32. static World *FDECL(insertworld,(World *world));
  33.  
  34. void   FDECL(addworld,(char *args));
  35. World *FDECL(new_world,(char *name, char *character, char *pass,
  36.              char *address, char *port, char *mfile));
  37. void   FDECL(remove_world,(char *args));
  38. void   FDECL(purge_world,(char *args));
  39. void   FDECL(write_worlds,(char *args));
  40. void   FDECL(list_worlds,(int full, char *pattern, TFILE *fp));
  41. void   FDECL(free_world,(World *w));
  42. void   FDECL(nuke_world,(World *w));
  43. World *NDECL(get_default_world);
  44. World *NDECL(get_world_header);
  45. World *FDECL(find_world,(char *name));
  46. void   FDECL(flush_world,(World *w));
  47.  
  48. static World *hworld = NULL, *defaultworld = NULL;
  49.  
  50. static void free_fields(w)
  51.     World *w;
  52. {
  53.     FREE(w->name);
  54.     FREE(w->character);
  55.     FREE(w->pass);
  56.     FREE(w->address);
  57.     FREE(w->port);
  58.     FREE(w->mfile);
  59. }
  60.  
  61. void free_world(w)
  62.     World *w;
  63. {
  64.     free_fields(w);
  65.     free_history(w->history);
  66.     free_queue(w->queue);
  67.     FREE(w);
  68. }
  69.  
  70. static void replace_world(old, new)
  71.     World *old, *new;
  72. {
  73.     free_fields(old);
  74.     old->name      = new->name;
  75.     old->character = new->character;
  76.     old->pass      = new->pass;
  77.     old->address   = new->address;
  78.     old->port      = new->port;
  79.     old->mfile     = new->mfile;
  80.     FREE(new);
  81.     do_hook(H_REDEF, "%% Redefined %s %s", "%s %s", "world", old->name);
  82. }
  83.  
  84. World *new_world(name, character, pass, address, port, mfile)
  85.     char *name, *character, *pass, *address, *port, *mfile;
  86. {
  87.     World *result;
  88.  
  89.     result = (World *) MALLOC(sizeof(World));
  90.     init_queue(result->queue);
  91.     result->socket = NULL;
  92.     result->history->alines = NULL;
  93.     result->name      = STRDUP(name);
  94.     result->character = STRDUP(character);
  95.     result->pass      = STRDUP(pass);
  96.     result->address   = STRDUP(address);
  97.     result->port      = STRDUP(port);
  98.     result->mfile     = STRDUP(mfile);
  99.     result->flags = 0;
  100.     return insertworld(result);
  101. }
  102.  
  103. void addworld(args)
  104.     char *args;
  105. {
  106.     int count, i;
  107.     static Stringp fields[6];
  108.     static int fields_inited = FALSE;
  109.     World *new;
  110.  
  111.     if (!fields_inited) {
  112.         for (i = 0; i < 6; i++) Stringinit(fields[i]);
  113.         fields_inited = TRUE;
  114.     }
  115.     for (count = 0; count < 6; count++) {
  116.         if (!*args) break;
  117.         args = getword(fields[count], args);
  118.     }
  119.     if (count < 3 || count > 6) {
  120.         oputs("% Illegal world format");
  121.         return;
  122.     } else if (count >= 5) {
  123.         new = new_world(fields[0]->s, fields[1]->s, fields[2]->s, fields[3]->s,
  124.             fields[4]->s, count == 6 ? fields[5]->s : "");
  125.     } else if (cstrcmp(fields[0]->s, "default") == 0) {
  126.         new = new_world(fields[0]->s, fields[1]->s, fields[2]->s, "", "",
  127.             count == 4 ? fields[3]->s : "");
  128.     } else {
  129.         new = new_world(fields[0]->s, "", "", fields[1]->s, fields[2]->s,
  130.             count == 4 ? fields[3]->s : "");
  131.     }
  132.     new->flags &= ~WORLD_TEMP;
  133. }
  134.  
  135. static World *insertworld(world)
  136.     World *world;
  137. {
  138.     World *w;
  139.  
  140.     if (cstrcmp(world->name, "default") == 0) {
  141.         if (defaultworld) replace_world(defaultworld, world);
  142.         else defaultworld = world;
  143.     } else if ((w = find_world(world->name)) != NULL) {
  144.         replace_world(w, world);
  145.         return w;
  146.     } else if (hworld == NULL) {
  147.         hworld = world;
  148.         world->next = NULL;
  149.     } else {
  150.         for (w = hworld; w->next != NULL; w = w->next);
  151.         w->next = world;
  152.         world->next = NULL;
  153.     }
  154.     return world;
  155. }
  156.  
  157. void nuke_world(w)
  158.     World *w;
  159. {
  160.     World *t;
  161.  
  162.     if (w->socket) {
  163.         oprintf("%% %s: Cannot nuke world currently in use.", w->name);
  164.     } else {
  165.         if (w == hworld) hworld = w->next;
  166.         else {
  167.             for (t = hworld; t->next != w; t = t->next);
  168.             t->next = w->next;
  169.         }
  170.         kill_procs_by_world(w);
  171.         free_world(w);
  172.     }
  173. }
  174.  
  175. void remove_world(args)
  176.     char *args;
  177. {
  178.     World *w;
  179.  
  180.     w = find_world(args);
  181.     if (w == NULL) oprintf("%% No world %s", args);
  182.     else nuke_world(w);
  183. }
  184.  
  185. void purge_world(args)
  186.     char *args;
  187. {
  188.     World *world, *next;
  189.  
  190.     if (!smatch_check(args)) return;
  191.     for (world = hworld; world; world = next) {
  192.         next = world->next;
  193.         if (equalstr(args, world->name)) nuke_world(world);
  194.     }
  195. }
  196.  
  197. void list_worlds(full, pattern, fp)
  198.     int full;
  199.     char *pattern;
  200.     TFILE *fp;
  201. {
  202.     World *p;
  203.     int first = 1;
  204.     STATIC_BUFFER(buf)
  205.  
  206.     for (p = defaultworld; p || first; p = first ? hworld : p->next, first=0) {
  207.         if (!p || pattern && !equalstr(pattern, p->name)) continue;
  208.         Sprintf(buf, fp ? "/addworld %s " : "%% %s ", p->name);
  209.         if (*p->character) {
  210.             Stringcat(buf, p->character);
  211.             Stringadd(buf, ' ');
  212.             if (full) {
  213.                 Stringcat(buf, p->pass);
  214.                 Stringadd(buf, ' ');
  215.             }
  216.         }
  217.         if (*p->address) {
  218.             Sprintf(buf, "\200%s %s ", p->address, p->port);
  219.         }
  220.         if (*p->mfile) {
  221.             Stringcat(buf, p->mfile);
  222.         }
  223.         if (fp) {
  224.             Stringadd(buf, '\n');
  225.             tfputs(buf->s, fp);
  226.         } else oputs(buf->s);
  227.     }
  228. }
  229.  
  230. void write_worlds(args)
  231.     char *args;
  232. {
  233.     TFILE *file;
  234.  
  235.     if ((file = tfopen(args, "WORLDFILE", "w")) == NULL) return;
  236.     list_worlds(TRUE, NULL, file);
  237.     tfclose(file);
  238. }
  239.  
  240. World *get_default_world()
  241. {
  242.     return defaultworld;
  243. }
  244.  
  245. World *get_world_header()
  246. {
  247.     return hworld;
  248. }
  249.  
  250. World *find_world(name)
  251.     char *name;
  252. {
  253.     World *p;
  254.  
  255.     for (p=hworld; p && (!p->name || cstrcmp(name, p->name) != 0); p = p->next);
  256.     return p;
  257. }
  258.  
  259. void flush_world(w)
  260.     World *w;
  261. {
  262.     Textnode *node;
  263.     extern Queue screen_queue[1];
  264.  
  265.     while (node = w->queue->head) {
  266.         /* moving the node saves free/malloc of dequeue/enqueue */
  267.         if (screen_queue->head)
  268.             screen_queue->tail->next = node;
  269.         else screen_queue->head = node;
  270.         if (!(w->queue->head = w->queue->head->next)) w->queue->tail = NULL;
  271.         (screen_queue->tail = node)->next = NULL;
  272.     }
  273.     w->history->index = w->history->pos;
  274.     oflush();
  275. }
  276.  
  277.