naev 0.11.5
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, 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)
 
static int ss_statsGetLuaInternal (lua_State *L, const ShipStats *s, ShipStatsType type, int internal)
 
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.
 
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.
 

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:23

Definition at line 58 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 60 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:26

Definition at line 73 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 75 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:22

Definition at line 53 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 55 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:273
Represents ship statistics, properties ship can use.
Definition shipstats.h:198

Definition at line 50 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:25

Definition at line 68 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 70 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 79 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:24

Definition at line 63 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 65 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 21 of file shipstats.c.

Function Documentation

◆ shipstat_sort()

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

Definition at line 366 of file shipstats.c.

◆ ss_check()

int ss_check ( void )

Checks for validity.

Definition at line 414 of file shipstats.c.

◆ ss_free()

void ss_free ( ShipStatList * ll)

Frees a list of ship stats.

Parameters
llList to free.

Definition at line 887 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 292 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 343 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 663 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 674 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 733 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 761 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 718 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 698 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 747 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 708 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 381 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 824 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 1040 of file shipstats.c.

◆ ss_statsGetInternal()

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

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

◆ ss_statsGetLuaInternal()

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

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

◆ ss_statsInit()

int ss_statsInit ( ShipStats * stats)

Initializes a stat structure.

Definition at line 430 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 780 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 474 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 627 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 645 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 530 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 579 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 899 of file shipstats.c.

◆ ss_statsSetList()

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

Definition at line 208 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 685 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 85 of file shipstats.c.