home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / t / toaster.zip / Compat / wiz_list.c < prev    next >
C/C++ Source or Header  |  1991-12-31  |  7KB  |  317 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "lint.h"
  4. #include "wiz_list.h"
  5. #include "interpret.h"
  6. #include "config.h"
  7.  
  8. #define DOMAINS
  9. /*
  10.  * Maintain the wizards high score list about most popular castle.
  11.  */
  12.  
  13. extern char *string_copy PROT((char *)), *xalloc PROT((int));
  14.  
  15. struct wiz_list *all_wiz;
  16. extern int eval_cost;
  17.  
  18. /*
  19.  * Sort the wiz list in ascending order.
  20.  */
  21.  
  22. static struct wiz_list *insert(w, wl)
  23.     struct wiz_list *w, *wl;
  24. {
  25.     if (wl == 0) {
  26.     w->next = 0;
  27.     return w;
  28.     }
  29.     if (w->score > wl->score) {
  30.     wl->next = insert(w, wl->next);
  31.     return wl;
  32.     }
  33.     w->next = wl;
  34.     return w;
  35. }
  36.  
  37. static void rebuild_list() {
  38.     struct wiz_list *wl, *w, *new_list = 0;
  39.  
  40.     for (w = all_wiz; w; w = wl) {
  41.     wl = w->next;
  42.     new_list = insert(w, new_list);
  43.     }
  44.     all_wiz = new_list;
  45. }
  46.  
  47. /*
  48.  * Find the data, if it exists.
  49.  */
  50. static struct wiz_list *find_wiz(name)
  51.     char *name;
  52. {
  53.     int length;
  54.     struct wiz_list *wl;
  55.  
  56.     length = strlen(name);
  57.     for (wl = all_wiz; wl; wl = wl->next)
  58.         if (wl->length == length && strcmp(wl->name, name) == 0)
  59.         return wl;
  60.     return 0;
  61. }
  62.  
  63. /*
  64.  * Check that a name exists. Add it, if it doesn't.
  65.  */
  66. struct wiz_list *add_name(str)
  67.     char *str;
  68. {
  69.     struct wiz_list *wl;
  70.  
  71.     wl = find_wiz(str);
  72.     if (wl)
  73.         return wl;
  74.     wl = (struct wiz_list *)xalloc(sizeof (struct wiz_list));
  75.     str = string_copy(str);
  76.     wl->name = str;
  77.     wl->length = strlen(str);
  78.     wl->score = 0;
  79.     wl->cost = 0;
  80.     wl->heart_beats = 0;
  81.     wl->total_worth = 0;
  82.     wl->next = all_wiz;
  83.     wl->size_array = 0;
  84.     wl->file_name = 0;
  85.     wl->error_message = 0;
  86.     all_wiz = wl;
  87.     return wl;
  88. }
  89.  
  90. /*
  91.  * Add score to an existing name.
  92.  */
  93. void add_score(name, score)
  94.     char *name;
  95.     int score;
  96. {
  97.     struct wiz_list *wl;
  98.  
  99.     wl = find_wiz(name);
  100.     if (!wl)
  101.         fatal("Add_score: could not find wizard %s\n", name);
  102.     wl->score += score;
  103. }
  104.  
  105. /*
  106.  * This one is called at every complete walkaround of reset.
  107.  */
  108. void wiz_decay() {
  109.     struct wiz_list *wl;
  110.     static int next_time;
  111.     extern int current_time;
  112.  
  113.     /* Perform this once every hour. */
  114.     if (next_time > current_time)
  115.     return;
  116.     next_time = current_time + 60 * 60;
  117.     for (wl = all_wiz; wl; wl = wl->next) {
  118.         wl->score = wl->score * 99 / 100;
  119.     wl->total_worth = wl->total_worth * 99 / 100;
  120.     wl->cost = wl->cost * 9 / 10;
  121.     wl->heart_beats = wl->heart_beats * 9 / 10;
  122.     }
  123. }
  124.  
  125. /*
  126.  * Load the wizlist file.
  127.  */
  128. void load_wiz_file()
  129. {
  130.     char buff[1000];        /* I hate not knowing how much I need. */
  131.     FILE *f;
  132.  
  133.     f = fopen("WIZLIST", "r");
  134.     if (f == NULL)
  135.         return;
  136.     while(fgets(buff, sizeof buff, f) != NULL) {
  137.         char *p;
  138.     int score;
  139.  
  140.     p = strchr(buff, ' ');
  141.     if (p == 0) {
  142.         fprintf(stderr, "Bad WIZLIST file.\n");
  143.         break;
  144.     }
  145.     *p = '\0';
  146.     p++;
  147.     if (*p == '\0') {
  148.         fprintf(stderr, "Bad WIZLIST file.\n");
  149.         break;
  150.     }
  151.     score = atoi(p);
  152.     if (score > 0) {
  153.         (void)add_name(buff);
  154.         add_score(buff, atoi(p));
  155.     }
  156.     }
  157.     fclose(f);
  158. }
  159.  
  160. /*
  161.  * Save the wizlist file.
  162.  */
  163. void save_wiz_file()
  164. {
  165.     struct wiz_list *wl;
  166.     FILE *f;
  167.  
  168.     f = fopen("WIZLIST", "w");
  169.     if (f == NULL) {
  170.         fprintf(stderr, "Could not open WIZLIST for write\n");
  171.         return;
  172.     }
  173.     for (wl = all_wiz; wl; wl = wl->next)
  174.         fprintf(f, "%s %d %d\n", wl->name, wl->score, wl->total_worth);
  175.     fclose(f);
  176. }
  177.  
  178. void wizlist(v)
  179.     char *v;
  180. {
  181.     struct wiz_list *wl, *this_wiz;
  182.     int total = 0, num = 0, this_pos, total_wizards;
  183.     extern struct object *command_giver;
  184.     int all = 0;
  185.     struct svalue *name;
  186.  
  187.     if (!command_giver)
  188.     return;
  189.     if (v == 0) {
  190.     name = apply("query_real_name", command_giver, 0);
  191.     if (!name || name->type != T_STRING)
  192.         return;
  193.     v = name->u.string;
  194.     }
  195.     if (strcmp(v, "ALL") == 0)
  196.     all = 1;
  197.     this_wiz = find_wiz(v);
  198.     rebuild_list();
  199.     for (num = 0, wl = all_wiz; wl; wl = wl->next) {
  200.         total += wl->score;
  201.     num++;
  202.     if (wl == this_wiz)
  203.         this_pos = num;
  204.     }
  205.     total_wizards = num;
  206.     add_message("\nWizard top score list\n\n");
  207.     this_pos = num - this_pos + 1;
  208.     if (total == 0)
  209.     total = 1;
  210.     for (wl = all_wiz; wl; wl = wl->next) {
  211.     if (!all && num > 15 && (num < this_pos - 2 || num > this_pos + 2))
  212.         ;
  213.     else
  214.         add_message("%-15s %5d %2d%% (%d)\t[%4dk,%5d] %6d %d\n", wl->name,
  215.             wl->score, wl->score * 100 / total, num,
  216.             wl->cost / 1000,
  217.             wl->heart_beats, wl->total_worth, wl->size_array);
  218.     num--;
  219.     }
  220.     add_message("\nTotal         %7d     (%d)\n\n", total, total_wizards);
  221. }
  222.  
  223. void remove_wiz_list() {
  224.     struct wiz_list *wl, *w;
  225.  
  226.     for (w = all_wiz; w; w = wl) {
  227.     free(w->name);
  228.     wl = w->next;
  229.     free((char *)w);
  230.     }
  231. }
  232.  
  233. void save_error(msg, file, line)
  234.     char *msg;
  235.     char *file;
  236.     int line;
  237. {
  238.     struct wiz_list *wl;
  239.     char name[100];
  240.     char *p;
  241.     int len, save_eval_cost = eval_cost;
  242.  
  243.     eval_cost = 0;
  244.     p = get_wiz_name(file);
  245.     eval_cost = save_eval_cost;
  246.     if(!p)
  247.     return;
  248.     strcpy(name, p);
  249.     wl = add_name(name);
  250.     if (wl->file_name)
  251.     free(wl->file_name);
  252.     len = strlen(file);
  253.     wl->file_name = xalloc(len + 4); /* May add .c plus the null byte, and / */
  254. #ifdef COMPAT_MODE
  255.     strcpy(wl->file_name, file);
  256. #else
  257.     strcpy(wl->file_name, "/");
  258.     strcat(wl->file_name, file);
  259.     len++;
  260. #endif
  261.     /*
  262.      * If it is a cloned object, we have to find out what the file
  263.      * name is, and add '.c'.
  264.      */
  265.     p = strrchr(wl->file_name, '#');
  266.     if (p) {
  267.     p[0] = '.';
  268.     p[1] = 'c';
  269.     p[2] = '\0';
  270.     len = p - wl->file_name + 2;
  271.     }
  272.     if (wl->file_name[len-1] != 'c' || wl->file_name[len-2] != '.')
  273.     strcat(wl->file_name, ".c");
  274.     if (wl->error_message)
  275.     free(wl->error_message);
  276.     wl->error_message = string_copy(msg);
  277.     wl->line_number = line;
  278. }
  279.  
  280. char *get_error_file(name)
  281.     char *name;
  282. {
  283.     struct wiz_list *wl;
  284.  
  285.     wl = add_name(name);
  286.     /*
  287.      * The error_message is used as a flag if there has been any error.
  288.      */
  289.     if (wl->error_message == 0) {
  290.     add_message("No error.\n");
  291.     return 0;
  292.     }
  293.     add_message("%s line %d: %s\n", wl->file_name, wl->line_number,
  294.         wl->error_message);
  295.     free(wl->error_message);
  296.     wl->error_message = 0;
  297.     return wl->file_name;
  298. }
  299.  
  300. /*
  301.  * Argument is a file name, which we want to get the owner of.
  302.  * Ask the master.c object !
  303.  */
  304. char *get_wiz_name(file)
  305.     char *file;
  306. {
  307.     struct svalue *ret;
  308.     static char buff[50];
  309.  
  310.     push_string(file, STRING_CONSTANT);
  311.     ret = apply_master_ob("get_wiz_name", 1);
  312.     if (ret == 0 || ret->type != T_STRING)
  313.     return 0;
  314.     strcpy(buff, ret->u.string);
  315.     return buff;
  316. }
  317.