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,2012 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. |
… | |
… | |
25 | #include <global.h> |
25 | #include <global.h> |
26 | #include <material.h> |
26 | #include <material.h> |
27 | |
27 | |
28 | //+GPL |
28 | //+GPL |
29 | |
29 | |
30 | /* convert materialname to materialtype_t */ |
30 | materialtype_t *materialt; |
31 | |
31 | |
|
|
32 | /* |
|
|
33 | materialtype material[NROFMATERIALS] = { |
|
|
34 | * P M F E C C A D W G P S P T F C D D C C G H B I * |
|
|
35 | * H A I L O O C R E H O L A U E A E E H O O O L N * |
|
|
36 | * Y G R E L N I A A O I O R R A N P A A U D L I T * |
|
|
37 | * S I E C D F D I P S S W A N R C L T O N Y N R * |
|
|
38 | * I C T U N O T O L E E H S T P D N * |
|
|
39 | {"paper", {15,10,17, 9, 5, 7,13, 0,20,15, 0,0,0,0,0,10,0,0,0,0,0,0,0,0}}, |
|
|
40 | {"metal", { 2,12, 3,12, 2,10, 7, 0,20,15, 0,0,0,0,0,10,0,0,0,0,0,0,0,0}}, |
|
|
41 | {"glass", {14,11, 8, 3,10, 5, 1, 0,20,15, 0,0,0,0,0, 0,0,0,0,0,0,0,0,0}}, |
|
|
42 | {"leather", { 5,10,10, 3, 3,10,10, 0,20,15, 0,0,0,0,0,12,0,0,0,0,0,0,0,0}}, |
|
|
43 | {"wood", {10,11,13, 2, 2,10, 9, 0,20,15, 0,0,0,0,0,12,0,0,0,0,0,0,0,0}}, |
|
|
44 | {"organics", { 3,12, 9,11, 3,10, 9, 0,20,15, 0,0,0,0,0, 0,0,0,0,0,0,0,0,0}}, |
|
|
45 | {"stone", { 2, 5, 2, 2, 2, 2, 1, 0,20,15, 0,0,0,0,0, 5,0,0,0,0,0,0,0,0}}, |
|
|
46 | {"cloth", {14,11,13, 4, 4, 5,10, 0,20,15, 0,0,0,0,0, 5,0,0,0,0,0,0,0,0}}, |
|
|
47 | {"adamant", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0, 0,0,0,0,0,0,0,0,0}}, |
|
|
48 | {"liquid", { 0, 8, 9, 6,17, 0,15, 0,20,15,12,0,0,0,0,11,0,0,0,0,0,0,0,0}}, |
|
|
49 | {"soft metal",{ 6,12, 6,14, 2,10, 1, 0,20,15, 0,0,0,0,0,10,0,0,0,0,0,0,0,0}}, |
|
|
50 | {"bone", {10, 9, 4, 5, 3,10,10, 0,20,15, 0,0,0,0,0, 2,0,0,0,0,0,0,0,0}}, |
|
|
51 | {"ice", {14,11,16, 5, 0, 5, 6, 0,20,15, 0,0,0,0,0, 7,0,0,0,0,0,0,0,0}} |
|
|
52 | }; |
|
|
53 | */ |
|
|
54 | |
|
|
55 | materialtype_t::materialtype_t () |
|
|
56 | { |
|
|
57 | next = 0; |
|
|
58 | reset (); |
|
|
59 | } |
|
|
60 | |
|
|
61 | void |
|
|
62 | materialtype_t::reset () |
|
|
63 | { |
|
|
64 | name = shstr_unknown; |
|
|
65 | description = shstr_unknown_material_description; |
|
|
66 | material = 0; |
|
|
67 | |
|
|
68 | for (int i = 0; i < NROFATTACKS; i++) |
|
|
69 | { |
|
|
70 | save [i] = 0; |
|
|
71 | mod [i] = 0; |
|
|
72 | } |
|
|
73 | |
|
|
74 | chance = 0; |
|
|
75 | difficulty = 0; |
|
|
76 | magic = 0; |
|
|
77 | damage = 0; |
|
|
78 | wc = 0; |
|
|
79 | ac = 0; |
|
|
80 | sp = 0; |
|
|
81 | weight = 100; |
|
|
82 | value = 100; |
|
|
83 | density = 1000; |
|
|
84 | } |
|
|
85 | |
|
|
86 | // create a new material of the given name |
32 | materialtype_t * |
87 | static materialtype_t * |
33 | name_to_material (const shstr_cmp name) |
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) |
34 | { |
107 | { |
35 | for (materialtype_t *mt = materialt; mt; mt = mt->next) |
108 | for (materialtype_t *mt = materialt; mt; mt = mt->next) |
36 | if (name == mt->name) |
109 | if (name == mt->name) |
37 | return mt; |
110 | return mt; |
38 | |
111 | |
39 | return 0; |
112 | return 0; |
40 | } |
113 | } |
41 | |
114 | |
|
|
115 | /* convert materialname to materialtype_t */ |
|
|
116 | materialtype_t * |
|
|
117 | name_to_material (const shstr_tmp name) |
|
|
118 | { |
|
|
119 | materialtype_t *mt = find (name); |
|
|
120 | |
|
|
121 | if (!mt) |
|
|
122 | { |
|
|
123 | LOG (llevError, "name_to_material called with nonexistent material '%s'\n", &name); |
|
|
124 | mt = dummy_material (name); |
|
|
125 | } |
|
|
126 | |
|
|
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 | } |
|
|
143 | } |
|
|
144 | |
42 | /* when doing transmutation of objects, we have to recheck the resistances, |
145 | /* when doing transmutation of objects, we have to recheck the resistances, |
43 | * as some that did not apply previously, may apply now. |
146 | * as some that did not apply previously, may apply now. |
44 | */ |
147 | */ |
45 | void |
148 | void |
46 | transmute_materialname (object *op, const object *change) |
149 | transmute_materialname (object *op, const object *change) |
47 | { |
150 | { |
48 | materialtype_t *mt; |
|
|
49 | int j; |
|
|
50 | |
|
|
51 | if (!op->materialname) |
|
|
52 | return; |
|
|
53 | |
|
|
54 | if (op->materialname != change->materialname) |
|
|
55 | return; |
|
|
56 | |
|
|
57 | if (!op->is_armor ()) |
151 | if (!op->is_armor ()) |
58 | return; |
152 | return; |
59 | |
153 | |
60 | mt = name_to_material (op->materialname); |
154 | if (op->material == MATERIAL_NULL) |
61 | if (!mt) |
|
|
62 | { |
|
|
63 | LOG (llevError, "archetype '%s>%s' uses nonexistent material '%s'\n", &op->arch->archname, &op->name, &op->materialname); |
|
|
64 | return; |
155 | return; |
65 | } |
|
|
66 | |
156 | |
|
|
157 | if (op->material != change->material) |
|
|
158 | return; |
|
|
159 | |
|
|
160 | materialtype_t *mt = op->material; |
|
|
161 | |
67 | for (j = 0; j < NROFATTACKS; j++) |
162 | for (int j = 0; j < NROFATTACKS; j++) |
68 | if (op->resist[j] == 0 && change->resist[j] != 0) |
163 | if (op->resist[j] == 0 && change->resist[j] != 0) |
69 | { |
164 | { |
70 | op->resist[j] += mt->mod[j]; |
165 | op->resist[j] += mt->mod[j]; |
71 | if (op->resist[j] > 100) |
166 | |
72 | op->resist[j] = 100; |
167 | if (op->resist[j] > 100) op->resist[j] = 100; |
73 | if (op->resist[j] < -100) |
168 | if (op->resist[j] < -100) op->resist[j] = -100; |
74 | op->resist[j] = -100; |
|
|
75 | } |
169 | } |
76 | } |
170 | } |
77 | |
171 | |
78 | /* set the materialname and type for an item */ |
172 | /* set the materialname and type for an item */ |
79 | void |
173 | void |
80 | set_materialname (object *op, int difficulty, materialtype_t *nmt) |
174 | select_material (object *op, int difficulty) |
81 | { |
175 | { |
82 | materialtype_t *mt, *lmt; |
176 | if (op->material != MATERIAL_NULL || !op->materials) |
83 | |
|
|
84 | if (!op->materialname) |
|
|
85 | return; |
177 | return; |
86 | |
178 | |
87 | if (nmt) |
179 | materialtype_t *lmt = 0; |
88 | lmt = nmt; |
180 | |
89 | else |
181 | //TODL: dead code? |
|
|
182 | for (materialtype_t *mt = materialt; mt; mt = mt->next) |
|
|
183 | if (op->materials & mt->material |
|
|
184 | && difficulty >= mt->difficulty |
|
|
185 | && rndm (1, 100) <= mt->chance |
|
|
186 | && (op->magic >= mt->magic || mt->magic == 0)) |
90 | { |
187 | { |
91 | lmt = 0; |
|
|
92 | |
|
|
93 | for (mt = materialt; mt; mt = mt->next) |
|
|
94 | if (op->materials & mt->material && rndm (1, 100) <= mt->chance && |
|
|
95 | difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) |
|
|
96 | { |
|
|
97 | lmt = mt; |
188 | lmt = mt; |
|
|
189 | |
98 | if (!(op->is_weapon () || op->is_armor ())) |
190 | if (!(op->is_weapon () || op->is_armor ())) |
99 | break; |
191 | break; |
100 | } |
|
|
101 | } |
192 | } |
102 | |
193 | |
103 | if (lmt) |
194 | if (lmt) |
104 | { |
195 | { |
105 | if (op->stats.dam && op->is_weapon ()) |
196 | if (op->stats.dam && op->is_weapon ()) |
106 | { |
197 | { |
107 | op->stats.dam += lmt->damage; |
198 | op->stats.dam += lmt->damage; |
108 | if (op->stats.dam < 1) |
199 | if (op->stats.dam < 1) |
109 | op->stats.dam = 1; |
200 | op->stats.dam = 1; |
110 | } |
201 | } |
111 | |
202 | |
112 | if (op->stats.sp && op->type == BOW) |
203 | if (op->stats.sp && op->type == BOW ) op->stats.sp += lmt->sp; |
113 | op->stats.sp += lmt->sp; |
|
|
114 | if (op->stats.wc && op->is_weapon ()) |
204 | if (op->stats.wc && op->is_weapon ()) op->stats.wc += lmt->wc; |
115 | op->stats.wc += lmt->wc; |
205 | |
116 | if (op->is_armor ()) |
206 | if (op->is_armor ()) |
117 | { |
207 | { |
118 | if (op->stats.ac) |
208 | if (op->stats.ac) |
119 | op->stats.ac += lmt->ac; |
209 | op->stats.ac += lmt->ac; |
120 | |
210 | |
121 | for (int j = 0; j < NROFATTACKS; j++) |
211 | for (int j = 0; j < NROFATTACKS; j++) |
122 | if (op->resist[j] != 0) |
212 | if (op->resist[j] != 0) |
123 | { |
213 | { |
124 | op->resist[j] += lmt->mod[j]; |
214 | op->resist[j] += lmt->mod[j]; |
125 | if (op->resist[j] > 100) |
215 | if (op->resist[j] > 100) op->resist[j] = 100; |
126 | op->resist[j] = 100; |
|
|
127 | if (op->resist[j] < -100) |
216 | if (op->resist[j] < -100) op->resist[j] = -100; |
128 | op->resist[j] = -100; |
|
|
129 | } |
217 | } |
130 | } |
218 | } |
131 | |
219 | |
132 | op->materialname = lmt->name; |
220 | op->material = lmt; |
|
|
221 | |
133 | /* dont make it unstackable if it doesn't need to be */ |
222 | /* dont make it unstackable if it doesn't need to be */ |
134 | if (op->is_weapon () || op->is_armor ()) |
223 | if (op->is_weapon () || op->is_armor ()) |
135 | { |
224 | { |
136 | op->weight = (op->weight * lmt->weight) / 100; |
225 | op->weight = op->weight * lmt->weight / 100; |
137 | op->value = (op->value * lmt->value) / 100; |
226 | op->value = op->value * lmt->value / 100; |
138 | } |
227 | } |
139 | } |
228 | } |
140 | } |
229 | } |
141 | |
230 | |
142 | //TODO: make this a constructor |
|
|
143 | static materialtype_t * |
|
|
144 | get_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 |
231 | //-GPL |
176 | |
232 | |
177 | void |
233 | void |
178 | load_materials (void) |
234 | reload_materials () |
179 | { |
235 | { |
180 | char filename[MAX_BUF]; |
236 | object_thawer thawer (settings.datadir, "materials"); |
181 | |
|
|
182 | sprintf (filename, "%s/materials", settings.datadir); |
|
|
183 | LOG (llevDebug, "Reading material type data from %s...\n", filename); |
|
|
184 | |
|
|
185 | //TODO: somehow free old materials, or update them in-place |
|
|
186 | materialt = 0; |
|
|
187 | |
|
|
188 | object_thawer thawer (filename); |
|
|
189 | |
237 | |
190 | if (!thawer) |
238 | if (!thawer) |
191 | { |
239 | { |
192 | LOG (llevError, "Cannot open %s for reading\n", filename); |
240 | LOG (llevError, "unable to load %s for reading\n", thawer.name); |
193 | goto done; |
241 | return; |
194 | } |
242 | } |
195 | |
243 | |
196 | while (thawer.kw != KW_name) |
244 | while (thawer.kw != KW_name) |
197 | { |
245 | { |
198 | thawer.next (); |
246 | thawer.next (); |
199 | |
247 | |
200 | if (thawer.kw == KW_EOF) |
248 | if (thawer.kw == KW_EOF) |
201 | goto done; |
249 | return; |
202 | } |
250 | } |
203 | |
251 | |
204 | materialtype_t *mt; |
252 | materialtype_t *mt; |
205 | |
253 | |
206 | for (;;) |
254 | for (;;) |
207 | { |
255 | { |
208 | switch (thawer.kw) |
256 | switch (thawer.kw) |
209 | { |
257 | { |
210 | case KW_name: |
258 | case KW_name: |
211 | mt = get_empty_mat (); |
259 | coroapi::cede_to_tick (); |
212 | mt->next = materialt; |
|
|
213 | materialt = mt; |
|
|
214 | |
260 | |
|
|
261 | { |
|
|
262 | // create a new dummy material, or find the existing material |
|
|
263 | shstr name; |
215 | thawer.get (mt->name); |
264 | thawer.get (name); |
|
|
265 | |
|
|
266 | mt = find (name); |
|
|
267 | |
|
|
268 | if (mt) |
|
|
269 | mt->reset (); |
|
|
270 | else |
|
|
271 | { |
|
|
272 | mt = new materialtype_t; |
|
|
273 | mt->next = materialt; materialt = mt; |
|
|
274 | } |
|
|
275 | |
|
|
276 | mt->name = name; |
216 | mt->description = mt->name; |
277 | mt->description = name; |
|
|
278 | } |
217 | break; |
279 | break; |
218 | |
280 | |
219 | case KW_description: |
281 | case KW_description: |
220 | thawer.get (mt->description); |
282 | thawer.get (mt->description); |
221 | break; |
283 | break; |
… | |
… | |
278 | case KW_weight: thawer.get (mt->weight); break; |
340 | case KW_weight: thawer.get (mt->weight); break; |
279 | case KW_value: thawer.get (mt->value); break; |
341 | case KW_value: thawer.get (mt->value); break; |
280 | case KW_density: thawer.get (mt->density); break; |
342 | case KW_density: thawer.get (mt->density); break; |
281 | |
343 | |
282 | case KW_EOF: |
344 | case KW_EOF: |
283 | goto done; |
345 | return; |
284 | |
346 | |
285 | default: |
347 | default: |
286 | if (!thawer.parse_error ("materials file", "materials")) |
348 | if (!thawer.parse_error ("materials file")) |
287 | goto done; |
349 | return; |
288 | break; |
350 | break; |
289 | } |
351 | } |
290 | |
352 | |
291 | thawer.next (); |
353 | thawer.next (); |
292 | } |
354 | } |
293 | |
|
|
294 | done: |
|
|
295 | if (!materialt) |
|
|
296 | materialt = get_empty_mat (); |
|
|
297 | |
|
|
298 | LOG (llevDebug, "Done.\n"); |
|
|
299 | } |
355 | } |
300 | |
356 | |