clan.c

clan.c · 10.3 KB

From the archive clan.zip

Download the archive   « Back to the archive contents

#include "merc_pref.h"
#include "clan.h"

DECLARE_DO_FUN( do_petition	);
DECLARE_DO_FUN( do_loadclans	);
DECLARE_DO_FUN( do_clans	);

short clan_count = 0;
struct clan_type *clan_table = NULL;

void read_clan_table(CHAR_DATA *);

const char *lookup_clan_status(int cs) {
    switch (cs) {
	case CLAN_LEADER: return "the leader";
	case CLAN_SECOND: return "the second in command";
	case CLAN_DEPUTY: return "a deputy";
	case CLAN_SENIOR: return "a senior member";
	case CLAN_JUNIOR: return "a junior member";
    }
    return "a member";
}
int min_level_for_status(int cs) {
    switch (cs) {
	case CLAN_LEADER: return 25;
	case CLAN_SECOND: return 25;
	case CLAN_DEPUTY: return 20;
	case CLAN_SENIOR: return 15;
	case CLAN_JUNIOR: return 10;
    }
    return 10;
}

int parse_clan_status(const char *s) {
    if (s[0]) {
	if (!str_prefix(s, "junior")) return CLAN_JUNIOR;
	if (!str_prefix(s, "senior")) return CLAN_SENIOR;
	if (!str_prefix(s, "deputy")) return CLAN_DEPUTY;
	if (!str_prefix(s, "second")) return CLAN_SECOND;
	if (!str_prefix(s, "leader")) return CLAN_LEADER;
    }
    return -1;
}

/*
 *  petition [clan]
 *  petition accept [player]
 *  petition reject [player]
 *  petition remove [player]
 *  petition raise <player> <status>
 */

void do_petition_list(int clan, CHAR_DATA *ch) {
    DESCRIPTOR_DATA *d;
    bool flag = FALSE;

    for (d = descriptor_list; d; d = d->next) {
	CHAR_DATA *victim;
	victim = d->original ? d->original : d->character;
	if (d->connected == CON_PLAYING) {
	    if (victim->petition == clan) {
		if (!flag) {
		    flag = TRUE;
		    send_to_char("The following characters have petitioned your clan:\n\r", ch);
		}
		send_to_charf(ch, "[%2d %6s %s] %s%s %s\n\r",
			victim->level,
			victim->race < MAX_PC_RACE ? pc_race_table[victim->race].who_name
					: "",
			class_table[victim->class].who_name,
			victim->name,
			IS_NPC(victim) ? "" : victim->pcdata->title,
			char_color(ch) ? get_clan_table(victim->clan)->who_name_color
				: get_clan_table(victim->clan)->who_name);
	    }
	}
    }
    if (!flag)
	send_to_char("No-one has petitioned your clan.\n\r", ch);
}

