From 930fe8c82782f31ac1b4e45d1358063463fabc80 Mon Sep 17 00:00:00 2001 From: Rumble Date: Thu, 28 Dec 2006 02:08:22 +0000 Subject: [PATCH] DG script variables now save to the player file instead of their own file. The load function has been kept for backwards compatibility. --- src/dg_scripts.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++ src/interpreter.c | 6 ++++- src/players.c | 8 ++++-- 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/src/dg_scripts.c b/src/dg_scripts.c index 9d9ef13..ece7a4c 100644 --- a/src/dg_scripts.c +++ b/src/dg_scripts.c @@ -2960,6 +2960,73 @@ void save_char_vars(struct char_data *ch) fclose(file); } +/* load in a character's saved variables from an ASCII pfile*/ +void read_saved_vars_ascii(FILE *file, struct char_data *ch, int count) +{ + long context; + char input_line[1024], *temp, *p; + char varname[READ_SIZE]; + char context_str[READ_SIZE]; + int i; + + /* + * If getting to the menu from inside the game, the vars aren't removed. + * So let's not allocate them again. + */ + if (SCRIPT(ch)) + return; + + /* create the space for the script structure which holds the vars */ + /* We need to do this first, because later calls to 'remote' will need */ + /* a script already assigned. */ + CREATE(SCRIPT(ch), struct script_data, 1); + + /* walk through each line in the file parsing variables */ + for (i = 0; i < count; i++) + { + if (get_line(file, input_line)>0) { + p = temp = strdup(input_line); + temp = any_one_arg(temp, varname); + temp = any_one_arg(temp, context_str); + skip_spaces(&temp); /* temp now points to the rest of the line */ + + context = atol(context_str); + add_var(&(SCRIPT(ch)->global_vars), varname, temp, context); + free(p); /* plug memory hole */ + } + } +} + +/* save a characters variables out to an ASCII pfile */ +void save_char_vars_ascii(FILE *file, struct char_data *ch) +{ + struct trig_var_data *vars; + int count = 0; + /* immediate return if no script (and therefore no variables) structure */ + /* has been created. this will happen when the player is logging in */ + if (SCRIPT(ch) == NULL) return; + + /* we should never be called for an NPC, but just in case... */ + if (IS_NPC(ch)) return; + + /* make sure this char has global variables to save */ + if (ch->script->global_vars == NULL) return; + + /* note that currently, context will always be zero. this may change */ + /* in the future */ + for (vars = ch->script->global_vars;vars;vars = vars->next) + if (*vars->name != '-') + count++; + + if (count != 0) { + fprintf(file, "Vars: %d\n", count); + + for (vars = ch->script->global_vars;vars;vars = vars->next) + if (*vars->name != '-') /* don't save if it begins with - */ + fprintf(file, "%s %ld %s\n", vars->name, vars->context, vars->value); + } +} + /* find_char() helpers */ // Must be power of 2 diff --git a/src/interpreter.c b/src/interpreter.c index dd20419..7b8fa78 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -1354,7 +1354,11 @@ int enter_player_game (struct descriptor_data *d) /* find_char helper */ add_to_lookup_table(GET_ID(d->character), (void *)d->character); - read_saved_vars(d->character); + // after moving saving of variables to the player file, this should only be called + // in case nothing was found in the pfile. If something was found, SCRIPT(ch) will + // be set + if (!SCRIPT(d->character)) + read_saved_vars(d->character); d->character->next = character_list; character_list = d->character; diff --git a/src/players.c b/src/players.c index 32670f3..1e4ab5b 100644 --- a/src/players.c +++ b/src/players.c @@ -402,6 +402,9 @@ int load_char(const char *name, struct char_data *ch) else if (!strcmp(tag, "Titl")) GET_TITLE(ch) = strdup(line); break; + case 'V': + if (!strcmp(tag, "Vars")) read_saved_vars_ascii(fl, ch, atoi(line)); + case 'W': if (!strcmp(tag, "Wate")) GET_WEIGHT(ch) = atoi(line); else if (!strcmp(tag, "Wimp")) GET_WIMP_LEV(ch) = atoi(line); @@ -527,7 +530,7 @@ void save_char(struct char_data * ch) tmp_aff[i].next = 0; } } - save_char_vars(ch); + // save_char_vars(ch); /* * remove the affections so that the raw values are stored; otherwise the @@ -643,7 +646,8 @@ void save_char(struct char_data * ch) fprintf(fl, "0 0 0 0 0\n"); } - if (GET_ALIASES(ch)) write_aliases_ascii(fl, ch); + write_aliases_ascii(fl, ch); + save_char_vars_ascii(fl, ch); fclose(fl);