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.14 by root, Thu Apr 29 12:24:04 2010 UTC vs.
Revision 1.15 by root, Thu May 6 22:35:41 2010 UTC

52}; 52};
53*/ 53*/
54 54
55materialtype_t::materialtype_t () 55materialtype_t::materialtype_t ()
56{ 56{
57 next = 0; 57 next = 0;
58 reset ();
59}
60
61void
62materialtype_t::reset ()
63{
58 name = shstr_unknown; 64 name = shstr_unknown;
59 description = shstr_unknown_material_description; 65 description = shstr_unknown_material_description;
60 material = 0; 66 material = 0;
61 67
62 for (int i = 0; i < NROFATTACKS; i++) 68 for (int i = 0; i < NROFATTACKS; i++)
94 mt->next = materialt; materialt = mt; 100 mt->next = materialt; materialt = mt;
95 101
96 return mt; 102 return mt;
97} 103}
98 104
105static materialtype_t *
106find (const shstr_tmp name)
107{
108 for (materialtype_t *mt = materialt; mt; mt = mt->next)
109 if (name == mt->name)
110 return mt;
111
112 return 0;
113}
114
99/* convert materialname to materialtype_t */ 115/* convert materialname to materialtype_t */
100materialtype_t * 116materialtype_t *
101name_to_material (const shstr_tmp name) 117name_to_material (const shstr_tmp name)
102{ 118{
103 for (materialtype_t *mt = materialt; mt; mt = mt->next) 119 materialtype_t *mt = find (name);
104 if (name == mt->name)
105 return mt;
106 120
121 if (!mt)
122 {
107 LOG (llevError, "name_to_material called with nonexistent material '%s'\n", &name); 123 LOG (llevError, "name_to_material called with nonexistent material '%s'\n", &name);
108
109 return dummy_material (name); 124 mt = dummy_material (name);
110} 125 }
111 126
127 return mt;
128}
129
112void 130void
113object_thawer::get (materialtype_t *&m) const 131object_thawer::get (materialtype_t *&mt) const
114{ 132{
115 shstr name; 133 shstr name;
116 get (name); 134 get (name);
117 135
118 for (materialtype_t *mt = materialt; mt; mt = mt->next) 136 mt = find (name);
119 if (name == mt->name) 137
138 if (!mt)
120 { 139 {
121 m = mt;
122 return;
123 }
124
125 parse_error (format ("material called %s requested, but not found, creating dummy material.\n", &name)); 140 parse_error (format ("material called %s requested, but not found, creating dummy material.\n", &name));
126
127 m = dummy_material (name); 141 mt = dummy_material (name);
142 }
128} 143}
129 144
130/* when doing transmutation of objects, we have to recheck the resistances, 145/* when doing transmutation of objects, we have to recheck the resistances,
131 * as some that did not apply previously, may apply now. 146 * as some that did not apply previously, may apply now.
132 */ 147 */
214} 229}
215 230
216//-GPL 231//-GPL
217 232
218void 233void
219load_materials () 234reload_materials ()
220{ 235{
221 //TODO: somehow free old materials, or update them in-place
222 // currently we effectively leak them.
223 material_null.next = 0; materialt = &material_null;
224
225 object_thawer thawer (settings.datadir, "materials"); 236 object_thawer thawer (settings.datadir, "materials");
226 237
227 if (!thawer) 238 if (!thawer)
228 { 239 {
229 LOG (llevError, "Cannot open %s for reading\n", thawer.name); 240 LOG (llevError, "unable to load %s for reading\n", thawer.name);
230 goto done; 241 return;
231 } 242 }
232 243
233 while (thawer.kw != KW_name) 244 while (thawer.kw != KW_name)
234 { 245 {
235 thawer.next (); 246 thawer.next ();
236 247
237 if (thawer.kw == KW_EOF) 248 if (thawer.kw == KW_EOF)
238 goto done; 249 return;
239 } 250 }
240 251
241 materialtype_t *mt; 252 materialtype_t *mt;
242 253
243 for (;;) 254 for (;;)
244 { 255 {
245 switch (thawer.kw) 256 switch (thawer.kw)
246 { 257 {
247 case KW_name: 258 case KW_name:
259 coroapi::cede_to_tick ();
260
261 {
262 // create a new dummy material, or find the existing material
263 shstr name;
264 thawer.get (name);
265
266 mt = find (name);
267
268 if (mt)
269 mt->reset ();
270 else
271 {
248 mt = new materialtype_t; 272 mt = new materialtype_t;
249 thawer.get (mt->name);
250 mt->description = mt->name;
251
252 mt->next = materialt; materialt = mt; 273 mt->next = materialt; materialt = mt;
274 }
275
276 mt->name = name;
277 mt->description = name;
278 }
253 break; 279 break;
254 280
255 case KW_description: 281 case KW_description:
256 thawer.get (mt->description); 282 thawer.get (mt->description);
257 break; 283 break;
314 case KW_weight: thawer.get (mt->weight); break; 340 case KW_weight: thawer.get (mt->weight); break;
315 case KW_value: thawer.get (mt->value); break; 341 case KW_value: thawer.get (mt->value); break;
316 case KW_density: thawer.get (mt->density); break; 342 case KW_density: thawer.get (mt->density); break;
317 343
318 case KW_EOF: 344 case KW_EOF:
319 goto done; 345 return;
320 346
321 default: 347 default:
322 if (!thawer.parse_error ("materials file")) 348 if (!thawer.parse_error ("materials file"))
323 goto done; 349 return;
324 break; 350 break;
325 } 351 }
326 352
327 thawer.next (); 353 thawer.next ();
328 } 354 }
329
330done:
331 LOG (llevDebug, "Done.\n");
332} 355}
333 356

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines