2 // nazghul - an old-school RPG engine
3 // Copyright (C) 2002, 2003 Gordon McNutt
5 // This program is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 2 of the License, or (at your option)
10 // This program is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 // You should have received a copy of the GNU General Public License along with
16 // this program; if not, write to the Free Foundation, Inc., 59 Temple Place,
17 // Suite 330, Boston, MA 02111-1307 USA
20 // gmcnutt@users.sourceforge.net
25 #include "session.h" /* for save */
30 #define dtable_is_valid_faction(dtable,fac) \
31 ((fac) >= 0 && (fac) < (dtable)->n_factions)
33 #define dtable_clamp_level(dtable,lvl) \
34 clamp((lvl), (dtable)->lower_bound, (dtable)->upper_bound)
36 struct dtable *dtable_new(int n_factions)
38 struct dtable *dtable;
40 /* allocate the "main" struct */
41 dtable = (struct dtable*)calloc(1, sizeof(*dtable));
44 dtable->n_factions = n_factions;
46 /* allocate the table of stack pointers */
47 dtable->table = (int*)calloc(n_factions * n_factions,
48 sizeof(dtable->table[0]));
49 assert(dtable->table);
51 /* For now, hardcode the table limits and settings. Note that we must
52 * set the upper and lower bounds before poking any entries into the
54 dtable_set_hostile(dtable, DTABLE_DEFAULT_HOSTILE);
55 dtable_set_allies(dtable, DTABLE_DEFAULT_ALLIES);
56 dtable_set_upper_bound(dtable, DTABLE_DEFAULT_UPPER_BOUND);
57 dtable_set_lower_bound(dtable, DTABLE_DEFAULT_LOWER_BOUND);
62 static int dtable_check(struct dtable *dtable, int faction)
64 if (! dtable_is_valid_faction(dtable, faction)) {
65 warn("dtable_check: invalid faction=%d\n", faction);
72 static int dtable_index(struct dtable *dtable, int f1, int f2)
74 if (dtable_check(dtable, f1))
77 if (dtable_check(dtable, f2))
80 return f1 * dtable->n_factions + f2;
84 void dtable_set(struct dtable *dtable, int f1, int f2, int level)
88 dtable_clamp_level(dtable, level);
90 if ((index = dtable_index(dtable, f1, f2)) < 0)
93 dtable->table[index] = level;
95 /* mirror the change on the other half of the table */
96 index = dtable_index(dtable, f2, f1);
97 dtable->table[index] = level;
100 int dtable_get(struct dtable *dtable, int f1, int f2)
104 if ((index = dtable_index(dtable, f1, f2)) < 0) {
105 warn("dtable_get: defaulting to neutral\n");
109 return dtable->table[index];
112 void dtable_del(struct dtable *dtable)
123 void dtable_save(struct dtable *dtable, struct save *save)
129 save->enter(save, "(kern-mk-dtable\n");
132 for (rows = 0; rows < dtable->n_factions; rows++) {
133 save->write(save, "(list ");
134 for (cols = 0; cols < dtable->n_factions; cols++) {
135 save->write(save, "%2d ", dtable->table[index]);
138 save->append(save, ")\n");
141 save->exit(save, ")\n");
144 void dtable_inc(struct dtable *dtable, int f1, int f2)
148 level = dtable_get(dtable, f1, f2);
149 dtable_set(dtable, f1, f2, level+1);
152 void dtable_dec(struct dtable *dtable, int f1, int f2)
156 level = dtable_get(dtable, f1, f2);
157 dtable_set(dtable, f1, f2, level-1);
161 extern const char *dtable_describe(struct dtable *dtable, int f1, int f2)
163 int level = dtable_get(dtable, f1, f2);
165 if (level <= dtable->hostile)
167 else if (level >= dtable->allies)