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.
This commit is contained in:
Markus Armbruster 2008-09-12 18:46:54 -04:00
parent 7a4b7f75a0
commit e7f5b517a0
5 changed files with 37 additions and 3 deletions

View file

@ -144,7 +144,8 @@ enum ns_seltype {
NS_AREA, /* rectangular area */ NS_AREA, /* rectangular area */
NS_ALL, /* everything */ NS_ALL, /* everything */
NS_XY, /* one sector area */ 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 */ /* Sector iterator */
@ -171,6 +172,7 @@ struct nstr_item {
int dist; /* NS_DIST: distance selector */ int dist; /* NS_DIST: distance selector */
coord cx, cy; /* NS_DIST: center x-y, NS_XY: xy */ coord cx, cy; /* NS_DIST: center x-y, NS_XY: xy */
char group; /* NS_GROUP: fleet/wing match */ char group; /* NS_GROUP: fleet/wing match */
short next; /* NS_CARGO: next item */
int size; /* NS_LIST: size of list */ int size; /* NS_LIST: size of list */
int index; /* NS_LIST: index */ int index; /* NS_LIST: index */
int list[NS_LSIZE]; /* NS_LIST: item list */ int list[NS_LSIZE]; /* NS_LIST: item list */

View file

@ -674,6 +674,7 @@ extern void snxtitem_all(struct nstr_item *, int);
extern void snxtitem_group(struct nstr_item *, int, char); extern void snxtitem_group(struct nstr_item *, int, char);
extern void snxtitem_rewind(struct nstr_item *); extern void snxtitem_rewind(struct nstr_item *);
extern int snxtitem_list(struct nstr_item *, int, int *, int); extern int snxtitem_list(struct nstr_item *, int, int *, int);
extern void snxtitem_cargo(struct nstr_item *, int, int, int);
/* snxtsct.c */ /* snxtsct.c */
extern int snxtsct(struct nstr_sect *, char *); extern int snxtsct(struct nstr_sect *, char *);
extern void snxtsct_area(struct nstr_sect *, struct range *); extern void snxtsct_area(struct nstr_sect *, struct range *);

View file

@ -41,6 +41,7 @@
#include "player.h" #include "player.h"
#include "prototypes.h" #include "prototypes.h"
#include "ship.h" #include "ship.h"
#include "unit.h"
#include "xy.h" #include "xy.h"
int int
@ -58,6 +59,11 @@ nxtitem(struct nstr_item *np, void *ptr)
if (np->index >= np->size) if (np->index >= np->size)
return 0; return 0;
np->cur = np->list[np->index]; 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 { } else {
np->cur++; np->cur++;
} }
@ -66,7 +72,7 @@ nxtitem(struct nstr_item *np, void *ptr)
selected = 1; selected = 1;
switch (np->sel) { switch (np->sel) {
case NS_LIST: case NS_LIST:
break; case NS_CARGO:
case NS_ALL: case NS_ALL:
break; break;
case NS_DIST: case NS_DIST:

View file

@ -41,6 +41,7 @@
#include "nsc.h" #include "nsc.h"
#include "file.h" #include "file.h"
#include "prototypes.h" #include "prototypes.h"
#include "unit.h"
/* /*
* setup the nstr structure for sector selection. * 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; np->size = len;
return 1; 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);
}

View file

@ -37,6 +37,7 @@
#include "land.h" #include "land.h"
#include "nsc.h" #include "nsc.h"
#include "ship.h" #include "ship.h"
#include "unit.h"
#include "update.h" #include "update.h"
void * void *
@ -53,6 +54,11 @@ nxtitemp(struct nstr_item *np)
if (np->index >= np->size) if (np->index >= np->size)
return 0; return 0;
np->cur = np->list[np->index]; 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 { } else {
np->cur++; np->cur++;
} }
@ -63,7 +69,7 @@ nxtitemp(struct nstr_item *np)
selected = 1; selected = 1;
switch (np->sel) { switch (np->sel) {
case NS_LIST: case NS_LIST:
break; case NS_CARGO:
case NS_ALL: case NS_ALL:
break; break;
case NS_DIST: case NS_DIST: