naev 0.12.5
shipstats.c File Reference

Handles the ship statistics. More...

#include "shipstats.h"
#include "log.h"
#include "nstring.h"
#include "physics.h"
Include dependency graph for shipstats.c:

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, u, d, i)
#define D__ELEM(t, n, dsp)
#define DI_ELEM(t, n, dsp)
#define A__ELEM(t, n, dsp, u)
#define AI_ELEM(t, n, dsp, u)
#define P__ELEM(t, n, dsp)
#define PI_ELEM(t, n, dsp)
#define I__ELEM(t, n, dsp, u)
#define II_ELEM(t, n, dsp)
#define B__ELEM(t, n, dsp)
#define BI_ELEM(t, n, dsp)
#define N__ELEM(t)

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.
static const char * ss_printI_colour (int i, const ShipStatsLookup *sl)
 Some colour coding for ship stats integers.
static int ss_printD (char *buf, int len, int newline, double d, const ShipStatsLookup *sl)
 Helper to print doubles.
static int ss_printA (char *buf, int len, int newline, double d, const ShipStatsLookup *sl)
 Helper to print absolute doubles.
static int ss_printI (char *buf, int len, int newline, int i, const ShipStatsLookup *sl)
 Helper to print integers.
static int ss_printB (char *buf, int len, int newline, int b, const ShipStatsLookup *sl)
 Helper to print booleans.
static double ss_statsGetInternal (const ShipStats *s, ShipStatsType type, int raw)
static int ss_statsGetLuaInternal (lua_State *L, const ShipStats *s, ShipStatsType type, int internal)
static void ss_adjustDoubleStat (double *statptr, double adjustment, int inverted)
 Makes adjustments so the stats are positively additive.
ShipStatListss_statsSetList (ShipStatList *head, ShipStatsType type, double value, int overwrite, int raw)
ShipStatListss_listFromXML (xmlNodePtr node)
 Creates a shipstat list element from an xml node.
int ss_listToXML (xmlTextWriterPtr writer, const ShipStatList *ll)
 Creatse a shipstat list element from an xml node.
static int shipstat_sort (const void *a, const void *b)
int ss_sort (ShipStatList **ll)
 Sorts the ship stats, useful if doing saving stuff.
int ss_check (void)
 Checks for validity.
int ss_statsInit (ShipStats *stats)
 Initializes a stat structure.
int ss_statsMerge (ShipStats *dest, const ShipStats *src)
 Merges two different ship stats.
int ss_statsMergeSingle (ShipStats *stats, const ShipStatList *list)
 Modifies a stat structure using a single element.
int ss_statsMergeSingleScale (ShipStats *stats, const ShipStatList *list, double scale)
 Modifies a stat structure using a single element.
int ss_statsMergeFromList (ShipStats *stats, const ShipStatList *list)
 Updates a stat structure from a stat list.
int ss_statsMergeFromListScale (ShipStats *stats, const ShipStatList *list, double scale)
 Updates a stat structure from a stat list.
const char * ss_nameFromType (ShipStatsType type)
 Gets the name from type.
size_t ss_offsetFromType (ShipStatsType type)
 Gets the offset from type.
ShipStatsType ss_typeFromName (const char *name)
 Gets the type from the name.
int ss_statsListDesc (const ShipStatList *ll, char *buf, int len, int newline)
 Writes the ship statistics description.
int ss_statsDesc (const ShipStats *s, char *buf, int len, int newline)
 Writes the ship statistics description.
void ss_free (ShipStatList *ll)
 Frees a list of ship stats.
int ss_statsSet (ShipStats *s, const char *name, double value, int overwrite)
 Sets a ship stat by name.
double ss_statsGet (const ShipStats *s, const char *name)
 Gets a ship stat value by name.
double ss_statsGetRaw (const ShipStats *s, ShipStatsType type)
 Gets a ship stat value by name.
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.
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.
int ss_statsGetLuaTableList (lua_State *L, const ShipStatList *list, int internal)
 Converts ship stats to a Lua table, which is pushed on the Lua stack.

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,
u )
Value:
ELEM( t, n, dsp, u, SS_DATA_TYPE_DOUBLE_ABSOLUTE, 0 )
@ SS_DATA_TYPE_DOUBLE_ABSOLUTE
Definition shipstats.c:24

Definition at line 67 of file shipstats.c.

◆ AI_ELEM

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

Definition at line 69 of file shipstats.c.

◆ B__ELEM

#define B__ELEM ( t,
n,
dsp )
Value:
ELEM( t, n, dsp, NULL, SS_DATA_TYPE_BOOLEAN, 0 )
@ SS_DATA_TYPE_BOOLEAN
Definition shipstats.c:27

Definition at line 80 of file shipstats.c.

◆ BI_ELEM

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

Definition at line 81 of file shipstats.c.

◆ D__ELEM

#define D__ELEM ( t,
n,
dsp )
Value:
ELEM( t, n, dsp, N_( "%" ), SS_DATA_TYPE_DOUBLE, 0 )
@ SS_DATA_TYPE_DOUBLE
Definition shipstats.c:23

Definition at line 62 of file shipstats.c.

◆ DI_ELEM

#define DI_ELEM ( t,
n,
dsp )
Value:
ELEM( t, n, dsp, N_( "%" ), SS_DATA_TYPE_DOUBLE, 1 )

Definition at line 64 of file shipstats.c.

◆ ELEM

#define ELEM ( t,
n,
dsp,
u,
d,
i )
Value:
{ .type = t, \
.name = #n, \
.display = dsp, \
.unit = u, \
.data = d, \
.inverted = i, \
.offset = offsetof( ShipStats, n ) }
static const double d[]
Definition rng.c:263
Represents ship statistics, properties ship can use.
Definition shipstats.h:229

Definition at line 53 of file shipstats.c.

◆ I__ELEM

#define I__ELEM ( t,
n,
dsp,
u )
Value:
ELEM( t, n, dsp, u, SS_DATA_TYPE_INTEGER, 0 )
@ SS_DATA_TYPE_INTEGER
Definition shipstats.c:26

Definition at line 77 of file shipstats.c.

◆ II_ELEM

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

Definition at line 78 of file shipstats.c.

◆ N__ELEM

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

Nil element.

Definition at line 84 of file shipstats.c.

◆ P__ELEM

#define P__ELEM ( t,
n,
dsp )
Value:
ELEM( t, n, dsp, N_( "%" ), SS_DATA_TYPE_DOUBLE_ABSOLUTE_PERCENT, 0 )
@ SS_DATA_TYPE_DOUBLE_ABSOLUTE_PERCENT
Definition shipstats.c:25

Definition at line 72 of file shipstats.c.

◆ PI_ELEM

#define PI_ELEM ( t,
n,
dsp )
Value:
ELEM( t, n, dsp, N_( "%" ), SS_DATA_TYPE_DOUBLE_ABSOLUTE_PERCENT, 1 )

Definition at line 74 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 percent double value.

SS_DATA_TYPE_INTEGER 

Absolute integer value.

SS_DATA_TYPE_BOOLEAN 

Boolean value, defaults 0.

Definition at line 22 of file shipstats.c.

Function Documentation

◆ shipstat_sort()

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

Definition at line 427 of file shipstats.c.

◆ ss_adjustDoubleStat()

void ss_adjustDoubleStat ( double * statptr,
double adjustment,
int inverted )
static

Makes adjustments so the stats are positively additive.

Definition at line 597 of file shipstats.c.

◆ ss_check()

int ss_check ( void )

Checks for validity.

Definition at line 474 of file shipstats.c.

◆ ss_free()

void ss_free ( ShipStatList * ll)

Frees a list of ship stats.

Parameters
llList to free.

Definition at line 934 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 353 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 404 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 703 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 714 of file shipstats.c.

◆ ss_printA()

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

Helper to print absolute doubles.

Definition at line 773 of file shipstats.c.

◆ ss_printB()

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

Helper to print booleans.

Definition at line 801 of file shipstats.c.

◆ ss_printD()

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

Helper to print doubles.

Definition at line 759 of file shipstats.c.

◆ ss_printD_colour()

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

Some colour coding for ship stats doubles.

Definition at line 739 of file shipstats.c.

◆ ss_printI()

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

Helper to print integers.

Definition at line 788 of file shipstats.c.

◆ ss_printI_colour()

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

Some colour coding for ship stats integers.

Definition at line 749 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 442 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 864 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 1094 of file shipstats.c.

◆ ss_statsGetInternal()

double ss_statsGetInternal ( const ShipStats * s,
ShipStatsType type,
int raw )
static

Definition at line 1008 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 1116 of file shipstats.c.

◆ ss_statsGetLuaInternal()

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

Definition at line 1045 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 1136 of file shipstats.c.

◆ ss_statsGetLuaTableList()

int ss_statsGetLuaTableList ( lua_State * L,
const ShipStatList * list,
int internal )

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

Definition at line 1157 of file shipstats.c.

◆ ss_statsGetRaw()

double ss_statsGetRaw ( const ShipStats * s,
ShipStatsType type )

Gets a ship stat value by name.

Definition at line 1108 of file shipstats.c.

◆ ss_statsInit()

int ss_statsInit ( ShipStats * stats)

Initializes a stat structure.

Definition at line 490 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 820 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 533 of file shipstats.c.

◆ ss_statsMergeFromList()

int ss_statsMergeFromList ( ShipStats * stats,
const ShipStatList * list )

Updates a stat structure from a stat list.

Parameters
statsStats to update.
listList to update from.
Returns
0 on success.

Definition at line 666 of file shipstats.c.

◆ ss_statsMergeFromListScale()

int ss_statsMergeFromListScale ( 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.
Returns
0 on success.

Definition at line 684 of file shipstats.c.

◆ ss_statsMergeSingle()

int ss_statsMergeSingle ( 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 589 of file shipstats.c.

◆ ss_statsMergeSingleScale()

int ss_statsMergeSingleScale ( 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 620 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 946 of file shipstats.c.

◆ ss_statsSetList()

ShipStatList * ss_statsSetList ( ShipStatList * head,
ShipStatsType type,
double value,
int overwrite,
int raw )

Definition at line 271 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 725 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 95 of file shipstats.c.