Skip to content

Commit b49dbfe

Browse files
Merge pull request #6832 from MjnMixael/team_colors_followup
Get, set, browse team colors with Lua
2 parents bfacc16 + ef631f9 commit b49dbfe

7 files changed

Lines changed: 283 additions & 35 deletions

File tree

code/scripting/api/libs/tables.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "scripting/api/objs/intelentry.h"
99
#include "scripting/api/objs/shipclass.h"
1010
#include "scripting/api/objs/shiptype.h"
11+
#include "scripting/api/objs/team_colors.h"
1112
#include "scripting/api/objs/weaponclass.h"
1213
#include "scripting/api/objs/wingformation.h"
1314

@@ -340,5 +341,37 @@ ADE_FUNC(__len, l_Tables_WingFormations, nullptr, "Number of wing formations", "
340341
return ade_set_args(L, "i", static_cast<int>(Wing_formations.size()) + 1);
341342
}
342343

344+
//*****SUBLIBRARY: Tables/TeamColors
345+
ADE_LIB_DERIV(l_Tables_TeamColors, "TeamColors", nullptr, nullptr, l_Tables);
346+
347+
ADE_INDEXER(l_Tables_TeamColors, "number/string IndexOrName", "Array of team colors", "teamcolor", "Team color handle, or invalid handle if name is invalid")
348+
{
349+
const char* name;
350+
if (!ade_get_args(L, "*s", &name))
351+
return ade_set_error(L, "o", l_TeamColor.Set(-1));
352+
353+
// look up by name
354+
for (int i = 0; i < static_cast<int>(Team_Names.size()); ++i) {
355+
if (!stricmp(Team_Names[i].c_str(), name)) {
356+
return ade_set_args(L, "o", l_TeamColor.Set(i));
357+
}
358+
}
359+
360+
// look up by number
361+
int idx = atoi(name);
362+
if (idx > 0) {
363+
idx--; // Lua --> C/C++
364+
} else {
365+
return ade_set_args(L, "o", l_TeamColor.Set(-1));
366+
}
367+
368+
return ade_set_args(L, "o", l_TeamColor.Set(idx));
369+
}
370+
371+
ADE_FUNC(__len, l_Tables_TeamColors, nullptr, "Number of team colors", "number", "Number of team colors")
372+
{
373+
return ade_set_args(L, "i", static_cast<int>(Team_Names.size()));
374+
}
375+
343376
}
344377
}

code/scripting/api/objs/parse_object.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
#include "vecmath.h"
88
#include "weaponclass.h"
99
#include "wing.h"
10-
//#include "globalincs/alphacolors.h" //Needed for team colors
10+
#include "team_colors.h"
11+
#include "globalincs/alphacolors.h" //Needed for team colors
1112

1213
#include "mission/missionparse.h"
1314

@@ -333,31 +334,35 @@ ADE_VIRTVAR(Team, l_ParseObject, "team", "The team of the parsed ship.", "team",
333334
return ade_set_args(L, "o", l_Team.Set(poh->getObject()->team));
334335
}
335336

