naev 0.10.4
Data Structures | Macros | Enumerations | Functions | Variables
shipstats.c File Reference

Handles the ship statistics. More...

#include "shipstats.h"
#include "log.h"
#include "nstring.h"

Go to the source code of this file.

Data Structures

struct  ShipStatsLookup
 Internal look up table for ship stats. More...
 

Macros

#define ELEM(t, n, dsp, d, i)    { .type=t, .name=#n, .display=dsp, .data=d, .inverted=i, .offset=offsetof( ShipStats, n ) }
 
#define D__ELEM(t, n, dsp)    ELEM( t, n, dsp, SS_DATA_TYPE_DOUBLE, 0 )
 
#define A__ELEM(t, n, dsp)    ELEM( t, n, dsp, SS_DATA_TYPE_DOUBLE_ABSOLUTE, 0 )
 
#define P__ELEM(t, n, dsp)    ELEM( t, n, dsp, SS_DATA_TYPE_DOUBLE_ABSOLUTE_PERCENT, 0 )
 
#define I__ELEM(t, n, dsp)    ELEM( t, n, dsp, SS_DATA_TYPE_INTEGER, 0 )
 
#define B__ELEM(t, n, dsp)    ELEM( t, n, dsp, SS_DATA_TYPE_BOOLEAN, 0 )
 
#define DI_ELEM(t, n, dsp)    ELEM( t, n, dsp, SS_DATA_TYPE_DOUBLE, 1 )
 
#define AI_ELEM(t, n, dsp)    ELEM( t, n, dsp, SS_DATA_TYPE_DOUBLE_ABSOLUTE, 1 )
 
#define PI_ELEM(t, n, dsp)    ELEM( t, n, dsp, SS_DATA_TYPE_DOUBLE_ABSOLUTE_PERCENT, 1 )
 
#define II_ELEM(t, n, dsp)    ELEM( t, n, dsp, SS_DATA_TYPE_INTEGER, 1 )
 
#define BI_ELEM(t, n, dsp)    ELEM( t, n, dsp, SS_DATA_TYPE_BOOLEAN, 1 )
 
#define N__ELEM(t)    { .type=t, .name=NULL, .display=NULL, .inverted=0, .offset=0 }
 

Enumerations

enum  StatDataType {
  SS_DATA_TYPE_DOUBLE , SS_DATA_TYPE_DOUBLE_ABSOLUTE , SS_DATA_TYPE_DOUBLE_ABSOLUTE_PERCENT , SS_DATA_TYPE_INTEGER ,
  SS_DATA_TYPE_BOOLEAN
}
 The data type. More...
 

Functions

static const char * ss_printD_colour (double d, const ShipStatsLookup *sl)
 Some colour coding for ship stats doubles. More...
 
static const char * ss_printI_colour (int i, const ShipStatsLookup *sl)
 Some colour coding for ship stats integers. More...
 
static int ss_printD (char *buf, int len, int newline, double d, const ShipStatsLookup *sl)
 Helper to print doubles. More...
 
static int ss_printA (char *buf, int len, int newline, double d, const ShipStatsLookup *sl)
 Helper to print absolute doubles. More...
 
static int ss_printI (char *buf, int len, int newline, int i, const ShipStatsLookup *sl)
 Helper to print integers. More...
 
static int ss_printB (char *buf, int len, int newline, int b, const ShipStatsLookup *sl)
 Helper to print booleans. More...
 
static double ss_statsGetInternal (const ShipStats *s, ShipStatsType type)
 
static int ss_statsGetLuaInternal (lua_State *L, const ShipStats *s, ShipStatsType type, int internal)
 
ShipStatListss_listFromXML (xmlNodePtr node)
 Creates a shipstat list element from an xml node. More...
 
int ss_listToXML (xmlTextWriterPtr writer, const ShipStatList *ll)
 Creatse a shipstat list element from an xml node. More...
 
static int shipstat_sort (const void *a, const void *b)
 
int ss_sort (ShipStatList **ll)
 Sorts the ship stats, useful if doing saving stuff. More...
 
int ss_check (void)
 Checks for validity. More...
 
int ss_statsInit (ShipStats *stats)
 Initializes a stat structure. More...
 