void do_petition(CHAR_DATA *ch, char *argument) {
    char arg1[MAX_INPUT_LENGTH];
    char arg2[MAX_INPUT_LENGTH];
    char arg3[MAX_INPUT_LENGTH];
    CHAR_DATA *victim;
    short status;

    argument = one_argument(argument, arg1);
    argument = one_argument(argument, arg2);
    argument = one_argument(argument, arg3);

    status = ch->clan ? ch->clan_status : 0;

    if (arg1[0] == 0) {
	if (ch->petition) {
	    ch->petition = 0;
	    send_to_char("You withdraw your petition.\n\r", ch);
	    return;
	}
	if (status >= CLAN_DEPUTY) {
	    do_petition_list(ch->clan, ch);
	    return;
	}
    } else if (arg2[0] == 0) {
	int clan;
	if (status >= CLAN_SECOND) {
	    send_to_charf(ch, "%s of a clan may not petition another clan.\n\r",
			capitalize(lookup_clan_status(ch->clan_status)));
	    return;
	}
	clan = clan_lookup(arg1);
	if (!clan || get_clan_table(clan)->independent) {
	    send_to_char("There is no clan by that name.\n\r", ch);
	    return;
	}
	ch->petition = clan;
	send_to_charf(ch, "You have petitioned the clan %s for membership.\n\r", 
		capitalize(get_clan_table(clan)->name));
	return;
    } else if (status >= CLAN_DEPUTY) {
	if (arg3[0] == 0) {
	    if (!str_prefix(arg1, "accept")) {
		if ((victim = get_char_world(ch, arg2)) == NULL) {
		    send_to_char("They are not playing.\n\r", ch);
		    return;
		}
		if (victim->petition != ch->clan) {
		    send_to_char("They have not petitioned your clan.\n\r", ch);
		    return;
		}
		victim->clan = ch->clan;
		victim->clan_status = CLAN_JUNIOR;
		victim->petition = 0;
		send_to_char("You have accepted them into your clan.\n\r", ch);
		send_to_char("Your clan application was successful.\n\r", victim);
		return;
	    } else if (!str_prefix(arg1, "reject")) {
		if ((victim = get_char_world(ch, arg2)) == NULL) {
		    send_to_char("They are not playing.\n\r", ch);
		    return;
		}
		if (victim->petition != ch->clan) {
		    send_to_char("They have not petitioned your clan.\n\r", ch);
		    return;
		}
		victim->petition = 0;
		send_to_char("You have rejected there application.\n\r", ch);
		send_to_char("Your clan application has been rejected.\n\r", victim);
		return;
	    } else if (!str_prefix(arg1, "remove") && status >= CLAN_SECOND) {
		if ((victim = get_char_world(ch, arg2)) == NULL) {
		    send_to_char("They are not playing.\n\r", ch);
		    return;
		}
		if (victim->clan != ch->clan) {
		    send_to_char("They have not a member of your clan.\n\r", ch);
		    return;
		}
		if (victim->clan_status >= ch->clan_status ||
			(victim->clan_status >= CLAN_SENIOR && ch->clan_status <= CLAN_DEPUTY)
		) {
		    send_to_char("You failed.\n\r", ch);
		    return;
		}
		victim->clan = 0;
		victim->clan_status = 0;
		send_to_char("You have removed them from the clan.\n\r", ch);
		send_to_char("You have been removed from the clan.\n\r", victim);
		return;
	    }
	} else if (status >= CLAN_SECOND) {
	    if (!str_prefix(arg1, "raise")) {
		int nstatus;
		if ((victim = get_char_world(ch, arg2)) == NULL) {
		    send_to_char("They are not playing.\n\r", ch);
		    return;
		}
		if (victim->clan != ch->clan) {
		    send_to_char("They are not a member of your clan.\n\r", ch);
		    return;
		}
		nstatus = parse_clan_status(arg3);
		if (nstatus < 0) {
		    send_to_char("There is no such status in a clan.\n\r", ch);
		    return;
		}
		if (nstatus >= status || victim->clan_status >= status
			|| (victim->clan_status >= CLAN_SENIOR && ch->clan_status <= CLAN_DEPUTY)
		) {
		    send_to_char("You failed.\n\r", ch);
		    return;
		}
		victim->clan_status = nstatus;
		send_to_charf(ch, "They are now %s of the clan.\n\r",
			lookup_clan_status(nstatus));
		send_to_charf(victim, "You are now %s of the clan.\n\r",
			lookup_clan_status(nstatus));
		return;
	    }
	}
    }
    if (ch->clan && ch->clan_status >= CLAN_SECOND)
	send_to_char(	"usage: petition accept [player]\n\r"
			"       petition reject [player]\n\r"
			"       petition remove [player]\n\r"
			"       petition raise <player> <status>\n\r", ch);
    else if (ch->clan && ch->clan_status >= CLAN_DEPUTY)
	send_to_char(	"usage: petition accept [player]\n\r"
			"       petition reject [player]\n\r"
			"       petition remove [player]\n\r", ch);
    else
	send_to_char("usage: petition <clan>\n\r", ch);
}

int parse_read_number(char **s) {
    char *tail;
    int n;
    n = strtol(*s, &tail, 0);
    *s = tail;
    return n;
}
char *parse_read_string(char **s) {
    static char t[100];
    t[0] = 0;
    while (isspace(**s)) (*s)++;
    if (**s == '"') {
	short i;
	(*s)++;
	for (i=0; **s && **s != '"' && i<99; i++)
	    t[i] = *((*s)++);
	t[i] = 0;
	if (**s == '"')
	    (*s)++;
    }
    return t;
}

void parse_clan(char *s, struct clan_type *cl) {
    cl->clan_id = parse_read_number(&s);
    cl->name = strdup(parse_read_string(&s));
    cl->who_name = strdup(parse_read_string(&s));
    cl->color = parse_read_number(&s);
    if (cl->color > 0 && cl->color < 16) {
	int len;
	len = strlen(cl->who_name) + strlen(color_table[cl->color]) + strlen(COLOR_NORMAL);
	if ((cl->who_name_color = malloc(len+1)))
	    sprintf(cl->who_name_color, "%s%s" COLOR_NORMAL, color_table[cl->color], cl->who_name);
    } else
	cl->who_name_color = cl->who_name;
    cl->hall = parse_read_number(&s);
    if (!cl->hall)
	cl->hall = ROOM_VNUM_ALTAR;
    cl->recall = parse_read_number(&s);
    if (!cl->recall)
	cl->recall = ROOM_VNUM_TEMPLE;
    cl->independent = parse_read_number(&s);
}

