Skip to content

Commit

Permalink
Use 64-bit cmids (instead of 32-bit acids) for crew IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
ec429 committed May 23, 2015
1 parent 2085d78 commit 90453f6
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 9 deletions.
24 changes: 24 additions & 0 deletions bits.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,30 @@ int gacid(const char from[8], acid *buf)
return(0);
}

void pcmid(cmid id, char buf[17])
{
for(size_t i=0;i<16;i++)
buf[i]=hex[(id>>(i<<2))&0xf];
buf[16]=0;
}

int gcmid(const char from[16], cmid *buf)
{
if(!buf) return(1);
*buf=0;
unsigned int val;
char fbuf[2]={0, 0};
for(size_t i=0;i<16;i++)
{
if(!from[i]) break; // Allow short strings. It's fine because we're little endian
if(!isxdigit(from[i])) return(1);
fbuf[0]=from[i];
sscanf(fbuf, "%x", &val);
*buf|=((uint64_t)val)<<(i<<2);
}
return(0);
}

#ifdef WINDOWS /* doesn't have strndup, we need to implement one */
char *strndup(const char *s, size_t size)
{
Expand Down
2 changes: 2 additions & 0 deletions bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ void free_string(string *s); // frees a string (is just free(s->buf); really)

void pacid(acid id, char buf[9]); // print an a/c id as hex
int gacid(const char from[8], acid *buf); // parse an a/c id from hex
void pcmid(cmid id, char buf[17]); // similar but for c/m ids
int gcmid(const char from[16], cmid *buf);

#ifdef WINDOWS /* doesn't have strndup, we need to implement one */
char *strndup(const char *s, size_t size);
Expand Down
4 changes: 2 additions & 2 deletions gensave.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def gencrews(line, i):
lrate = poisson(ml)
tops = random.randint(0, tops)
if windows:
return "%s %c:%s,%u,%u,%u,%s"%(stat, cls, float_to_hex(skill), lrate, tops, ft, acid)
return "%s %c:%s,%u,%u,%u,00000000%s"%(stat, cls, float_to_hex(skill), lrate, tops, ft, acid)
else:
return "%s %c:%u,%u,%u,%u,%s"%(stat, cls, skill, lrate, tops, ft, acid)
return "%s %c:%u,%u,%u,%u,00000000%s"%(stat, cls, skill, lrate, tops, ft, acid)

windows = '--windows' in sys.argv

Expand Down
2 changes: 1 addition & 1 deletion post_raid.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ void refill_students(game *state)
}
state->crews=new;
for(unsigned int j=state->ncrews;j<nc;j++)
state->crews[j]=(crewman){.id=rand_acid(), .class=i, .status=CSTATUS_STUDENT, .skill=0, .lrate=60+irandu(60), .tour_ops=0, .assignment=1};
state->crews[j]=(crewman){.id=rand_cmid(), .class=i, .status=CSTATUS_STUDENT, .skill=0, .lrate=60+irandu(60), .tour_ops=0, .assignment=1};
state->ncrews=nc;
}
else if(scount>pool)
Expand Down
7 changes: 7 additions & 0 deletions rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ acid rand_acid(void)
// but 0xffffffff is probably RAND_MAX anyway
return(rand()&0xffffffff);
}

cmid rand_cmid(void)
{
// and this is unbelievably corny
cmid l=rand_acid(), r=rand_acid();
return((l<<32)|r);
}
1 change: 1 addition & 0 deletions rand.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ int irandu(int n); // generate integer from U{0, 1, ... n-1}
double drandu(double m); // generate real from U[0,m)
bool brandp(double p); // generate Bern(p)
acid rand_acid(void); // generate a/c ID
cmid rand_cmid(void); // generate c/m ID
10 changes: 5 additions & 5 deletions saving.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,8 @@ int loadgame(const char *fn, game *state)
char class;
double skill;
unsigned int lrate, tops, ft;
char p_id[9];
f=sscanf(line, "%10s %c:%la,%u,%u,%u,%8s\n", status, &class, &skill, &lrate, &tops, &ft, p_id);
char p_id[17];
f=sscanf(line, "%10s %c:%la,%u,%u,%u,%16s\n", status, &class, &skill, &lrate, &tops, &ft, p_id);
if(f!=7)
{
fprintf(stderr, "1 Too few arguments to part %u of tag \"%s\"\n", i, tag);
Expand All @@ -376,7 +376,7 @@ int loadgame(const char *fn, game *state)
e|=32;
break;
}
if(gacid(p_id, &state->crews[i].id))
if(gcmid(p_id, &state->crews[i].id))
{
fprintf(stderr, "32 Invalid value \"%s\" for c/m ID in tag \"%s\"\n", p_id, tag);
e|=32;
Expand Down Expand Up @@ -761,7 +761,7 @@ int savegame(const char *fn, game state)
perror("fopen");
return(1);
}
char p_id[9];
char p_id[17];
fprintf(fs, "HARR:%u.%u.%u\n", VER_MAJ, VER_MIN, VER_REV);
fprintf(fs, "DATE:%02d-%02d-%04d\n", state.now.day, state.now.month, state.now.year);
fprintf(fs, "DClasses:%u\n", DIFFICULTY_CLASSES);
Expand Down Expand Up @@ -791,7 +791,7 @@ int savegame(const char *fn, game state)
fprintf(fs, "Crews:%u\n", state.ncrews);
for(unsigned int i=0;i<state.ncrews;i++)
{
pacid(state.crews[i].id, p_id);
pcmid(state.crews[i].id, p_id);
fprintf(fs, "%s %c:%la,%u,%u,%u,%s\n", cstatuses[state.crews[i].status], cclasses[state.crews[i].class].letter, state.crews[i].skill, state.crews[i].lrate, state.crews[i].tour_ops, state.crews[i].full_tours, p_id);
}
fprintf(fs, "GProd:%u\n", ICLASS_MIXED);
Expand Down
3 changes: 2 additions & 1 deletion types.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef struct
harris_time;

typedef uint32_t acid; // a/c ID
typedef uint64_t cmid; // crewman ID

typedef struct
{
Expand Down Expand Up @@ -281,7 +282,7 @@ raid;

typedef struct
{
acid id;
cmid id;
enum cclass class;
enum cstatus status;
double skill;
Expand Down

0 comments on commit 90453f6

Please sign in to comment.