1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
#include "shields.h"
#include "enums.h"
#include "object/objectshield.h"
namespace scripting {
namespace api {
//**********HANDLE: Shields
ADE_OBJ(l_Shields, object_h, "shields", "Shields handle");
ADE_FUNC(__len, l_Shields, NULL, "Number of shield segments", "number", "Number of shield segments or 0 if handle is invalid")
{
object_h *objh;
if(!ade_get_args(L, "o", l_Shields.GetPtr(&objh)))
return ade_set_error(L, "i", -1);
if(!objh->isValid())
return ade_set_error(L, "i", -1);
return ade_set_args(L, "i", objh->objp()->n_quadrants);
}
ADE_INDEXER(l_Shields, "enumeration/number", "Gets or sets shield segment strength. Use \"SHIELD_*\" enumerations (for standard 4-quadrant shields) or index of a specific segment, or NONE for the entire shield", "number", "Segment/shield strength, or 0 if handle is invalid")
{
object_h *objh;
float nval = -1.0f;
object *objp = NULL;
int qdx = -1;
if(lua_isstring(L, 2))
{
const char* qd = nullptr;
if(!ade_get_args(L, "os|f", l_Shields.GetPtr(&objh), &qd, &nval))
return ade_set_error(L, "f", 0.0f);
if(!objh->isValid())
return ade_set_error(L, "f", 0.0f);
objp = objh->objp();
//Which quadrant?
int qdi;
if(qd == NULL)
qdx = -1;
else if((qdi = atoi(qd)) > 0 && qdi <= objp->n_quadrants)
qdx = qdi-1; //LUA->FS2
else
return ade_set_error(L, "f", 0.0f);
} else {
enum_h *qd = NULL;
if(!ade_get_args(L, "oo|f", l_Shields.GetPtr(&objh), l_Enum.GetPtr(&qd), &nval))
return 0;
if(!objh->isValid())
return ade_set_error(L, "f", 0.0f);
objp = objh->objp();
switch(qd->index)
{
case LE_NONE:
qdx = -1;
break;
case LE_SHIELD_FRONT:
qdx = FRONT_QUAD;
break;
case LE_SHIELD_LEFT:
qdx = LEFT_QUAD;
break;
case LE_SHIELD_RIGHT:
qdx = RIGHT_QUAD;
break;
case LE_SHIELD_BACK:
qdx = REAR_QUAD;
break;
default:
return ade_set_error(L, "f", 0.0f);
}
}
//Set/get all quadrants
if(qdx == -1) {
if(ADE_SETTING_VAR && nval >= 0.0f)
shield_set_strength(objp, nval);
return ade_set_args(L, "f", shield_get_strength(objp));
}
//Set one quadrant?
if(ADE_SETTING_VAR && nval >= 0.0f)
shield_set_quad(objp, qdx, nval);
//Get one quadrant
return ade_set_args(L, "f", shield_get_quad(objp, qdx));
}
//WMC - Not sure if I want this to be a variable. It'd make more sense
//as a function, since it modifies all quadrant variables
//WMC - Ehh, screw it.
ADE_VIRTVAR(CombinedLeft, l_Shields, "number", "Total shield hitpoints left (for all segments combined)", "number", "Combined shield strength, or 0 if handle is invalid")
{
object_h *objh;
float nval = -1.0f;
if(!ade_get_args(L, "o|f", l_Shields.GetPtr(&objh), &nval))
return ade_set_error(L, "f", 0.0f);
if(!objh->isValid())
return ade_set_error(L, "f", 0.0f);
if(ADE_SETTING_VAR && nval >= 0.0f) {
shield_set_strength(objh->objp(), nval);
}
return ade_set_args(L, "f", shield_get_strength(objh->objp()));
}
ADE_VIRTVAR(CombinedMax, l_Shields, "number", "Maximum shield hitpoints (for all segments combined)", "number", "Combined maximum shield strength, or 0 if handle is invalid")
{
object_h *objh;
float nval = -1.0f;
if(!ade_get_args(L, "o|f", l_Shields.GetPtr(&objh), &nval))
return 0;
if(!objh->isValid())
return ade_set_error(L, "f", 0.0f);
if(ADE_SETTING_VAR && nval >= 0.0f) {
shield_set_max_strength(objh->objp(), nval);
}
return ade_set_args(L, "f", shield_get_max_strength(objh->objp()));
}
ADE_FUNC(isValid, l_Shields, NULL, "Detects whether handle is valid", "boolean", "true if valid, false if handle is invalid, nil if a syntax/type error occurs")
{
object_h *oh;
if(!ade_get_args(L, "o", l_Shields.GetPtr(&oh)))
return ADE_RETURN_NIL;
return ade_set_args(L, "b", oh->isValid());
}
}
}
|