int ss_statsMerge (ShipStats *dest, const ShipStats *src)
 Merges two different ship stats. More...
 
int ss_statsModSingle (ShipStats *stats, const ShipStatList *list)
 Modifies a stat structure using a single element. More...
 
int ss_statsModSingleScale (ShipStats *stats, const ShipStatList *list, double scale)
 Modifies a stat structure using a single element. More...
 
int ss_statsModFromList (ShipStats *stats, const ShipStatList *list)
 Updates a stat structure from a stat list. More...
 
int ss_statsModFromListScale (ShipStats *stats, const ShipStatList *list, double scale)
 Updates a stat structure from a stat list. More...
 
const char * ss_nameFromType (ShipStatsType type)
 Gets the name from type. More...
 
size_t ss_offsetFromType (ShipStatsType type)
 Gets the offset from type. More...
 
ShipStatsType ss_typeFromName (const char *name)
 Gets the type from the name. More...
 
int ss_statsListDesc (const ShipStatList *ll, char *buf, int len, int newline)
 Writes the ship statistics description. More...
 
int ss_statsDesc (const ShipStats *s, char *buf, int len, int newline)
 Writes the ship statistics description. More...
 
void ss_free (ShipStatList *ll)
 Frees a list of ship stats. More...
 
int ss_statsSet (ShipStats *s, const char *name, double value, int overwrite)
 Sets a ship stat by name. More...
 
double ss_statsGet (const ShipStats *s, const char *name)
 Gets a ship stat value by name. More...
 
int ss_statsGetLua (lua_State *L, const ShipStats *s, const char *name, int internal)
 Gets a ship stat value by name and pushes it to Lua. More...
 
int ss_statsGetLuaTable (lua_State *L, const ShipStats *s, int internal)
 Converts ship stats to a Lua table, which is pushed on the Lua stack. More...
 

Variables

static const ShipStatsLookup ss_lookup []
 

Detailed Description

Handles the ship statistics.

Definition in file shipstats.c.

Macro Definition Documentation

◆ A__ELEM

#define A__ELEM (   t,
  n,
  dsp 
)     ELEM( t, n, dsp, SS_DATA_TYPE_DOUBLE_ABSOLUTE, 0 )

Definition at line 54 of file shipstats.c.

◆ AI_ELEM

#define AI_ELEM (   t,
  n,
  dsp 
)     ELEM( t, n, dsp, SS_DATA_TYPE_DOUBLE_ABSOLUTE, 1 )

Definition at line 65 of file shipstats.c.

◆ B__ELEM

#define B__ELEM (   t,
  n,
  dsp 
)     ELEM( t, n, dsp, SS_DATA_TYPE_BOOLEAN, 0 )

Definition at line 60 of file shipstats.c.

◆ BI_ELEM

#define BI_ELEM (   t,
  n,
  dsp 
)     ELEM( t, n, dsp, SS_DATA_TYPE_BOOLEAN, 1 )

Definition at line 71 of file shipstats.c.

◆ D__ELEM

#define D__ELEM (   t,
  n,
  dsp 
)     ELEM( t, n, dsp, SS_DATA_TYPE_DOUBLE, 0 )

Definition at line 52 of file shipstats.c.

◆ DI_ELEM

#define DI_ELEM (   t,
  n,
  dsp 
)     ELEM( t, n, dsp, SS_DATA_TYPE_DOUBLE, 1 )

Definition at line 63 of file shipstats.c.

◆ ELEM

#define ELEM (   t,
  n,
  dsp,
  d,
 
)     { .type=t, .name=#n, .display=dsp, .data=d, .inverted=i, .offset=offsetof( ShipStats, n ) }

Definition at line 49 of file shipstats.c.

◆ I__ELEM

#define I__ELEM (   t,
  n,
  dsp 
)     ELEM( t, n, dsp, SS_DATA_TYPE_INTEGER, 0 )

Definition at line 58 of file shipstats.c.

◆ II_ELEM

#define II_ELEM (   t,
  n,
  dsp 
)     ELEM( t, n, dsp, SS_DATA_TYPE_INTEGER, 1 )

Definition at line 69 of file shipstats.c.

◆ N__ELEM

