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 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
5 | * Copyright (©) 2002 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 1992,2007 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. |
… | |
… | |
52 | }; |
52 | }; |
53 | */ |
53 | */ |
54 | |
54 | |
55 | materialtype_t::materialtype_t () |
55 | materialtype_t::materialtype_t () |
56 | { |
56 | { |
57 | next = 0; |
57 | next = 0; |
|
|
58 | reset (); |
|
|
59 | } |
|
|
60 | |
|
|
61 | void |
|
|
62 | materialtype_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++) |
63 | { |
69 | { |
64 | save [i] = 10; |
70 | save [i] = 0; |
65 | mod [i] = 9; |
71 | mod [i] = 0; |
66 | } |
72 | } |
67 | |
73 | |
68 | chance = 0; |
74 | chance = 0; |
69 | difficulty = 0; |
75 | difficulty = 0; |
70 | magic = 0; |
76 | magic = 0; |
… | |
… | |
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 |
|
|
87 | static materialtype_t * |
|
|
88 | dummy_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 | |
|
|
105 | static materialtype_t * |
|
|
106 | find (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 */ |
81 | materialtype_t * |
116 | materialtype_t * |
82 | name_to_material (const shstr_tmp name) |
117 | name_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 |
125 | } |
91 | materialtype_t *mt = new materialtype_t; |
|
|
92 | mt->name = name; |
|
|
93 | |
|
|
94 | mt->next = materialt; |
|
|
95 | materialt = mt; |
|
|
96 | |
126 | |
97 | return mt; |
127 | return mt; |
|
|
128 | } |
|
|
129 | |
|
|
130 | void |
|
|
131 | object_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 | } |
98 | } |
143 | } |
99 | |
144 | |
100 | /* when doing transmutation of objects, we have to recheck the resistances, |
145 | /* when doing transmutation of objects, we have to recheck the resistances, |
101 | * as some that did not apply previously, may apply now. |
146 | * as some that did not apply previously, may apply now. |
102 | */ |
147 | */ |
103 | void |
148 | void |
104 | transmute_materialname (object *op, const object *change) |
149 | transmute_materialname (object *op, const object *change) |
105 | { |
150 | { |
106 | int j; |
151 | if (!op->is_armor ()) |
|
|
152 | return; |
|
|
153 | |
|
|
154 | if (op->material == MATERIAL_NULL) |
|
|
155 | return; |
107 | |
156 | |
108 | if (op->material != change->material) |
157 | if (op->material != change->material) |
109 | return; |
158 | return; |
110 | |
159 | |
111 | if (!op->is_armor ()) |
|
|
112 | return; |
|
|
113 | |
|
|
114 | materialtype_t *mt = op->material; |
160 | materialtype_t *mt = op->material; |
115 | |
161 | |
116 | for (j = 0; j < NROFATTACKS; j++) |
162 | for (int j = 0; j < NROFATTACKS; j++) |
117 | if (op->resist[j] == 0 && change->resist[j] != 0) |
163 | if (op->resist[j] == 0 && change->resist[j] != 0) |
118 | { |
164 | { |
119 | op->resist[j] += mt->mod[j]; |
165 | op->resist[j] += mt->mod[j]; |
120 | |
166 | |
121 | if (op->resist[j] > 100) op->resist[j] = 100; |
167 | if (op->resist[j] > 100) op->resist[j] = 100; |
… | |
… | |
125 | |
171 | |
126 | /* set the materialname and type for an item */ |
172 | /* set the materialname and type for an item */ |
127 | void |
173 | void |
128 | select_material (object *op, int difficulty) |
174 | select_material (object *op, int difficulty) |
129 | { |
175 | { |
130 | if (op->material != &material_null || !op->materials) |
176 | if (op->material != MATERIAL_NULL || !op->materials) |
131 | return; |
177 | return; |
132 | |
178 | |
133 | materialtype_t *lmt = 0; |
179 | materialtype_t *lmt = 0; |
134 | |
180 | |
135 | //TODL: dead code? |
181 | //TODL: dead code? |
… | |
… | |
183 | } |
229 | } |
184 | |
230 | |
185 | //-GPL |
231 | //-GPL |
186 | |
232 | |
187 | void |
233 | void |
188 | load_materials (void) |
234 | reload_materials () |
189 | { |
235 | { |
190 | char filename[MAX_BUF]; |
236 | object_thawer thawer (settings.datadir, "materials"); |
191 | |
|
|
192 | sprintf (filename, "%s/materials", settings.datadir); |
|
|
193 | LOG (llevDebug, "Reading material type data from %s...\n", filename); |
|
|
194 | |
|
|
195 | //TODO: somehow free old materials, or update them in-place |
|
|
196 | material_null.next = 0; |
|
|
197 | materialt = &material_null; |
|
|
198 | |
|
|
199 | object_thawer thawer (filename); |
|
|
200 | |
237 | |
201 | if (!thawer) |
238 | if (!thawer) |
202 | { |
239 | { |
203 | LOG (llevError, "Cannot open %s for reading\n", filename); |
240 | LOG (llevError, "unable to load %s for reading\n", thawer.name); |
204 | goto done; |
241 | return; |
205 | } |
242 | } |
206 | |
243 | |
207 | while (thawer.kw != KW_name) |
244 | while (thawer.kw != KW_name) |
208 | { |
245 | { |
209 | thawer.next (); |
246 | thawer.next (); |
210 | |
247 | |
211 | if (thawer.kw == KW_EOF) |
248 | if (thawer.kw == KW_EOF) |
212 | goto done; |
249 | return; |
213 | } |
250 | } |
214 | |
251 | |
215 | materialtype_t *mt; |
252 | materialtype_t *mt; |
216 | |
253 | |
217 | for (;;) |
254 | for (;;) |
218 | { |
255 | { |
219 | switch (thawer.kw) |
256 | switch (thawer.kw) |
220 | { |
257 | { |
221 | 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 | { |
222 | mt = new materialtype_t; |
272 | mt = new materialtype_t; |
223 | thawer.get (mt->name); |
273 | mt->next = materialt; materialt = mt; |
|
|
274 | } |
|
|
275 | |
|
|
276 | mt->name = name; |
224 | mt->description = mt->name; |
277 | mt->description = name; |
225 | |
278 | } |
226 | mt->next = materialt; |
|
|
227 | materialt = mt; |
|
|
228 | break; |
279 | break; |
229 | |
280 | |
230 | case KW_description: |
281 | case KW_description: |
231 | thawer.get (mt->description); |
282 | thawer.get (mt->description); |
232 | break; |
283 | break; |
… | |
… | |
289 | case KW_weight: thawer.get (mt->weight); break; |
340 | case KW_weight: thawer.get (mt->weight); break; |
290 | case KW_value: thawer.get (mt->value); break; |
341 | case KW_value: thawer.get (mt->value); break; |
291 | case KW_density: thawer.get (mt->density); break; |
342 | case KW_density: thawer.get (mt->density); break; |
292 | |
343 | |
293 | case KW_EOF: |
344 | case KW_EOF: |
294 | goto done; |
345 | return; |
295 | |
346 | |
296 | default: |
347 | default: |
297 | if (!thawer.parse_error ("materials file", "materials")) |
348 | if (!thawer.parse_error ("materials file")) |
298 | goto done; |
349 | return; |
299 | break; |
350 | break; |
300 | } |
351 | } |
301 | |
352 | |
302 | thawer.next (); |
353 | thawer.next (); |
303 | } |
354 | } |
304 | |
|
|
305 | done: |
|
|
306 | if (!materialt) |
|
|
307 | materialt = new materialtype_t; |
|
|
308 | |
|
|
309 | LOG (llevDebug, "Done.\n"); |
|
|
310 | } |
355 | } |
311 | |
356 | |