From 6c0757c90833095e0d47d487dc49ac9aeaabcdb7 Mon Sep 17 00:00:00 2001
From: Agent X <44549182+Agent-11@users.noreply.github.com>
Date: Mon, 26 Sep 2022 22:11:51 -0400
Subject: [PATCH] Color type and palette/color functions (#199)
Added a Color type (typedef u8 Color[3])
Added network_player_color_to_palette and network_player_palette_to_color
Reran autogen
---
autogen/common.py | 2 +-
autogen/convert_functions.py | 18 ++++++++++
autogen/convert_structs.py | 3 +-
autogen/lua_definitions/functions.lua | 16 +++++++++
autogen/lua_definitions/structs.lua | 5 +++
docs/lua/functions-3.md | 44 ++++++++++++++++++++++++
docs/lua/functions.md | 2 ++
docs/lua/structs.md | 13 ++++++++
include/types.h | 2 ++
src/pc/lua/smlua_cobject.h | 1 +
src/pc/lua/smlua_functions_autogen.c | 48 +++++++++++++++++++++++++++
src/pc/lua/smlua_utils.c | 9 +++++
src/pc/lua/smlua_utils.h | 1 +
src/pc/network/network_player.c | 24 ++++++++++++++
src/pc/network/network_player.h | 3 ++
15 files changed, 189 insertions(+), 2 deletions(-)
diff --git a/autogen/common.py b/autogen/common.py
index 79d1207a..8ef5d75c 100644
--- a/autogen/common.py
+++ b/autogen/common.py
@@ -1,7 +1,7 @@
import os
usf_types = ['u8', 'u16', 'u32', 'u64', 's8', 's16', 's32', 's64', 'f32']
-vec3_types = ['Vec3s', 'Vec3f']
+vec3_types = ['Vec3s', 'Vec3f', 'Color']
typedef_pointers = ['BehaviorScript', 'ObjectAnimPointer', 'Collision', 'LevelScript', 'Trajectory']
exclude_structs = [
diff --git a/autogen/convert_functions.py b/autogen/convert_functions.py
index 449cf62c..3e25b2c5 100644
--- a/autogen/convert_functions.py
+++ b/autogen/convert_functions.py
@@ -155,6 +155,24 @@ param_override_build['Vec3s'] = {
'after': param_vec3s_after_call
}
+param_color_before_call = """
+ u8* $[IDENTIFIER] = smlua_get_color_from_buffer();
+ $[IDENTIFIER][0] = smlua_get_integer_field($[INDEX], "r");
+ $[IDENTIFIER][1] = smlua_get_integer_field($[INDEX], "g");
+ $[IDENTIFIER][2] = smlua_get_integer_field($[INDEX], "b");
+"""
+
+param_color_after_call = """
+ smlua_push_integer_field($[INDEX], "r", $[IDENTIFIER][0]);
+ smlua_push_integer_field($[INDEX], "g", $[IDENTIFIER][1]);
+ smlua_push_integer_field($[INDEX], "b", $[IDENTIFIER][2]);
+"""
+
+param_override_build['Color'] = {
+ 'before': param_color_before_call,
+ 'after': param_color_after_call
+}
+
############################################################################
manual_index_documentation = """
diff --git a/autogen/convert_structs.py b/autogen/convert_structs.py
index ca82153e..bb15a278 100644
--- a/autogen/convert_structs.py
+++ b/autogen/convert_structs.py
@@ -98,7 +98,8 @@ sLuaManuallyDefinedStructs = [{
'path': 'n/a',
'structs': [
'struct Vec3f { float x; float y; float z; }',
- 'struct Vec3s { s16 x; s16 y; s16 z; }'
+ 'struct Vec3s { s16 x; s16 y; s16 z; }',
+ 'struct Color { u8 r; u8 g; u8 b; }'
]
}]
diff --git a/autogen/lua_definitions/functions.lua b/autogen/lua_definitions/functions.lua
index be1df1fb..9f7761aa 100644
--- a/autogen/lua_definitions/functions.lua
+++ b/autogen/lua_definitions/functions.lua
@@ -5233,6 +5233,14 @@ function get_network_player_smallest_global()
-- ...
end
+--- @param np NetworkPlayer
+--- @param part PlayerParts
+--- @param color Color
+--- @return nil
+function network_player_color_to_palette(np, part, color)
+ -- ...
+end
+
--- @return integer
function network_player_connected_count()
-- ...
@@ -5244,6 +5252,14 @@ function network_player_from_global_index(globalIndex)
-- ...
end
+--- @param np NetworkPlayer
+--- @param part PlayerParts
+--- @param out Color
+--- @return nil
+function network_player_palette_to_color(np, part, out)
+ -- ...
+end
+
--- @param np NetworkPlayer
--- @param description string
--- @param r integer
diff --git a/autogen/lua_definitions/structs.lua b/autogen/lua_definitions/structs.lua
index 96e87435..f67e0da9 100644
--- a/autogen/lua_definitions/structs.lua
+++ b/autogen/lua_definitions/structs.lua
@@ -1804,6 +1804,11 @@
--- @field public y integer
--- @field public z integer
+--- @class Color
+--- @field public b integer
+--- @field public g integer
+--- @field public r integer
+
--- @class Pointer_integer
--- @class Pointer_Trajectory
--- @class Pointer_LevelScript
diff --git a/docs/lua/functions-3.md b/docs/lua/functions-3.md
index f9b4e602..d7707e79 100644
--- a/docs/lua/functions-3.md
+++ b/docs/lua/functions-3.md
@@ -7164,6 +7164,28 @@
+## [network_player_color_to_palette](#network_player_color_to_palette)
+
+### Lua Example
+`network_player_color_to_palette(np, part, color)`
+
+### Parameters
+| Field | Type |
+| ----- | ---- |
+| np | [NetworkPlayer](structs.md#NetworkPlayer) |
+| part | [enum PlayerParts](constants.md#enum-PlayerParts) |
+| color | `Color` |
+
+### Returns
+- None
+
+### C Prototype
+`void network_player_color_to_palette(struct NetworkPlayer *np, enum PlayerParts part, Color color);`
+
+[:arrow_up_small:](#)
+
+
+
## [network_player_connected_count](#network_player_connected_count)
### Lua Example
@@ -7202,6 +7224,28 @@
+## [network_player_palette_to_color](#network_player_palette_to_color)
+
+### Lua Example
+`network_player_palette_to_color(np, part, out)`
+
+### Parameters
+| Field | Type |
+| ----- | ---- |
+| np | [NetworkPlayer](structs.md#NetworkPlayer) |
+| part | [enum PlayerParts](constants.md#enum-PlayerParts) |
+| out | `Color` |
+
+### Returns
+- None
+
+### C Prototype
+`void network_player_palette_to_color(struct NetworkPlayer *np, enum PlayerParts part, Color out);`
+
+[:arrow_up_small:](#)
+
+
+
## [network_player_set_description](#network_player_set_description)
### Lua Example
diff --git a/docs/lua/functions.md b/docs/lua/functions.md
index ba5415e2..8da00408 100644
--- a/docs/lua/functions.md
+++ b/docs/lua/functions.md
@@ -1016,8 +1016,10 @@
- [get_network_player_from_area](functions-3.md#get_network_player_from_area)
- [get_network_player_from_level](functions-3.md#get_network_player_from_level)
- [get_network_player_smallest_global](functions-3.md#get_network_player_smallest_global)
+ - [network_player_color_to_palette](functions-3.md#network_player_color_to_palette)
- [network_player_connected_count](functions-3.md#network_player_connected_count)
- [network_player_from_global_index](functions-3.md#network_player_from_global_index)
+ - [network_player_palette_to_color](functions-3.md#network_player_palette_to_color)
- [network_player_set_description](functions-3.md#network_player_set_description)
diff --git a/docs/lua/structs.md b/docs/lua/structs.md
index 52b887ad..aaa3d3ff 100644
--- a/docs/lua/structs.md
+++ b/docs/lua/structs.md
@@ -15,6 +15,7 @@
- [CameraTrigger](#CameraTrigger)
- [ChainSegment](#ChainSegment)
- [Character](#Character)
+- [Color](#Color)
- [Controller](#Controller)
- [CustomLevelInfo](#CustomLevelInfo)
- [Cutscene](#Cutscene)
@@ -489,6 +490,18 @@
+## [Color](#Color)
+
+| Field | Type | Access |
+| ----- | ---- | ------ |
+| b | `integer` | |
+| g | `integer` | |
+| r | `integer` | |
+
+[:arrow_up_small:](#)
+
+
+
## [Controller](#Controller)
| Field | Type | Access |
diff --git a/include/types.h b/include/types.h
index c07d6f32..ca5b36af 100644
--- a/include/types.h
+++ b/include/types.h
@@ -54,6 +54,8 @@ typedef s16 PaintingData;
typedef uintptr_t BehaviorScript;
typedef u8 Texture;
+typedef u8 Color[3];
+
enum SpTaskState {
SPTASK_STATE_NOT_STARTED,
SPTASK_STATE_RUNNING,
diff --git a/src/pc/lua/smlua_cobject.h b/src/pc/lua/smlua_cobject.h
index 7c0a4843..a1fa1028 100644
--- a/src/pc/lua/smlua_cobject.h
+++ b/src/pc/lua/smlua_cobject.h
@@ -42,6 +42,7 @@ enum LuaObjectType {
LOT_NONE = 0,
LOT_VEC3S,
LOT_VEC3F,
+ LOT_COLOR,
LOT_POINTER,
LOT_MAX,
};
diff --git a/src/pc/lua/smlua_functions_autogen.c b/src/pc/lua/smlua_functions_autogen.c
index b4fe52f9..7699bf83 100644
--- a/src/pc/lua/smlua_functions_autogen.c
+++ b/src/pc/lua/smlua_functions_autogen.c
@@ -11762,6 +11762,29 @@ int smlua_func_get_network_player_smallest_global(UNUSED lua_State* L) {
return 1;
}
+int smlua_func_network_player_color_to_palette(lua_State* L) {
+ if(!smlua_functions_valid_param_count(L, 3)) { return 0; }
+
+ struct NetworkPlayer* np = (struct NetworkPlayer*)smlua_to_cobject(L, 1, LOT_NETWORKPLAYER);
+ if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter 1 for function 'network_player_color_to_palette'"); return 0; }
+ int part = smlua_to_integer(L, 2);
+ if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter 2 for function 'network_player_color_to_palette'"); return 0; }
+
+ u8* color = smlua_get_color_from_buffer();
+ color[0] = smlua_get_integer_field(3, "r");
+ color[1] = smlua_get_integer_field(3, "g");
+ color[2] = smlua_get_integer_field(3, "b");
+ if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter 3 for function 'network_player_color_to_palette'"); return 0; }
+
+ network_player_color_to_palette(np, part, color);
+
+ smlua_push_integer_field(3, "r", color[0]);
+ smlua_push_integer_field(3, "g", color[1]);
+ smlua_push_integer_field(3, "b", color[2]);
+
+ return 1;
+}
+
int smlua_func_network_player_connected_count(UNUSED lua_State* L) {
if(!smlua_functions_valid_param_count(L, 0)) { return 0; }
@@ -11782,6 +11805,29 @@ int smlua_func_network_player_from_global_index(lua_State* L) {
return 1;
}
+int smlua_func_network_player_palette_to_color(lua_State* L) {
+ if(!smlua_functions_valid_param_count(L, 3)) { return 0; }
+
+ struct NetworkPlayer* np = (struct NetworkPlayer*)smlua_to_cobject(L, 1, LOT_NETWORKPLAYER);
+ if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter 1 for function 'network_player_palette_to_color'"); return 0; }
+ int part = smlua_to_integer(L, 2);
+ if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter 2 for function 'network_player_palette_to_color'"); return 0; }
+
+ u8* out = smlua_get_color_from_buffer();
+ out[0] = smlua_get_integer_field(3, "r");
+ out[1] = smlua_get_integer_field(3, "g");
+ out[2] = smlua_get_integer_field(3, "b");
+ if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter 3 for function 'network_player_palette_to_color'"); return 0; }
+
+ network_player_palette_to_color(np, part, out);
+
+ smlua_push_integer_field(3, "r", out[0]);
+ smlua_push_integer_field(3, "g", out[1]);
+ smlua_push_integer_field(3, "b", out[2]);
+
+ return 1;
+}
+
int smlua_func_network_player_set_description(lua_State* L) {
if(!smlua_functions_valid_param_count(L, 6)) { return 0; }
@@ -19135,8 +19181,10 @@ void smlua_bind_functions_autogen(void) {
smlua_bind_function(L, "get_network_player_from_area", smlua_func_get_network_player_from_area);
smlua_bind_function(L, "get_network_player_from_level", smlua_func_get_network_player_from_level);
smlua_bind_function(L, "get_network_player_smallest_global", smlua_func_get_network_player_smallest_global);
+ smlua_bind_function(L, "network_player_color_to_palette", smlua_func_network_player_color_to_palette);
smlua_bind_function(L, "network_player_connected_count", smlua_func_network_player_connected_count);
smlua_bind_function(L, "network_player_from_global_index", smlua_func_network_player_from_global_index);
+ smlua_bind_function(L, "network_player_palette_to_color", smlua_func_network_player_palette_to_color);
smlua_bind_function(L, "network_player_set_description", smlua_func_network_player_set_description);
// network_utils.h
diff --git a/src/pc/lua/smlua_utils.c b/src/pc/lua/smlua_utils.c
index 07f2be2a..cdeafebb 100644
--- a/src/pc/lua/smlua_utils.c
+++ b/src/pc/lua/smlua_utils.c
@@ -12,6 +12,10 @@ static u8 sVec3fBufferIndex = 0;
static Vec3s sVec3sBuffer[VEC3S_BUFFER_COUNT] = { 0 };
static u8 sVec3sBufferIndex = 0;
+#define COLOR_BUFFER_COUNT 64
+static Color sColorBuffer[COLOR_BUFFER_COUNT] = { 0 };
+static u8 sColorBufferIndex = 0;
+
f32* smlua_get_vec3f_from_buffer(void) {
if (sVec3fBufferIndex >= VEC3F_BUFFER_COUNT) { sVec3fBufferIndex = 0; }
return sVec3fBuffer[sVec3fBufferIndex++];
@@ -22,6 +26,11 @@ s16* smlua_get_vec3s_from_buffer(void) {
return sVec3sBuffer[sVec3sBufferIndex++];
}
+u8* smlua_get_color_from_buffer(void) {
+ if (sColorBufferIndex >= COLOR_BUFFER_COUNT) { sColorBufferIndex = 0; }
+ return sColorBuffer[sColorBufferIndex++];
+}
+
f32 *smlua_get_vec3f_for_play_sound(f32 *pos) {
if (pos < (f32 *) sVec3fBuffer || pos >= (f32 *) (sVec3fBuffer + VEC3F_BUFFER_COUNT)) {
return pos;
diff --git a/src/pc/lua/smlua_utils.h b/src/pc/lua/smlua_utils.h
index b50399b6..ed2a4e0a 100644
--- a/src/pc/lua/smlua_utils.h
+++ b/src/pc/lua/smlua_utils.h
@@ -8,6 +8,7 @@ struct LSTNetworkType;
f32* smlua_get_vec3f_from_buffer(void);
s16* smlua_get_vec3s_from_buffer(void);
+u8* smlua_get_color_from_buffer(void);
void smlua_bind_function(lua_State* L, const char* name, void* func);
bool smlua_is_table_empty(int index);
diff --git a/src/pc/network/network_player.c b/src/pc/network/network_player.c
index 77236172..0dfdaf58 100644
--- a/src/pc/network/network_player.c
+++ b/src/pc/network/network_player.c
@@ -122,6 +122,30 @@ struct NetworkPlayer *get_network_player_smallest_global(void) {
return smallest;
}
+void network_player_color_to_palette(struct NetworkPlayer *np, enum PlayerParts part, Color color) {
+ if (np == NULL || !(part < PLAYER_PART_MAX && part >= 0)) { return; }
+
+ np->palette.parts[part][0] = color[0];
+ np->palette.parts[part][1] = color[1];
+ np->palette.parts[part][2] = color[2];
+ np->overridePalette = np->palette;
+}
+
+void network_player_palette_to_color(struct NetworkPlayer *np, enum PlayerParts part, Color out) {
+ if (np == NULL || !(part < PLAYER_PART_MAX && part >= 0)) {
+ if (np == NULL) { // output config palette instead if np is NULL
+ out[0] = configPlayerPalette.parts[part][0];
+ out[1] = configPlayerPalette.parts[part][1];
+ out[2] = configPlayerPalette.parts[part][2];
+ }
+ return;
+ }
+
+ out[0] = np->palette.parts[part][0];
+ out[1] = np->palette.parts[part][1];
+ out[2] = np->palette.parts[part][2];
+}
+
void network_player_update(void) {
for (s32 i = 0; i < MAX_PLAYERS; i++) {
struct NetworkPlayer *np = &gNetworkPlayers[i];
diff --git a/src/pc/network/network_player.h b/src/pc/network/network_player.h
index 1e4b05ce..dbb4e3c6 100644
--- a/src/pc/network/network_player.h
+++ b/src/pc/network/network_player.h
@@ -73,6 +73,9 @@ struct NetworkPlayer* get_network_player_from_level(s16 courseNum, s16 actNum, s
struct NetworkPlayer* get_network_player_from_area(s16 courseNum, s16 actNum, s16 levelNum, s16 areaIndex);
struct NetworkPlayer* get_network_player_smallest_global(void);
+void network_player_color_to_palette(struct NetworkPlayer *np, enum PlayerParts part, Color color);
+void network_player_palette_to_color(struct NetworkPlayer *np, enum PlayerParts part, Color out);
+
void network_player_update(void);
u8 network_player_connected(enum NetworkPlayerType type, u8 globalIndex, u8 modelIndex, const struct PlayerPalette* playerPalette, char* name);