Skip to content

Commit

Permalink
commodities
Browse files Browse the repository at this point in the history
  • Loading branch information
smcameron committed Jan 20, 2014
1 parent 18d18db commit 3f73621
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 5 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ COMMONOBJS=mathutils.o snis_alloc.o snis_socket_io.o snis_marshal.o \
bline.o shield_strength.o stacktrace.o snis_ship_type.o \
snis_faction.o mtwist.o infinite-taunt.o
SERVEROBJS=${COMMONOBJS} snis_server.o names.o starbase-comms.o \
power-model.o quat.o vec4.o matrix.o snis_event_callback.o space-part.o fleet.o
power-model.o quat.o vec4.o matrix.o snis_event_callback.o space-part.o fleet.o \
commodities.c

CLIENTOBJS=${COMMONOBJS} ${OGGOBJ} ${SNDOBJS} shader.o graph_dev_opengl.o snis_ui_element.o snis_graph.o \
snis_client.o snis_font.o snis_text_input.o \
Expand Down Expand Up @@ -356,6 +357,9 @@ mtwist.o: mtwist.c mtwist.h
fleet.o: fleet.c fleet.h
$(Q)$(COMPILE)

commodities.o: commodities.c commodities.h
$(Q)$(COMPILE)

test_matrix: matrix.c matrix.h
$(CC) ${MYCFLAGS} ${GTKCFLAGS} -DTEST_MATRIX -o test_matrix matrix.c -lm

Expand Down
200 changes: 200 additions & 0 deletions commodities.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/*
Copyright (C) 2014 Stephen M. Cameron
Author: Stephen M. Cameron
This file is part of Spacenerds In Space.
Spacenerds in Space is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Spacenerds in Space is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Spacenerds in Space; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#include "commodities.h"

#if 0
struct commodity {
char name[40];
char unit[40];
float base_price;
float volatility;
float legality;
float government_adjust;
float economy_adjust;
float tech_adjust;
};
#endif

static void clean_spaces(char *line)
{
char *s, *d;
int skip_spaces = 1;

s = line;
d = line;

while (*s) {
if ((*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r') && skip_spaces) {
s++;
continue;
}
skip_spaces = 0;
if (*s == '\t' || *s == '\n' || *s == '\r')
*s = ' ';
if (*s == ' ')
skip_spaces = 1;
*d = *s;
s++;
d++;
}
}

static int parse_error(char *filename, char *line, int ln, char *bad_word)
{
if (bad_word)
printf("Error at line %s:%d at %s\n", filename, ln, bad_word);
else
printf("Error at line %s:%d\n", filename, ln);
return -1;
}

static int parse_float_field(char *filename, char *line, int ln, float *value)
{
char *c;
char word[100];
int rc;

c = strtok(NULL, ",");
if (!c)
return parse_error(filename, line, ln, NULL);
strcpy(word, c);
clean_spaces(word);
rc = sscanf(word, "%f", value);
if (rc != 1)
return parse_error(filename, line, ln, word);
return 0;
}

static void uppercase(char *w)
{
char *i;

for (i = w; *i; i++)
*i = toupper(*i);
}

static int parse_line(char *filename, char *line, int ln, struct commodity *c)
{
char *x;
char word[100];
int rc;

if (line[0] == '#')
return 1;
clean_spaces(line);
if (strcmp(line, "") == 0)
return 1;

/* Name */
x = strtok(line, ",");
if (!x)
return parse_error(filename, line, ln, NULL);
strcpy(word, x);
clean_spaces(word);
uppercase(word);
strcpy(c->name, word);

/* unit */
x = strtok(NULL, ",");
if (!x)
return parse_error(filename, line, ln, NULL);
strcpy(word, x);
clean_spaces(word);
uppercase(word);
strcpy(c->unit, word);

rc = parse_float_field(filename, line, ln, &c->base_price);
if (rc)
return rc;
rc = parse_float_field(filename, line, ln, &c->volatility);
if (rc)
return rc;
rc = parse_float_field(filename, line, ln, &c->legality);
if (rc)
return rc;
rc = parse_float_field(filename, line, ln, &c->government_adjust);
if (rc)
return rc;
rc = parse_float_field(filename, line, ln, &c->economy_adjust);
if (rc)
return rc;
rc = parse_float_field(filename, line, ln, &c->tech_adjust);
if (rc)
return rc;
return 0;
}

#define MAX_COMMODITIES 100

struct commodity **read_commodities(char *filename, int *ncommodities)
{
FILE *f;
char line[1000];
char *l;
struct commodity c, *clist;
int rc, ln = 0;
int n = 0;

n = 0;
clist = malloc(sizeof(*clist) * MAX_COMMODITIES);
memset(clist, 0, sizeof(*clist) * MAX_COMMODITIES);

f = fopen(filename, "r");
if (!f)
return NULL;

while (!feof(f)) {
l = fgets(line, 1000, f);
if (!l)
break;
line[strlen(line) - 1] = '\0';
ln++;
rc = parse_line(filename, line, ln, &c);
switch (rc) {
case 0:
clist[n] = c;
n++;
#if 0
printf("%s:%s:%f:%f:%f:%f:%f:%f\n",
c.name, c.unit,
c.base_price,
c.volatility,
c.legality,
c.government_adjust,
c.economy_adjust,
c.tech_adjust);
#endif
continue;
case 1: /* comment */
continue;
case -1:
return NULL;
}
}
*ncommodities = n;
return (struct commodity **) clist;
}
37 changes: 37 additions & 0 deletions commodities.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef COMMODITIES_H__
#define COMMODITIES_H__
/*
Copyright (C) 2014 Stephen M. Cameron
Author: Stephen M. Cameron
This file is part of Spacenerds In Space.
Spacenerds in Space is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Spacenerds in Space is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Spacenerds in Space; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

struct commodity {
char name[40];
char unit[40];
float base_price;
float volatility;
float legality;
float government_adjust;
float economy_adjust;
float tech_adjust;
};

struct commodity **read_commodities(char *filename, int *ncommodities);

#endif
9 changes: 9 additions & 0 deletions snis.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#define NPLANET_MATERIALS 4
#define NPLANETS 10
#define NBASES (NPLANETS + 3)
#define COMMODITIES_PER_BASE 10

#define MAXPLAYERS 10

Expand Down Expand Up @@ -347,11 +348,19 @@ struct ship_data {
float braking_factor;
};

struct marketplace_data {
int item;
float qty;
float refill_rate;
};

struct starbase_data {
uint8_t under_attack;
uint32_t last_time_called_for_help;
uint8_t lifeform_count;
char name[16];
struct marketplace_data *mkt;
int associated_planet_id;
};

struct nebula_data {
Expand Down
Loading

0 comments on commit 3f73621

Please sign in to comment.