social-edit.c
src/social-edit.c · 11.7 KB
From the archive cyhawk_mud.tar.gz
Download the archive « Back to the archive contents
/***************************************************************************
* Original Diku Mud copyright (C) 1990, 1991 by Sebastian Hammer, *
* Michael Seifert, Hans Henrik St{rfeldt, Tom Madsen, and Katja Nyboe. *
* *
* Merc Diku Mud improvments copyright (C) 1992, 1993 by Michael *
* Chastain, Michael Quan, and Mitchell Tse. *
* *
* In order to use any part of this Merc Diku Mud, you must comply with *
* both the original Diku license in 'license.doc' as well the Merc *
* license in 'license.txt'. In particular, you may not remove either of *
* these copyright notices. *
* *
* Much time and thought has gone into this software and you are *
* benefitting. We hope that you share your changes too. What goes *
* around, comes around. *
***************************************************************************/
/* Online Socialx Editting Module,
* (c) 1996 Erwin S. Andreasen <erwin@pip.dknet.dk>
* See the file "License" for important licensing information
*/
#if defined(macintosh)
#include <types.h>
#else
#include <sys/types.h>
#endif
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "merc.h"
#define SOCIAL_FILE "../data/socials.txt" /* or whatever fits you */
int maxSocialx; /* max number of socialxs */
#ifndef CONST_SOCIAL
struct socialx_type *socialx_table; /* and socialx table */
void load_socialx (FILE *fp, struct socialx_type *socialx)
{
socialx->name = fread_string (fp);
socialx->char_no_arg = fread_string (fp);
socialx->others_no_arg = fread_string (fp);
socialx->char_found = fread_string (fp);
socialx->others_found = fread_string (fp);
socialx->vict_found = fread_string (fp);
socialx->char_auto = fread_string (fp);
socialx->others_auto = fread_string (fp);
}
void load_socialx_table ()
{
FILE *fp;
int i;
fp = fopen (SOCIAL_FILE, "r");
if (!fp)
{
bug ("Could not open " SOCIAL_FILE " for reading.",0);
exit(1);
}
fscanf (fp, "%d\n", &maxSocialx);
/* IMPORTANT to use malloc so we can realloc later on */
socialx_table = malloc (sizeof(struct socialx_type) * (maxSocialx+1));
for (i = 0; i < maxSocialx; i++)
load_socialx (fp,&socialx_table[i]);
/* For backwards compatibility */
socialx_table[maxSocialx].name = str_dup(""); /* empty! */
fclose (fp);
}
#endif /* CONST_SOCIAL */
void save_socialx (const struct socialx_type *s, FILE *fp)
{
/* get rid of (null) */
fprintf (fp, "%s~\n%s~\n%s~\n%s~\n%s~\n%s~\n%s~\n%s~\n\n",
s->name ? s->name : "" ,
s->char_no_arg ? s->char_no_arg : "" ,
s->others_no_arg ? s->others_no_arg : "" ,
s->char_found ? s->char_found : "" ,
s->others_found ? s->others_found : "" ,
s->vict_found ? s->vict_found : "" ,
s->char_auto ? s->char_auto : "" ,
s->others_auto ? s->others_auto : ""
);
}
void save_socialx_table()
{
FILE *fp;
int i;
fp = fopen (SOCIAL_FILE, "w");
if (!fp)
{
bug ("Could not open " SOCIAL_FILE " for writing.",0);
return;
}
#ifdef CONST_SOCIAL /* If old table still in use, count socialxs first */
for (maxSocialx = 0 ; socialx_table[maxSocialx].name[0] ; maxSocialx++)
; /* empty */
#endif
fprintf (fp, "%d\n", maxSocialx);
for ( i = 0 ; i < maxSocialx ; i++)
save_socialx (&socialx_table[i], fp);
fclose (fp);
}
/* Find a socialx based on name */
int socialx_lookup (const char *name)
{
int i;
for (i = 0; i < maxSocialx ; i++)
if (!str_cmp(name, socialx_table[i].name))
return i;
return -1;
}
/*
* Socialx editting command
*/
#ifndef CONST_SOCIAL
void do_sedit (CHAR_DATA *ch, char *argument)
{
char cmd[MAX_INPUT_LENGTH], socialx[MAX_INPUT_LENGTH];
char buf[MAX_STRING_LENGTH];
int iSocialx;
smash_tilde (argument);
argument = one_argument (argument,cmd);
argument = one_argument (argument,socialx);
if (!cmd[0])
{
send_to_char ("Huh? Type HELP SEDIT to see syntax.\n\r",ch);
return;
}
if (!socialx[0])
{
send_to_char ("What socialx do you want to operate on?\n\r",ch);
return;
}
iSocialx = socialx_lookup (socialx);
if (str_cmp(cmd,"new") && (iSocialx == -1))
{
send_to_char ("No such socialx exists.\n\r",ch);
return;
}
if (!str_cmp(cmd, "delete")) /* Remove a socialx */
{
int i,j;
struct socialx_type *new_table = malloc (sizeof(struct socialx_type) * maxSocialx);
if (!new_table)
{
send_to_char ("Memory allocation failed. Brace for impact...\n\r",ch);
return;
}
/* Copy all elements of old table into new table, except the deleted socialx */
for (i = 0, j = 0; i < maxSocialx+1; i++)
if (i != iSocialx) /* copy, increase only if copied */
{
new_table[j] = socialx_table[i];
j++;
}
free (socialx_table);
socialx_table = new_table;
maxSocialx--; /* Important :() */
send_to_char ("That socialx is history now.\n\r",ch);
}
else if (!str_cmp(cmd, "new")) /* Create a new socialx */
{
struct socialx_type *new_table;
if (iSocialx != -1)
{
send_to_char ("A socialx with that name already exists\n\r",ch);
return;
}
/* reallocate the table */
/* Note that the table contains maxSocialx socialxs PLUS one empty spot! */
maxSocialx++;
new_table = realloc (socialx_table, sizeof(struct socialx_type) * maxSocialx + 1);
if (!new_table) /* realloc failed */
{
send_to_char ("Memory allocation failed. Brace for impact.\n\r",ch);
return;
}
socialx_table = new_table;
socialx_table[maxSocialx-1].name = str_dup (socialx);
socialx_table[maxSocialx-1].char_no_arg = str_dup ("");
socialx_table[maxSocialx-1].others_no_arg = str_dup ("");
socialx_table[maxSocialx-1].char_found = str_dup ("");
socialx_table[maxSocialx-1].others_found = str_dup ("");
socialx_table[maxSocialx-1].vict_found = str_dup ("");
socialx_table[maxSocialx-1].char_auto = str_dup ("");
socialx_table[maxSocialx-1].others_auto = str_dup ("");
socialx_table[maxSocialx].name = str_dup (""); /* 'terminating' empty string */
send_to_char ("New socialx added.\n\r",ch);
}
else if (!str_cmp(cmd, "show")) /* Show a certain socialx */
{
sprintf (buf, "Socialx: %s\n\r"
"(cnoarg) No argument given, character sees:\n\r"
"%s\n\r\n\r"
"(onoarg) No argument given, others see:\n\r"
"%s\n\r\n\r"
"(cfound) Target found, character sees:\n\r"
"%s\n\r\n\r"
"(ofound) Target found, others see:\n\r"
"%s\n\r\n\r"
"(vfound) Target found, victim sees:\n\r"
"%s\n\r\n\r"
"(cself) Target is character himself:\n\r"
"%s\n\r\n\r"
"(oself) Target is character himself, others see:\n\r"
"%s\n\r",
socialx_table[iSocialx].name,
socialx_table[iSocialx].char_no_arg,
socialx_table[iSocialx].others_no_arg,
socialx_table[iSocialx].char_found,
socialx_table[iSocialx].others_found,
socialx_table[iSocialx].vict_found,
socialx_table[iSocialx].char_auto,
socialx_table[iSocialx].others_auto);
send_to_char (buf,ch);
return; /* return right away, do not save the table */
}
else if (!str_cmp(cmd, "cnoarg")) /* Set that argument */
{
free_string (socialx_table[iSocialx].char_no_arg);
socialx_table[iSocialx].char_no_arg = str_dup(argument);
if (!argument[0])
send_to_char ("Character will now see nothing when this socialx is used without arguments.\n\r",ch);
else
printf_to_char (ch,"New message is now:\n\r%s\n\r", argument);
}
else if (!str_cmp(cmd, "onoarg"))
{
free_string (socialx_table[iSocialx].others_no_arg);
socialx_table[iSocialx].others_no_arg = str_dup(argument);
if (!argument[0])
send_to_char ("Others will now see nothing when this socialx is used without arguments.\n\r",ch);
else
printf_to_char (ch,"New message is now:\n\r%s\n\r", argument);
}
else if (!str_cmp(cmd, "cfound"))
{
free_string (socialx_table[iSocialx].char_found);
socialx_table[iSocialx].char_found = str_dup(argument);
if (!argument[0])
send_to_char ("The character will now see nothing when a target is found.\n\r",ch);
else
printf_to_char (ch,"New message is now:\n\r%s\n\r", argument);
}
else if (!str_cmp(cmd, "ofound"))
{
free_string (socialx_table[iSocialx].others_found);
socialx_table[iSocialx].others_found = str_dup(argument);
if (!argument[0])
send_to_char ("Others will now see nothing when a target is found.\n\r",ch);
else
printf_to_char (ch,"New message is now:\n\r%s\n\r", argument);
}
else if (!str_cmp(cmd, "vfound"))
{
free_string (socialx_table[iSocialx].vict_found);
socialx_table[iSocialx].vict_found = str_dup(argument);
if (!argument[0])
send_to_char ("Victim will now see nothing when a target is found.\n\r",ch);
else
printf_to_char (ch,"New message is now:\n\r%s\n\r", argument);
}
else if (!str_cmp(cmd, "cself"))
{
free_string (socialx_table[iSocialx].char_auto);
socialx_table[iSocialx].char_auto = str_dup(argument);
if (!argument[0])
send_to_char ("Character will now see nothing when targetting self.\n\r",ch);
else
printf_to_char (ch,"New message is now:\n\r%s\n\r", argument);
}
else if (!str_cmp(cmd, "oself"))
{
free_string (socialx_table[iSocialx].others_auto);
socialx_table[iSocialx].others_auto = str_dup(argument);
if (!argument[0])
send_to_char ("Others will now see nothing when character targets self.\n\r",ch);
else
printf_to_char (ch,"New message is now:\n\r%s\n\r", argument);
}
else
{
send_to_char ("Huh? Try HELP SEDIT.\n\r",ch);
return;
}
/* We have done something. update socialx table */
save_socialx_table();
}
#endif /* CONST_SOCIAL */