… | |
… | |
24 | |
24 | |
25 | #include <global.h> |
25 | #include <global.h> |
26 | #include <material.h> |
26 | #include <material.h> |
27 | #include <loader.h> |
27 | #include <loader.h> |
28 | #include <sproto.h> |
28 | #include <sproto.h> |
29 | |
|
|
30 | //TODO: make this a constructor |
|
|
31 | static materialtype_t * |
|
|
32 | get_empty_mat (void) |
|
|
33 | { |
|
|
34 | materialtype_t *mt; |
|
|
35 | int i; |
|
|
36 | |
|
|
37 | mt = new materialtype_t; |
|
|
38 | |
|
|
39 | mt->name = shstr_unknown; |
|
|
40 | mt->description = 0; |
|
|
41 | |
|
|
42 | for (i = 0; i < NROFATTACKS; i++) |
|
|
43 | { |
|
|
44 | mt->save[i] = 0; |
|
|
45 | mt->mod[i] = 0; |
|
|
46 | } |
|
|
47 | |
|
|
48 | mt->chance = 0; |
|
|
49 | mt->difficulty = 0; |
|
|
50 | mt->magic = 0; |
|
|
51 | mt->damage = 0; |
|
|
52 | mt->wc = 0; |
|
|
53 | mt->ac = 0; |
|
|
54 | mt->sp = 0; |
|
|
55 | mt->weight = 100; |
|
|
56 | mt->value = 100; |
|
|
57 | mt->density = 1; |
|
|
58 | mt->next = 0; |
|
|
59 | |
|
|
60 | return mt; |
|
|
61 | } |
|
|
62 | |
|
|
63 | void |
|
|
64 | load_materials (void) |
|
|
65 | { |
|
|
66 | char filename[MAX_BUF]; |
|
|
67 | |
|
|
68 | sprintf (filename, "%s/materials", settings.datadir); |
|
|
69 | LOG (llevDebug, "Reading material type data from %s...\n", filename); |
|
|
70 | |
|
|
71 | //TODO: somehow free old materials, or update them in-place |
|
|
72 | materialt = 0; |
|
|
73 | |
|
|
74 | object_thawer thawer (filename); |
|
|
75 | |
|
|
76 | if (!thawer) |
|
|
77 | { |
|
|
78 | LOG (llevError, "Cannot open %s for reading\n", filename); |
|
|
79 | goto done; |
|
|
80 | } |
|
|
81 | |
|
|
82 | while (thawer.kw != KW_name) |
|
|
83 | { |
|
|
84 | thawer.next (); |
|
|
85 | |
|
|
86 | if (thawer.kw == KW_EOF) |
|
|
87 | goto done; |
|
|
88 | } |
|
|
89 | |
|
|
90 | materialtype_t *mt; |
|
|
91 | |
|
|
92 | for (;;) |
|
|
93 | { |
|
|
94 | switch (thawer.kw) |
|
|
95 | { |
|
|
96 | case KW_name: |
|
|
97 | mt = get_empty_mat (); |
|
|
98 | mt->next = materialt; |
|
|
99 | materialt = mt; |
|
|
100 | |
|
|
101 | thawer.get (mt->name); |
|
|
102 | mt->description = mt->name; |
|
|
103 | break; |
|
|
104 | |
|
|
105 | case KW_description: |
|
|
106 | thawer.get (mt->description); |
|
|
107 | break; |
|
|
108 | |
|
|
109 | case KW_material: |
|
|
110 | thawer.get (mt->material); |
|
|
111 | break; |
|
|
112 | |
|
|
113 | case KW_saves: |
|
|
114 | { |
|
|
115 | const char *cp = thawer.get_str () - 1; |
|
|
116 | |
|
|
117 | for (int i = 0; i < NROFATTACKS; i++) |
|
|
118 | { |
|
|
119 | if (!cp) |
|
|
120 | { |
|
|
121 | mt->save[i] = 0; |
|
|
122 | continue; |
|
|
123 | } |
|
|
124 | |
|
|
125 | int value; |
|
|
126 | ++cp; |
|
|
127 | sscanf (cp, "%d", &value); |
|
|
128 | mt->save[i] = (sint8) value; |
|
|
129 | cp = strchr (cp, ','); |
|
|
130 | } |
|
|
131 | } |
|
|
132 | break; |
|
|
133 | |
|
|
134 | case KW_mods: |
|
|
135 | { |
|
|
136 | const char *cp = thawer.get_str () - 1; |
|
|
137 | |
|
|
138 | for (int i = 0; i < NROFATTACKS; i++) |
|
|
139 | { |
|
|
140 | if (!cp) |
|
|
141 | { |
|
|
142 | mt->save[i] = 0; |
|
|
143 | continue; |
|
|
144 | } |
|
|
145 | |
|
|
146 | ++cp; |
|
|
147 | int value; |
|
|
148 | sscanf (cp, "%d", &value); |
|
|
149 | mt->mod[i] = (sint8) value; |
|
|
150 | cp = strchr (cp, ','); |
|
|
151 | } |
|
|
152 | } |
|
|
153 | break; |
|
|
154 | |
|
|
155 | case KW_chance: thawer.get (mt->chance); break; |
|
|
156 | case KW_difficulty: // cf+ alias, not original cf |
|
|
157 | case KW_diff: thawer.get (mt->difficulty); break; |
|
|
158 | case KW_magic: thawer.get (mt->magic); break; |
|
|
159 | case KW_dam: // cf+ alias, not original cf |
|
|
160 | case KW_damage: thawer.get (mt->damage); break; |
|
|
161 | case KW_wc: thawer.get (mt->wc); break; |
|
|
162 | case KW_ac: thawer.get (mt->ac); break; |
|
|
163 | case KW_sp: thawer.get (mt->sp); break; |
|
|
164 | case KW_weight: thawer.get (mt->weight); break; |
|
|
165 | case KW_value: thawer.get (mt->value); break; |
|
|
166 | case KW_density: thawer.get (mt->density); break; |
|
|
167 | |
|
|
168 | case KW_EOF: |
|
|
169 | goto done; |
|
|
170 | |
|
|
171 | default: |
|
|
172 | if (!thawer.parse_error ("materials file", "materials")) |
|
|
173 | goto done; |
|
|
174 | break; |
|
|
175 | } |
|
|
176 | |
|
|
177 | thawer.next (); |
|
|
178 | } |
|
|
179 | |
|
|
180 | done: |
|
|
181 | if (!materialt) |
|
|
182 | materialt = get_empty_mat (); |
|
|
183 | |
|
|
184 | LOG (llevDebug, "Done.\n"); |
|
|
185 | } |
|
|
186 | |
29 | |
187 | /* This loads the settings file. There could be debate whether this should |
30 | /* This loads the settings file. There could be debate whether this should |
188 | * be here or in the common directory - but since only the server needs this |
31 | * be here or in the common directory - but since only the server needs this |
189 | * information, having it here probably makes more sense. |
32 | * information, having it here probably makes more sense. |
190 | */ |
33 | */ |