OSDN Git Service

Leon氏の勧めに従って、Vanillaのコードと同様に各ソースファイルの頭の
[hengband/hengband.git] / src / obj_kind.c
1 /* File: obj_kind.c */
2
3 /*
4  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
5  *
6  * This software may be copied and distributed for educational, research,
7  * and not for profit purposes provided that this copyright and statement
8  * are included in all such copies.  Other copyrights may also apply.
9  */
10
11 /* Purpose: Code for the object templates */
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 */
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 */
38 errr k_info_free(void)
39 {
40         k_info_size = K_INFO_BASE_SIZE;
41
42         C_KILL(k_info, k_info_size, object_kind);
43
44         /* Success */
45         return 0;
46 }
47
48
49 void k_info_reset(void)
50 {
51         int i;
52
53         /* Reset the "objects" */
54         for (i = 1; i < max_k_idx; 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 */
68 object_kind *k_info_add(object_kind *k_info_entry)
69 {
70         /* Resize if necessary */
71         while (k_info_size <= max_k_idx)
72         {
73                 k_info_size += K_INFO_RESIZE;
74
75                 /* Reallocate the extra memory */
76                 k_info = realloc(k_info, k_info_size * sizeof(object_kind));
77
78                 /* Failure */
79 #ifdef JP
80 if (!k_info) quit("¥á¥â¥ê¡¼ÉÔ­!");
81 #else
82                 if (!k_info) quit("Out of memory!");
83 #endif
84
85
86                 /* Wipe the new memory */
87                 (void)C_WIPE(&k_info[(k_info_size - K_INFO_RESIZE)], K_INFO_RESIZE, object_kind);
88         }
89
90         /* Increase the maximum index of the array */
91         max_k_idx++;
92
93         /* Copy the new object_kind */
94         COPY(&k_info[max_k_idx-1], k_info_entry, object_kind);
95
96         /* Success */
97         return (&k_info[max_k_idx-1]);
98 }
99
100
101 /*
102  * Initialize some other arrays
103  */
104 errr init_object_alloc(void)
105 {
106         int i, j;
107         object_kind *k_ptr;
108         alloc_entry *table;
109         s16b num[MAX_DEPTH];
110         s16b aux[MAX_DEPTH];
111
112
113         /*** Analyze object allocation info ***/
114
115         /* Clear the "aux" array */
116         (void)C_WIPE(&aux, MAX_DEPTH, s16b);
117
118         /* Clear the "num" array */
119         (void)C_WIPE(&num, MAX_DEPTH, s16b);
120
121         /* Free the old "alloc_kind_table" (if it exists) */
122         if (alloc_kind_table)
123         {
124                 C_KILL(alloc_kind_table, alloc_kind_size, alloc_entry);
125         }
126
127         /* Size of "alloc_kind_table" */
128         alloc_kind_size = 0;
129
130         /* Scan the objects */
131         for (i = 1; i < max_k_idx; i++)
132         {
133                 k_ptr = &k_info[i];
134
135                 /* Scan allocation pairs */
136                 for (j = 0; j < 4; j++)
137                 {
138                         /* Count the "legal" entries */
139                         if (k_ptr->chance[j])
140                         {
141                                 /* Count the entries */
142                                 alloc_kind_size++;
143
144                                 /* Group by level */
145                                 num[k_ptr->locale[j]]++;
146                         }
147                 }
148         }
149
150         /* Collect the level indexes */
151         for (i = 1; i < MAX_DEPTH; i++)
152         {
153                 /* Group by level */
154                 num[i] += num[i-1];
155         }
156
157         /* Paranoia */
158 #ifdef JP
159 if (!num[0]) quit("Ä®¤Î¥¢¥¤¥Æ¥à¤¬¤Ê¤¤¡ª");
160 #else
161         if (!num[0]) quit("No town objects!");
162 #endif
163
164
165
166         /*** Initialize object allocation info ***/
167
168         /* Allocate the alloc_kind_table */
169         C_MAKE(alloc_kind_table, alloc_kind_size, alloc_entry);
170
171         /* Access the table entry */
172         table = alloc_kind_table;
173
174         /* Scan the objects */
175         for (i = 1; i < max_k_idx; i++)
176         {
177                 k_ptr = &k_info[i];
178
179                 /* Scan allocation pairs */
180                 for (j = 0; j < 4; j++)
181                 {
182                         /* Count the "legal" entries */
183                         if (k_ptr->chance[j])
184                         {
185                                 int p, x, y, z;
186
187                                 /* Extract the base level */
188                                 x = k_ptr->locale[j];
189
190                                 /* Extract the base probability */
191                                 p = (100 / k_ptr->chance[j]);
192
193                                 /* Skip entries preceding our locale */
194                                 y = (x > 0) ? num[x-1] : 0;
195
196                                 /* Skip previous entries at this locale */
197                                 z = y + aux[x];
198
199                                 /* Load the entry */
200                                 table[z].index = i;
201                                 table[z].level = x;
202                                 table[z].prob1 = p;
203                                 table[z].prob2 = p;
204                                 table[z].prob3 = p;
205
206                                 /* Another entry complete for this locale */
207                                 aux[x]++;
208                         }
209                 }
210         }
211
212         /* Success */
213         return (0);
214 }
215
216
217 int get_object_level(object_type *o_ptr)
218 {
219 #if 0
220         return (byte)get_object_level_callback(o_ptr);
221 #else
222         return k_info[o_ptr->k_idx].level;
223 #endif
224 }
225
226
227 s32b get_object_cost(object_type *o_ptr)
228 {
229 #if 0
230         return get_object_cost_callback(o_ptr);
231 #else
232         return k_info[o_ptr->k_idx].cost;
233 #endif
234 }
235
236
237 cptr get_object_name(object_type *o_ptr)
238 {
239 #if 0
240         return get_object_name_callback(o_ptr);
241 #else
242         return (k_name + k_info[o_ptr->k_idx].name);
243 #endif
244 }
245
246
247 /* The player is "aware" of the item's effects */
248 bool get_object_aware(object_type *o_ptr)
249 {
250 #if 0
251         return get_object_aware_callback(o_ptr);
252 #else
253         return k_info[o_ptr->k_idx].aware;
254 #endif
255 }
256
257
258 /* The player has "tried" one of the items */
259 bool get_object_tried(object_type *o_ptr)
260 {
261 #if 0
262         return get_object_tried_callback(o_ptr);
263 #else
264         return k_info[o_ptr->k_idx].tried;
265 #endif
266 }
267
268
269 bool object_is_potion(object_type *o_ptr)
270 {
271         return (k_info[o_ptr->k_idx].tval == TV_POTION);
272 }
273
274 bool object_is_shoukinkubi(object_type *o_ptr)
275 {
276         int i;
277         if (p_ptr->today_mon > 0 && o_ptr->pval == p_ptr->today_mon) return TRUE;
278         if (o_ptr->pval == MON_TSUCHINOKO) return TRUE;
279         for (i = 0; i < MAX_KUBI; i++)
280                 if (o_ptr->pval == kubi_r_idx[i]) break;
281         if (i < MAX_KUBI) return TRUE;
282         return FALSE;
283 }