ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/material.C
(Generate patch)

Comparing deliantra/server/common/material.C (file contents):
Revision 1.1 by root, Wed Nov 4 00:02:48 2009 UTC vs.
Revision 1.4 by root, Tue Nov 10 00:01:31 2009 UTC

22 * The authors can be reached via e-mail to <support@deliantra.net> 22 * The authors can be reached via e-mail to <support@deliantra.net>
23 */ 23 */
24 24
25#include <global.h> 25#include <global.h>
26#include <material.h> 26#include <material.h>
27
28//+GPL
27 29
28/* convert materialname to materialtype_t */ 30/* convert materialname to materialtype_t */
29 31
30materialtype_t * 32materialtype_t *
31name_to_material (const shstr_cmp name) 33name_to_material (const shstr_cmp name)
135 op->value = (op->value * lmt->value) / 100; 137 op->value = (op->value * lmt->value) / 100;
136 } 138 }
137 } 139 }
138} 140}
139 141
142//TODO: make this a constructor
143static materialtype_t *
144get_empty_mat (void)
145{
146 materialtype_t *mt;
147 int i;
148
149 mt = new materialtype_t;
150
151 mt->name = shstr_unknown;
152 mt->description = 0;
153
154 for (i = 0; i < NROFATTACKS; i++)
155 {
156 mt->save[i] = 0;
157 mt->mod[i] = 0;
158 }
159
160 mt->chance = 0;
161 mt->difficulty = 0;
162 mt->magic = 0;
163 mt->damage = 0;
164 mt->wc = 0;
165 mt->ac = 0;
166 mt->sp = 0;
167 mt->weight = 100;
168 mt->value = 100;
169 mt->density = 1;
170 mt->next = 0;
171
172 return mt;
173}
174
175//-GPL
176
177const materialtype_t *
178object::dominant_material () const
179{
180 if (materialtype_t *mt = name_to_material (materialname))
181 return mt;
182
183 return name_to_material (shstr_unknown);
184}
185
186void
187load_materials (void)
188{
189 char filename[MAX_BUF];
190
191 sprintf (filename, "%s/materials", settings.datadir);
192 LOG (llevDebug, "Reading material type data from %s...\n", filename);
193
194 //TODO: somehow free old materials, or update them in-place
195 materialt = 0;
196
197 object_thawer thawer (filename);
198
199 if (!thawer)
200 {
201 LOG (llevError, "Cannot open %s for reading\n", filename);
202 goto done;
203 }
204
205 while (thawer.kw != KW_name)
206 {
207 thawer.next ();
208
209 if (thawer.kw == KW_EOF)
210 goto done;
211 }
212
213 materialtype_t *mt;
214
215 for (;;)
216 {
217 switch (thawer.kw)
218 {
219 case KW_name:
220 mt = get_empty_mat ();
221 mt->next = materialt;
222 materialt = mt;
223
224 thawer.get (mt->name);
225 mt->description = mt->name;
226 break;
227
228 case KW_description:
229 thawer.get (mt->description);
230 break;
231
232 case KW_material:
233 thawer.get (mt->material);
234 break;
235
236 case KW_saves:
237 {
238 const char *cp = thawer.get_str () - 1;
239
240 for (int i = 0; i < NROFATTACKS; i++)
241 {
242 if (!cp)
243 {
244 mt->save[i] = 0;
245 continue;
246 }
247
248 int value;
249 ++cp;
250 sscanf (cp, "%d", &value);
251 mt->save[i] = (sint8) value;
252 cp = strchr (cp, ',');
253 }
254 }
255 break;
256
257 case KW_mods:
258 {
259 const char *cp = thawer.get_str () - 1;
260
261 for (int i = 0; i < NROFATTACKS; i++)
262 {
263 if (!cp)
264 {
265 mt->save[i] = 0;
266 continue;
267 }
268
269 ++cp;
270 int value;
271 sscanf (cp, "%d", &value);
272 mt->mod[i] = (sint8) value;
273 cp = strchr (cp, ',');
274 }
275 }
276 break;
277
278 case KW_chance: thawer.get (mt->chance); break;
279 case KW_difficulty: // cf+ alias, not original cf
280 case KW_diff: thawer.get (mt->difficulty); break;
281 case KW_magic: thawer.get (mt->magic); break;
282 case KW_dam: // cf+ alias, not original cf
283 case KW_damage: thawer.get (mt->damage); break;
284 case KW_wc: thawer.get (mt->wc); break;
285 case KW_ac: thawer.get (mt->ac); break;
286 case KW_sp: thawer.get (mt->sp); break;
287 case KW_weight: thawer.get (mt->weight); break;
288 case KW_value: thawer.get (mt->value); break;
289 case KW_density: thawer.get (mt->density); break;
290
291 case KW_EOF:
292 goto done;
293
294 default:
295 if (!thawer.parse_error ("materials file", "materials"))
296 goto done;
297 break;
298 }
299
300 thawer.next ();
301 }
302
303done:
304 if (!materialt)
305 materialt = get_empty_mat ();
306
307 LOG (llevDebug, "Done.\n");
308}
309

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines