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.2 by root, Wed Nov 4 00:08:44 2009 UTC vs.
Revision 1.14 by root, Thu Apr 29 12:24:04 2010 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 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008,2009,2010 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.
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] = 0;
65 mod [i] = 0;
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
80// create a new material of the given name
81static materialtype_t *
82dummy_material (shstr_tmp name)
83{
84 materialtype_t *mt = new materialtype_t;
85 mt->name = name;
86
87 // make it susceptible to attacks
88 for (int i = 0; i < NROFATTACKS; i++)
89 {
90 mt->save [i] = 10;
91 mt->mod [i] = 9;
92 }
93
94 mt->next = materialt; materialt = mt;
95
96 return mt;
97}
98
28/* convert materialname to materialtype_t */ 99/* convert materialname to materialtype_t */
29
30materialtype_t * 100materialtype_t *
31name_to_material (const shstr_cmp name) 101name_to_material (const shstr_tmp name)
32{ 102{
33 for (materialtype_t *mt = materialt; mt; mt = mt->next) 103 for (materialtype_t *mt = materialt; mt; mt = mt->next)
34 if (name == mt->name) 104 if (name == mt->name)
35 return mt; 105 return mt;
36 106
37 return 0; 107 LOG (llevError, "name_to_material called with nonexistent material '%s'\n", &name);
108
109 return dummy_material (name);
110}
111
112void
113object_thawer::get (materialtype_t *&m) const
114{
115 shstr name;
116 get (name);
117
118 for (materialtype_t *mt = materialt; mt; mt = mt->next)
119 if (name == mt->name)
120 {
121 m = mt;
122 return;
123 }
124
125 parse_error (format ("material called %s requested, but not found, creating dummy material.\n", &name));
126
127 m = dummy_material (name);
38} 128}
39 129
40/* when doing transmutation of objects, we have to recheck the resistances, 130/* when doing transmutation of objects, we have to recheck the resistances,
41 * as some that did not apply previously, may apply now. 131 * as some that did not apply previously, may apply now.
42 */ 132 */
43void 133void
44transmute_materialname (object *op, const object *change) 134transmute_materialname (object *op, const object *change)
45{ 135{
46 materialtype_t *mt;
47 int j;
48
49 if (!op->materialname)
50 return;
51
52 if (op->materialname != change->materialname)
53 return;
54
55 if (!op->is_armor ()) 136 if (!op->is_armor ())
56 return; 137 return;
57 138
58 mt = name_to_material (op->materialname); 139 if (op->material == MATERIAL_NULL)
59 if (!mt)
60 {
61 LOG (llevError, "archetype '%s>%s' uses nonexistent material '%s'\n", &op->arch->archname, &op->name, &op->materialname);
62 return; 140 return;
63 }
64 141
142 if (op->material != change->material)
143 return;
144
145 materialtype_t *mt = op->material;
146
65 for (j = 0; j < NROFATTACKS; j++) 147 for (int j = 0; j < NROFATTACKS; j++)
66 if (op->resist[j] == 0 && change->resist[j] != 0) 148 if (op->resist[j] == 0 && change->resist[j] != 0)
67 { 149 {
68 op->resist[j] += mt->mod[j]; 150 op->resist[j] += mt->mod[j];
69 if (op->resist[j] > 100) 151
70 op->resist[j] = 100; 152 if (op->resist[j] > 100) op->resist[j] = 100;
71 if (op->resist[j] < -100) 153 if (op->resist[j] < -100) op->resist[j] = -100;
72 op->resist[j] = -100;
73 } 154 }
74} 155}
75 156
76/* set the materialname and type for an item */ 157/* set the materialname and type for an item */
77void 158void
78set_materialname (object *op, int difficulty, materialtype_t *nmt) 159select_material (object *op, int difficulty)
79{ 160{
80 materialtype_t *mt, *lmt; 161 if (op->material != MATERIAL_NULL || !op->materials)
81
82 if (!op->materialname)
83 return; 162 return;
84 163
85 if (nmt) 164 materialtype_t *lmt = 0;
86 lmt = nmt; 165
87 else 166 //TODL: dead code?
167 for (materialtype_t *mt = materialt; mt; mt = mt->next)
168 if (op->materials & mt->material
169 && difficulty >= mt->difficulty
170 && rndm (1, 100) <= mt->chance
171 && (op->magic >= mt->magic || mt->magic == 0))
88 { 172 {
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; 173 lmt = mt;
174
96 if (!(op->is_weapon () || op->is_armor ())) 175 if (!(op->is_weapon () || op->is_armor ()))
97 break; 176 break;
98 }
99 } 177 }
100 178
101 if (lmt) 179 if (lmt)
102 { 180 {
103 if (op->stats.dam && op->is_weapon ()) 181 if (op->stats.dam && op->is_weapon ())
104 { 182 {
105 op->stats.dam += lmt->damage; 183 op->stats.dam += lmt->damage;
106 if (op->stats.dam < 1) 184 if (op->stats.dam < 1)
107 op->stats.dam = 1; 185 op->stats.dam = 1;
108 } 186 }
109 187
110 if (op->stats.sp && op->type == BOW) 188 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 ()) 189 if (op->stats.wc && op->is_weapon ()) op->stats.wc += lmt->wc;
113 op->stats.wc += lmt->wc; 190
114 if (op->is_armor ()) 191 if (op->is_armor ())
115 { 192 {
116 if (op->stats.ac) 193 if (op->stats.ac)
117 op->stats.ac += lmt->ac; 194 op->stats.ac += lmt->ac;
118 195
119 for (int j = 0; j < NROFATTACKS; j++) 196 for (int j = 0; j < NROFATTACKS; j++)
120 if (op->resist[j] != 0) 197 if (op->resist[j] != 0)
121 { 198 {
122 op->resist[j] += lmt->mod[j]; 199 op->resist[j] += lmt->mod[j];
123 if (op->resist[j] > 100) 200 if (op->resist[j] > 100) op->resist[j] = 100;
124 op->resist[j] = 100;
125 if (op->resist[j] < -100) 201 if (op->resist[j] < -100) op->resist[j] = -100;
126 op->resist[j] = -100;
127 } 202 }
128 } 203 }
129 204
130 op->materialname = lmt->name; 205 op->material = lmt;
206
131 /* dont make it unstackable if it doesn't need to be */ 207 /* dont make it unstackable if it doesn't need to be */
132 if (op->is_weapon () || op->is_armor ()) 208 if (op->is_weapon () || op->is_armor ())
133 { 209 {
134 op->weight = (op->weight * lmt->weight) / 100; 210 op->weight = op->weight * lmt->weight / 100;
135 op->value = (op->value * lmt->value) / 100; 211 op->value = op->value * lmt->value / 100;
136 } 212 }
137 } 213 }
138} 214}
139 215
140//TODO: make this a constructor 216//-GPL
141static materialtype_t *
142get_empty_mat (void)
143{
144 materialtype_t *mt;
145 int i;
146
147 mt = new materialtype_t;
148
149 mt->name = shstr_unknown;
150 mt->description = 0;
151
152 for (i = 0; i < NROFATTACKS; i++)
153 {
154 mt->save[i] = 0;
155 mt->mod[i] = 0;
156 }
157
158 mt->chance = 0;
159 mt->difficulty = 0;
160 mt->magic = 0;
161 mt->damage = 0;
162 mt->wc = 0;
163 mt->ac = 0;
164 mt->sp = 0;
165 mt->weight = 100;
166 mt->value = 100;
167 mt->density = 1;
168 mt->next = 0;
169
170 return mt;
171}
172 217
173void 218void
174load_materials (void) 219load_materials ()
175{ 220{
176 char filename[MAX_BUF];
177
178 sprintf (filename, "%s/materials", settings.datadir);
179 LOG (llevDebug, "Reading material type data from %s...\n", filename);
180
181 //TODO: somehow free old materials, or update them in-place 221 //TODO: somehow free old materials, or update them in-place
182 materialt = 0; 222 // currently we effectively leak them.
223 material_null.next = 0; materialt = &material_null;
183 224
184 object_thawer thawer (filename); 225 object_thawer thawer (settings.datadir, "materials");
185 226
186 if (!thawer) 227 if (!thawer)
187 { 228 {
188 LOG (llevError, "Cannot open %s for reading\n", filename); 229 LOG (llevError, "Cannot open %s for reading\n", thawer.name);
189 goto done; 230 goto done;
190 } 231 }
191 232
192 while (thawer.kw != KW_name) 233 while (thawer.kw != KW_name)
193 { 234 {
202 for (;;) 243 for (;;)
203 { 244 {
204 switch (thawer.kw) 245 switch (thawer.kw)
205 { 246 {
206 case KW_name: 247 case KW_name:
207 mt = get_empty_mat ();
208 mt->next = materialt; 248 mt = new materialtype_t;
209 materialt = mt;
210
211 thawer.get (mt->name); 249 thawer.get (mt->name);
212 mt->description = mt->name; 250 mt->description = mt->name;
251
252 mt->next = materialt; materialt = mt;
213 break; 253 break;
214 254
215 case KW_description: 255 case KW_description:
216 thawer.get (mt->description); 256 thawer.get (mt->description);
217 break; 257 break;
277 317
278 case KW_EOF: 318 case KW_EOF:
279 goto done; 319 goto done;
280 320
281 default: 321 default:
282 if (!thawer.parse_error ("materials file", "materials")) 322 if (!thawer.parse_error ("materials file"))
283 goto done; 323 goto done;
284 break; 324 break;
285 } 325 }
286 326
287 thawer.next (); 327 thawer.next ();
288 } 328 }
289 329
290done: 330done:
291 if (!materialt)
292 materialt = get_empty_mat ();
293
294 LOG (llevDebug, "Done.\n"); 331 LOG (llevDebug, "Done.\n");
295} 332}
296 333

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines