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.1 by root, Wed Nov 4 00:02:48 2009 UTC vs.
Revision 1.5 by root, Tue Nov 10 04:38:45 2009 UTC

23 */ 23 */
24 24
25#include <global.h> 25#include <global.h>
26#include <material.h> 26#include <material.h>
27 27
28//+GPL
29
30materialtype_t *materialt;
31
32/*
33materialtype 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
55materialtype_t::materialtype_t ()
56{
57 next = 0;
58 name = shstr_unknown;
59 description = shstr_unknown_material_description;
60 material = 0;
61
62 for (int i = 0; i < NROFATTACKS; i++)
63 {
64 save [i] = 10;
65 mod [i] = 9;
66 }
67
68 chance = 0;
69 difficulty = 0;
70 magic = 0;
71 damage = 0;
72 wc = 0;
73 ac = 0;
74 sp = 0;
75 weight = 100;
76 value = 100;
77 density = 1000;
78}
79
28/* convert materialname to materialtype_t */ 80/* convert materialname to materialtype_t */
29
30materialtype_t * 81materialtype_t *
31name_to_material (const shstr_cmp name) 82name_to_material (const shstr_tmp name)
32{ 83{
33 for (materialtype_t *mt = materialt; mt; mt = mt->next) 84 for (materialtype_t *mt = materialt; mt; mt = mt->next)
34 if (name == mt->name) 85 if (name == mt->name)
35 return mt; 86 return mt;
36 87
88 LOG (llevError, "name_to_material called with nonexistent material '%s'\n", &name);
89
90 // create a new material of the given name
91 materialtype_t *mt = new materialtype_t;
92 mt->name = name;
93
94 mt->next = materialt;
95 materialt = mt;
96
37 return 0; 97 return mt;
38} 98}
39 99
40/* when doing transmutation of objects, we have to recheck the resistances, 100/* when doing transmutation of objects, we have to recheck the resistances,
41 * as some that did not apply previously, may apply now. 101 * as some that did not apply previously, may apply now.
42 */ 102 */
43void 103void
44transmute_materialname (object *op, const object *change) 104transmute_materialname (object *op, const object *change)
45{ 105{
46 materialtype_t *mt;
47 int j; 106 int j;
48 107
49 if (!op->materialname)
50 return;
51
52 if (op->materialname != change->materialname) 108 if (op->material != change->material)
53 return; 109 return;
54 110
55 if (!op->is_armor ()) 111 if (!op->is_armor ())
56 return; 112 return;
57 113
58 mt = name_to_material (op->materialname); 114 materialtype_t *mt = op->material;
59 if (!mt)
60 {
61 LOG (llevError, "archetype '%s>%s' uses nonexistent material '%s'\n", &op->arch->archname, &op->name, &op->materialname);
62 return;
63 }
64 115
65 for (j = 0; j < NROFATTACKS; j++) 116 for (j = 0; j < NROFATTACKS; j++)
66 if (op->resist[j] == 0 && change->resist[j] != 0) 117 if (op->resist[j] == 0 && change->resist[j] != 0)
67 { 118 {
68 op->resist[j] += mt->mod[j]; 119 op->resist[j] += mt->mod[j];
69 if (op->resist[j] > 100) 120
70 op->resist[j] = 100; 121 if (op->resist[j] > 100) op->resist[j] = 100;
71 if (op->resist[j] < -100) 122 if (op->resist[j] < -100) op->resist[j] = -100;
72 op->resist[j] = -100;
73 } 123 }
74} 124}
75 125
76/* set the materialname and type for an item */ 126/* set the materialname and type for an item */
77void 127void
78set_materialname (object *op, int difficulty, materialtype_t *nmt) 128select_material (object *op, int difficulty)
79{ 129{
80 materialtype_t *mt, *lmt; 130 if (op->material != &material_null || !op->materials)
81
82 if (!op->materialname)
83 return; 131 return;
84 132
85 if (nmt) 133 materialtype_t *lmt = 0;
86 lmt = nmt; 134
87 else 135 //TODL: dead code?
136 for (materialtype_t *mt = materialt; mt; mt = mt->next)
137 if (op->materials & mt->material
138 && difficulty >= mt->difficulty
139 && rndm (1, 100) <= mt->chance
140 && (op->magic >= mt->magic || mt->magic == 0))
88 { 141 {
89 lmt = 0;
90
91 for (mt = materialt; mt; mt = mt->next)
92 if (op->materials & mt->material && rndm (1, 100) <= mt->chance &&
93 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0))
94 {
95 lmt = mt; 142 lmt = mt;
143
96 if (!(op->is_weapon () || op->is_armor ())) 144 if (!(op->is_weapon () || op->is_armor ()))
97 break; 145 break;
98 }
99 } 146 }
100 147
101 if (lmt) 148 if (lmt)
102 { 149 {
103 if (op->stats.dam && op->is_weapon ()) 150 if (op->stats.dam && op->is_weapon ())
104 { 151 {
105 op->stats.dam += lmt->damage; 152 op->stats.dam += lmt->damage;
106 if (op->stats.dam < 1) 153 if (op->stats.dam < 1)
107 op->stats.dam = 1; 154 op->stats.dam = 1;
108 } 155 }
109 156
110 if (op->stats.sp && op->type == BOW) 157 if (op->stats.sp && op->type == BOW ) op->stats.sp += lmt->sp;
111 op->stats.sp += lmt->sp;
112 if (op->stats.wc && op->is_weapon ()) 158 if (op->stats.wc && op->is_weapon ()) op->stats.wc += lmt->wc;
113 op->stats.wc += lmt->wc; 159
114 if (op->is_armor ()) 160 if (op->is_armor ())
115 { 161 {
116 if (op->stats.ac) 162 if (op->stats.ac)
117 op->stats.ac += lmt->ac; 163 op->stats.ac += lmt->ac;
118 164
119 for (int j = 0; j < NROFATTACKS; j++) 165 for (int j = 0; j < NROFATTACKS; j++)
120 if (op->resist[j] != 0) 166 if (op->resist[j] != 0)
121 { 167 {
122 op->resist[j] += lmt->mod[j]; 168 op->resist[j] += lmt->mod[j];
123 if (op->resist[j] > 100) 169 if (op->resist[j] > 100) op->resist[j] = 100;
124 op->resist[j] = 100;
125 if (op->resist[j] < -100) 170 if (op->resist[j] < -100) op->resist[j] = -100;
126 op->resist[j] = -100;
127 } 171 }
128 } 172 }
129 173
130 op->materialname = lmt->name; 174 op->material = lmt;
175
131 /* dont make it unstackable if it doesn't need to be */ 176 /* dont make it unstackable if it doesn't need to be */
132 if (op->is_weapon () || op->is_armor ()) 177 if (op->is_weapon () || op->is_armor ())
133 { 178 {
134 op->weight = (op->weight * lmt->weight) / 100; 179 op->weight = op->weight * lmt->weight / 100;
135 op->value = (op->value * lmt->value) / 100; 180 op->value = op->value * lmt->value / 100;
136 } 181 }
137 } 182 }
138} 183}
139 184
185//-GPL
186
187void
188load_materials (void)
189{
190 char filename[MAX_BUF];
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
201 if (!thawer)
202 {
203 LOG (llevError, "Cannot open %s for reading\n", filename);
204 goto done;
205 }
206
207 while (thawer.kw != KW_name)
208 {
209 thawer.next ();
210
211 if (thawer.kw == KW_EOF)
212 goto done;
213 }
214
215 materialtype_t *mt;
216
217 for (;;)
218 {
219 switch (thawer.kw)
220 {
221 case KW_name:
222 mt = new materialtype_t;
223 thawer.get (mt->name);
224 mt->description = mt->name;
225
226 mt->next = materialt;
227 materialt = mt;
228 break;
229
230 case KW_description:
231 thawer.get (mt->description);
232 break;
233
234 case KW_material:
235 thawer.get (mt->material);
236 break;
237
238 case KW_saves:
239 {
240 const char *cp = thawer.get_str () - 1;
241
242 for (int i = 0; i < NROFATTACKS; i++)
243 {
244 if (!cp)
245 {
246 mt->save[i] = 0;
247 continue;
248 }
249
250 int value;
251 ++cp;
252 sscanf (cp, "%d", &value);
253 mt->save[i] = (sint8) value;
254 cp = strchr (cp, ',');
255 }
256 }
257 break;
258
259 case KW_mods:
260 {
261 const char *cp = thawer.get_str () - 1;
262
263 for (int i = 0; i < NROFATTACKS; i++)
264 {
265 if (!cp)
266 {
267 mt->save[i] = 0;
268 continue;
269 }
270
271 ++cp;
272 int value;
273 sscanf (cp, "%d", &value);
274 mt->mod[i] = (sint8) value;
275 cp = strchr (cp, ',');
276 }
277 }
278 break;
279
280 case KW_chance: thawer.get (mt->chance); break;
281 case KW_difficulty: // cf+ alias, not original cf
282 case KW_diff: thawer.get (mt->difficulty); break;
283 case KW_magic: thawer.get (mt->magic); break;
284 case KW_dam: // cf+ alias, not original cf
285 case KW_damage: thawer.get (mt->damage); break;
286 case KW_wc: thawer.get (mt->wc); break;
287 case KW_ac: thawer.get (mt->ac); break;
288 case KW_sp: thawer.get (mt->sp); break;
289 case KW_weight: thawer.get (mt->weight); break;
290 case KW_value: thawer.get (mt->value); break;
291 case KW_density: thawer.get (mt->density); break;
292
293 case KW_EOF:
294 goto done;
295
296 default:
297 if (!thawer.parse_error ("materials file", "materials"))
298 goto done;
299 break;
300 }
301
302 thawer.next ();
303 }
304
305done:
306 if (!materialt)
307 materialt = new materialtype_t;
308
309 LOG (llevDebug, "Done.\n");
310}
311

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines