Skip to content

Commit bbb838a

Browse files
authored
custom data for personas (#6861)
1 parent 7a655e4 commit bbb838a

3 files changed

Lines changed: 50 additions & 13 deletions

File tree

code/mission/missionmessage.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ void persona_parse()
322322
this_persona.flags |= PERSONA_FLAG_NO_AUTOMATIC_ASSIGNMENT;
323323
}
324324

325+
if (optional_string("$Custom data:")) {
326+
parse_string_map(this_persona.custom_data, "$end_custom_data", "+Val:");
327+
}
328+
325329
if (!dup) {
326330
int persona_index = (int) Personas.size();
327331
Personas.push_back(this_persona);

code/mission/missionmessage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ struct Persona {
239239
char name[NAME_LENGTH];
240240
int flags;
241241
int species_bitfield;
242+
SCP_map<SCP_string, SCP_string> custom_data;
242243
};
243244

244245
extern SCP_vector<Persona> Personas;

code/scripting/api/objs/message.cpp

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ ADE_VIRTVAR(Name, l_Persona, "string", "The name of the persona", "string", "The
2323
if (!ade_get_args(L, "o", l_Persona.Get(&idx)))
2424
return ade_set_error(L, "s", "");
2525

26-
if (Personas.empty())
27-
return ade_set_error(L, "s", "");
28-
29-
if (idx < 0 || idx >= (int)Personas.size())
26+
if (!SCP_vector_inbounds(Personas, idx))
3027
return ade_set_error(L, "s", "");
3128

3229
return ade_set_args(L, "s", Personas[idx].name);
@@ -39,9 +36,6 @@ ADE_VIRTVAR(Index, l_Persona, nullptr, nullptr, "number", "The index of the pers
3936
if (!ade_get_args(L, "o", l_Persona.Get(&idx)))
4037
return ade_set_args(L, "i", -1);
4138

42-
if (Personas.empty())
43-
return ade_set_args(L, "i", -1);
44-
4539
if (!SCP_vector_inbounds(Personas, idx))
4640
return ade_set_args(L, "i", -1);
4741

@@ -51,14 +45,52 @@ ADE_VIRTVAR(Index, l_Persona, nullptr, nullptr, "number", "The index of the pers
5145
return ade_set_args(L, "i", idx + 1);
5246
}
5347

54-
ADE_FUNC(isValid, l_Persona, NULL, "Detect if the handle is valid", "boolean", "true if valid, false otherwise")
48+
ADE_VIRTVAR(CustomData, l_Persona, nullptr, "Gets the custom data table for this persona", "table", "The persona's custom data table or nil if an error occurs.")
49+
{
50+
int idx = -1;
51+
52+
if (!ade_get_args(L, "o", l_Persona.Get(&idx)))
53+
return ADE_RETURN_NIL;
54+
55+
if (!SCP_vector_inbounds(Personas, idx))
56+
return ADE_RETURN_NIL;
57+
58+
if (ADE_SETTING_VAR) {
59+
LuaError(L, "This property is read only.");
60+
}
61+
62+
auto table = luacpp::LuaTable::create(L);
63+
64+
for (const auto& pair : Personas[idx].custom_data)
65+
{
66+
table.addValue(pair.first, pair.second);
67+
}
68+
69+
return ade_set_args(L, "t", &table);
70+
}
71+
72+
ADE_FUNC(hasCustomData, l_Persona, nullptr, "Detects whether the persona has any custom data", "boolean", "true if the persona's custom_data is not empty, false otherwise. Nil if an error occurs.")
73+
{
74+
int idx = -1;
75+
76+
if (!ade_get_args(L, "o", l_Persona.Get(&idx)))
77+
return ADE_RETURN_NIL;
78+
79+
if (!SCP_vector_inbounds(Personas, idx))
80+
return ADE_RETURN_NIL;
81+
82+
bool result = !Personas[idx].custom_data.empty();
83+
return ade_set_args(L, "b", result);
84+
}
85+
86+
ADE_FUNC(isValid, l_Persona, nullptr, "Detect if the handle is valid", "boolean", "true if valid, false otherwise")
5587
{
5688
int idx = -1;
5789

5890
if (!ade_get_args(L, "o", l_Persona.Get(&idx)))
5991
return ADE_RETURN_FALSE;
6092

61-
return ade_set_args(L, "b", idx >= 0 && idx < (int)Personas.size());
93+
return ade_set_args(L, "b", SCP_vector_inbounds(Personas, idx));
6294
}
6395

6496
//**********HANDLE: Message
@@ -88,7 +120,7 @@ ADE_VIRTVAR(Message, l_Message, "string", "The unaltered text of the message, se
88120
if (!SCP_vector_inbounds(Messages, idx))
89121
return ade_set_error(L, "s", "");
90122

91-
if (ADE_SETTING_VAR && newText != NULL)
123+
if (ADE_SETTING_VAR && newText != nullptr)
92124
{
93125
if (strlen(newText) > MESSAGE_LENGTH)
94126
LuaError(L, "New message text is too long, maximum is %d!", MESSAGE_LENGTH);
@@ -151,7 +183,7 @@ ADE_VIRTVAR(Persona, l_Message, "persona", "The persona of the message", "person
151183
if (!SCP_vector_inbounds(Messages, idx))
152184
return ade_set_error(L, "o", l_Soundfile.Set(soundfile_h()));
153185

154-
if (ADE_SETTING_VAR && newPersona >= 0 && newPersona < (int)Personas.size())
186+
if (ADE_SETTING_VAR && SCP_vector_inbounds(Personas, newPersona))
155187
{
156188
Messages[idx].persona_index = newPersona;
157189
}
@@ -183,13 +215,13 @@ ADE_FUNC(getMessage, l_Message, "[boolean replaceVars = true]", "Gets the text o
183215
}
184216
}
185217

186-
ADE_FUNC(isValid, l_Message, NULL, "Checks if the message handle is valid", "boolean", "true if valid, false otherwise")
218+
ADE_FUNC(isValid, l_Message, nullptr, "Checks if the message handle is valid", "boolean", "true if valid, false otherwise")
187219
{
188220
int idx = -1;
189221
if (!ade_get_args(L, "o", l_Message.Get(&idx)))
190222
return ADE_RETURN_FALSE;
191223

192-
return ade_set_args(L, "b", idx >= 0 && idx < (int) Messages.size());
224+
return ade_set_args(L, "b", SCP_vector_inbounds(Messages, idx));
193225
}
194226

195227

0 commit comments

Comments
 (0)