#define N__ELEM (   t)     { .type=t, .name=NULL, .display=NULL, .inverted=0, .offset=0 }

Nil element.

Definition at line 74 of file shipstats.c.

◆ P__ELEM

#define P__ELEM (   t,
  n,
  dsp 
)     ELEM( t, n, dsp, SS_DATA_TYPE_DOUBLE_ABSOLUTE_PERCENT, 0 )

Definition at line 56 of file shipstats.c.

◆ PI_ELEM

#define PI_ELEM (   t,
  n,
  dsp 
)     ELEM( t, n, dsp, SS_DATA_TYPE_DOUBLE_ABSOLUTE_PERCENT, 1 )

Definition at line 67 of file shipstats.c.

Enumeration Type Documentation

◆ StatDataType

The data type.

Enumerator
SS_DATA_TYPE_DOUBLE 

Relative [0:inf] value.

SS_DATA_TYPE_DOUBLE_ABSOLUTE 

Absolute double value.

SS_DATA_TYPE_DOUBLE_ABSOLUTE_PERCENT 

Absolute double value as a percent.

SS_DATA_TYPE_INTEGER 

Absolute integer value.

SS_DATA_TYPE_BOOLEAN 

Boolean value, defaults 0.

Definition at line 21 of file shipstats.c.

Function Documentation

◆ shipstat_sort()

static int shipstat_sort ( const void *  a,
const void *  b 
)
static

Definition at line 283 of file shipstats.c.

◆ ss_check()

int ss_check ( void  )

Checks for validity.

Definition at line 331 of file shipstats.c.

◆ ss_free()

void ss_free ( ShipStatList ll)

Frees a list of ship stats.

Parameters
llList to free.

Definition at line 807 of file shipstats.c.

◆ ss_listFromXML()

ShipStatList * ss_listFromXML ( xmlNodePtr  node)

Creates a shipstat list element from an xml node.

Parameters
nodeNode to create element from.
Returns
List element created from node.

Definition at line 209 of file shipstats.c.

◆ ss_listToXML()

int ss_listToXML ( xmlTextWriterPtr  writer,
const ShipStatList ll 
)

Creatse a shipstat list element from an xml node.

Parameters
writerWriter to use to write the XML data.
llShipStats to save.
Returns
0 on success.

Definition at line 260 of file shipstats.c.

◆ ss_nameFromType()

const char * ss_nameFromType ( ShipStatsType  type)

Gets the name from type.

O(1) look up.

Parameters
typeType to get name of.
Returns
Name of the type.

Definition at line 574 of file shipstats.c.

◆ ss_offsetFromType()

size_t ss_offsetFromType ( ShipStatsType  type)

Gets the offset from type.

Parameters
typeType to get offset of.
Returns
Offset of the type.

Definition at line 585 of file shipstats.c.

◆ ss_printA()

static int ss_printA ( char *  buf,
int  len,
int  newline,
double  d,
const ShipStatsLookup sl 
)
static

Helper to print absolute doubles.

Definition at line 654 of file shipstats.c.

◆ ss_printB()

static int ss_printB ( char *  buf,
int  len,
int  newline,
int  b,
const ShipStatsLookup sl 
)
static

Helper to print booleans.

Definition at line 680 of file shipstats.c.

◆ ss_printD()

static int ss_printD ( char *  buf,
int  len,
int  newline,
double  d,
const ShipStatsLookup sl 
)
static

Helper to print doubles.

Definition at line 641 of file shipstats.c.

◆ ss_printD_colour()

static const char * ss_printD_colour ( double  d,
const ShipStatsLookup sl 
)
static

Some colour coding for ship stats doubles.

Definition at line 609 of file shipstats.c.

◆ ss_printI()

static int ss_printI ( char *  buf,
int  len,
int  newline,
int  i,
const ShipStatsLookup sl 
)
static

Helper to print integers.

Definition at line 667 of file shipstats.c.

◆ ss_printI_colour()

static const char * ss_printI_colour ( int  i,
const ShipStatsLookup sl 
)
static

Some colour coding for ship stats integers.

Definition at line 625 of file shipstats.c.

◆ ss_sort()

int ss_sort ( ShipStatList **  ll)

Sorts the ship stats, useful if doing saving stuff.

