parse construction requirements for items.

This commit is contained in:
Enno Rehling 2018-04-28 21:12:28 +02:00
parent 9d3385de97
commit 9dbfaea708

View file

@ -235,7 +235,11 @@ static void handle_weapon(userdata *ud, const XML_Char *el, const XML_Char **att
wtype->flags = flags; wtype->flags = flags;
} }
static void XMLCALL handle_resources(userdata *ud, const XML_Char *el, const XML_Char **attr) { #define MAX_REQUIREMENTS 8
static requirement reqs[MAX_REQUIREMENTS];
static int nreqs;
static void XMLCALL start_resources(userdata *ud, const XML_Char *el, const XML_Char **attr) {
resource_type *rtype = (resource_type *)ud->object; resource_type *rtype = (resource_type *)ud->object;
if (xml_strcmp(el, "resource") == 0) { if (xml_strcmp(el, "resource") == 0) {
handle_resource(ud, el, attr); handle_resource(ud, el, attr);
@ -273,11 +277,23 @@ static void XMLCALL handle_resources(userdata *ud, const XML_Char *el, const XML
} }
} }
itype->construction = con; itype->construction = con;
nreqs = 0;
} }
else if (xml_strcmp(el, "requirement") == 0) { else if (xml_strcmp(el, "requirement") == 0) {
requirement *req;
int i;
assert(itype->construction); assert(itype->construction);
/* TODO */ assert(nreqs < MAX_REQUIREMENTS);
++ud->errors; req = reqs + nreqs;
for (i = 0; attr[i]; i += 2) {
if (xml_strcmp(attr[i], "type") == 0) {
req->rtype = rt_get_or_create((const char *)attr[i + 1]);
}
else if (xml_strcmp(attr[i], "quantity") == 0) {
req->number = xml_int(attr[i + 1]);
}
}
++nreqs;
} }
else if (xml_strcmp(el, "luxury") == 0) { else if (xml_strcmp(el, "luxury") == 0) {
rtype->ltype = new_luxurytype(itype, 0); rtype->ltype = new_luxurytype(itype, 0);
@ -359,7 +375,7 @@ static void XMLCALL handle_start(void *data, const XML_Char *el, const XML_Char
else { else {
switch (ud->type) { switch (ud->type) {
case EXP_RESOURCES: case EXP_RESOURCES:
handle_resources(ud, el, attr); start_resources(ud, el, attr);
break; break;
default: default:
/* not implemented */ /* not implemented */
@ -369,14 +385,32 @@ static void XMLCALL handle_start(void *data, const XML_Char *el, const XML_Char
++ud->depth; ++ud->depth;
} }
static void end_resources(userdata *ud, const XML_Char *el) {
resource_type *rtype = (resource_type *)ud->object;
if (xml_strcmp(el, "construction") == 0) {
if (nreqs > 0) {
construction *con = rtype->itype->construction;
con->materials = calloc(sizeof(requirement), nreqs + 1);
memcpy(con->materials, reqs, sizeof(requirement) * nreqs);
nreqs = 0;
}
}
}
static void XMLCALL handle_end(void *data, const XML_Char *el) { static void XMLCALL handle_end(void *data, const XML_Char *el) {
userdata *ud = (userdata *)data; userdata *ud = (userdata *)data;
--ud->depth;
if (ud->cdata) { if (ud->type == EXP_RESOURCES) {
free(ud->cdata); end_resources(ud, el);
ud->cdata = NULL;
ud->clength = 0;
} }
else {
if (ud->cdata) {
free(ud->cdata);
ud->cdata = NULL;
ud->clength = 0;
}
}
--ud->depth;
if (ud->depth == 0) { if (ud->depth == 0) {
ud->type = EXP_UNKNOWN; ud->type = EXP_UNKNOWN;
} }