continued: new argument type "items" or "resources" (previous solution didn't allow lists of resources, which come in handy for build-error messages).

This commit is contained in:
Enno Rehling 2006-01-13 21:20:19 +00:00
parent 8ca86912cf
commit 7eee06c285
5 changed files with 42 additions and 30 deletions

View File

@ -3172,23 +3172,23 @@ eval_order(struct opstack ** stack, const void * userdata) /* order -> string */
} }
static void static void
eval_items(struct opstack ** stack, const void * userdata) /* order -> string */ eval_resources(struct opstack ** stack, const void * userdata) /* order -> string */
{ {
const faction * report = (const faction*)userdata; const faction * report = (const faction*)userdata;
const struct item * itm = (const struct item *)opop(stack).v; const struct resource * res = (const struct resource *)opop(stack).v;
static char buf[256]; static char buf[256];
size_t len = sizeof(buf); size_t len = sizeof(buf);
variant var; variant var;
char * edit = buf; char * edit = buf;
while (itm!=NULL && len > 4) { while (res!=NULL && len > 4) {
const char * rname = resourcename(itm->type->rtype, (itm->number!=1)?NMF_PLURAL:0); const char * rname = resourcename(res->type, (res->number!=1)?NMF_PLURAL:0);
int written = snprintf(edit, len, "%d %s", itm->number, LOC(report->locale, rname)); int written = snprintf(edit, len, "%d %s", res->number, LOC(report->locale, rname));
len -= written; len -= written;
edit += written; edit += written;
itm = itm->next; res = res->next;
if (itm!=NULL && len>2) { if (res!=NULL && len>2) {
strcat(edit, ", "); strcat(edit, ", ");
edit += 2; edit += 2;
len -= 2; len -= 2;
@ -3259,7 +3259,7 @@ report_init(void)
add_function("int36", &eval_int36); add_function("int36", &eval_int36);
add_function("trail", &eval_trail); add_function("trail", &eval_trail);
add_function("spell", &eval_spell); add_function("spell", &eval_spell);
add_function("items", &eval_items); add_function("resources", &eval_resources);
register_reporttype("nr", &report_plaintext, 1<<O_REPORT); register_reporttype("nr", &report_plaintext, 1<<O_REPORT);
register_reporttype("txt", &report_template, 1<<O_ZUGVORLAGE); register_reporttype("txt", &report_template, 1<<O_ZUGVORLAGE);

View File

@ -882,18 +882,17 @@ build_building(unit * u, const building_type * btype, int want, order * ord)
case ENOMATERIALS: { case ENOMATERIALS: {
/* something missing from the list of materials */ /* something missing from the list of materials */
const construction * cons = btype->construction; const construction * cons = btype->construction;
char * ch = buf; resource * reslist = NULL;
assert(cons); assert(cons);
for (c=0;cons->materials[c].number; c++) { for (c=0;cons->materials[c].number; ++c) {
int n; resource * res = malloc(sizeof(resource));
if (c!=0) strcat(ch++, ","); res->number = cons->materials[c].number / cons->reqsize;
n = cons->materials[c].number / cons->reqsize; res->type = cons->materials[c].rtype;
sprintf(ch, " %d %s", n?n:1, res->next = reslist;
LOC(lang, resourcename(cons->materials[c].rtype, cons->materials[c].number!=1))); reslist = res->next;
ch = ch+strlen(ch);
} }
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "build_required", ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "build_required",
"required", buf)); "required", reslist));
return; return;
} }
case ELOWSKILL: case ELOWSKILL:

View File

@ -31,6 +31,12 @@ typedef struct item {
struct item * next; struct item * next;
} item; } item;
typedef struct resource {
const struct resource_type * type;
int number;
struct resource * next;
} resource;
/* bitfield values for resource_type::flags */ /* bitfield values for resource_type::flags */
#define RTF_NONE 0 #define RTF_NONE 0
#define RTF_ITEM (1<<0) /* this resource is an item */ #define RTF_ITEM (1<<0) /* this resource is an item */

View File

@ -1697,24 +1697,30 @@ var_free_order(variant x)
static variant static variant
var_copy_items(variant x) var_copy_items(variant x)
{ {
item * isrc, * idst = NULL, ** iptr = &idst; item * isrc;
resource * rdst = NULL, ** rptr = &rdst;
for (isrc = (item*)x.v; isrc!=NULL; isrc=isrc->next) { for (isrc = (item*)x.v; isrc!=NULL; isrc=isrc->next) {
item * itm = malloc(sizeof(item)); resource * res = malloc(sizeof(resource));
itm->number = isrc->number; res->number = isrc->number;
itm->type = isrc->type; res->type = isrc->type->rtype;
*iptr = itm; *rptr = res;
iptr = &itm->next; rptr = &res->next;
} }
*iptr = NULL; *rptr = NULL;
x.v = idst; x.v = rdst;
return x; return x;
} }
static void static void
var_free_items(variant x) var_free_resources(variant x)
{ {
i_freeall((item**)&x.v); resource ** rsrc = (resource**)&x.v;
while (*rsrc) {
resource * res = *rsrc;
*rsrc = res->next;
free(res);
}
} }
void void
@ -1735,7 +1741,8 @@ reports_init(void)
register_argtype("int", NULL, NULL, VAR_INT); register_argtype("int", NULL, NULL, VAR_INT);
register_argtype("string", var_free_string, var_copy_string, VAR_VOIDPTR); register_argtype("string", var_free_string, var_copy_string, VAR_VOIDPTR);
register_argtype("order", var_free_order, var_copy_order, VAR_VOIDPTR); register_argtype("order", var_free_order, var_copy_order, VAR_VOIDPTR);
register_argtype("items", var_free_items, var_copy_items, VAR_VOIDPTR); register_argtype("resources", var_free_resources, NULL, VAR_VOIDPTR);
register_argtype("items", var_free_resources, var_copy_items, VAR_VOIDPTR);
/* register alternative visibility functions */ /* register alternative visibility functions */
register_function((pf_generic)view_neighbours, "view_neighbours"); register_function((pf_generic)view_neighbours, "view_neighbours");

View File

@ -4,8 +4,8 @@
<type> <type>
<arg name="items" type="items"/> <arg name="items" type="items"/>
</type> </type>
<text locale="de">"Einheiten können die folgenden Gegenstände beanspruchen: $items($items)"</text> <text locale="de">"Einheiten können die folgenden Gegenstände beanspruchen: $resources($items)"</text>
<text locale="en">"Units can claim the following items: $items($items)"</text> <text locale="en">"Units can claim the following items: $resources($items)"</text>
</message> </message>
<message name="sighting" section="events"> <message name="sighting" section="events">
<type> <type>