Parameters
llShip stat list to sort.
Returns
0 on success.

Definition at line 298 of file shipstats.c.

◆ ss_statsDesc()

int ss_statsDesc ( const ShipStats s,
char *  buf,
int  len,
int  newline 
)

Writes the ship statistics description.

Parameters
sShip stats to use.
bufBuffer to write to.
lenSpace left in the buffer.
newlineAdd a newline at start.
Returns
Number of characters written.

Definition at line 744 of file shipstats.c.

◆ ss_statsGet()

double ss_statsGet ( const ShipStats s,
const char *  name 
)

Gets a ship stat value by name.

Definition at line 953 of file shipstats.c.

◆ ss_statsGetInternal()

static double ss_statsGetInternal ( const ShipStats s,
ShipStatsType  type 
)
static

Definition at line 875 of file shipstats.c.

◆ ss_statsGetLua()

int ss_statsGetLua ( lua_State *  L,
const ShipStats s,
const char *  name,
int  internal 
)

Gets a ship stat value by name and pushes it to Lua.

Definition at line 967 of file shipstats.c.

◆ ss_statsGetLuaInternal()

static int ss_statsGetLuaInternal ( lua_State *  L,
const ShipStats s,
ShipStatsType  type,
int  internal 
)
static

Definition at line 905 of file shipstats.c.

◆ ss_statsGetLuaTable()

int ss_statsGetLuaTable ( lua_State *  L,
const ShipStats s,
int  internal 
)

Converts ship stats to a Lua table, which is pushed on the Lua stack.

Definition at line 986 of file shipstats.c.

◆ ss_statsInit()

int ss_statsInit ( ShipStats stats)

Initializes a stat structure.

Definition at line 347 of file shipstats.c.

◆ ss_statsListDesc()

int ss_statsListDesc ( const ShipStatList ll,
char *  buf,
int  len,
int  newline 
)

Writes the ship statistics description.

Parameters
llShip stats to use.
bufBuffer to write to.
lenSpace left in the buffer.
newlineAdd a newline at start.
Returns
Number of characters written.

Definition at line 699 of file shipstats.c.

◆ ss_statsMerge()

int ss_statsMerge ( ShipStats dest,
const ShipStats src 
)

Merges two different ship stats.

Parameters
destDestination ship stats.
srcSource to be merged with destination.

Definition at line 391 of file shipstats.c.

◆ ss_statsModFromList()

int ss_statsModFromList ( ShipStats stats,
const ShipStatList list 
)

Updates a stat structure from a stat list.

Parameters
statsStats to update.
listList to update from.

Definition at line 543 of file shipstats.c.

◆ ss_statsModFromListScale()

int ss_statsModFromListScale ( ShipStats stats,
const ShipStatList list,
double  scale 
)

Updates a stat structure from a stat list.

Parameters
statsStats to update.
listList to update from.
scaleScaling factor.

Definition at line 558 of file shipstats.c.

◆ ss_statsModSingle()

int ss_statsModSingle ( ShipStats stats,
const ShipStatList list 
)

Modifies a stat structure using a single element.

Parameters
statsStat structure to modify.
listSingle element to apply.
Returns
0 on success.

Definition at line 447 of file shipstats.c.

◆ ss_statsModSingleScale()

int ss_statsModSingleScale ( ShipStats stats,
const ShipStatList list,
double  scale 
)

Modifies a stat structure using a single element.

Parameters
statsStat structure to modify.
listSingle element to apply.
scaleScaling factor.
Returns
0 on success.

Definition at line 496 of file shipstats.c.

◆ ss_statsSet()

int ss_statsSet ( ShipStats s,
const char *  name,
double  value,
int  overwrite 
)

Sets a ship stat by name.

Definition at line 819 of file shipstats.c.

◆ ss_typeFromName()

ShipStatsType ss_typeFromName ( const char *  name)

Gets the type from the name.

Parameters
nameName to get type of.
Returns
Type matching the name.

Definition at line 596 of file shipstats.c.

Variable Documentation

◆ ss_lookup

const ShipStatsLookup ss_lookup[]
static

The ultimate look up table for ship stats, everything goes through this.

Definition at line 80 of file shipstats.c.