336-
ADE_VIRTVAR(TeamColor, l_ParseObject, "string", "The team color", "string", "The name of the team color or empty if not set or invalid.")
337+
ADE_VIRTVAR(TeamColor, l_ParseObject, "teamcolor", "The team color. Setting the team color here will not be reflected in the mission if the ship is already created. You must do that on the Ship object instead.", "teamcolor", "The team color handle or nil if not set or invalid.")
337338
{
338339
parse_object_h* poh = nullptr;
339-
const char* team_color = nullptr;
340-
if (!ade_get_args(L, "o|s", l_ParseObject.GetPtr(&poh), &team_color))
341-
return ade_set_error(L, "s", "");
340+
int idx = -1;
341+
if (!ade_get_args(L, "o|o", l_ParseObject.GetPtr(&poh), l_TeamColor.Get(&idx)))
342+
return ADE_RETURN_NIL;
342343

343344
if (!poh->isValid())
344-
return ade_set_error(L, "s", "");
345-
346-
//Set team color
347-
if (ADE_SETTING_VAR && team_color != nullptr) {
345+
return ADE_RETURN_NIL;
348346

347+
// Set team color
348+
if (ADE_SETTING_VAR && SCP_vector_inbounds(Team_Names, idx)) {
349349
// Verify
350-
/*if (Team_Colors.find(team_color) == Team_Colors.end()) {
350+
const auto& it = Team_Colors.find(Team_Names[idx]);
351+
if (it == Team_Colors.end()) {
351352
mprintf(("Invalid team color specified in mission file for ship %s. Not setting!\n", poh->getObject()->name));
352353
} else {
353-
poh->getObject()->team_color_setting = team_color;
354-
}*/
354+
poh->getObject()->team_color_setting = Team_Names[idx];
355+
}
356+
}
355357

356-
LuaError(L, "Setting team colors is not yet supported!");
357-
358-
}
358+
// look up by name
359+
for (int i = 0; i < static_cast<int>(Team_Names.size()); ++i) {
360+
if (lcase_equal(Team_Names[i], poh->getObject()->team_color_setting)) {
361+
return ade_set_args(L, "o", l_TeamColor.Set(i));
362+
}
363+
}
359364

360-
return ade_set_args(L, "s", poh->getObject()->team_color_setting);
365+
return ADE_RETURN_NIL;
361366
}
362367

