use quicklist to store ship types.

This commit is contained in:
Enno Rehling 2011-03-06 21:11:17 -08:00
parent 3677e899af
commit 36e53069e0
2 changed files with 80 additions and 77 deletions

View file

@ -35,6 +35,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/language.h> #include <util/language.h>
#include <util/lists.h> #include <util/lists.h>
#include <util/umlaut.h> #include <util/umlaut.h>
#include <util/quicklist.h>
#include <util/storage.h> #include <util/storage.h>
#include <util/xml.h> #include <util/xml.h>
@ -45,52 +46,59 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <string.h> #include <string.h>
ship_typelist *shiptypes = NULL; quicklist *shiptypes = NULL;
static local_names * snames; static local_names * snames;
const ship_type * const ship_type *
findshiptype(const char * name, const struct locale * lang) findshiptype(const char * name, const struct locale * lang)
{ {
local_names * sn = snames; local_names * sn = snames;
variant var; variant var;
while (sn) { while (sn) {
if (sn->lang==lang) break; if (sn->lang==lang) break;
sn=sn->next; sn=sn->next;
} }
if (!sn) { if (!sn) {
struct ship_typelist * stl = shiptypes; quicklist * ql;
sn = calloc(sizeof(local_names), 1); int qi;
sn->next = snames;
sn->lang = lang; sn = (local_names *)calloc(sizeof(local_names), 1);
while (stl) { sn->next = snames;
sn->lang = lang;
for (qi=0,ql=shiptypes;ql;ql_advance(&ql, &qi, 1)) {
ship_type * stype = (ship_type *)ql_get(ql, qi);
variant var; variant var;
const char * n = locale_string(lang, stl->type->name[0]); const char * n = locale_string(lang, stype->name[0]);
var.v = (void*)stl->type; var.v = (void*)stype;
addtoken(&sn->names, n, var); addtoken(&sn->names, n, var);
stl = stl->next; }
} snames = sn;
snames = sn; }
} if (findtoken(&sn->names, name, &var)==E_TOK_NOMATCH) return NULL;
if (findtoken(&sn->names, name, &var)==E_TOK_NOMATCH) return NULL; return (const ship_type*)var.v;
return (const ship_type*)var.v;
} }
const ship_type * const ship_type *
st_find(const char* name) st_find(const char* name)
{ {
const struct ship_typelist * stl = shiptypes; quicklist * ql;
while (stl && strcmp(stl->type->name[0], name)) stl = stl->next; int qi;
return stl?stl->type:NULL;
for (qi=0,ql=shiptypes;ql;ql_advance(&ql, &qi, 1)) {
ship_type * stype = (ship_type *)ql_get(ql, qi);
if (strcmp(stype->name[0], name)==0) {
return stype;
}
}
return NULL;
} }
void void
st_register(const ship_type * type) { st_register(const ship_type * type) {
struct ship_typelist * stl = malloc(sizeof(ship_type)); ql_push(&shiptypes, (void *)type);
stl->type = type;
stl->next = shiptypes;
shiptypes = stl;
} }
#define SMAXHASH 7919 #define SMAXHASH 7919
@ -98,43 +106,43 @@ ship *shiphash[SMAXHASH];
void void
shash(ship * s) shash(ship * s)
{ {
ship *old = shiphash[s->no % SMAXHASH]; ship *old = shiphash[s->no % SMAXHASH];
shiphash[s->no % SMAXHASH] = s; shiphash[s->no % SMAXHASH] = s;
s->nexthash = old; s->nexthash = old;
} }
void void
sunhash(ship * s) sunhash(ship * s)
{ {
ship **show; ship **show;
for (show = &shiphash[s->no % SMAXHASH]; *show; show = &(*show)->nexthash) { for (show = &shiphash[s->no % SMAXHASH]; *show; show = &(*show)->nexthash) {
if ((*show)->no == s->no) if ((*show)->no == s->no)
break; break;
} }
if (*show) { if (*show) {
assert(*show == s); assert(*show == s);
*show = (*show)->nexthash; *show = (*show)->nexthash;
s->nexthash = 0; s->nexthash = 0;
} }
} }
static ship * static ship *
sfindhash(int i) sfindhash(int i)
{ {
ship *old; ship *old;
for (old = shiphash[i % SMAXHASH]; old; old = old->nexthash) for (old = shiphash[i % SMAXHASH]; old; old = old->nexthash)
if (old->no == i) if (old->no == i)
return old; return old;
return 0; return 0;
} }
struct ship * struct ship *
findship(int i) findship(int i)
{ {
return sfindhash(i); return sfindhash(i);
} }
struct ship * struct ship *
@ -154,19 +162,19 @@ findshipr(const region *r, int n)
void void
damage_ship(ship * sh, double percent) damage_ship(ship * sh, double percent)
{ {
double damage = DAMAGE_SCALE * sh->type->damage * percent * sh->size + sh->damage; double damage = DAMAGE_SCALE * sh->type->damage * percent * sh->size + sh->damage;
sh->damage = (int)damage; sh->damage = (int)damage;
} }
unit * unit *
captain(ship *sh) captain(ship *sh)
{ {
unit *u; unit *u;
for(u = sh->region->units; u; u = u->next) for(u = sh->region->units; u; u = u->next)
if(u->ship == sh && fval(u, UFL_OWNER)) return u; if(u->ship == sh && fval(u, UFL_OWNER)) return u;
return NULL; return NULL;
} }
/* Alte Schiffstypen: */ /* Alte Schiffstypen: */
@ -293,29 +301,29 @@ getshipweight(const ship * sh, int *sweight, int *scabins)
unit * unit *
shipowner(const ship * sh) shipowner(const ship * sh)
{ {
unit *u; unit *u;
unit *first = NULL; unit *first = NULL;
const region * r = sh->region; const region * r = sh->region;
/* Prüfen ob Eigentümer am leben. */ /* Prüfen ob Eigentümer am leben. */
for (u = r->units; u; u = u->next) { for (u = r->units; u; u = u->next) {
if (u->ship == sh) { if (u->ship == sh) {
if (!first && u->number > 0) if (!first && u->number > 0)
first = u; first = u;
if (fval(u, UFL_OWNER) && u->number > 0) if (fval(u, UFL_OWNER) && u->number > 0)
return u; return u;
if (u->number == 0) if (u->number == 0)
freset(u, UFL_OWNER); freset(u, UFL_OWNER);
} }
} }
/* Eigentümer tot oder kein Eigentümer vorhanden. Erste lebende Einheit /* Eigentümer tot oder kein Eigentümer vorhanden. Erste lebende Einheit
* nehmen. */ * nehmen. */
if (first) if (first)
fset(first, UFL_OWNER); fset(first, UFL_OWNER);
return first; return first;
} }
void void

View file

@ -59,12 +59,7 @@ typedef struct ship_type {
struct construction * construction; /* how to build a ship */ struct construction * construction; /* how to build a ship */
} ship_type; } ship_type;
typedef struct ship_typelist { extern struct quicklist *shiptypes;
struct ship_typelist * next;
const ship_type * type;
} ship_typelist;
extern ship_typelist *shiptypes;
/* Alte Schiffstypen: */ /* Alte Schiffstypen: */