mirror of
https://github.com/coop-deluxe/sm64coopdx.git
synced 2024-10-19 03:32:41 +00:00
Lua : remember up to 16 extra fields to send on spawn lua object
This commit is contained in:
parent
9538c551e4
commit
41275835e7
4 changed files with 67 additions and 6 deletions
|
@ -9,6 +9,7 @@
|
|||
#include "pc/lua/smlua.h"
|
||||
#include "pc/lua/utils/smlua_anim_utils.h"
|
||||
#include "pc/lua/utils/smlua_collision_utils.h"
|
||||
#include "pc/lua/utils/smlua_obj_utils.h"
|
||||
#include "pc/mods/mods.h"
|
||||
|
||||
#define LUA_VEC3S_FIELD_COUNT 3
|
||||
|
@ -468,6 +469,12 @@ static int smlua__set_field(lua_State* L) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
if (data->immutable) {
|
||||
LOG_LUA("_set_field on immutable key '%s'", key);
|
||||
smlua_logline();
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((u32)lot == (u32)LOT_OBJECT) {
|
||||
struct Object* obj = (struct Object*)pointer;
|
||||
if (!smlua_is_valid_object_field(obj, data)) {
|
||||
|
@ -475,12 +482,9 @@ static int smlua__set_field(lua_State* L) {
|
|||
smlua_logline();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (data->immutable) {
|
||||
LOG_LUA("_set_field on immutable key '%s'", key);
|
||||
smlua_logline();
|
||||
return 0;
|
||||
if (gSpawningObject) {
|
||||
spawn_object_remember_field(data);
|
||||
}
|
||||
}
|
||||
|
||||
void* valuePointer = NULL;
|
||||
|
|
|
@ -8,6 +8,30 @@
|
|||
#include "smlua_model_utils.h"
|
||||
#include "pc/debuglog.h"
|
||||
|
||||
#define MAX_SPAWN_OBJECT_FIELDS 16
|
||||
struct LuaObjectField gSpawnObjectFields[MAX_SPAWN_OBJECT_FIELDS] = { 0 };
|
||||
u8 gSpawnObjectFieldCount = 0;
|
||||
bool gSpawningObject = false;
|
||||
|
||||
void spawn_object_remember_field(struct LuaObjectField* field) {
|
||||
// make sure it's a field that isn't normally sent in spawn object packet
|
||||
if (field->valueOffset < (sizeof(s32) * 80)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// check for duplicates
|
||||
for (int i = 0; i < gSpawnObjectFieldCount; i++) {
|
||||
if (field->valueOffset == gSpawnObjectFields[i].valueOffset) { return; }
|
||||
}
|
||||
|
||||
// remember field
|
||||
memcpy(&gSpawnObjectFields[gSpawnObjectFieldCount], field, sizeof(struct LuaObjectField));
|
||||
gSpawnObjectFieldCount++;
|
||||
if (gSpawnObjectFieldCount >= MAX_SPAWN_OBJECT_FIELDS) {
|
||||
gSpawnObjectFieldCount = MAX_SPAWN_OBJECT_FIELDS - 1;
|
||||
}
|
||||
}
|
||||
|
||||
static struct Object* spawn_object_internal(enum BehaviorId behaviorId, enum ModelExtendedId modelId, f32 x, f32 y, f32 z, LuaFunction objSetupFunction, bool doSync) {
|
||||
if (doSync) {
|
||||
// prevent spawning objects before area is synchronized
|
||||
|
@ -53,6 +77,8 @@ static struct Object* spawn_object_internal(enum BehaviorId behaviorId, enum Mod
|
|||
if (!doSync) { obj->coopFlags |= COOP_OBJ_FLAG_NON_SYNC; }
|
||||
|
||||
if (objSetupFunction != 0) {
|
||||
gSpawningObject = true;
|
||||
gSpawnObjectFieldCount = 0;
|
||||
lua_State* L = gLuaState;
|
||||
lua_rawgeti(L, LUA_REGISTRYINDEX, objSetupFunction);
|
||||
smlua_push_object(L, LOT_OBJECT, obj);
|
||||
|
@ -72,6 +98,9 @@ static struct Object* spawn_object_internal(enum BehaviorId behaviorId, enum Mod
|
|||
network_send_spawn_objects(spawn_objects, models, 1);
|
||||
}
|
||||
|
||||
gSpawningObject = false;
|
||||
gSpawnObjectFieldCount = 0;
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,11 @@
|
|||
#include "smlua_model_utils.h"
|
||||
#include "game/object_list_processor.h"
|
||||
|
||||
extern struct LuaObjectField gSpawnObjectFields[];
|
||||
extern u8 gSpawnObjectFieldCount;
|
||||
extern bool gSpawningObject;
|
||||
void spawn_object_remember_field(struct LuaObjectField* field);
|
||||
|
||||
struct Object* spawn_sync_object(enum BehaviorId behaviorId, enum ModelExtendedId modelId, f32 x, f32 y, f32 z, LuaFunction objSetupFunction);
|
||||
struct Object* spawn_non_sync_object(enum BehaviorId behaviorId, enum ModelExtendedId modelId, f32 x, f32 y, f32 z, LuaFunction objSetupFunction);
|
||||
|
||||
|
|
|
@ -7,7 +7,9 @@
|
|||
#include "src/game/area.h"
|
||||
#include "behavior_data.h"
|
||||
#include "behavior_table.h"
|
||||
#include "src/pc/lua/smlua.h"
|
||||
#include "src/pc/lua/utils/smlua_model_utils.h"
|
||||
#include "src/pc/lua/utils/smlua_obj_utils.h"
|
||||
//#define DISABLE_MODULE_LOG 1
|
||||
#include "pc/debuglog.h"
|
||||
|
||||
|
@ -22,6 +24,7 @@ struct SpawnObjectData {
|
|||
s32 rawData[80];
|
||||
u8 globalPlayerIndex;
|
||||
u16 extendedModelId;
|
||||
u8 extraFieldCount;
|
||||
};
|
||||
#pragma pack()
|
||||
|
||||
|
@ -87,6 +90,13 @@ void network_send_spawn_objects_to(u8 sendToLocalIndex, struct Object* objects[]
|
|||
packet_write(&p, &o->header.gfx.scale[2], sizeof(f32));
|
||||
packet_write(&p, &o->globalPlayerIndex, sizeof(u8));
|
||||
packet_write(&p, &extendedModelId, sizeof(u16));
|
||||
packet_write(&p, &gSpawnObjectFieldCount, sizeof(u8));
|
||||
for (u8 j = 0; j < gSpawnObjectFieldCount; j++) {
|
||||
struct LuaObjectField* field = &gSpawnObjectFields[j];
|
||||
u64 valueOffset = field->valueOffset;
|
||||
packet_write(&p, &valueOffset, sizeof(u64));
|
||||
packet_write(&p, (&o->rawData.asU32[0] + field->valueOffset), sizeof(u32));
|
||||
}
|
||||
}
|
||||
|
||||
if (sendToLocalIndex == PACKET_DESTINATION_BROADCAST) {
|
||||
|
@ -123,6 +133,7 @@ void network_receive_spawn_objects(struct Packet* p) {
|
|||
packet_read(p, &scale[2], sizeof(f32));
|
||||
packet_read(p, &data.globalPlayerIndex, sizeof(u8));
|
||||
packet_read(p, &data.extendedModelId, sizeof(u16));
|
||||
packet_read(p, &data.extraFieldCount, sizeof(u8));
|
||||
|
||||
struct Object* parentObj = NULL;
|
||||
if (data.parentId == (u8)-1) {
|
||||
|
@ -181,6 +192,18 @@ void network_receive_spawn_objects(struct Packet* p) {
|
|||
// correct the temporary parent with the object itself
|
||||
if (data.parentId == (u8)-1) { o->parentObj = o; }
|
||||
|
||||
// get extra fields
|
||||
for (int j = 0; j < data.extraFieldCount; j++) {
|
||||
u64 valueOffset;
|
||||
u32 value;
|
||||
packet_read(p, &valueOffset, sizeof(u64));
|
||||
if (valueOffset < (u64)(&o->rawData.asU32[0] - &o->rawData.asU32[0x50])) {
|
||||
packet_read(p, (&o->rawData.asU32[0] + valueOffset), sizeof(u32));
|
||||
} else {
|
||||
packet_read(p, &value, sizeof(u32));
|
||||
}
|
||||
}
|
||||
|
||||
if (o->oSyncID != 0 && o->oSyncID >= RESERVED_IDS_SYNC_OBJECT_OFFSET) {
|
||||
if (o->oSyncID >= MAX_SYNC_OBJECTS) {
|
||||
LOG_ERROR("Invalid spawn object sync id: %u", o->oSyncID);
|
||||
|
|
Loading…
Reference in a new issue