363368
ADE_VIRTVAR(InitialHull, l_ParseObject, "number", "The initial hull percentage of this parsed ship.", "number",

code/scripting/api/objs/ship.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "shipclass.h"
1616
#include "subsystem.h"
1717
#include "team.h"
18+
#include "team_colors.h"
1819
#include "texture.h"
1920
#include "vecmath.h"
2021
#include "weaponclass.h"
@@ -867,6 +868,39 @@ ADE_VIRTVAR(Team, l_Ship, "team", "Ship's team", "team", "Ship team, or invalid
867868
return ade_set_args(L, "o", l_Team.Set(shipp->team));
868869
}
869870

871+
ADE_VIRTVAR(TeamColor, l_Ship, "teamcolor", "The team color. Note that setting the team color here is instant. If you need a fade, then use the sexp.", "teamcolor", "The team color handle or nil if not set or invalid.")
872+
{
873+
object_h* oh = nullptr;
874+
int idx = -1;
875+
if (!ade_get_args(L, "o|o", l_Ship.GetPtr(&oh), l_TeamColor.Get(&idx)))
876+
return ADE_RETURN_NIL;
877+
878+
if (!oh->isValid())
879+
return ADE_RETURN_NIL;
880+
881+
ship* shipp = &Ships[oh->objp()->instance];
882+
883+
//Set team color
884+
if (ADE_SETTING_VAR && SCP_vector_inbounds(Team_Names, idx)) {
885+
// Verify
886+
const auto& it = Team_Colors.find(Team_Names[idx]);
887+
if (it == Team_Colors.end()) {
888+
mprintf(("Invalid team color specified in mission file for ship %s. Not setting!\n", shipp->ship_name));
889+
} else {
890+
shipp->team_name = Team_Names[idx];
891+
}
892+
}
893+
894+
// look up by name
895+
for (int i = 0; i < static_cast<int>(Team_Names.size()); ++i) {
896+
if (lcase_equal(Team_Names[i], shipp->team_name)) {
897+
return ade_set_args(L, "o", l_TeamColor.Set(i));
898+
}
899+
}
900+
901+
return ADE_RETURN_NIL;
902+
}
903+
870904
ADE_VIRTVAR_DEPRECATED(PersonaIndex, l_Ship, "number", "Persona index", "number", "The index of the persona from messages.tbl, 0 if no persona is set", gameversion::version(25, 0), "Deprecated in favor of Persona")
871905
{
872906
object_h *objh;

code/scripting/api/objs/shipclass.cpp

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "cockpit_display.h"
99
#include "species.h"
1010
#include "shiptype.h"
11+
#include "team_colors.h"
1112
#include "vecmath.h"
1213
#include "ship/ship.h"
1314
#include "playerman/player.h"
@@ -1113,7 +1114,7 @@ ADE_FUNC(isInTechroom, l_Shipclass, NULL, "Gets whether or not the ship class is
11131114
ADE_FUNC(renderTechModel,
11141115
l_Shipclass,
11151116
"number X1, number Y1, number X2, number Y2, [number RotationPercent =0, number PitchPercent =0, number "
1116-
"BankPercent=40, number Zoom=1.3, boolean Lighting=true, string TeamColor=nil]",
1117+
"BankPercent=40, number Zoom=1.3, boolean Lighting=true, teamcolor TeamColor=nil]",
11171118
"Draws ship model as if in techroom. True for regular lighting, false for flat lighting.",
11181119
"boolean",
11191120
"Whether ship was rendered")
@@ -1123,8 +1124,8 @@ ADE_FUNC(renderTechModel,
11231124
int idx;
11241125
float zoom = 1.3f;
11251126
bool lighting = true;
1126-
const char* team_color = nullptr;
1127-
if(!ade_get_args(L, "oiiii|ffffbs", l_Shipclass.Get(&idx), &x1, &y1, &x2, &y2, &rot_angles.h, &rot_angles.p, &rot_angles.b, &zoom, &lighting, &team_color))
1127+
int tc_idx = -1;
1128+
if(!ade_get_args(L, "oiiii|ffffbo", l_Shipclass.Get(&idx), &x1, &y1, &x2, &y2, &rot_angles.h, &rot_angles.p, &rot_angles.b, &zoom, &lighting, l_TeamColor.Get(&tc_idx)))
11281129
return ade_set_error(L, "b", false);
11291130

11301131
if(idx < 0 || idx >= ship_info_size())
@@ -1133,6 +1134,8 @@ ADE_FUNC(renderTechModel,
11331134
if(x2 < x1 || y2 < y1)
11341135
return ade_set_args(L, "b", false);
11351136

1137+
ship_info* sip = &Ship_info[idx];
1138+
11361139
CLAMP(rot_angles.p, 0.0f, 100.0f);
11371140
CLAMP(rot_angles.b, 0.0f, 100.0f);
11381141
CLAMP(rot_angles.h, 0.0f, 100.0f);
@@ -1147,20 +1150,26 @@ ADE_FUNC(renderTechModel,
11471150
rot_angles.h = (rot_angles.h*0.01f) * PI2;
11481151
vm_rotate_matrix_by_angles(&orient, &rot_angles);
11491152

1150-
SCP_string tcolor = team_color ? team_color : "";
1153+
SCP_string tcolor = sip->default_team_name;
1154+
if (SCP_vector_inbounds(Team_Names, tc_idx)) {
1155+
const auto& it = Team_Colors.find(Team_Names[tc_idx]);
1156+
if (it != Team_Colors.end()) {
1157+
tcolor = Team_Names[tc_idx];
1158+
}
1159+
}
11511160

11521161
return ade_set_args(L, "b", render_tech_model(TECH_SHIP, x1, y1, x2, y2, zoom, lighting, idx, &orient, tcolor));
11531162
}
11541163

11551164
// Nuke's alternate tech model rendering function
1156-
ADE_FUNC(renderTechModel2, l_Shipclass, "number X1, number Y1, number X2, number Y2, [orientation Orientation=nil, number Zoom=1.3, string TeamColor=nil]", "Draws ship model as if in techroom", "boolean", "Whether ship was rendered")
1165+
ADE_FUNC(renderTechModel2, l_Shipclass, "number X1, number Y1, number X2, number Y2, [orientation Orientation=nil, number Zoom=1.3, teamcolor TeamColor=nil]", "Draws ship model as if in techroom", "boolean", "Whether ship was rendered")
11571166
{
11581167
int x1,y1,x2,y2;
11591168
int idx;
11601169
float zoom = 1.3f;
11611170
matrix_h *mh = nullptr;
1162-
const char* team_color = nullptr;
1163-
if(!ade_get_args(L, "oiiiio|fs", l_Shipclass.Get(&idx), &x1, &y1, &x2, &y2, l_Matrix.GetPtr(&mh), &zoom, &team_color))
1171+
int tc_idx = -1;
1172+
if(!ade_get_args(L, "oiiiio|fo", l_Shipclass.Get(&idx), &x1, &y1, &x2, &y2, l_Matrix.GetPtr(&mh), &zoom, l_TeamColor.Get(&tc_idx)))
11641173
return ade_set_error(L, "b", false);
11651174

11661175
if(idx < 0 || idx >= ship_info_size())
@@ -1169,17 +1178,25 @@ ADE_FUNC(renderTechModel2, l_Shipclass, "number X1, number Y1, number X2, number
11691178
if(x2 < x1 || y2 < y1)
11701179
return ade_set_args(L, "b", false);
11711180

1181+
ship_info* sip = &Ship_info[idx];
1182+
11721183
//Handle angles
11731184
matrix *orient = mh->GetMatrix();
11741185

1175-
SCP_string tcolor = team_color ? team_color : "";
1186+
SCP_string tcolor = sip->default_team_name;
1187+
if (SCP_vector_inbounds(Team_Names, tc_idx)) {
1188+
const auto& it = Team_Colors.find(Team_Names[tc_idx]);
1189+
if (it != Team_Colors.end()) {
1190+
tcolor = Team_Names[tc_idx];
1191+
}
1192+
}
11761193

11771194
return ade_set_args(L, "b", render_tech_model(TECH_SHIP, x1, y1, x2, y2, zoom, true, idx, orient, tcolor));
11781195
}
11791196

11801197
ADE_FUNC(renderSelectModel,
11811198
l_Shipclass,
1182-
"boolean restart, number x, number y, [number width = 629, number height = 355, number currentEffectSetting = default, number zoom = 1.3, string TeamColor=nil]",
1199+
"boolean restart, number x, number y, [number width = 629, number height = 355, number currentEffectSetting = default, number zoom = 1.3, teamcolor TeamColor=nil]",
11831200
"Draws the 3D select ship model with the chosen effect at the specified coordinates. Restart should "
11841201
"be true on the first frame this is called and false on subsequent frames. Valid selection effects are 1 (fs1) or 2 (fs2), "
11851202
"defaults to the mod setting or the model's setting. Zoom is a multiplier to the model's closeup_zoom value.",
@@ -1194,8 +1211,8 @@ ADE_FUNC(renderSelectModel,
11941211
int y2 = 355;
11951212
int effect = -1;
11961213
float zoom = 1.3f;
1197-
const char* team_color = nullptr;
1198-
if (!ade_get_args(L, "obii|iiifs", l_Shipclass.Get(&idx), &restart, &x1, &y1, &x2, &y2, &effect, &zoom, &team_color))
1214+
int tc_idx = -1;
1215+
if (!ade_get_args(L, "obii|iiifo", l_Shipclass.Get(&idx), &restart, &x1, &y1, &x2, &y2, &effect, &zoom, l_TeamColor.Get(&tc_idx)))
11991216
return ADE_RETURN_NIL;
12001217

12011218
if (idx < 0 || idx >= ship_info_size())
@@ -1230,7 +1247,14 @@ ADE_FUNC(renderSelectModel,
12301247
model_render_params render_info;
12311248

12321249
if (sip->uses_team_colors) {
1233-
SCP_string tcolor = team_color ? team_color : sip->default_team_name;
1250+
SCP_string tcolor = sip->default_team_name;
1251+
1252+
if (SCP_vector_inbounds(Team_Names, tc_idx)) {
1253+
const auto& it = Team_Colors.find(Team_Names[tc_idx]);
1254+
if (it != Team_Colors.end()) {
1255+
tcolor = Team_Names[tc_idx];
1256+
}
1257+
}
12341258
render_info.set_team_color(tcolor, "none", 0, 0);
12351259
}
12361260

@@ -1260,7 +1284,7 @@ ADE_FUNC(renderOverheadModel,
12601284
"number x, number y, [number width = 467, number height = 362, number|table /* selectedSlot = -1 or empty table */, number selectedWeapon = -1, number hoverSlot = -1, "
12611285
"number bank1_x = 170, number bank1_y = 203, number bank2_x = 170, number bank2_y = 246, number bank3_x = 170, number bank3_y = 290, "
12621286
"number bank4_x = 552, number bank4_y = 203, number bank5_x = 552, number bank5_y = 246, number bank6_x = 552, number bank6_y = 290, "
1263-
"number bank7_x = 552, number bank7_y = 333, number style = 0, string TeamColor=nil]",
1287+
"number bank7_x = 552, number bank7_y = 333, number style = 0, teamcolor TeamColor=nil]",
12641288
"Draws the 3D overhead ship model with the lines pointing from bank weapon selections to bank firepoints. SelectedSlot refers to loadout "
12651289
"ship slots 1-12 where wing 1 is 1-4, wing 2 is 5-8, and wing 3 is 9-12. SelectedWeapon is the index into weapon classes. HoverSlot refers "
12661290
"to the bank slots 1-7 where 1-3 are primaries and 4-6 are secondaries. Lines will be drawn from any bank containing the SelectedWeapon to "
@@ -1301,12 +1325,12 @@ ADE_FUNC(renderOverheadModel,
13011325

13021326
int weapon_list[MAX_SHIP_WEAPONS] = {-1, -1, -1, -1, -1, -1, -1};
13031327

1304-
const char* team_color = nullptr;
1328+
int tc_idx = -1;
13051329

13061330
if (lua_isnumber(L, 6)) {
13071331

13081332
if (!ade_get_args(L,
1309-
"oii|iiiiiiiiiiiiiiiiiiiis",
1333+
"oii|iiiiiiiiiiiiiiiiiiiio",
13101334
l_Shipclass.Get(&idx),
13111335
&x1,
13121336
&y1,
@@ -1330,7 +1354,7 @@ ADE_FUNC(renderOverheadModel,
13301354
&bank7_x,
13311355
&bank7_y,
13321356
&style,
1333-
&team_color))
1357+
l_TeamColor.Get(&tc_idx)))
13341358
return ADE_RETURN_NIL;
13351359

13361360
// Convert this from the Lua index
@@ -1344,7 +1368,7 @@ ADE_FUNC(renderOverheadModel,
13441368
}
13451369
} else {
13461370
if (!ade_get_args(L,
1347-
"oii|iitiiiiiiiiiiiiiiiiis",
1371+
"oii|iitiiiiiiiiiiiiiiiiio",
13481372
l_Shipclass.Get(&idx),
13491373
&x1,
13501374
&y1,
@@ -1368,7 +1392,7 @@ ADE_FUNC(renderOverheadModel,
13681392
&bank7_x,
13691393
&bank7_y,
13701394
&style,
1371-
&team_color))
1395+
l_TeamColor.Get(&tc_idx)))
13721396
return ADE_RETURN_NIL;
13731397

13741398
int count = 0;
@@ -1415,7 +1439,13 @@ ADE_FUNC(renderOverheadModel,
14151439

14161440
ship_info* sip = &Ship_info[idx];
14171441

1418-
SCP_string tcolor = team_color ? team_color : sip->default_team_name;
1442+
SCP_string tcolor = sip->default_team_name;
1443+
if (SCP_vector_inbounds(Team_Names, tc_idx)) {
1444+
const auto& it = Team_Colors.find(Team_Names[tc_idx]);
1445+
if (it != Team_Colors.end()) {
1446+
tcolor = Team_Names[tc_idx];
1447+
}
1448+
}
14191449

14201450
int modelNum = model_load(sip->pof_file, sip);
14211451
model_page_in_textures(modelNum, idx);

0 commit comments

Comments
 (0)