OSDN Git Service

aa9d21faca22304ad6ae959ff1b7abdd070ff2c1
[nazghul-jp/nazghul-jp.git] / src / dtable.c
1 //
2 // nazghul - an old-school RPG engine
3 // Copyright (C) 2002, 2003 Gordon McNutt
4 //
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)
8 // any later version.
9 //
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
13 // more details.
14 //
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
18 //
19 // Gordon McNutt
20 // gmcnutt@users.sourceforge.net
21 //
22
23 #include "dtable.h"
24 #include "debug.h"
25 #include "session.h" /* for save */
26
27 #include <assert.h>
28 #include <stdlib.h>
29
30 #define dtable_is_valid_faction(dtable,fac) \
31         ((fac) >= 0 && (fac) < (dtable)->n_factions)
32
33 #define dtable_clamp_level(dtable,lvl) \
34         clamp((lvl), (dtable)->lower_bound, (dtable)->upper_bound)
35
36 struct dtable *dtable_new(int n_factions)
37 {
38         struct dtable *dtable;
39
40         /* allocate the "main" struct */
41         dtable = (struct dtable*)calloc(1, sizeof(*dtable));
42         assert(dtable);
43         
44         dtable->n_factions = n_factions;
45
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);
50
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
53          * table. */
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);
58         
59         return dtable;        
60 }
61
62 static int dtable_check(struct dtable *dtable, int faction)
63 {
64         if (! dtable_is_valid_faction(dtable, faction)) {
65                 warn("dtable_check: invalid faction=%d\n", faction);
66                 return -1;
67         }
68
69         return 0;
70 }
71
72 static int dtable_index(struct dtable *dtable, int f1, int f2)
73 {
74         if (dtable_check(dtable, f1))
75                 return -1;
76
77         if (dtable_check(dtable, f2))
78                 return -1;
79
80         return f1 * dtable->n_factions + f2;
81         
82 }
83
84 void dtable_set(struct dtable *dtable, int f1, int f2, int level)
85 {
86         int index;
87
88         dtable_clamp_level(dtable, level);
89
90         if ((index = dtable_index(dtable, f1, f2)) < 0)
91                 return;
92
93         dtable->table[index] = level;
94         
95         /* mirror the change on the other half of the table */
96         index =  dtable_index(dtable, f2, f1);
97         dtable->table[index] = level;
98 }
99
100 int dtable_get(struct dtable *dtable, int f1, int f2)
101 {
102         int index;
103
104         if ((index = dtable_index(dtable, f1, f2)) < 0) {
105                 warn("dtable_get: defaulting to neutral\n");
106                 return 0;
107         }
108
109         return dtable->table[index];
110 }
111
112 void dtable_del(struct dtable *dtable)
113 {
114         assert(dtable);
115
116         if (dtable->table) {
117                 free(dtable->table);
118         }
119         free(dtable);
120 }
121
122
123 void dtable_save(struct dtable *dtable, struct save *save)
124 {
125         int rows;
126         int cols;
127         int index;
128
129         save->enter(save, "(kern-mk-dtable\n");
130
131         index = 0;
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]);
136                         index++;
137                 }
138                 save->append(save, ")\n");
139         }
140         
141         save->exit(save, ")\n");
142 }
143
144 void dtable_inc(struct dtable *dtable, int f1, int f2)
145 {
146         int level;
147
148         level = dtable_get(dtable, f1, f2);
149         dtable_set(dtable, f1, f2, level+1);
150 }
151
152 void dtable_dec(struct dtable *dtable, int f1, int f2)
153 {
154         int level;
155
156         level = dtable_get(dtable, f1, f2);
157         dtable_set(dtable, f1, f2, level-1);
158
159 }
160
161 extern const char *dtable_describe(struct dtable *dtable, int f1, int f2)
162 {
163         int level = dtable_get(dtable, f1, f2);
164         
165         if (level <= dtable->hostile)
166                 return "hostile";
167         else if (level >= dtable->allies)
168                 return "allied";
169         else
170                 return "neutral";
171 }