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.4 by root, Tue Nov 10 00:01:31 2009 UTC vs.
Revision 1.12 by root, Thu Apr 15 21:49:15 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.
25#include <global.h> 25#include <global.h>
26#include <material.h> 26#include <material.h>
27 27
28//+GPL 28//+GPL
29 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
30/* convert materialname to materialtype_t */ 80/* convert materialname to materialtype_t */
31
32materialtype_t * 81materialtype_t *
33name_to_material (const shstr_cmp name) 82name_to_material (const shstr_tmp name)
34{ 83{
35 for (materialtype_t *mt = materialt; mt; mt = mt->next) 84 for (materialtype_t *mt = materialt; mt; mt = mt->next)
36 if (name == mt->name) 85 if (name == mt->name)
37 return mt; 86 return mt;
38 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 // 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
39 return 0; 103 return mt;
40} 104}
41 105
42/* when doing transmutation of objects, we have to recheck the resistances, 106/* when doing transmutation of objects, we have to recheck the resistances,
43 * as some that did not apply previously, may apply now. 107 * as some that did not apply previously, may apply now.
44 */ 108 */
45void 109void
46transmute_materialname (object *op, const object *change) 110transmute_materialname (object *op, const object *change)
47{ 111{
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 ()) 112 if (!op->is_armor ())
58 return; 113 return;
59 114
60 mt = name_to_material (op->materialname); 115 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; 116 return;
65 }
66 117
118 if (op->material != change->material)
119 return;
120
121 materialtype_t *mt = op->material;
122
67 for (j = 0; j < NROFATTACKS; j++) 123 for (int j = 0; j < NROFATTACKS; j++)
68 if (op->resist[j] == 0 && change->resist[j] != 0) 124 if (op->resist[j] == 0 && change->resist[j] != 0)
69 { 125 {
70 op->resist[j] += mt->mod[j]; 126 op->resist[j] += mt->mod[j];
71 if (op->resist[j] > 100) 127
72 op->resist[j] = 100; 128 if (op->resist[j] > 100) op->resist[j] = 100;
73 if (op->resist[j] < -100) 129 if (op->resist[j] < -100) op->resist[j] = -100;
74 op->resist[j] = -100;
75 } 130 }
76} 131}
77 132
78/* set the materialname and type for an item */ 133/* set the materialname and type for an item */
79void 134void
80set_materialname (object *op, int difficulty, materialtype_t *nmt) 135select_material (object *op, int difficulty)
81{ 136{
82 materialtype_t *mt, *lmt; 137 if (op->material != MATERIAL_NULL || !op->materials)
83
84 if (!op->materialname)
85 return; 138 return;
86 139
87 if (nmt) 140 materialtype_t *lmt = 0;
88 lmt = nmt; 141
89 else 142 //TODL: dead code?
143 for (materialtype_t *mt = materialt; mt; mt = mt->next)
144 if (op->materials & mt->material
145 && difficulty >= mt->difficulty
146 && rndm (1, 100) <= mt->chance
147 && (op->magic >= mt->magic || mt->magic == 0))
90 { 148 {
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; 149 lmt = mt;
150
98 if (!(op->is_weapon () || op->is_armor ())) 151 if (!(op->is_weapon () || op->is_armor ()))
99 break; 152 break;
100 }
101 } 153 }
102 154
103 if (lmt) 155 if (lmt)
104 { 156 {
105 if (op->stats.dam && op->is_weapon ()) 157 if (op->stats.dam && op->is_weapon ())
106 { 158 {
107 op->stats.dam += lmt->damage; 159 op->stats.dam += lmt->damage;
108 if (op->stats.dam < 1) 160 if (op->stats.dam < 1)
109 op->stats.dam = 1; 161 op->stats.dam = 1;
110 } 162 }
111 163
112 if (op->stats.sp && op->type == BOW) 164 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 ()) 165 if (op->stats.wc && op->is_weapon ()) op->stats.wc += lmt->wc;
115 op->stats.wc += lmt->wc; 166
116 if (op->is_armor ()) 167 if (op->is_armor ())
117 { 168 {
118 if (op->stats.ac) 169 if (op->stats.ac)
119 op->stats.ac += lmt->ac; 170 op->stats.ac += lmt->ac;
120 171
121 for (int j = 0; j < NROFATTACKS; j++) 172 for (int j = 0; j < NROFATTACKS; j++)
122 if (op->resist[j] != 0) 173 if (op->resist[j] != 0)
123 { 174 {
124 op->resist[j] += lmt->mod[j]; 175 op->resist[j] += lmt->mod[j];
125 if (op->resist[j] > 100) 176 if (op->resist[j] > 100) op->resist[j] = 100;
126 op->resist[j] = 100;
127 if (op->resist[j] < -100) 177 if (op->resist[j] < -100) op->resist[j] = -100;
128 op->resist[j] = -100;
129 } 178 }
130 } 179 }
131 180
132 op->materialname = lmt->name; 181 op->material = lmt;
182
133 /* dont make it unstackable if it doesn't need to be */ 183 /* dont make it unstackable if it doesn't need to be */
134 if (op->is_weapon () || op->is_armor ()) 184 if (op->is_weapon () || op->is_armor ())
135 { 185 {
136 op->weight = (op->weight * lmt->weight) / 100; 186 op->weight = op->weight * lmt->weight / 100;
137 op->value = (op->value * lmt->value) / 100; 187 op->value = op->value * lmt->value / 100;
138 } 188 }
139 } 189 }
140} 190}
141 191
142//TODO: make this a constructor
143static materialtype_t *
144get_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 192//-GPL
176 193
177const materialtype_t *
178object::dominant_material () const
179{
180 if (materialtype_t *mt = name_to_material (materialname))
181 return mt;
182
183 return name_to_material (shstr_unknown);
184}
185
186void 194void
187load_materials (void) 195load_materials ()
188{ 196{
189 char filename[MAX_BUF]; 197 char filename[MAX_BUF];
190 198
191 sprintf (filename, "%s/materials", settings.datadir); 199 sprintf (filename, "%s/materials", settings.datadir);
192 LOG (llevDebug, "Reading material type data from %s...\n", filename); 200 LOG (llevDebug, "Reading material type data from %s...\n", filename);
193 201
194 //TODO: somehow free old materials, or update them in-place 202 //TODO: somehow free old materials, or update them in-place
195 materialt = 0; 203 // currently we effectively leak them.
204 material_null.next = 0; materialt = &material_null;
196 205
197 object_thawer thawer (filename); 206 object_thawer thawer (filename);
198 207
199 if (!thawer) 208 if (!thawer)
200 { 209 {
215 for (;;) 224 for (;;)
216 { 225 {
217 switch (thawer.kw) 226 switch (thawer.kw)
218 { 227 {
219 case KW_name: 228 case KW_name:
220 mt = get_empty_mat ();
221 mt->next = materialt; 229 mt = new materialtype_t;
222 materialt = mt;
223
224 thawer.get (mt->name); 230 thawer.get (mt->name);
225 mt->description = mt->name; 231 mt->description = mt->name;
232
233 mt->next = materialt; materialt = mt;
226 break; 234 break;
227 235
228 case KW_description: 236 case KW_description:
229 thawer.get (mt->description); 237 thawer.get (mt->description);
230 break; 238 break;
290 298
291 case KW_EOF: 299 case KW_EOF:
292 goto done; 300 goto done;
293 301
294 default: 302 default:
295 if (!thawer.parse_error ("materials file", "materials")) 303 if (!thawer.parse_error ("materials file"))
296 goto done; 304 goto done;
297 break; 305 break;
298 } 306 }
299 307
300 thawer.next (); 308 thawer.next ();
301 } 309 }
302 310
303done: 311done:
304 if (!materialt)
305 materialt = get_empty_mat ();
306
307 LOG (llevDebug, "Done.\n"); 312 LOG (llevDebug, "Done.\n");
308} 313}
309 314

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines