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 (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2002 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 2002 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 1992 Frank Tore Johansen |
7 | * Copyright (©) 1992 Frank Tore Johansen |
7 | * |
8 | * |
8 | * Deliantra is free software: you can redistribute it and/or modify it under |
9 | * 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 |
10 | * 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 |
11 | * Free Software Foundation, either version 3 of the License, or (at your |
11 | * option) any later version. |
12 | * option) any later version. |
12 | * |
13 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
17 | * GNU General Public License for more details. |
17 | * |
18 | * |
18 | * You should have received a copy of the Affero GNU General Public License |
19 | * 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 |
20 | * and the GNU General Public License along with this program. If not, see |
20 | * <http://www.gnu.org/licenses/>. |
21 | * <http://www.gnu.org/licenses/>. |
21 | * |
22 | * |
22 | * The authors can be reached via e-mail to <support@deliantra.net> |
23 | * The authors can be reached via e-mail to <support@deliantra.net> |
23 | */ |
24 | */ |
24 | |
25 | |
25 | #include <global.h> |
26 | #include <global.h> |
26 | #include <material.h> |
27 | #include <material.h> |
… | |
… | |
52 | }; |
53 | }; |
53 | */ |
54 | */ |
54 | |
55 | |
55 | materialtype_t::materialtype_t () |
56 | materialtype_t::materialtype_t () |
56 | { |
57 | { |
57 | next = 0; |
58 | next = 0; |
|
|
59 | reset (); |
|
|
60 | } |
|
|
61 | |
|
|
62 | void |
|
|
63 | materialtype_t::reset () |
|
|
64 | { |
58 | name = shstr_unknown; |
65 | name = shstr_unknown; |
59 | description = shstr_unknown_material_description; |
66 | description = shstr_unknown_material_description; |
60 | material = 0; |
67 | material = 0; |
61 | |
68 | |
62 | for (int i = 0; i < NROFATTACKS; i++) |
69 | for (int i = 0; i < NROFATTACKS; i++) |
… | |
… | |
75 | weight = 100; |
82 | weight = 100; |
76 | value = 100; |
83 | value = 100; |
77 | density = 1000; |
84 | density = 1000; |
78 | } |
85 | } |
79 | |
86 | |
|
|
87 | // create a new material of the given name |
|
|
88 | static materialtype_t * |
|
|
89 | dummy_material (shstr_tmp name) |
|
|
90 | { |
|
|
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 | { |
|
|
97 | mt->save [i] = 10; |
|
|
98 | mt->mod [i] = 9; |
|
|
99 | } |
|
|
100 | |
|
|
101 | mt->next = materialt; materialt = mt; |
|
|
102 | |
|
|
103 | return mt; |
|
|
104 | } |
|
|
105 | |
|
|
106 | static materialtype_t * |
|
|
107 | find (const shstr_tmp name) |
|
|
108 | { |
|
|
109 | for (materialtype_t *mt = materialt; mt; mt = mt->next) |
|
|
110 | if (name == mt->name) |
|
|
111 | return mt; |
|
|
112 | |
|
|
113 | return 0; |
|
|
114 | } |
|
|
115 | |
80 | /* convert materialname to materialtype_t */ |
116 | /* convert materialname to materialtype_t */ |
81 | materialtype_t * |
117 | materialtype_t * |
82 | name_to_material (const shstr_tmp name) |
118 | name_to_material (const shstr_tmp name) |
83 | { |
119 | { |
84 | for (materialtype_t *mt = materialt; mt; mt = mt->next) |
120 | materialtype_t *mt = find (name); |
85 | if (name == mt->name) |
|
|
86 | return mt; |
|
|
87 | |
121 | |
|
|
122 | if (!mt) |
|
|
123 | { |
88 | LOG (llevError, "name_to_material called with nonexistent material '%s'\n", &name); |
124 | LOG (llevError, "name_to_material called with nonexistent material '%s'\n", &name); |
89 | |
125 | 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 | { |
126 | } |
97 | mt->save [i] = 10; |
|
|
98 | mt->mod [i] = 9; |
|
|
99 | } |
|
|
100 | |
|
|
101 | mt->next = materialt; materialt = mt; |
|
|
102 | |
127 | |
103 | return mt; |
128 | return mt; |
|
|
129 | } |
|
|
130 | |
|
|
131 | void |
|
|
132 | object_thawer::get (materialtype_t *&mt) const |
|
|
133 | { |
|
|
134 | shstr name; |
|
|
135 | get (name); |
|
|
136 | |
|
|
137 | mt = find (name); |
|
|
138 | |
|
|
139 | if (!mt) |
|
|
140 | { |
|
|
141 | parse_error (format ("material called %s requested, but not found, creating dummy material.\n", &name)); |
|
|
142 | mt = dummy_material (name); |
|
|
143 | } |
104 | } |
144 | } |
105 | |
145 | |
106 | /* when doing transmutation of objects, we have to recheck the resistances, |
146 | /* when doing transmutation of objects, we have to recheck the resistances, |
107 | * as some that did not apply previously, may apply now. |
147 | * as some that did not apply previously, may apply now. |
108 | */ |
148 | */ |
… | |
… | |
190 | } |
230 | } |
191 | |
231 | |
192 | //-GPL |
232 | //-GPL |
193 | |
233 | |
194 | void |
234 | void |
195 | load_materials () |
235 | _reload_materials () |
196 | { |
236 | { |
197 | char filename[MAX_BUF]; |
237 | 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 | material_null.next = 0; materialt = &material_null; |
|
|
204 | |
|
|
205 | object_thawer thawer (filename); |
|
|
206 | |
238 | |
207 | if (!thawer) |
239 | if (!thawer) |
208 | { |
240 | { |
209 | LOG (llevError, "Cannot open %s for reading\n", filename); |
241 | LOG (llevError, "unable to load %s for reading\n", thawer.name); |
210 | goto done; |
242 | return; |
211 | } |
243 | } |
212 | |
244 | |
213 | while (thawer.kw != KW_name) |
245 | while (thawer.kw != KW_name) |
214 | { |
246 | { |
215 | thawer.next (); |
247 | thawer.next (); |
216 | |
248 | |
217 | if (thawer.kw == KW_EOF) |
249 | if (thawer.kw == KW_EOF) |
218 | goto done; |
250 | return; |
219 | } |
251 | } |
220 | |
252 | |
221 | materialtype_t *mt; |
253 | materialtype_t *mt; |
222 | |
254 | |
223 | for (;;) |
255 | for (;;) |
224 | { |
256 | { |
225 | switch (thawer.kw) |
257 | switch (thawer.kw) |
226 | { |
258 | { |
227 | case KW_name: |
259 | case KW_name: |
|
|
260 | coroapi::cede_to_tick (); |
|
|
261 | |
|
|
262 | { |
|
|
263 | // create a new dummy material, or find the existing material |
|
|
264 | shstr name; |
|
|
265 | thawer.get (name); |
|
|
266 | |
|
|
267 | mt = find (name); |
|
|
268 | |
|
|
269 | if (mt) |
|
|
270 | mt->reset (); |
|
|
271 | else |
|
|
272 | { |
228 | mt = new materialtype_t; |
273 | mt = new materialtype_t; |
229 | thawer.get (mt->name); |
|
|
230 | mt->description = mt->name; |
|
|
231 | |
|
|
232 | mt->next = materialt; materialt = mt; |
274 | mt->next = materialt; materialt = mt; |
|
|
275 | } |
|
|
276 | |
|
|
277 | mt->name = name; |
|
|
278 | mt->description = name; |
|
|
279 | } |
233 | break; |
280 | break; |
234 | |
281 | |
235 | case KW_description: |
282 | case KW_description: |
236 | thawer.get (mt->description); |
283 | thawer.get (mt->description); |
237 | break; |
284 | break; |
… | |
… | |
294 | case KW_weight: thawer.get (mt->weight); break; |
341 | case KW_weight: thawer.get (mt->weight); break; |
295 | case KW_value: thawer.get (mt->value); break; |
342 | case KW_value: thawer.get (mt->value); break; |
296 | case KW_density: thawer.get (mt->density); break; |
343 | case KW_density: thawer.get (mt->density); break; |
297 | |
344 | |
298 | case KW_EOF: |
345 | case KW_EOF: |
299 | goto done; |
346 | return; |
300 | |
347 | |
301 | default: |
348 | default: |
302 | if (!thawer.parse_error ("materials file", "materials")) |
349 | if (!thawer.parse_error ("materials file")) |
303 | goto done; |
350 | return; |
304 | break; |
351 | break; |
305 | } |
352 | } |
306 | |
353 | |
307 | thawer.next (); |
354 | thawer.next (); |
308 | } |
355 | } |
309 | |
|
|
310 | done: |
|
|
311 | LOG (llevDebug, "Done.\n"); |
|
|
312 | } |
356 | } |
313 | |
357 | |