void destroy_clan_table(void) {
    if (clan_table) {
	short i;
	for (i=2; i<clan_count; i++) {
	    if (clan_table[i].name)
		free(clan_table[i].name);
	    if (clan_table[i].who_name)
		free(clan_table[i].who_name);
	    if (clan_table[i].who_name_color &&
			clan_table[i].who_name != clan_table[i].who_name_color)
		free(clan_table[i].who_name_color);
	}
	free(clan_table);
	clan_table = NULL;
    }
    clan_count = 0;
}

void do_clans(CHAR_DATA *ch, char *argument) {
    short i;
    if (!ch) return;
    for (i=0; i<clan_count; i++) {
	send_to_charf(ch, char_color(ch) ?
		"%3d %-20s %s%-20s" COLOR_NORMAL " %5d %5d\n\r" :
		"%3d %-20s %-20s %5d %5d\n\r",
		clan_table[i].clan_id,
		clan_table[i].name,
		char_color(ch) ? color_table[clan_table[i].color] : "",
		clan_table[i].who_name,
		clan_table[i].hall,
		clan_table[i].recall);
    }
}

void do_loadclans(CHAR_DATA *ch, char *argument) {
    read_clan_table(ch);
    do_clans(ch, NULL);
}
void read_clan_table(CHAR_DATA *ch) {
    FILE *f;
    int count;
    fclose(fpReserve);
    if ((f = fopen(CLAN_FILENAME, "r"))) {
	if (clan_table)
	    destroy_clan_table();
	fscanf(f, "%d\n", &count);
	clan_count = count;
	clan_table = calloc(sizeof(struct clan_type), count);
	if (clan_table) {
	    short i;
	    char s[100];
	    for (i=0; i<count; i++) {
		if (!fgets(s, 100, f)) break;
		if (strlen(s) > 1 && s[0] != '#')
		    parse_clan(s, &clan_table[i]);
	    }
	    if (i == count) {
		fclose(f);
		fpReserve = fopen(NULL_FILE, "r");
		return;
	    }
	} else if (ch) {
	    send_to_char("FAILED! Out of memory!!!\n\r", ch);
	}
	fclose(f);
    } else if (ch) {
	send_to_char("FAILED! Couldn't open file!!!\n\r", ch);
    }
    if (!ch) {
	bug("read_clan_table", 0);
	exit(1);
    }
    fpReserve = fopen(NULL_FILE, "r");
}
void write_clan_table(void) {
    FILE *f;
    if ((f = fopen(CLAN_FILENAME, "w"))) {
	short i;
	fprintf(f, "%d\n", clan_count);
	for (i=0; i<clan_count; i++) {
	    fprintf(f, "%d\t\"%s\"\t\"%s\"\t%d\t%d\t%d\t%d\n",
			clan_table[i].clan_id,
			clan_table[i].name,
			clan_table[i].who_name,
			clan_table[i].color,
			clan_table[i].hall,
			clan_table[i].recall,
			clan_table[i].independent);
	}
	fclose(f);
    }
}

struct clan_type *get_clan_table(int id) {
    static struct clan_type null_clan = {
	0, "", "", "", ROOM_VNUM_ALTAR, ROOM_VNUM_TEMPLE, TRUE
    };
    short i;
    for (i=0; i<clan_count; i++)
	if (clan_table[i].clan_id == id)
	    return &clan_table[i];
    return &null_clan;
}

int clan_lookup(const char *name) {
    short i;
    for (i=0; i<clan_count; i++)
	if (UPPER(name[0]) == UPPER(clan_table[i].name[0])
	    && !str_prefix(name, clan_table[i].name))
		return clan_table[i].clan_id;
    return 0;
}

int get_clan_index(int id) {
    short i;
    for (i=0; i<clan_count; i++)
	if (id == clan_table[i].clan_id)
	    return i;
    return 0;
}

bool is_independent(CHAR_DATA *ch) {
    return get_clan_table(ch->clan)->independent;
}

Site Stats

Snippets: 1791
Codebases: 571