1 /* File: obj_kind.c */
2
3 /* Purpose: Code for the object templates */
4
5 /*
6 * Copyright (c) 1989 James E. Wilson, Robert A. Koeneke
7 *
8 * This software may be copied and distributed for educational, research, and
9 * not for profit purposes provided that this copyright and statement are
10 * included in all such copies.
11 */
12
13 #include "angband.h"
14
15
16 /* Base size of k_info */
17 #define K_INFO_BASE_SIZE 500
18
19 /* Amount of entries to add when resizing k_info */
20 #define K_INFO_RESIZE 50
21
22 /* Size of the allocated */
23 static s32b k_info_size = K_INFO_BASE_SIZE;
24
25
26 /* Allocate k_info */
k_info_alloc(void)27 errr k_info_alloc(void)
28 {
29 /* Create the storage for the object templates */
30 C_MAKE(k_info, k_info_size, object_kind);
31
32 /* Success */
33 return 0;
34 }
35
36
37 /* Free k_info */
k_info_free(void)38 errr k_info_free(void)
39 {
40 k_info_size = K_INFO_BASE_SIZE;
41
42 KILL(k_info);
43
44 /* Success */
45 return 0;
46 }
47
48
k_info_reset(void)49 void k_info_reset(void)
50 {
51 int i;
52
53 /* Reset the "objects" */
54 for (i = 1; i < z_info->k_max; i++)
55 {
56 object_kind *k_ptr = &k_info[i];
57
58 /* Reset "tried" */
59 k_ptr->tried = FALSE;
60
61 /* Reset "aware" */
62 k_ptr->aware = FALSE;
63 }
64 }
65
66
67 /* Add a new object template */
k_info_add(object_kind * k_info_entry)68 object_kind *k_info_add(object_kind *k_info_entry)
69 {
70 /* Resize if necessary */
71 while (k_info_size <= z_info->k_max)
72 {
73 k_info_size += K_INFO_RESIZE;
74
75 /* Reallocate the extra memory */
76 k_info =
77 (object_kind *)realloc(k_info, k_info_size * sizeof(object_kind));
78
79 /* Failure */
80 if (!k_info) quit("Out of memory!");
81
82 /* Wipe the new memory */
83 (void)C_WIPE(&k_info[(k_info_size - K_INFO_RESIZE)], K_INFO_RESIZE,
84 object_kind);
85 }
86
87 /* Increase the maximum index of the array */
88 z_info->k_max++;
89
90 /* Copy the new object_kind */
91 COPY(&k_info[z_info->k_max - 1], k_info_entry, object_kind);
92
93 /* Success */
94 return (&k_info[z_info->k_max - 1]);
95 }
96
97
98 /*
99 * Initialize some other arrays
100 */
init_object_alloc(void)101 errr init_object_alloc(void)
102 {
103 int i, j, p, x, y, z;
104 object_kind *k_ptr;
105 ego_item_type *e_ptr;
106 alloc_entry *table;
107 s16b num[MAX_DEPTH];
108 s16b aux[MAX_DEPTH];
109
110
111 /*** Analyze object allocation info ***/
112
113 /* Clear the "aux" array */
114 (void)C_WIPE(aux, MAX_DEPTH, s16b);
115
116 /* Clear the "num" array */
117 (void)C_WIPE(num, MAX_DEPTH, s16b);
118
119 /* Free the old "alloc_kind_table" (if it exists) */
120 if (alloc_kind_table)
121 {
122 KILL(alloc_kind_table);
123 }
124
125 /* Size of "alloc_kind_table" */
126 alloc_kind_size = 0;
127
128 /* Scan the objects */
129 for (i = 1; i < z_info->k_max; i++)
130 {
131 k_ptr = &k_info[i];
132
133 /* Scan allocation pairs */
134 for (j = 0; j < 4; j++)
135 {
136 /* Count the "legal" entries */
137 if (k_ptr->chance[j])
138 {
139 /* Count the entries */
140 alloc_kind_size++;
141
142 /* Group by level */
143 num[k_ptr->locale[j]]++;
144 }
145 }
146 }
147
148 /* Collect the level indexes */
149 for (i = 1; i < MAX_DEPTH; i++)
150 {
151 /* Group by level */
152 num[i] += num[i - 1];
153 }
154
155 /* Paranoia */
156 if (!num[0]) quit("No town objects!");
157
158
159 /*** Initialize object allocation info ***/
160
161 /* Allocate the alloc_kind_table */
162 C_MAKE(alloc_kind_table, alloc_kind_size, alloc_entry);
163
164 /* Access the table entry */
165 table = alloc_kind_table;
166
167 /* Scan the objects */
168 for (i = 1; i < z_info->k_max; i++)
169 {
170 k_ptr = &k_info[i];
171
172 /* Scan allocation pairs */
173 for (j = 0; j < 4; j++)
174 {
175 /* Count the "legal" entries */
176 if (k_ptr->chance[j])
177 {
178 /* Extract the base level */
179 x = k_ptr->locale[j];
180
181 /* Extract the base probability */
182 p = (255 / k_ptr->chance[j]);
183
184 /* Skip entries preceding our locale */
185 y = (x > 0) ? num[x - 1] : 0;
186
187 /* Skip previous entries at this locale */
188 z = y + aux[x];
189
190 /* Load the entry */
191 table[z].index = i;
192 table[z].level = x;
193 table[z].prob1 = p;
194 table[z].prob2 = p;
195
196 /* Another entry complete for this locale */
197 aux[x]++;
198 }
199 }
200 }
201
202 /* Clear the temp arrays */
203 (void)C_WIPE(aux, MAX_DEPTH, s16b);
204 (void)C_WIPE(num, MAX_DEPTH, s16b);
205
206 /* Free the old ego item allocation table (if it exists) */
207 if (alloc_ego_table)
208 {
209 KILL(alloc_ego_table);
210 }
211
212 /* Create the ego item allocation table */
213 C_MAKE(alloc_ego_table, z_info->e_max, alloc_entry);
214
215 /* Access the table */
216 table = alloc_ego_table;
217
218 /* No ego items in the table yet */
219 alloc_ego_size = 0;
220
221 /* Count the number of legal entries */
222 for (i = 1; i < z_info->e_max; i++)
223 {
224 e_ptr = &e_info[i];
225
226 if (e_ptr->slot)
227 {
228 /* Count the item */
229 alloc_ego_size++;
230
231 /* Group by level */
232 num[e_ptr->level]++;
233 }
234 }
235
236 /* Collect the level indexes */
237 for (i = 1; i < MAX_DEPTH; i++)
238 {
239 /* Group by level */
240 num[i] += num[i - 1];
241 }
242
243
244 /* Scan the ego items */
245 for (i = 1; i < z_info->e_max; i++)
246 {
247 e_ptr = &e_info[i];
248
249 if (e_ptr->slot)
250 {
251
252 /* Extract the base level */
253 x = e_ptr->level;
254
255 /* Extract the base probability */
256 p = (255 / e_ptr->rarity);
257
258 /* Skip entries preceding our locale */
259 y = (x > 0) ? num[x - 1] : 0;
260
261 /* Skip previous entries at this locale */
262 z = y + aux[x];
263
264 /* Load the entry */
265 table[z].index = i;
266 table[z].level = x;
267 table[z].prob1 = p;
268 table[z].prob2 = p;
269
270 /* Another entry complete for this locale */
271 aux[x]++;
272 }
273 }
274
275
276 /* Success */
277 return (0);
278 }
279
280
get_object_level(const object_type * o_ptr)281 byte get_object_level(const object_type *o_ptr)
282 {
283 #if 0
284 return (byte)get_object_level_callback(o_ptr);
285 #else
286 return k_info[o_ptr->k_idx].level;
287 #endif
288 }
289
290
get_object_name(const object_type * o_ptr)291 cptr get_object_name(const object_type *o_ptr)
292 {
293 #if 0
294 return get_object_name_callback(o_ptr);
295 #else
296 return (k_name + k_info[o_ptr->k_idx].name);
297 #endif
298 }
299
300
object_is_potion(const object_type * o_ptr)301 bool object_is_potion(const object_type *o_ptr)
302 {
303 return (k_info[o_ptr->k_idx].tval == TV_POTION);
304 }
305