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.11 by root, Thu Apr 15 03:34:30 2010 UTC vs.
Revision 1.19 by root, Sun Nov 4 01:01:13 2012 UTC

1/* 1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992 Frank Tore Johansen 6 * Copyright (©) 1992 Frank Tore Johansen
7 * 7 *
8 * Deliantra is free software: you can redistribute it and/or modify it under 8 * Deliantra is free software: you can redistribute it and/or modify it under
9 * the terms of the Affero GNU General Public License as published by the 9 * the terms of the Affero GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your 10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version. 11 * option) any later version.
12 * 12 *
13 * This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 16 * GNU General Public License for more details.
17 * 17 *
18 * You should have received a copy of the Affero GNU General Public License 18 * You should have received a copy of the Affero GNU General Public License
19 * and the GNU General Public License along with this program. If not, see 19 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>. 20 * <http://www.gnu.org/licenses/>.
21 * 21 *
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>
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++)
75 weight = 100; 81 weight = 100;
76 value = 100; 82 value = 100;
77 density = 1000; 83 density = 1000;
78} 84}
79 85
86// create a new material of the given name
87static materialtype_t *
88dummy_material (shstr_tmp name)
89{
90 materialtype_t *mt = new materialtype_t;
91 mt->name = name;
92
93 // make it susceptible to attacks
94 for (int i = 0; i < NROFATTACKS; i++)
95 {
96 mt->save [i] = 10;
97 mt->mod [i] = 9;
98 }
99
100 mt->next = materialt; materialt = mt;
101
102 return mt;
103}
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
80/* convert materialname to materialtype_t */ 115/* convert materialname to materialtype_t */
81materialtype_t * 116materialtype_t *
82name_to_material (const shstr_tmp name) 117name_to_material (const shstr_tmp name)
83{ 118{
84 for (materialtype_t *mt = materialt; mt; mt = mt->next) 119 materialtype_t *mt = find (name);
85 if (name == mt->name)
86 return mt;
87 120
121 if (!mt)
122 {
88 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);
89 124 mt = dummy_material (name);
90 // create a new material of the given name
91 materialtype_t *mt = new materialtype_t;
92 mt->name = name;
93
94 // make it susceptible to attacks
95 for (int i = 0; i < NROFATTACKS; i++)
96 { 125 }
97 mt->save [i] = 10;
98 mt->mod [i] = 9;
99 }
100
101 mt->next = materialt; materialt = mt;
102 126
103 return mt; 127 return mt;
128}
129
130void
131object_thawer::get (materialtype_t *&mt) const
132{
133 shstr name;
134 get (name);
135
136 mt = find (name);
137
138 if (!mt)
139 {
140 parse_error (format ("material called %s requested, but not found, creating dummy material.\n", &name));
141 mt = dummy_material (name);
142 }
104} 143}
105 144
106/* when doing transmutation of objects, we have to recheck the resistances, 145/* when doing transmutation of objects, we have to recheck the resistances,
107 * as some that did not apply previously, may apply now. 146 * as some that did not apply previously, may apply now.
108 */ 147 */
190} 229}
191 230
192//-GPL 231//-GPL
193 232
194void 233void
195load_materials () 234_reload_materials ()
196{ 235{
197 char filename[MAX_BUF]; 236 object_thawer thawer (settings.datadir, "materials");
198
199 sprintf (filename, "%s/materials", settings.datadir);
200 LOG (llevDebug, "Reading material type data from %s...\n", filename);
201
202 //TODO: somehow free old materials, or update them in-place
203 // currently we effectively leak them.
204 material_null.next = 0; materialt = &material_null;
205
206 object_thawer thawer (filename);
207 237
208 if (!thawer) 238 if (!thawer)
209 { 239 {
210 LOG (llevError, "Cannot open %s for reading\n", filename); 240 LOG (llevError, "unable to load %s for reading\n", thawer.name);
211 goto done; 241 return;
212 } 242 }
213 243
214 while (thawer.kw != KW_name) 244 while (thawer.kw != KW_name)
215 { 245 {
216 thawer.next (); 246 thawer.next ();
217 247
218 if (thawer.kw == KW_EOF) 248 if (thawer.kw == KW_EOF)
219 goto done; 249 return;
220 } 250 }
221 251
222 materialtype_t *mt; 252 materialtype_t *mt;
223 253
224 for (;;) 254 for (;;)
225 { 255 {
226 switch (thawer.kw) 256 switch (thawer.kw)
227 { 257 {
228 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 {
229 mt = new materialtype_t; 272 mt = new materialtype_t;
230 thawer.get (mt->name);
231 mt->description = mt->name;
232
233 mt->next = materialt; materialt = mt; 273 mt->next = materialt; materialt = mt;
274 }
275
276 mt->name = name;
277 mt->description = name;
278 }
234 break; 279 break;
235 280
236 case KW_description: 281 case KW_description:
237 thawer.get (mt->description); 282 thawer.get (mt->description);
238 break; 283 break;
295 case KW_weight: thawer.get (mt->weight); break; 340 case KW_weight: thawer.get (mt->weight); break;
296 case KW_value: thawer.get (mt->value); break; 341 case KW_value: thawer.get (mt->value); break;
297 case KW_density: thawer.get (mt->density); break; 342 case KW_density: thawer.get (mt->density); break;
298 343
299 case KW_EOF: 344 case KW_EOF:
300 goto done; 345 return;
301 346
302 default: 347 default:
303 if (!thawer.parse_error ("materials file", "materials")) 348 if (!thawer.parse_error ("materials file"))
304 goto done; 349 return;
305 break; 350 break;
306 } 351 }
307 352
308 thawer.next (); 353 thawer.next ();
309 } 354 }
310
311done:
312 LOG (llevDebug, "Done.\n");
313} 355}
314 356

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines