From e7f5b517a025ee3fbecef2d7854f1fee62d2425c Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Fri, 12 Sep 2008 18:46:54 -0400 Subject: [PATCH] Make the item iterator capable of iterating over a cargo list New snxtitem_cargo() initializes an iterator for a cargo list, with new enum ns_seltype member NS_GROUP and new struct nstr_item member next. Extend nxtitem() and nxtitemp() to step through the list. --- include/nsc.h | 4 +++- include/prototypes.h | 1 + src/lib/subs/nxtitem.c | 8 +++++++- src/lib/subs/snxtitem.c | 19 +++++++++++++++++++ src/lib/update/nxtitemp.c | 8 +++++++- 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/include/nsc.h b/include/nsc.h index 70c69f6e6..a5c0d3af3 100644 --- a/include/nsc.h +++ b/include/nsc.h @@ -144,7 +144,8 @@ enum ns_seltype { NS_AREA, /* rectangular area */ NS_ALL, /* everything */ NS_XY, /* one sector area */ - NS_GROUP /* group, i.e. fleet, wing, army */ + NS_GROUP, /* group, i.e. fleet, wing, army */ + NS_CARGO /* loaded on the same carrier */ }; /* Sector iterator */ @@ -171,6 +172,7 @@ struct nstr_item { int dist; /* NS_DIST: distance selector */ coord cx, cy; /* NS_DIST: center x-y, NS_XY: xy */ char group; /* NS_GROUP: fleet/wing match */ + short next; /* NS_CARGO: next item */ int size; /* NS_LIST: size of list */ int index; /* NS_LIST: index */ int list[NS_LSIZE]; /* NS_LIST: item list */ diff --git a/include/prototypes.h b/include/prototypes.h index 3afaaa6de..cfcce83c1 100644 --- a/include/prototypes.h +++ b/include/prototypes.h @@ -674,6 +674,7 @@ extern void snxtitem_all(struct nstr_item *, int); extern void snxtitem_group(struct nstr_item *, int, char); extern void snxtitem_rewind(struct nstr_item *); extern int snxtitem_list(struct nstr_item *, int, int *, int); +extern void snxtitem_cargo(struct nstr_item *, int, int, int); /* snxtsct.c */ extern int snxtsct(struct nstr_sect *, char *); extern void snxtsct_area(struct nstr_sect *, struct range *); diff --git a/src/lib/subs/nxtitem.c b/src/lib/subs/nxtitem.c index 494f78cff..51bc027c5 100644 --- a/src/lib/subs/nxtitem.c +++ b/src/lib/subs/nxtitem.c @@ -41,6 +41,7 @@ #include "player.h" #include "prototypes.h" #include "ship.h" +#include "unit.h" #include "xy.h" int @@ -58,6 +59,11 @@ nxtitem(struct nstr_item *np, void *ptr) if (np->index >= np->size) return 0; np->cur = np->list[np->index]; + } else if (np->sel == NS_CARGO) { + if (np->next < 0) + return 0; + np->cur = np->next; + np->next = unit_cargo_next(np->type, np->next); } else { np->cur++; } @@ -66,7 +72,7 @@ nxtitem(struct nstr_item *np, void *ptr) selected = 1; switch (np->sel) { case NS_LIST: - break; + case NS_CARGO: case NS_ALL: break; case NS_DIST: diff --git a/src/lib/subs/snxtitem.c b/src/lib/subs/snxtitem.c index 1e799fa33..8fa968fea 100644 --- a/src/lib/subs/snxtitem.c +++ b/src/lib/subs/snxtitem.c @@ -41,6 +41,7 @@ #include "nsc.h" #include "file.h" #include "prototypes.h" +#include "unit.h" /* * setup the nstr structure for sector selection. @@ -222,3 +223,21 @@ snxtitem_list(struct nstr_item *np, int type, int *list, int len) np->size = len; return 1; } + +/* + * Initialize NP to iterate over the items of type TYPE in a carrier. + * The carrier has file type CARRIER_TYPE and uid CARRIER_UID. + * Note: you can take an item gotten with nxtitem() off its carrier + * without disturbing the iterator. Whether the iterator will pick up + * stuff you load onto the carrier during iteration is unspecified. + */ +void +snxtitem_cargo(struct nstr_item *np, int type, + int carrier_type, int carrier_uid) +{ + memset(np, 0, sizeof(*np)); + np->cur = -1; + np->type = type; + np->sel = NS_CARGO; + np->next = unit_cargo_first(carrier_type, carrier_uid, type); +} diff --git a/src/lib/update/nxtitemp.c b/src/lib/update/nxtitemp.c index 04b285fca..6d01e2ccb 100644 --- a/src/lib/update/nxtitemp.c +++ b/src/lib/update/nxtitemp.c @@ -37,6 +37,7 @@ #include "land.h" #include "nsc.h" #include "ship.h" +#include "unit.h" #include "update.h" void * @@ -53,6 +54,11 @@ nxtitemp(struct nstr_item *np) if (np->index >= np->size) return 0; np->cur = np->list[np->index]; + } else if (np->sel == NS_CARGO) { + if (np->next < 0) + return 0; + np->cur = np->next; + np->next = unit_cargo_next(np->type, np->next); } else { np->cur++; } @@ -63,7 +69,7 @@ nxtitemp(struct nstr_item *np) selected = 1; switch (np->sel) { case NS_LIST: - break; + case NS_CARGO: case NS_ALL: break; case NS_DIST: -- 2.43.0