From 2a78404ef5cd04e8bae04e7a95beeb4fe6a1146b Mon Sep 17 00:00:00 2001 From: Fazana <52551480+FazanaJ@users.noreply.github.com> Date: Tue, 7 Sep 2021 12:13:40 +0100 Subject: [PATCH 01/18] Puppylights --- actors/mario/model.inc.c | 12 +- include/config.h | 2 + include/level_commands.h | 1 + include/object_constants.h | 5 +- include/object_fields.h | 3 + include/types.h | 60 ++++--- src/boot/main.c | 4 + src/engine/behavior_script.c | 5 + src/engine/level_script.c | 56 ++++++- src/game/behavior_actions.c | 1 + src/game/level_update.c | 28 +++- src/game/obj_behaviors.c | 1 + src/game/obj_behaviors_2.c | 1 + src/game/puppylights.c | 311 +++++++++++++++++++++++++++++++++++ src/game/puppylights.h | 50 ++++++ src/game/spawn_object.c | 1 + 16 files changed, 505 insertions(+), 36 deletions(-) create mode 100644 src/game/puppylights.c create mode 100644 src/game/puppylights.h diff --git a/actors/mario/model.inc.c b/actors/mario/model.inc.c index 9fccd9c7..9fdf3bd4 100644 --- a/actors/mario/model.inc.c +++ b/actors/mario/model.inc.c @@ -1,37 +1,37 @@ // Mario // 0x04000000 # solid color blue - butt, left thigh, right thigh - all poly types -static const Lights1 mario_blue_lights_group = gdSPDefLights1( +const Lights1 mario_blue_lights_group = gdSPDefLights1( 0x00, 0x00, 0x7f, 0x00, 0x00, 0xff, 0x28, 0x28, 0x28 ); // 0x04000018 # solid color red - left & right arm, torso (tshirt part), caps - all poly types -static const Lights1 mario_red_lights_group = gdSPDefLights1( +const Lights1 mario_red_lights_group = gdSPDefLights1( 0x7f, 0x00, 0x00, 0xff, 0x00, 0x00, 0x28, 0x28, 0x28 ); // 0x04000030 # solid color white - metal butt & left thigh - normal left & right hand closed & open (with cap too) and all wings - all poly types -static const Lights1 mario_white_lights_group = gdSPDefLights1( +const Lights1 mario_white_lights_group = gdSPDefLights1( 0x7f, 0x7f, 0x7f, 0xff, 0xff, 0xff, 0x28, 0x28, 0x28 ); // 0x04000048 # solid color brown 1 - foot - all poly types -static const Lights1 mario_brown1_lights_group = gdSPDefLights1( +const Lights1 mario_brown1_lights_group = gdSPDefLights1( 0x39, 0x0e, 0x07, 0x72, 0x1c, 0x0e, 0x28, 0x28, 0x28 ); // 0x04000060 # solid color beige skin - face (cap on and off dls) - all poly types -static const Lights1 mario_beige_lights_group = gdSPDefLights1( +const Lights1 mario_beige_lights_group = gdSPDefLights1( 0x7f, 0x60, 0x3c, 0xfe, 0xc1, 0x79, 0x28, 0x28, 0x28 ); // 0x04000078 # solid color brown 2 - hair - all poly types -static const Lights1 mario_brown2_lights_group = gdSPDefLights1( +const Lights1 mario_brown2_lights_group = gdSPDefLights1( 0x39, 0x03, 0x00, 0x73, 0x06, 0x00, 0x28, 0x28, 0x28 ); diff --git a/include/config.h b/include/config.h index 0b0cf0bf..730ed557 100644 --- a/include/config.h +++ b/include/config.h @@ -127,6 +127,8 @@ //#define VISUAL_DEBUG // Number of supported areas per level. #define AREA_COUNT 8 +// Lightweight directional lighting engine by Fazana. Intended for giving proximity and positional pointlights to small objects. +#define PUPPYLIGHTS // BUG/GAME QOL FIXES // Fix instant warp offset not working when warping across different areas diff --git a/include/level_commands.h b/include/level_commands.h index 940706a4..07328e0e 100644 --- a/include/level_commands.h +++ b/include/level_commands.h @@ -5,6 +5,7 @@ #include "level_table.h" #include "config.h" +#include "game/puppylights.h" #define OP_AND 0 #define OP_NAND 1 diff --git a/include/object_constants.h b/include/object_constants.h index cca7c870..17a18c01 100644 --- a/include/object_constants.h +++ b/include/object_constants.h @@ -47,6 +47,7 @@ #ifdef AUTO_COLLISION_DISTANCE #define OBJ_FLAG_DONT_CALC_COLL_DIST (1 << 16) // 0x00010000 #endif +#define OBJ_FLAG_EMIT_LIGHT (1 << 17) // 0x00020000 #define OBJ_FLAG_HITBOX_WAS_SET (1 << 30) // 0x40000000 /* oHeldState */ @@ -283,7 +284,7 @@ #define BOWSER_SUB_ACT_HIT_MINE_START 0 #define BOWSER_SUB_ACT_HIT_MINE_FALL 1 #define BOWSER_SUB_ACT_HIT_MINE_STOP 2 - + #define BOWSER_SUB_ACT_JUMP_ON_STAGE_IDLE 0 #define BOWSER_SUB_ACT_JUMP_ON_STAGE_START 1 #define BOWSER_SUB_ACT_JUMP_ON_STAGE_LAND 2 @@ -299,7 +300,7 @@ /* oAction */ #define FISH_SPAWNER_ACT_SPAWN 0 #define FISH_SPAWNER_ACT_IDLE 1 - #define FISH_SPAWNER_ACT_RESPAWN 2 + #define FISH_SPAWNER_ACT_RESPAWN 2 /* oBehParams2ndByte */ #define FISH_SPAWNER_BP_MANY_BLUE 0 #define FISH_SPAWNER_BP_FEW_BLUE 1 diff --git a/include/object_fields.h b/include/object_fields.h index f474fe29..922abb14 100644 --- a/include/object_fields.h +++ b/include/object_fields.h @@ -134,6 +134,9 @@ #define /*0x1BC*/ oAngleToHome OBJECT_FIELD_S32(0x4D) #define /*0x1C0*/ oFloor OBJECT_FIELD_SURFACE(0x4E) #define /*0x1C4*/ oDeathSound OBJECT_FIELD_S32(0x4F) +#ifdef PUPPYLIGHTS +#define /*0x1C4*/ oLightID OBJECT_FIELD_S32(0x50) +#endif /* Pathed (see obj_follow_path) */ #define /*0x0FC*/ oPathedStartWaypoint OBJECT_FIELD_WAYPOINT(0x1D) diff --git a/include/types.h b/include/types.h index f2467ea0..32aed982 100644 --- a/include/types.h +++ b/include/types.h @@ -147,9 +147,22 @@ struct ObjectNode struct ObjectNode *prev; }; +#ifdef PUPPYLIGHTS +struct PuppyLight { + Vec3t pos[2]; //The location of the light. First index is the absolute position, second index are offsets. + s16 yaw; //Used by cubes. Allows epic rotating of the volume. + s8 epicentre; //What percentage inside the volume you'll be before maximum light strength is applied. (E.g: 100 will be full strength always, and 0 will be full strength at the centre.) + u8 flags; //Some stuff to define how the volume is used. Mostly just shape stuff, but can potentially have other uses. + u8 rgba[4]; //Colour. Go on, take even the tiniest guess as to what this entails. + u8 active:1; //Whether the light will actually work. Mostly intended to be used for objects. +}; +#endif + // NOTE: Since ObjectNode is the first member of Object, it is difficult to determine // whether some of these pointers point to ObjectNode or Object. +#define MAX_OBJECT_FIELDS 0x51 + struct Object { /*0x000*/ struct ObjectNode header; @@ -163,33 +176,33 @@ struct Object union { // Object fields. See object_fields.h. - u32 asU32[0x50]; - s32 asS32[0x50]; - s16 asS16[0x50][2]; - f32 asF32[0x50]; + u32 asU32[MAX_OBJECT_FIELDS]; + s32 asS32[MAX_OBJECT_FIELDS]; + s16 asS16[MAX_OBJECT_FIELDS][2]; + f32 asF32[MAX_OBJECT_FIELDS]; #if !IS_64_BIT - s16 *asS16P[0x50]; - s32 *asS32P[0x50]; - struct Animation **asAnims[0x50]; - struct Waypoint *asWaypoint[0x50]; - struct ChainSegment *asChainSegment[0x50]; - struct Object *asObject[0x50]; - struct Surface *asSurface[0x50]; - void *asVoidPtr[0x50]; - const void *asConstVoidPtr[0x50]; + s16 *asS16P[MAX_OBJECT_FIELDS]; + s32 *asS32P[MAX_OBJECT_FIELDS]; + struct Animation **asAnims[MAX_OBJECT_FIELDS]; + struct Waypoint *asWaypoint[MAX_OBJECT_FIELDS]; + struct ChainSegment *asChainSegment[MAX_OBJECT_FIELDS]; + struct Object *asObject[MAX_OBJECT_FIELDS]; + struct Surface *asSurface[MAX_OBJECT_FIELDS]; + void *asVoidPtr[MAX_OBJECT_FIELDS]; + const void *asConstVoidPtr[MAX_OBJECT_FIELDS]; #endif } rawData; #if IS_64_BIT union { - s16 *asS16P[0x50]; - s32 *asS32P[0x50]; - struct Animation **asAnims[0x50]; - struct Waypoint *asWaypoint[0x50]; - struct ChainSegment *asChainSegment[0x50]; - struct Object *asObject[0x50]; - struct Surface *asSurface[0x50]; - void *asVoidPtr[0x50]; - const void *asConstVoidPtr[0x50]; + s16 *asS16P[MAX_OBJECT_FIELDS]; + s32 *asS32P[MAX_OBJECT_FIELDS]; + struct Animation **asAnims[MAX_OBJECT_FIELDS]; + struct Waypoint *asWaypoint[MAX_OBJECT_FIELDS]; + struct ChainSegment *asChainSegment[MAX_OBJECT_FIELDS]; + struct Object *asObject[MAX_OBJECT_FIELDS]; + struct Surface *asSurface[MAX_OBJECT_FIELDS]; + void *asVoidPtr[MAX_OBJECT_FIELDS]; + const void *asConstVoidPtr[MAX_OBJECT_FIELDS]; } ptrData; #endif /*0x1C8*/ u32 unused1; @@ -209,6 +222,9 @@ struct Object /*0x218*/ void *collisionData; /*0x21C*/ Mat4 transform; /*0x25C*/ void *respawnInfo; +#ifdef PUPPYLIGHTS + struct PuppyLight puppylight; +#endif }; struct ObjectHitbox diff --git a/src/boot/main.c b/src/boot/main.c index d27d0b5a..c4e3b8f6 100644 --- a/src/boot/main.c +++ b/src/boot/main.c @@ -19,6 +19,7 @@ #include "usb/debug.h" #endif #include "game/puppyprint.h" +#include "game/puppylights.h" // Message IDs #define MESG_SP_COMPLETE 100 @@ -115,6 +116,9 @@ void alloc_pool(void) { main_pool_init(start, end); gEffectsMemoryPool = mem_pool_init(0x4000, MEMORY_POOL_LEFT); + #ifdef PUPPYLIGHTS + gLightsPool = mem_pool_init(PUPPYLIGHTS_POOL, MEMORY_POOL_LEFT); + #endif } void create_thread(OSThread *thread, OSId id, void (*entry)(void *), void *arg, void *sp, OSPri pri) { diff --git a/src/engine/behavior_script.c b/src/engine/behavior_script.c index 5617e5b9..001da0ba 100644 --- a/src/engine/behavior_script.c +++ b/src/engine/behavior_script.c @@ -13,6 +13,7 @@ #include "game/object_list_processor.h" #include "graph_node.h" #include "surface_collision.h" +#include "game/puppylights.h" // Macros for retrieving arguments from behavior scripts. #define BHV_CMD_GET_1ST_U8(index) (u8)((gCurBhvCommand[index] >> 24) & 0xFF) // unused @@ -982,6 +983,10 @@ void cur_obj_update(void) { obj_update_gfx_pos_and_angle(gCurrentObject); } +#ifdef PUPPYLIGHTS + puppylights_object_emit(gCurrentObject); +#endif + // Handle visibility of object if (gCurrentObject->oRoom != -1) { // If the object is in a room, only show it when Mario is in the room. diff --git a/src/engine/level_script.c b/src/engine/level_script.c index e656482e..becb9bf5 100644 --- a/src/engine/level_script.c +++ b/src/engine/level_script.c @@ -24,7 +24,10 @@ #include "math_util.h" #include "surface_collision.h" #include "surface_load.h" +#include "string.h" #include "game/puppycam2.h" +#include "game/puppyprint.h" +#include "game/puppylights.h" #include "config.h" @@ -801,6 +804,9 @@ static void level_cmd_puppyvolume(void) { sCurrentCmd = CMD_NEXT; gPuppyError |= PUPPY_ERROR_POOL_FULL; + #if PUPPYPRINT_DEBUG + append_puppyprint_log("Puppycamera volume allocation failed."); + #endif return; } @@ -830,6 +836,52 @@ static void level_cmd_puppyvolume(void) sCurrentCmd = CMD_NEXT; } +static void level_cmd_puppylight_environment(void) +{ +#ifdef PUPPYLIGHTS + Lights1 temp = gdSPDefLights1(CMD_GET(u8, 2), CMD_GET(u8, 3), CMD_GET(u8, 4), CMD_GET(u8, 5), CMD_GET(u8, 6), CMD_GET(u8, 7), CMD_GET(u8, 8), CMD_GET(u8, 9), CMD_GET(u8, 10)); + + memcpy(&gLevelLight, &temp, sizeof(Lights1)); + levelAmbient = TRUE; +#endif + sCurrentCmd = CMD_NEXT; +} + +static void level_cmd_puppylight_node(void) +{ +#ifdef PUPPYLIGHTS + if ((gPuppyLights[gNumLights] = mem_pool_alloc(gLightsPool, sizeof(struct PuppyLight))) == NULL) + { +#if PUPPYPRINT_DEBUG + append_puppyprint_log("Puppylight allocation failed."); +#endif + sCurrentCmd = CMD_NEXT; + return; + } + + gPuppyLights[gNumLights]->rgba[0] = CMD_GET(u8, 2); + gPuppyLights[gNumLights]->rgba[1] = CMD_GET(u8, 3); + gPuppyLights[gNumLights]->rgba[2] = CMD_GET(u8, 4); + gPuppyLights[gNumLights]->rgba[3] = CMD_GET(u8, 5); + + gPuppyLights[gNumLights]->pos[0][0] = CMD_GET(s16, 6); + gPuppyLights[gNumLights]->pos[0][1] = CMD_GET(s16, 8); + gPuppyLights[gNumLights]->pos[0][2] = CMD_GET(s16, 10); + + gPuppyLights[gNumLights]->pos[1][0] = CMD_GET(s16, 12); + gPuppyLights[gNumLights]->pos[1][1] = CMD_GET(s16, 14); + gPuppyLights[gNumLights]->pos[1][2] = CMD_GET(s16, 16); + gPuppyLights[gNumLights]->yaw = CMD_GET(s16, 18); + + gPuppyLights[gNumLights]->epicentre = CMD_GET(u8, 20); + gPuppyLights[gNumLights]->flags |= CMD_GET(u8, 21); + gPuppyLights[gNumLights]->active = TRUE; + + gNumLights++; + +#endif + sCurrentCmd = CMD_NEXT; +} static void (*LevelScriptJumpTable[])(void) = { /*00*/ level_cmd_load_and_execute, @@ -894,7 +946,9 @@ static void (*LevelScriptJumpTable[])(void) = { /*3B*/ level_cmd_create_whirlpool, /*3C*/ level_cmd_get_or_set_var, /*3D*/ level_cmd_puppyvolume, - /*3E*/ level_cmd_change_area_skybox, + /*3E*/ level_cmd_change_area_skybox, + /*3F*/ level_cmd_puppylight_environment, + /*40*/ level_cmd_puppylight_node, }; struct LevelCommand *level_script_execute(struct LevelCommand *cmd) { diff --git a/src/game/behavior_actions.c b/src/game/behavior_actions.c index 0240e095..d3566d5f 100644 --- a/src/game/behavior_actions.c +++ b/src/game/behavior_actions.c @@ -44,6 +44,7 @@ #include "spawn_object.h" #include "spawn_sound.h" #include "rumble_init.h" +#include "puppylights.h" #define o gCurrentObject diff --git a/src/game/level_update.c b/src/game/level_update.c index 5d8e1bf5..64204b2d 100644 --- a/src/game/level_update.c +++ b/src/game/level_update.c @@ -30,6 +30,7 @@ #include "rumble_init.h" #include "puppycam2.h" #include "puppyprint.h" +#include "puppylights.h" #include "config.h" @@ -642,9 +643,11 @@ void initiate_warp(s16 destLevel, s16 destArea, s16 destWarpNode, s32 arg3) { sWarpDest.areaIdx = destArea; sWarpDest.nodeId = destWarpNode; sWarpDest.arg = arg3; - - #ifdef PUPPYCAM +//lol +#if defined(PUPPYCAM) || defined(PUPPYLIGHTS) s32 i = 0; +#endif +#ifdef PUPPYCAM if (sWarpDest.type != WARP_TYPE_SAME_AREA) { for (i = 0; i < gPuppyVolumeCount; i++) @@ -653,7 +656,18 @@ void initiate_warp(s16 destLevel, s16 destArea, s16 destWarpNode, s32 arg3) { } gPuppyVolumeCount = 0; } - #endif +#endif +#ifdef PUPPYLIGHTS + if (sWarpDest.type != WARP_TYPE_SAME_AREA) + { + for (i = 0; i < gNumLights; i++) + { + mem_pool_free(gLightsPool, gPuppyLights[i]); + } + gNumLights = 0; + levelAmbient = FALSE; + } +#endif } // From Surface 0xD3 to 0xFC @@ -1265,10 +1279,14 @@ s32 init_level(void) { sound_banks_disable(SEQ_PLAYER_SFX, SOUND_BANKS_DISABLED_DURING_INTRO_CUTSCENE); } - #if PUPPYPRINT_DEBUG +#ifdef PUPPYLIGHTS + puppylights_allocate(); +#endif + +#if PUPPYPRINT_DEBUG sprintf(textBytes, "Level loaded in %dus", (s32)(OS_CYCLES_TO_USEC(osGetTime() - first))); append_puppyprint_log(textBytes); - #endif +#endif return 1; } diff --git a/src/game/obj_behaviors.c b/src/game/obj_behaviors.c index 4432645a..6b869ec1 100644 --- a/src/game/obj_behaviors.c +++ b/src/game/obj_behaviors.c @@ -32,6 +32,7 @@ #include "spawn_object.h" #include "spawn_sound.h" #include "rumble_init.h" +#include "puppylights.h" /** * @file obj_behaviors.c diff --git a/src/game/obj_behaviors_2.c b/src/game/obj_behaviors_2.c index 323074ea..c46d7a65 100644 --- a/src/game/obj_behaviors_2.c +++ b/src/game/obj_behaviors_2.c @@ -45,6 +45,7 @@ #include "save_file.h" #include "seq_ids.h" #include "spawn_sound.h" +#include "puppylights.h" #define POS_OP_SAVE_POSITION 0 #define POS_OP_COMPUTE_VELOCITY 1 diff --git a/src/game/puppylights.c b/src/game/puppylights.c new file mode 100644 index 00000000..0263729a --- /dev/null +++ b/src/game/puppylights.c @@ -0,0 +1,311 @@ +///Puppylights 2.0 by Fazana. What happened to 1.0? Tragic accident. +/** +Intended for use with manipulating existing Lights1 structs for objects in real time. +Can support static lights that are loaded with the level, or lights created by objects. + +Puppylights is generally intended to be used with things that don't directly use lights to colour +themselves. Inside the main function, you can pass through a colour to override the default light +but it will not be affected by environmental tinting. If you wish for an object to emit a light, +simply set the object flag OBJ_FLAG_EMIT_LIGHT and set some values to o->puppylight. +**/ + +#include +#include "types.h" +#include "puppylights.h" +#include "area.h" +#include "engine/math_util.h" +#include "string.h" +#include "object_fields.h" +#include "object_constants.h" +#include "camera.h" +#include "memory.h" +#include "print.h" +#include "debug_box.h" +#include "object_list_processor.h" +#include "level_update.h" + +Lights1 gLevelLight; //Existing ambient light in the area. Will be set by the level script, though can always be changed afterwards if desired. +u8 levelAmbient = FALSE; +Lights1 *sLightBase; //The base value where lights are written to when worked with. +Lights1 sDefaultLights = gdSPDefLights1(0x7F, 0x7F, 0x7F, 0xFE, 0xFE, 0xFE, 0x28, 0x28, 0x28); //Default lights default lights +u16 gNumLights = 0; //How many lights are loaded. +u16 gDynLightStart = 0; //Where the dynamic lights will start. +struct PuppyLight *gPuppyLights[MAX_LIGHTS]; //This contains all the loaded data. +struct MemoryPool *gLightsPool; //The memory pool where the above is stored. + +//Runs after an area load, allocates the dynamic light slots. +void puppylights_allocate(void) +{ + s32 numAllocate = MIN(MAX_LIGHTS - gNumLights, MAX_LIGHTS_DYNAMIC); + s32 i; + + gDynLightStart = gNumLights; + + if (numAllocate <= 0) //If this happens you've allocated too many static lights and therefore cucked dynamic. + return; + //Now it has the number it wants, it will allocate this many extra lights, intended for dynamic lights. + for (i = 0; i < numAllocate; i++) + { + gPuppyLights[gNumLights] = mem_pool_alloc(gLightsPool, sizeof(struct PuppyLight)); + if (gPuppyLights[gNumLights] == NULL) + return; + gPuppyLights[gNumLights]->flags = PUPPYLIGHT_DYNAMIC; + gPuppyLights[gNumLights]->active = FALSE; + gNumLights++; + } +} + +extern Mat4 gMatStack[32]; + +//Function that iterates through each light. +void puppylights_iterate(struct PuppyLight *light, Lights1 *src, struct Object *obj) +{ + Lights1 *tempLight; + s32 lightPos[2]; + Vec3i lightRelative; + Vec3i lightDir; + s32 lightIntensity = 0; + s32 i; + s32 colour; + s32 ambient; + f64 scale = 1.0f; + f32 scale2; + f64 scaleVal = 1.0f; + f64 preScale; + + //Relative positions of the object vs the centre of the node. + lightRelative[0] = light->pos[0][0] - obj->oPosX; + lightRelative[1] = light->pos[0][1] - obj->oPosY; + lightRelative[2] = light->pos[0][2] - obj->oPosZ; + + //If the node is a cube, then calculate the distance a bit differently. + if (light->flags & PUPPYLIGHT_SHAPE_CUBE) + { + //Get the position based off the rotation of the cube. + lightPos[0] = lightRelative[2] * sins(light->yaw) + lightRelative[0] * coss(light->yaw); + lightPos[1] = lightRelative[2] * coss(light->yaw) - lightRelative[0] * sins(light->yaw); + + #ifdef VISUAL_DEBUG + Vec3f debugPos[2]; + vec3f_set(debugPos[0], light->pos[0][0], light->pos[0][1], light->pos[0][2]); + vec3f_set(debugPos[1], light->pos[1][0], light->pos[1][1], light->pos[1][2]); + debug_box_color(0x00FFFF00); + debug_box_rot(debugPos[0], debugPos[1], light->yaw, DEBUG_SHAPE_BOX); + #endif + + if (-light->pos[1][0] < lightPos[0] && lightPos[0] < light->pos[1][0] && + -light->pos[1][1] < lightRelative[1] && lightRelative[1] < light->pos[1][1] && + -light->pos[1][2] < lightPos[1] && lightPos[1] < light->pos[1][2]) + { + scale = (lightPos[0] * lightPos[0]) + (lightRelative[1] * lightRelative[1]) + (lightPos[1] * lightPos[1]); + scaleVal = (light->pos[1][0] + light->pos[1][1] + light->pos[1][2])/3; + scaleVal *= scaleVal; + } + else + return; + } + else + if (light->flags & PUPPYLIGHT_SHAPE_CYLINDER) + { + + #ifdef VISUAL_DEBUG + Vec3f debugPos[2]; + vec3f_set(debugPos[0], light->pos[0][0], light->pos[0][1], light->pos[0][2]); + vec3f_set(debugPos[1], light->pos[1][0], light->pos[1][1], light->pos[1][2]); + debug_box_color(0x00FFFF00); + debug_box_rot(debugPos[0], debugPos[1], 0, DEBUG_SHAPE_CYLINDER); + #endif + //First do an AABB check, since it's a fair bit faster than doing just a distance check. + if (-light->pos[1][0] < lightRelative[0] && lightRelative[0] < light->pos[1][0] && + -light->pos[1][1] < lightRelative[1] && lightRelative[1] < light->pos[1][1] && + -light->pos[1][2] < lightRelative[2] && lightRelative[2] < light->pos[1][2]) + { + //Okay now we can do a distance check + scale = (lightRelative[0] * lightRelative[0]) + (lightRelative[1] * lightRelative[1]) + (lightRelative[2] * lightRelative[2]); + scaleVal = (light->pos[1][0] + light->pos[1][1] + light->pos[1][2])/3; + scaleVal *= scaleVal; + if (scale > scaleVal) + return; + } + else + return; + } + else + return; + + tempLight = segmented_to_virtual(src); + //Now we have a scale value and a scale factor, we can start lighting things up. + //Convert to a percentage. + preScale = scale; + scale /= scaleVal; + scale = CLAMP(scale, 0.0f, 1.0f); + //Reduce scale2 by the epicentre. + scale2 = (scale - (f32)(light->epicentre/100.0f)) * (1+(f32)(light->epicentre/100.0f)); + scale2 = CLAMP(scale2, 0.0f, 1.0f); + //Normalise the light brightness. + lightIntensity = (tempLight->a.l.col[0]+tempLight->a.l.col[1]+tempLight->a.l.col[2])/3.0f; + //Get the direction numbers we want by applying some maths to the relative positions. We use 64 because light directions range from -64 to 63. + //Note: can this be optimised further? Simply squaring lightRelative and then dividing it by preScale doesn't work. + if (light->flags & PUPPYLIGHT_DIRECTIONAL) + { + lightDir[0] = ((lightRelative[0]) * 64.0f) / sqrtf(preScale); + lightDir[1] = ((lightRelative[1]) * 64.0f) / sqrtf(preScale); + lightDir[2] = ((lightRelative[2]) * 64.0f) / sqrtf(preScale); + } + //Get direction if applicable. + for (i = 0; i < 3; i++) + { + //So it works by starting from the final colour, and then lerping to the original colour, by a factor of the epicentre corrected scale. Light opacity affects this further. + colour = approach_f32_asymptotic(light->rgba[i], sLightBase->l[0].l.col[i], scale2*((f32)light->rgba[3]/255.0f)); + //If it's a directional light, then increase the current ambient by 50%, to give the effect better. + //Otherwise, just normalise the brightness to keep it in line with the current ambient. + if (light->flags & PUPPYLIGHT_DIRECTIONAL) + ambient = approach_f32_asymptotic(MIN(tempLight->a.l.col[i]*1.5f, 0xFF), tempLight->a.l.col[i], scale*((f32)light->rgba[3]/255.0f)); + else + ambient = approach_f32_asymptotic(lightIntensity, tempLight->a.l.col[i], scale*((f32)light->rgba[3]/255.0f)); + //And now to apply the values. + tempLight->l[0].l.col[i] = colour; + tempLight->l[0].l.colc[i] = colour; + //Ambient, too. + tempLight->a.l.col[i] = ambient; + tempLight->a.l.colc[i] = ambient; + //Apply direction. It takes the relative positions, and then multiplies them with the perspective matrix to get a correct direction. + //Index 1 of the first dimension of gMatStack is perspective. Note that if you ever decide to cheat your way into rendering things after the game does :^) + if (light->flags & PUPPYLIGHT_DIRECTIONAL) + tempLight->l->l.dir[i] = approach_f32_asymptotic((s8)(lightDir[0] * gMatStack[1][0][i] + lightDir[1] * gMatStack[1][1][i] + lightDir[2] * gMatStack[1][2][i]), tempLight->l->l.dir[i], scale); + } +} + +//Main function. Run this in the object you wish to illuminate, and just give it its light, object pointer and any potential flags if you want to use them. +//If the object has multiple lights, then you run this for each light. +void puppylights_run(Lights1 *src, struct Object *obj, s32 flags, s32 baseColour) +{ + s32 i; + + if (gCurrLevelNum < 4) + return; + //Checks if there's a hardset colour. Colours are only the first 3 bytes, so you can really put whatever you want in the last. + //If there isn't a colour, then it decides whether to apply the ambient lighting, or the default lighting as the baseline. + //Otherwise, it hardsets a colour to begin with. I don't recommend you use this, simply because it's intended to be used + //As a hacky quick-fix for models coloured by lights. Lightcoloured models don't blend nearly as nicely as ones coloured + //By other means. + if (baseColour < 0x100) + { + if (levelAmbient) + sLightBase = &gLevelLight; + else + sLightBase = &sDefaultLights; + } + else + { + s32 colour; + Lights1 tempLight; + sLightBase = &tempLight; + for (i = 0; i < 3; i++) + { + colour = (((baseColour >> (24-(i*8)))) & 0xFF); + sLightBase->l[0].l.col[i] = colour; + sLightBase->l[0].l.colc[i] = colour; + sLightBase->a.l.col[i] = colour/2; + sLightBase->a.l.colc[i] = colour/2; + sLightBase->l->l.dir[i] = 0x28; + } + } + memcpy(segmented_to_virtual(src), &sLightBase[0], sizeof(Lights1)); + + for (i = 0; i < gNumLights; i++) + { + if (gPuppyLights[i]->rgba[3] > 0 && gPuppyLights[i]->active == TRUE) + puppylights_iterate(gPuppyLights[i], src, obj); + } +} + +//Sets and updates dynamic lights from objects. +//0xFFFF is essentially the null ID. If the display flag is met, it will find and set an ID, otherwise it frees up the spot. +void puppylights_object_emit(struct Object *obj) +{ + s32 i; + if (gCurrLevelNum < 4) + return; + + if (obj->oFlags & OBJ_FLAG_EMIT_LIGHT) + { + f64 dist = ((obj->oPosX - gMarioState->pos[0]) * (obj->oPosX - gMarioState->pos[0])) + + ((obj->oPosY - gMarioState->pos[1]) * (obj->oPosY - gMarioState->pos[1])) + + ((obj->oPosZ - gMarioState->pos[2]) * (obj->oPosZ - gMarioState->pos[2])); + f64 lightSize = ((obj->puppylight.pos[1][0]) * (obj->puppylight.pos[1][0])) + + ((obj->puppylight.pos[1][1]) * (obj->puppylight.pos[1][1])) + + ((obj->puppylight.pos[1][2]) * (obj->puppylight.pos[1][2])); + if (dist > lightSize) + goto deallocate; //That's right. I used a goto. Eat your heart out xkcd. + if (obj->oLightID == 0xFFFF) + { + if (ABS(gNumLights - gDynLightStart) < MAX_LIGHTS_DYNAMIC) + goto deallocate; + for (i = gDynLightStart; i < MAX_LIGHTS; i++) + { + if (gPuppyLights[i]->active == TRUE) + continue; + memcpy(gPuppyLights[i], &obj->puppylight, sizeof(struct PuppyLight)); + gPuppyLights[i]->active = TRUE; + obj->oLightID = i; + goto updatepos; + } + } + else + { + updatepos: + gPuppyLights[obj->oLightID]->pos[0][0] = obj->oPosX; + gPuppyLights[obj->oLightID]->pos[0][1] = obj->oPosY; + gPuppyLights[obj->oLightID]->pos[0][2] = obj->oPosZ; + } + } + else + { + deallocate: + if (obj->oLightID != 0xFFFF) + gPuppyLights[obj->oLightID]->active = FALSE; + obj->oLightID = 0xFFFF; + } +} + +//A bit unorthodox, but anything to avoid having to set up data to pass through in the original function. +//Objects will completely ignore X, Y, Z and active though. +void set_light_properties(struct PuppyLight *light, s32 x, s32 y, s32 z, s32 offsetX, s32 offsetY, s32 offsetZ, s32 yaw, s32 epicentre, s32 colour, s32 flags, s32 active) +{ + light->active = active; + light->pos[0][0] = x; + light->pos[0][1] = y; + light->pos[0][2] = z; + light->pos[1][0] = offsetX; + light->pos[1][1] = offsetY; + light->pos[1][2] = offsetZ; + light->rgba[0] = (colour >> 24) & 0xFF; + light->rgba[1] = (colour >> 16) & 0xFF; + light->rgba[2] = (colour >> 8) & 0xFF; + light->rgba[3] = colour & 0xFF; + light->yaw = yaw; + light->epicentre = epicentre; + light->flags |= flags | PUPPYLIGHT_DYNAMIC; +} + +//You can run these in objects to enable or disable their light properties. +void cur_obj_enable_light(void) +{ + gCurrentObject->oFlags |= OBJ_FLAG_EMIT_LIGHT; +} + +void cur_obj_disable_light(void) +{ + gCurrentObject->oFlags &= ~OBJ_FLAG_EMIT_LIGHT; +} + +void obj_enable_light(struct Object *obj) +{ + obj->oFlags |= OBJ_FLAG_EMIT_LIGHT; +} + +void obj_disable_light(struct Object *obj) +{ + obj->oFlags &= ~OBJ_FLAG_EMIT_LIGHT; +} diff --git a/src/game/puppylights.h b/src/game/puppylights.h new file mode 100644 index 00000000..3c063b64 --- /dev/null +++ b/src/game/puppylights.h @@ -0,0 +1,50 @@ +#ifndef PUPPYLIGHTS_H +#define PUPPYLIGHTS_H +#include "types.h" +#include "command_macros_base.h" + +//The maximum number of lights that can be loaded at once. Any further lights that attempt to be created past this will simply not spawn. +#define MAX_LIGHTS 32 +//The maximum number of dynamic lights available at one time. +#define MAX_LIGHTS_DYNAMIC 8 + +//Two shapes. Choose your destiny. +#define PUPPYLIGHT_SHAPE_CUBE 0x1 +#define PUPPYLIGHT_SHAPE_CYLINDER 0x2 +#define PUPPYLIGHT_DYNAMIC 0x4 +#define PUPPYLIGHT_DIRECTIONAL 0x8 + +#define PUPPYLIGHT_FLAG_SHADOW 0x1 +#define PUPPYLIGHT_FLAG_WET 0x2 + +#define PUPPYLIGHT_ENVIRONMENT(ambientR, ambientG, ambientB, diffuseR, diffuseG, diffuseB, diffuseX, diffuseY, diffuseZ) \ + CMD_BBBB(0x3F, 0x0C, ambientR, ambientG), \ + CMD_BBBB(ambientB, diffuseR, diffuseG, diffuseB), \ + CMD_BBBB(diffuseX, diffuseY, diffuseZ, 0x0) + +#define PUPPYLIGHT_NODE(r, g, b, a, x, y, z, offsetX, offsetY, offsetZ, yaw, epicentre, flags) \ + CMD_BBBB(0x40, 0x18, r, g), \ + CMD_BBH(b, a, x), \ + CMD_HH(y, z), \ + CMD_HH(offsetX, offsetY), \ + CMD_HH(offsetZ, yaw), \ + CMD_BBH(epicentre, flags, 0x0) + +//How much RAM is allocated to puppylights +#define PUPPYLIGHTS_POOL sizeof(struct PuppyLight) * MAX_LIGHTS + +extern Lights1 gLevelLight; +extern u16 gNumLights; +extern u8 levelAmbient; +extern struct PuppyLight *gPuppyLights[MAX_LIGHTS]; +extern struct MemoryPool *gLightsPool; +extern void puppylights_run(Lights1 *src, struct Object *obj, s32 flags, s32 baseColour); +extern void puppylights_object_emit(struct Object *obj); +extern void cur_obj_enable_light(void); +extern void cur_obj_disable_light(void); +extern void obj_enable_light(struct Object *obj); +extern void obj_disable_light(struct Object *obj); +extern void set_light_properties(struct PuppyLight *light, s32 x, s32 y, s32 z, s32 offsetX, s32 offsetY, s32 offsetZ, s32 yaw, s32 epicentre, s32 colour, s32 flags, s32 active); +extern void puppylights_allocate(void); + +#endif diff --git a/src/game/spawn_object.c b/src/game/spawn_object.c index 4cc1636b..cb850eff 100644 --- a/src/game/spawn_object.c +++ b/src/game/spawn_object.c @@ -289,6 +289,7 @@ struct Object *allocate_object(struct ObjectNode *objList) { obj->header.gfx.pos[1] = -10000.0f; obj->header.gfx.pos[2] = -10000.0f; obj->header.gfx.throwMatrix = NULL; + obj->oLightID = 0xFFFF; return obj; } From 5630194ff329f048f4c1b62cdf3d5fbee4d5148a Mon Sep 17 00:00:00 2001 From: Fazana <52551480+FazanaJ@users.noreply.github.com> Date: Tue, 7 Sep 2021 15:40:00 +0100 Subject: [PATCH 02/18] Update config.h --- include/config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/config.h b/include/config.h index 730ed557..a6076b32 100644 --- a/include/config.h +++ b/include/config.h @@ -128,7 +128,7 @@ // Number of supported areas per level. #define AREA_COUNT 8 // Lightweight directional lighting engine by Fazana. Intended for giving proximity and positional pointlights to small objects. -#define PUPPYLIGHTS +//#define PUPPYLIGHTS // BUG/GAME QOL FIXES // Fix instant warp offset not working when warping across different areas From 98bc1fac825ed77d3337fd4b1ad6a1069c7f6a44 Mon Sep 17 00:00:00 2001 From: Fazana <52551480+FazanaJ@users.noreply.github.com> Date: Tue, 7 Sep 2021 15:55:34 +0100 Subject: [PATCH 03/18] Update puppylights.c --- src/game/puppylights.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/game/puppylights.c b/src/game/puppylights.c index 0263729a..c5620e02 100644 --- a/src/game/puppylights.c +++ b/src/game/puppylights.c @@ -9,6 +9,8 @@ but it will not be affected by environmental tinting. If you wish for an object simply set the object flag OBJ_FLAG_EMIT_LIGHT and set some values to o->puppylight. **/ +#ifdef PUPPYLIGHTS + #include #include "types.h" #include "puppylights.h" @@ -309,3 +311,5 @@ void obj_disable_light(struct Object *obj) { obj->oFlags &= ~OBJ_FLAG_EMIT_LIGHT; } + +#endif From c9cb181b4cb7d4abb1e61d30a186eb759ca516cd Mon Sep 17 00:00:00 2001 From: Fazana <52551480+FazanaJ@users.noreply.github.com> Date: Tue, 7 Sep 2021 15:56:01 +0100 Subject: [PATCH 04/18] Update puppylights.h --- src/game/puppylights.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/game/puppylights.h b/src/game/puppylights.h index 3c063b64..58b28402 100644 --- a/src/game/puppylights.h +++ b/src/game/puppylights.h @@ -1,5 +1,8 @@ #ifndef PUPPYLIGHTS_H #define PUPPYLIGHTS_H + +#ifdef PUPPYLIGHTS + #include "types.h" #include "command_macros_base.h" @@ -48,3 +51,4 @@ extern void set_light_properties(struct PuppyLight *light, s32 x, s32 y, s32 z, extern void puppylights_allocate(void); #endif +#endif From e368b33c0baaaa35bd5ba96f22a3f8f2ac31f717 Mon Sep 17 00:00:00 2001 From: Fazana <52551480+FazanaJ@users.noreply.github.com> Date: Tue, 7 Sep 2021 18:08:56 +0100 Subject: [PATCH 05/18] Ifdef fixes --- src/game/puppylights.c | 4 ++-- src/game/puppylights.h | 3 +-- src/game/puppyprint.c | 2 +- src/game/spawn_object.c | 2 ++ 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/game/puppylights.c b/src/game/puppylights.c index c5620e02..17c3098a 100644 --- a/src/game/puppylights.c +++ b/src/game/puppylights.c @@ -9,8 +9,6 @@ but it will not be affected by environmental tinting. If you wish for an object simply set the object flag OBJ_FLAG_EMIT_LIGHT and set some values to o->puppylight. **/ -#ifdef PUPPYLIGHTS - #include #include "types.h" #include "puppylights.h" @@ -26,6 +24,8 @@ simply set the object flag OBJ_FLAG_EMIT_LIGHT and set some values to o->puppyli #include "object_list_processor.h" #include "level_update.h" +#ifdef PUPPYLIGHTS + Lights1 gLevelLight; //Existing ambient light in the area. Will be set by the level script, though can always be changed afterwards if desired. u8 levelAmbient = FALSE; Lights1 *sLightBase; //The base value where lights are written to when worked with. diff --git a/src/game/puppylights.h b/src/game/puppylights.h index 58b28402..47f06b5b 100644 --- a/src/game/puppylights.h +++ b/src/game/puppylights.h @@ -1,8 +1,7 @@ +#ifdef PUPPYLIGHTS #ifndef PUPPYLIGHTS_H #define PUPPYLIGHTS_H -#ifdef PUPPYLIGHTS - #include "types.h" #include "command_macros_base.h" diff --git a/src/game/puppyprint.c b/src/game/puppyprint.c index dfca552f..1e15f53b 100644 --- a/src/game/puppyprint.c +++ b/src/game/puppyprint.c @@ -126,7 +126,7 @@ void puppyprint_calculate_ram_usage(void) //These are a bit hacky, but what can ye do eh? //gEffectsMemoryPool is 0x4000, gObjectsMemoryPool is 0x800. Epic C limitations mean I can't just sizeof their values :) - ramsizeSegment[5] = 0x4000 + 0x800; + ramsizeSegment[5] = 0x4000 + 0x800 + 0x4000 + 0x800; ramsizeSegment[6] = (SURFACE_NODE_POOL_SIZE * sizeof(struct SurfaceNode)) + (SURFACE_POOL_SIZE * sizeof(struct Surface)); ramsizeSegment[7] = gAudioHeapSize + gAudioInitPoolSize; ramsizeSegment[8] = audioPool[0] + audioPool[1] + audioPool[2] + audioPool[3] + audioPool[4] + audioPool[5] + diff --git a/src/game/spawn_object.c b/src/game/spawn_object.c index b30aedef..7176956b 100644 --- a/src/game/spawn_object.c +++ b/src/game/spawn_object.c @@ -285,7 +285,9 @@ struct Object *allocate_object(struct ObjectNode *objList) { obj->header.gfx.pos[1] = -10000.0f; obj->header.gfx.pos[2] = -10000.0f; obj->header.gfx.throwMatrix = NULL; +#ifdef PUPPYLIGHTS obj->oLightID = 0xFFFF; +#endif return obj; } From ba8cc7cd5c1a1ca266d9663945735489d731a152 Mon Sep 17 00:00:00 2001 From: Fazana <52551480+FazanaJ@users.noreply.github.com> Date: Tue, 7 Sep 2021 22:47:01 +0100 Subject: [PATCH 06/18] Optimisation --- src/game/puppylights.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/game/puppylights.c b/src/game/puppylights.c index 17c3098a..7b46726d 100644 --- a/src/game/puppylights.c +++ b/src/game/puppylights.c @@ -65,7 +65,7 @@ void puppylights_iterate(struct PuppyLight *light, Lights1 *src, struct Object * Lights1 *tempLight; s32 lightPos[2]; Vec3i lightRelative; - Vec3i lightDir; + Vec3i lightDir = {0, 0, 0}; s32 lightIntensity = 0; s32 i; s32 colour; @@ -73,7 +73,6 @@ void puppylights_iterate(struct PuppyLight *light, Lights1 *src, struct Object * f64 scale = 1.0f; f32 scale2; f64 scaleVal = 1.0f; - f64 preScale; //Relative positions of the object vs the centre of the node. lightRelative[0] = light->pos[0][0] - obj->oPosX; @@ -138,7 +137,6 @@ void puppylights_iterate(struct PuppyLight *light, Lights1 *src, struct Object * tempLight = segmented_to_virtual(src); //Now we have a scale value and a scale factor, we can start lighting things up. //Convert to a percentage. - preScale = scale; scale /= scaleVal; scale = CLAMP(scale, 0.0f, 1.0f); //Reduce scale2 by the epicentre. @@ -150,9 +148,9 @@ void puppylights_iterate(struct PuppyLight *light, Lights1 *src, struct Object * //Note: can this be optimised further? Simply squaring lightRelative and then dividing it by preScale doesn't work. if (light->flags & PUPPYLIGHT_DIRECTIONAL) { - lightDir[0] = ((lightRelative[0]) * 64.0f) / sqrtf(preScale); - lightDir[1] = ((lightRelative[1]) * 64.0f) / sqrtf(preScale); - lightDir[2] = ((lightRelative[2]) * 64.0f) / sqrtf(preScale); + lightDir[0] = ((lightRelative[0]) * 64.0f) / light->pos[1][0]; + lightDir[1] = ((lightRelative[1]) * 64.0f) / light->pos[1][1]; + lightDir[2] = ((lightRelative[2]) * 64.0f) / light->pos[1][2]; } //Get direction if applicable. for (i = 0; i < 3; i++) From 315eac02ae04ede0887ca89c7425799c3968aeac Mon Sep 17 00:00:00 2001 From: Fazana <52551480+FazanaJ@users.noreply.github.com> Date: Tue, 7 Sep 2021 22:52:12 +0100 Subject: [PATCH 07/18] Signedness fix --- src/game/puppylights.c | 2 +- src/game/puppylights.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/game/puppylights.c b/src/game/puppylights.c index 7b46726d..66072749 100644 --- a/src/game/puppylights.c +++ b/src/game/puppylights.c @@ -178,7 +178,7 @@ void puppylights_iterate(struct PuppyLight *light, Lights1 *src, struct Object * //Main function. Run this in the object you wish to illuminate, and just give it its light, object pointer and any potential flags if you want to use them. //If the object has multiple lights, then you run this for each light. -void puppylights_run(Lights1 *src, struct Object *obj, s32 flags, s32 baseColour) +void puppylights_run(Lights1 *src, struct Object *obj, s32 flags, u32 baseColour) { s32 i; diff --git a/src/game/puppylights.h b/src/game/puppylights.h index 47f06b5b..fbab4420 100644 --- a/src/game/puppylights.h +++ b/src/game/puppylights.h @@ -40,7 +40,7 @@ extern u16 gNumLights; extern u8 levelAmbient; extern struct PuppyLight *gPuppyLights[MAX_LIGHTS]; extern struct MemoryPool *gLightsPool; -extern void puppylights_run(Lights1 *src, struct Object *obj, s32 flags, s32 baseColour); +extern void puppylights_run(Lights1 *src, struct Object *obj, s32 flags, u32 baseColour); extern void puppylights_object_emit(struct Object *obj); extern void cur_obj_enable_light(void); extern void cur_obj_disable_light(void); From f1377af58a03153b611e3efd081671ed486156fa Mon Sep 17 00:00:00 2001 From: Fazana <52551480+FazanaJ@users.noreply.github.com> Date: Tue, 7 Sep 2021 23:44:09 +0100 Subject: [PATCH 08/18] Unlock all define, to open every vanilla locked thing --- include/config.h | 2 ++ src/game/behaviors/boo.inc.c | 16 ++++++++++------ src/game/behaviors/ddd_warp.inc.c | 4 ++++ src/game/interaction.c | 9 ++++++++- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/include/config.h b/include/config.h index a6076b32..0f499d6b 100644 --- a/include/config.h +++ b/include/config.h @@ -129,6 +129,8 @@ #define AREA_COUNT 8 // Lightweight directional lighting engine by Fazana. Intended for giving proximity and positional pointlights to small objects. //#define PUPPYLIGHTS +// Open all courses and doors. Used for debugging purposes to unlock all content. +//#define UNLOCK_ALL // BUG/GAME QOL FIXES // Fix instant warp offset not working when warping across different areas diff --git a/src/game/behaviors/boo.inc.c b/src/game/behaviors/boo.inc.c index aef903fe..682a2b86 100644 --- a/src/game/behaviors/boo.inc.c +++ b/src/game/behaviors/boo.inc.c @@ -85,10 +85,12 @@ static s32 boo_should_be_active(void) { void bhv_courtyard_boo_triplet_init(void) { s32 i; struct Object *boo; - +#ifndef UNLOCK_ALL if (gHudDisplay.stars < 12) { obj_mark_for_deletion(o); - } else { + } else +#endif + { for (i = 0; i < 3; i++) { boo = spawn_object_relative( 0x01, @@ -725,10 +727,12 @@ static void boo_with_cage_act_3(void) { void bhv_boo_with_cage_init(void) { struct Object* cage; - +#ifndef UNLOCK_ALL if (gHudDisplay.stars < 12) { obj_mark_for_deletion(o); - } else { + } else +#endif + { cage = spawn_object(o, MODEL_HAUNTED_CAGE, bhvBooCage); cage->oBehParams = o->oBehParams; } @@ -809,11 +813,11 @@ void bhv_boo_in_castle_loop(void) { if (o->oAction == 0) { cur_obj_hide(); - +#ifndef UNLOCK_ALL if (gHudDisplay.stars < 12) { obj_mark_for_deletion(o); } - +#endif if (gMarioCurrentRoom == 1) { o->oAction++; } diff --git a/src/game/behaviors/ddd_warp.inc.c b/src/game/behaviors/ddd_warp.inc.c index 617e850f..2b594528 100644 --- a/src/game/behaviors/ddd_warp.inc.c +++ b/src/game/behaviors/ddd_warp.inc.c @@ -1,8 +1,12 @@ // ddd_warp.c.inc void bhv_ddd_warp_loop(void) { +#ifndef UNLOCK_ALL if (gDddPaintingStatus & BOWSERS_SUB_BEATEN) +#endif o->collisionData = segmented_to_virtual(inside_castle_seg7_collision_ddd_warp_2); +#ifndef UNLOCK_ALL else o->collisionData = segmented_to_virtual(inside_castle_seg7_collision_ddd_warp); +#endif } diff --git a/src/game/interaction.c b/src/game/interaction.c index 4990d0d0..03f4ceb3 100644 --- a/src/game/interaction.c +++ b/src/game/interaction.c @@ -915,6 +915,7 @@ u32 interact_warp_door(struct MarioState *m, UNUSED u32 interactType, struct Obj u32 actionArg; if (m->action == ACT_WALKING || m->action == ACT_DECELERATING) { +#ifndef UNLOCK_ALL if (warpDoorId == 1 && !(saveFlags & SAVE_FLAG_UNLOCKED_UPSTAIRS_DOOR)) { if (!(saveFlags & SAVE_FLAG_HAVE_KEY_2)) { if (!sDisplayingDoorText) { @@ -943,6 +944,7 @@ u32 interact_warp_door(struct MarioState *m, UNUSED u32 interactType, struct Obj doorAction = ACT_UNLOCKING_KEY_DOOR; } +#endif if (m->action == ACT_WALKING || m->action == ACT_DECELERATING) { actionArg = should_push_or_pull_door(m, o) + 0x00000004; @@ -1009,7 +1011,9 @@ u32 interact_door(struct MarioState *m, UNUSED u32 interactType, struct Object * s16 numStars = save_file_get_total_star_count(gCurrSaveFileNum - 1, COURSE_MIN - 1, COURSE_MAX - 1); if (m->action == ACT_WALKING || m->action == ACT_DECELERATING) { +#ifndef UNLOCK_ALL if (numStars >= requiredNumStars) { +#endif u32 actionArg = should_push_or_pull_door(m, o); u32 enterDoorAction; u32 doorSaveFileFlag; @@ -1028,11 +1032,13 @@ u32 interact_door(struct MarioState *m, UNUSED u32 interactType, struct Object * enterDoorAction = ACT_ENTERING_STAR_DOOR; } +#ifndef UNLOCK_ALL if (doorSaveFileFlag != 0 && !(save_file_get_flags() & doorSaveFileFlag)) { enterDoorAction = ACT_UNLOCKING_STAR_DOOR; } - +#endif return set_mario_action(m, enterDoorAction, actionArg); +#ifndef UNLOCK_ALL } else if (!sDisplayingDoorText) { u32 text = DIALOG_022 << 16; @@ -1062,6 +1068,7 @@ u32 interact_door(struct MarioState *m, UNUSED u32 interactType, struct Object * sDisplayingDoorText = TRUE; return set_mario_action(m, ACT_READING_AUTOMATIC_DIALOG, text); } +#endif } else if (m->action == ACT_IDLE && sDisplayingDoorText == TRUE && requiredNumStars == 70) { m->interactObj = o; m->usedObj = o; From f0fd048e5b037d88a3c9e55c9ee2e564b1bb95b6 Mon Sep 17 00:00:00 2001 From: Fazana <52551480+FazanaJ@users.noreply.github.com> Date: Tue, 7 Sep 2021 23:44:46 +0100 Subject: [PATCH 09/18] Some puppylight failsafes In case you mess up your light formatting. --- src/game/puppylights.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/game/puppylights.c b/src/game/puppylights.c index 66072749..68e5188c 100644 --- a/src/game/puppylights.c +++ b/src/game/puppylights.c @@ -277,15 +277,17 @@ void set_light_properties(struct PuppyLight *light, s32 x, s32 y, s32 z, s32 off light->pos[0][0] = x; light->pos[0][1] = y; light->pos[0][2] = z; - light->pos[1][0] = offsetX; - light->pos[1][1] = offsetY; - light->pos[1][2] = offsetZ; + light->pos[1][0] = MAX(offsetX, 10); + light->pos[1][1] = MAX(offsetY, 10); + light->pos[1][2] = MAX(offsetZ, 10); light->rgba[0] = (colour >> 24) & 0xFF; light->rgba[1] = (colour >> 16) & 0xFF; light->rgba[2] = (colour >> 8) & 0xFF; light->rgba[3] = colour & 0xFF; light->yaw = yaw; light->epicentre = epicentre; + if (!(flags & PUPPYLIGHT_SHAPE_CYLINDER) && flags & PUPPYLIGHT_SHAPE_CUBE) + light->flags |= PUPPYLIGHT_SHAPE_CYLINDER; light->flags |= flags | PUPPYLIGHT_DYNAMIC; } From f224f845d25e62f83d03c016838a4334b81aa0fe Mon Sep 17 00:00:00 2001 From: Fazana <52551480+FazanaJ@users.noreply.github.com> Date: Wed, 8 Sep 2021 14:54:02 +0100 Subject: [PATCH 10/18] Allocation fixes --- src/game/level_update.c | 4 ++++ src/game/object_helpers.c | 4 +++- src/game/puppylights.c | 4 ++++ src/game/spawn_object.c | 2 ++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/game/level_update.c b/src/game/level_update.c index 64204b2d..110c899b 100644 --- a/src/game/level_update.c +++ b/src/game/level_update.c @@ -1128,6 +1128,10 @@ s32 play_mode_change_area(void) { set_play_mode(PLAY_MODE_NORMAL); } +#ifdef PUPPYLIGHTS + puppylights_allocate(); +#endif + return 0; } diff --git a/src/game/object_helpers.c b/src/game/object_helpers.c index eae57a85..84eb45fb 100644 --- a/src/game/object_helpers.c +++ b/src/game/object_helpers.c @@ -26,6 +26,7 @@ #include "rendering_graph_node.h" #include "spawn_object.h" #include "spawn_sound.h" +#include "puppylights.h" static s8 sBbhStairJiggleOffsets[] = { -8, 8, -4, 4 }; static s16 sPowersOfTwo[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }; @@ -1057,7 +1058,7 @@ void cur_obj_set_y_vel_and_animation(f32 yVel, s32 animIndex) { void cur_obj_unrender_set_action_and_anim(s32 animIndex, s32 action) { cur_obj_become_intangible(); cur_obj_disable_rendering(); - + // only set animation if non-negative value if (animIndex >= 0) { cur_obj_init_animation_with_sound(animIndex); @@ -1139,6 +1140,7 @@ void obj_mark_for_deletion(struct Object *obj) { // setting it to 0 could potentially enable unexpected behavior. After an // object is marked for deletion, it still updates on that frame (I think), // so this is worth looking into. + obj_disable_light(obj); obj->activeFlags = ACTIVE_FLAG_DEACTIVATED; } diff --git a/src/game/puppylights.c b/src/game/puppylights.c index 68e5188c..adac9ec1 100644 --- a/src/game/puppylights.c +++ b/src/game/puppylights.c @@ -300,6 +300,8 @@ void cur_obj_enable_light(void) void cur_obj_disable_light(void) { gCurrentObject->oFlags &= ~OBJ_FLAG_EMIT_LIGHT; + if (gPuppyLights[gCurrentObject->oLightID] && gCurrentObject->oLightID != 0xFFFF) + gPuppyLights[gCurrentObject->oLightID]->active = FALSE; } void obj_enable_light(struct Object *obj) @@ -310,6 +312,8 @@ void obj_enable_light(struct Object *obj) void obj_disable_light(struct Object *obj) { obj->oFlags &= ~OBJ_FLAG_EMIT_LIGHT; + if (gPuppyLights[obj->oLightID] && obj->oLightID != 0xFFFF) + gPuppyLights[obj->oLightID]->active = FALSE; } #endif diff --git a/src/game/spawn_object.c b/src/game/spawn_object.c index 7176956b..04f2e7d8 100644 --- a/src/game/spawn_object.c +++ b/src/game/spawn_object.c @@ -12,6 +12,7 @@ #include "object_list_processor.h" #include "spawn_object.h" #include "types.h" +#include "puppylights.h" /** * An unused linked list struct that seems to have been replaced by ObjectNode. @@ -356,5 +357,6 @@ struct Object *create_object(const BehaviorScript *bhvScript) { */ void mark_obj_for_deletion(struct Object *obj) { //! Same issue as obj_mark_for_deletion + obj_disable_light(obj); obj->activeFlags = ACTIVE_FLAG_DEACTIVATED; } From c663ad90ed019459c7c72539d6208b6b24c0405d Mon Sep 17 00:00:00 2001 From: Fazana <52551480+FazanaJ@users.noreply.github.com> Date: Wed, 8 Sep 2021 19:42:25 +0100 Subject: [PATCH 11/18] Area node fix --- include/types.h | 2 ++ src/engine/level_script.c | 3 +++ src/game/level_update.c | 8 ++------ src/game/puppycam2.h | 1 + src/game/puppylights.c | 8 ++++++-- src/game/puppylights.h | 6 +++--- 6 files changed, 17 insertions(+), 11 deletions(-) diff --git a/include/types.h b/include/types.h index 32aed982..b77762e7 100644 --- a/include/types.h +++ b/include/types.h @@ -151,9 +151,11 @@ struct ObjectNode struct PuppyLight { Vec3t pos[2]; //The location of the light. First index is the absolute position, second index are offsets. s16 yaw; //Used by cubes. Allows epic rotating of the volume. + s16 room; //Which room to use. -1 is visible from all rooms. s8 epicentre; //What percentage inside the volume you'll be before maximum light strength is applied. (E.g: 100 will be full strength always, and 0 will be full strength at the centre.) u8 flags; //Some stuff to define how the volume is used. Mostly just shape stuff, but can potentially have other uses. u8 rgba[4]; //Colour. Go on, take even the tiniest guess as to what this entails. + u8 area; //Which section of the level this light is stored in. u8 active:1; //Whether the light will actually work. Mostly intended to be used for objects. }; #endif diff --git a/src/engine/level_script.c b/src/engine/level_script.c index becb9bf5..c1cbae09 100644 --- a/src/engine/level_script.c +++ b/src/engine/level_script.c @@ -830,6 +830,7 @@ static void level_cmd_puppyvolume(void) sPuppyVolumeStack[gPuppyVolumeCount]->shape = CMD_GET(u8, 33); sPuppyVolumeStack[gPuppyVolumeCount]->room = CMD_GET(s16, 34); + sPuppyVolumeStack[gPuppyVolumeCount]->area = sCurrAreaIndex; gPuppyVolumeCount++; #endif @@ -876,6 +877,8 @@ static void level_cmd_puppylight_node(void) gPuppyLights[gNumLights]->epicentre = CMD_GET(u8, 20); gPuppyLights[gNumLights]->flags |= CMD_GET(u8, 21); gPuppyLights[gNumLights]->active = TRUE; + gPuppyLights[gNumLights]->area = sCurrAreaIndex; + gPuppyLights[gNumLights]->room = CMD_GET(s16, 22); gNumLights++; diff --git a/src/game/level_update.c b/src/game/level_update.c index 110c899b..665d6a70 100644 --- a/src/game/level_update.c +++ b/src/game/level_update.c @@ -648,7 +648,7 @@ void initiate_warp(s16 destLevel, s16 destArea, s16 destWarpNode, s32 arg3) { s32 i = 0; #endif #ifdef PUPPYCAM - if (sWarpDest.type != WARP_TYPE_SAME_AREA) + if (sWarpDest.type == WARP_TYPE_CHANGE_LEVEL) { for (i = 0; i < gPuppyVolumeCount; i++) { @@ -658,7 +658,7 @@ void initiate_warp(s16 destLevel, s16 destArea, s16 destWarpNode, s32 arg3) { } #endif #ifdef PUPPYLIGHTS - if (sWarpDest.type != WARP_TYPE_SAME_AREA) + if (sWarpDest.type == WARP_TYPE_CHANGE_LEVEL) { for (i = 0; i < gNumLights; i++) { @@ -1128,10 +1128,6 @@ s32 play_mode_change_area(void) { set_play_mode(PLAY_MODE_NORMAL); } -#ifdef PUPPYLIGHTS - puppylights_allocate(); -#endif - return 0; } diff --git a/src/game/puppycam2.h b/src/game/puppycam2.h index e094671c..59a07ac2 100644 --- a/src/game/puppycam2.h +++ b/src/game/puppycam2.h @@ -135,6 +135,7 @@ struct sPuppyVolume u8 flagPersistance; //Decides if adding or removing the flags is temporary or permanent. u8 shape; s16 room; + u8 area; }; enum gPuppyCamBeh diff --git a/src/game/puppylights.c b/src/game/puppylights.c index adac9ec1..e1b587f8 100644 --- a/src/game/puppylights.c +++ b/src/game/puppylights.c @@ -215,7 +215,7 @@ void puppylights_run(Lights1 *src, struct Object *obj, s32 flags, u32 baseColour for (i = 0; i < gNumLights; i++) { - if (gPuppyLights[i]->rgba[3] > 0 && gPuppyLights[i]->active == TRUE) + if (gPuppyLights[i]->rgba[3] > 0 && gPuppyLights[i]->active == TRUE && gPuppyLights[i]->area == gCurrAreaIndex && (gPuppyLights[i]->room == -1 || gPuppyLights[i]->room == gMarioCurrentRoom)) puppylights_iterate(gPuppyLights[i], src, obj); } } @@ -248,6 +248,8 @@ void puppylights_object_emit(struct Object *obj) continue; memcpy(gPuppyLights[i], &obj->puppylight, sizeof(struct PuppyLight)); gPuppyLights[i]->active = TRUE; + gPuppyLights[i]->area = gCurrAreaIndex; + gPuppyLights[i]->room = obj->oRoom; obj->oLightID = i; goto updatepos; } @@ -271,7 +273,7 @@ void puppylights_object_emit(struct Object *obj) //A bit unorthodox, but anything to avoid having to set up data to pass through in the original function. //Objects will completely ignore X, Y, Z and active though. -void set_light_properties(struct PuppyLight *light, s32 x, s32 y, s32 z, s32 offsetX, s32 offsetY, s32 offsetZ, s32 yaw, s32 epicentre, s32 colour, s32 flags, s32 active) +void set_light_properties(struct PuppyLight *light, s32 x, s32 y, s32 z, s32 offsetX, s32 offsetY, s32 offsetZ, s32 yaw, s32 epicentre, s32 colour, s32 flags, s32 active, s32 room) { light->active = active; light->pos[0][0] = x; @@ -285,6 +287,8 @@ void set_light_properties(struct PuppyLight *light, s32 x, s32 y, s32 z, s32 off light->rgba[2] = (colour >> 8) & 0xFF; light->rgba[3] = colour & 0xFF; light->yaw = yaw; + light->area = gCurrAreaIndex; + light->room = room; light->epicentre = epicentre; if (!(flags & PUPPYLIGHT_SHAPE_CYLINDER) && flags & PUPPYLIGHT_SHAPE_CUBE) light->flags |= PUPPYLIGHT_SHAPE_CYLINDER; diff --git a/src/game/puppylights.h b/src/game/puppylights.h index fbab4420..f4bee05b 100644 --- a/src/game/puppylights.h +++ b/src/game/puppylights.h @@ -24,13 +24,13 @@ CMD_BBBB(ambientB, diffuseR, diffuseG, diffuseB), \ CMD_BBBB(diffuseX, diffuseY, diffuseZ, 0x0) -#define PUPPYLIGHT_NODE(r, g, b, a, x, y, z, offsetX, offsetY, offsetZ, yaw, epicentre, flags) \ +#define PUPPYLIGHT_NODE(r, g, b, a, x, y, z, offsetX, offsetY, offsetZ, yaw, epicentre, flags, room) \ CMD_BBBB(0x40, 0x18, r, g), \ CMD_BBH(b, a, x), \ CMD_HH(y, z), \ CMD_HH(offsetX, offsetY), \ CMD_HH(offsetZ, yaw), \ - CMD_BBH(epicentre, flags, 0x0) + CMD_BBH(epicentre, flags, room) //How much RAM is allocated to puppylights #define PUPPYLIGHTS_POOL sizeof(struct PuppyLight) * MAX_LIGHTS @@ -46,7 +46,7 @@ extern void cur_obj_enable_light(void); extern void cur_obj_disable_light(void); extern void obj_enable_light(struct Object *obj); extern void obj_disable_light(struct Object *obj); -extern void set_light_properties(struct PuppyLight *light, s32 x, s32 y, s32 z, s32 offsetX, s32 offsetY, s32 offsetZ, s32 yaw, s32 epicentre, s32 colour, s32 flags, s32 active); +extern void set_light_properties(struct PuppyLight *light, s32 x, s32 y, s32 z, s32 offsetX, s32 offsetY, s32 offsetZ, s32 yaw, s32 epicentre, s32 colour, s32 flags, s32 active, s32 room); extern void puppylights_allocate(void); #endif From cc0d19592c24df7d25fb18482c738644fc4ec569 Mon Sep 17 00:00:00 2001 From: Fazana <52551480+FazanaJ@users.noreply.github.com> Date: Wed, 8 Sep 2021 20:34:24 +0100 Subject: [PATCH 12/18] tweak :telephone: --- src/engine/level_script.c | 4 ++-- src/game/puppylights.c | 8 +++++++- src/game/puppylights.h | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/engine/level_script.c b/src/engine/level_script.c index c1cbae09..98c72769 100644 --- a/src/engine/level_script.c +++ b/src/engine/level_script.c @@ -376,7 +376,7 @@ static void level_cmd_free_level_pool(void) { alloc_only_pool_resize(sLevelPool, sLevelPool->usedSpace); sLevelPool = NULL; - for (i = 0; i < 8; i++) { + for (i = 0; i < AREA_COUNT; i++) { if (gAreaData[i].terrainData != NULL) { alloc_surface_pools(); break; @@ -390,7 +390,7 @@ static void level_cmd_begin_area(void) { u8 areaIndex = CMD_GET(u8, 2); void *geoLayoutAddr = CMD_GET(void *, 4); - if (areaIndex < 8) { + if (areaIndex < AREA_COUNT) { struct GraphNodeRoot *screenArea = (struct GraphNodeRoot *) process_geo_layout(sLevelPool, geoLayoutAddr); struct GraphNodeCamera *node = (struct GraphNodeCamera *) screenArea->views[0]; diff --git a/src/game/puppylights.c b/src/game/puppylights.c index e1b587f8..a42b13b5 100644 --- a/src/game/puppylights.c +++ b/src/game/puppylights.c @@ -7,6 +7,10 @@ Puppylights is generally intended to be used with things that don't directly use themselves. Inside the main function, you can pass through a colour to override the default light but it will not be affected by environmental tinting. If you wish for an object to emit a light, simply set the object flag OBJ_FLAG_EMIT_LIGHT and set some values to o->puppylight. + +For easy light modification, you can call set_light_properties, so set all the attributes of any +given loaded puppylight struct. Objects will ignore x, y, z, active and room, as it will set all +of these automatically. It will force the PUPPYLIGHT_DYNAMIC flag, too. **/ #include @@ -173,6 +177,8 @@ void puppylights_iterate(struct PuppyLight *light, Lights1 *src, struct Object * //Index 1 of the first dimension of gMatStack is perspective. Note that if you ever decide to cheat your way into rendering things after the game does :^) if (light->flags & PUPPYLIGHT_DIRECTIONAL) tempLight->l->l.dir[i] = approach_f32_asymptotic((s8)(lightDir[0] * gMatStack[1][0][i] + lightDir[1] * gMatStack[1][1][i] + lightDir[2] * gMatStack[1][2][i]), tempLight->l->l.dir[i], scale); + else + tempLight->l->l.dir[i] = 0x28; } } @@ -273,7 +279,7 @@ void puppylights_object_emit(struct Object *obj) //A bit unorthodox, but anything to avoid having to set up data to pass through in the original function. //Objects will completely ignore X, Y, Z and active though. -void set_light_properties(struct PuppyLight *light, s32 x, s32 y, s32 z, s32 offsetX, s32 offsetY, s32 offsetZ, s32 yaw, s32 epicentre, s32 colour, s32 flags, s32 active, s32 room) +void set_light_properties(struct PuppyLight *light, s32 x, s32 y, s32 z, s32 offsetX, s32 offsetY, s32 offsetZ, s32 yaw, s32 epicentre, s32 colour, s32 flags, s32 room, s32 active) { light->active = active; light->pos[0][0] = x; diff --git a/src/game/puppylights.h b/src/game/puppylights.h index f4bee05b..cd09dce1 100644 --- a/src/game/puppylights.h +++ b/src/game/puppylights.h @@ -46,7 +46,7 @@ extern void cur_obj_enable_light(void); extern void cur_obj_disable_light(void); extern void obj_enable_light(struct Object *obj); extern void obj_disable_light(struct Object *obj); -extern void set_light_properties(struct PuppyLight *light, s32 x, s32 y, s32 z, s32 offsetX, s32 offsetY, s32 offsetZ, s32 yaw, s32 epicentre, s32 colour, s32 flags, s32 active, s32 room); +extern void set_light_properties(struct PuppyLight *light, s32 x, s32 y, s32 z, s32 offsetX, s32 offsetY, s32 offsetZ, s32 yaw, s32 epicentre, s32 colour, s32 flags, s32 room, s32 active); extern void puppylights_allocate(void); #endif From b5b886ab26ecb781e7be9f92139dda5dc8faacef Mon Sep 17 00:00:00 2001 From: Fazana <52551480+FazanaJ@users.noreply.github.com> Date: Wed, 8 Sep 2021 20:45:35 +0100 Subject: [PATCH 13/18] actually lets not --- src/game/puppylights.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/game/puppylights.c b/src/game/puppylights.c index a42b13b5..3b3537a5 100644 --- a/src/game/puppylights.c +++ b/src/game/puppylights.c @@ -177,8 +177,6 @@ void puppylights_iterate(struct PuppyLight *light, Lights1 *src, struct Object * //Index 1 of the first dimension of gMatStack is perspective. Note that if you ever decide to cheat your way into rendering things after the game does :^) if (light->flags & PUPPYLIGHT_DIRECTIONAL) tempLight->l->l.dir[i] = approach_f32_asymptotic((s8)(lightDir[0] * gMatStack[1][0][i] + lightDir[1] * gMatStack[1][1][i] + lightDir[2] * gMatStack[1][2][i]), tempLight->l->l.dir[i], scale); - else - tempLight->l->l.dir[i] = 0x28; } } From f20e5a24b2779efaa668037c1c85577b455b8d90 Mon Sep 17 00:00:00 2001 From: Fazana <52551480+FazanaJ@users.noreply.github.com> Date: Thu, 9 Sep 2021 10:59:50 +0100 Subject: [PATCH 14/18] Light polish Which sounds conceptually impossible, given what light is. --- src/engine/level_script.c | 2 +- src/game/area.c | 4 ++-- src/game/level_update.c | 10 +++++++--- src/game/puppylights.c | 29 ++++++++++++++++++++++++++--- src/game/puppylights.h | 7 ++++--- src/game/puppyprint.c | 21 +++++++++++++++++++-- src/game/puppyprint.h | 2 +- 7 files changed, 60 insertions(+), 15 deletions(-) diff --git a/src/engine/level_script.c b/src/engine/level_script.c index 98c72769..4d76ee82 100644 --- a/src/engine/level_script.c +++ b/src/engine/level_script.c @@ -875,7 +875,7 @@ static void level_cmd_puppylight_node(void) gPuppyLights[gNumLights]->yaw = CMD_GET(s16, 18); gPuppyLights[gNumLights]->epicentre = CMD_GET(u8, 20); - gPuppyLights[gNumLights]->flags |= CMD_GET(u8, 21); + gPuppyLights[gNumLights]->flags = CMD_GET(u8, 21); gPuppyLights[gNumLights]->active = TRUE; gPuppyLights[gNumLights]->area = sCurrAreaIndex; gPuppyLights[gNumLights]->room = CMD_GET(s16, 22); diff --git a/src/game/area.c b/src/game/area.c index ce9fefd3..3fc1518b 100644 --- a/src/game/area.c +++ b/src/game/area.c @@ -228,7 +228,7 @@ void load_area(s32 index) { if (gCurrentArea == NULL && gAreaData[index].unk04 != NULL) { gCurrentArea = &gAreaData[index]; gCurrAreaIndex = gCurrentArea->index; - + if (gCurrentArea->terrainData != NULL) { load_area_terrain(index, gCurrentArea->terrainData, gCurrentArea->surfaceRooms, gCurrentArea->macroObjects); @@ -239,7 +239,7 @@ void load_area(s32 index) { } load_obj_warp_nodes(); - geo_call_global_function_nodes(&gCurrentArea->unk04->node, GEO_CONTEXT_AREA_LOAD); + geo_call_global_function_nodes(&gCurrentArea->unk04->node, GEO_CONTEXT_AREA_LOAD); } } diff --git a/src/game/level_update.c b/src/game/level_update.c index 665d6a70..24308761 100644 --- a/src/game/level_update.c +++ b/src/game/level_update.c @@ -1001,6 +1001,9 @@ void update_hud_values(void) { void basic_update(UNUSED s16 *arg) { area_update_objects(); update_hud_values(); +#ifdef PUPPYLIGHTS + delete_lights(); +#endif if (gCurrentArea != NULL) { update_camera(gCurrentArea->camera); @@ -1028,6 +1031,9 @@ s32 play_mode_normal(void) { area_update_objects(); update_hud_values(); +#ifdef PUPPYLIGHTS + delete_lights(); +#endif if (gCurrentArea != NULL) { update_camera(gCurrentArea->camera); @@ -1203,7 +1209,6 @@ s32 update_level(void) { s32 init_level(void) { s32 val4 = 0; #if PUPPYPRINT_DEBUG - char textBytes[64]; OSTime first = osGetTime(); #endif @@ -1284,8 +1289,7 @@ s32 init_level(void) { #endif #if PUPPYPRINT_DEBUG - sprintf(textBytes, "Level loaded in %dus", (s32)(OS_CYCLES_TO_USEC(osGetTime() - first))); - append_puppyprint_log(textBytes); + append_puppyprint_log("Level loaded in %dus", (s32)(OS_CYCLES_TO_USEC(osGetTime() - first))); #endif return 1; } diff --git a/src/game/puppylights.c b/src/game/puppylights.c index 3b3537a5..9e513a92 100644 --- a/src/game/puppylights.c +++ b/src/game/puppylights.c @@ -27,6 +27,8 @@ of these automatically. It will force the PUPPYLIGHT_DYNAMIC flag, too. #include "debug_box.h" #include "object_list_processor.h" #include "level_update.h" +#include "engine/surface_collision.h" +#include "surface_terrains.h" #ifdef PUPPYLIGHTS @@ -55,8 +57,8 @@ void puppylights_allocate(void) gPuppyLights[gNumLights] = mem_pool_alloc(gLightsPool, sizeof(struct PuppyLight)); if (gPuppyLights[gNumLights] == NULL) return; - gPuppyLights[gNumLights]->flags = PUPPYLIGHT_DYNAMIC; gPuppyLights[gNumLights]->active = FALSE; + gPuppyLights[gNumLights]->flags = 0; gNumLights++; } } @@ -309,7 +311,7 @@ void cur_obj_disable_light(void) { gCurrentObject->oFlags &= ~OBJ_FLAG_EMIT_LIGHT; if (gPuppyLights[gCurrentObject->oLightID] && gCurrentObject->oLightID != 0xFFFF) - gPuppyLights[gCurrentObject->oLightID]->active = FALSE; + gPuppyLights[gCurrentObject->oLightID]->flags |= PUPPYLIGHT_DELETE; } void obj_enable_light(struct Object *obj) @@ -321,7 +323,28 @@ void obj_disable_light(struct Object *obj) { obj->oFlags &= ~OBJ_FLAG_EMIT_LIGHT; if (gPuppyLights[obj->oLightID] && obj->oLightID != 0xFFFF) - gPuppyLights[obj->oLightID]->active = FALSE; + gPuppyLights[obj->oLightID]->flags |= PUPPYLIGHT_DELETE; +} + +//This is ran during a standard area update +void delete_lights(void) +{ + s32 i; + + for (i = 0; i < gNumLights; i++) + { + if (gPuppyLights[i]->active == TRUE && gPuppyLights[i]->flags & PUPPYLIGHT_DELETE) + { + gPuppyLights[i]->pos[1][0] = approach_f32_asymptotic(gPuppyLights[i]->pos[1][0], 0, 0.15f); + gPuppyLights[i]->pos[1][1] = approach_f32_asymptotic(gPuppyLights[i]->pos[1][1], 0, 0.15f); + gPuppyLights[i]->pos[1][2] = approach_f32_asymptotic(gPuppyLights[i]->pos[1][2], 0, 0.15f); + if (gPuppyLights[i]->pos[1][0] < 1.0f && gPuppyLights[i]->pos[1][1] < 1.0f && gPuppyLights[i]->pos[1][2] < 1.0f) + { + gPuppyLights[i]->flags &= ~ PUPPYLIGHT_DELETE; + gPuppyLights[i]->active = FALSE; + } + } + } } #endif diff --git a/src/game/puppylights.h b/src/game/puppylights.h index cd09dce1..bf428b56 100644 --- a/src/game/puppylights.h +++ b/src/game/puppylights.h @@ -15,9 +15,9 @@ #define PUPPYLIGHT_SHAPE_CYLINDER 0x2 #define PUPPYLIGHT_DYNAMIC 0x4 #define PUPPYLIGHT_DIRECTIONAL 0x8 - -#define PUPPYLIGHT_FLAG_SHADOW 0x1 -#define PUPPYLIGHT_FLAG_WET 0x2 +#define PUPPYLIGHT_SHADOW 0x10 +#define PUPPYLIGHT_WET 0x20 +#define PUPPYLIGHT_DELETE 0x40 #define PUPPYLIGHT_ENVIRONMENT(ambientR, ambientG, ambientB, diffuseR, diffuseG, diffuseB, diffuseX, diffuseY, diffuseZ) \ CMD_BBBB(0x3F, 0x0C, ambientR, ambientG), \ @@ -48,6 +48,7 @@ extern void obj_enable_light(struct Object *obj); extern void obj_disable_light(struct Object *obj); extern void set_light_properties(struct PuppyLight *light, s32 x, s32 y, s32 z, s32 offsetX, s32 offsetY, s32 offsetZ, s32 yaw, s32 epicentre, s32 colour, s32 flags, s32 room, s32 active); extern void puppylights_allocate(void); +extern void delete_lights(void); #endif #endif diff --git a/src/game/puppyprint.c b/src/game/puppyprint.c index 1e15f53b..ee1e37b6 100644 --- a/src/game/puppyprint.c +++ b/src/game/puppyprint.c @@ -33,6 +33,8 @@ a modern game engine's developer's console. #include "print.h" #include "segment2.h" #include "string.h" +#include "stdarg.h" +#include "printf.h" #include "engine/math_util.h" #include "engine/behavior_script.h" #include "camera.h" @@ -309,14 +311,29 @@ void print_which_benchmark(void) char consoleLogTable[LOG_BUFFER_SIZE][255]; -void append_puppyprint_log(char str[255]) +static char *write_to_buf(char *buffer, const char *data, size_t size) { + return (char *) memcpy(buffer, data, size) + size; +} + +void append_puppyprint_log(const char *str, ...) { s32 i; + char textBytes[255]; + + memset(textBytes, 0, sizeof(textBytes)); + va_list arguments; + va_start(arguments, str); + if ((_Printf(write_to_buf, textBytes, str, arguments)) <= 0) + { + va_end(arguments); + return; + } for (i = 0; i < LOG_BUFFER_SIZE-1; i++) { memcpy(consoleLogTable[i], consoleLogTable[i+1], 255); } - memcpy(consoleLogTable[LOG_BUFFER_SIZE-1], str, 255); + memcpy(consoleLogTable[LOG_BUFFER_SIZE-1], textBytes, 255); + va_end(arguments); } #define LINE_HEIGHT 8 + ((LOG_BUFFER_SIZE-1)*12) diff --git a/src/game/puppyprint.h b/src/game/puppyprint.h index 3004ca3c..a563388d 100644 --- a/src/game/puppyprint.h +++ b/src/game/puppyprint.h @@ -67,7 +67,7 @@ extern s32 get_text_width(const char *str); extern void prepare_blank_box(void); extern void finish_blank_box(void); extern void render_blank_box(s16 x1, s16 y1, s16 x2, s16 y2, u8 r, u8 g, u8 b, u8 a); -extern void append_puppyprint_log(char str[255]); +extern void append_puppyprint_log(const char *str, ...); extern char consoleLogTable[LOG_BUFFER_SIZE][255]; #endif From 8b55b543b3f9ee571825758d720da616a1dca96b Mon Sep 17 00:00:00 2001 From: Fazana <52551480+FazanaJ@users.noreply.github.com> Date: Thu, 9 Sep 2021 13:03:40 +0100 Subject: [PATCH 15/18] Rotation fix :flushed: Also consolidating the iteration into just one if because legit nothing else is necessary --- src/game/puppylights.c | 81 +++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 44 deletions(-) diff --git a/src/game/puppylights.c b/src/game/puppylights.c index 9e513a92..cd73ed58 100644 --- a/src/game/puppylights.c +++ b/src/game/puppylights.c @@ -85,57 +85,50 @@ void puppylights_iterate(struct PuppyLight *light, Lights1 *src, struct Object * lightRelative[1] = light->pos[0][1] - obj->oPosY; lightRelative[2] = light->pos[0][2] - obj->oPosZ; - //If the node is a cube, then calculate the distance a bit differently. - if (light->flags & PUPPYLIGHT_SHAPE_CUBE) - { - //Get the position based off the rotation of the cube. - lightPos[0] = lightRelative[2] * sins(light->yaw) + lightRelative[0] * coss(light->yaw); - lightPos[1] = lightRelative[2] * coss(light->yaw) - lightRelative[0] * sins(light->yaw); + //Get the position based off the rotation of the box. + lightPos[0] = lightRelative[2] * sins(-light->yaw) + lightRelative[0] * coss(-light->yaw); + lightPos[1] = lightRelative[2] * coss(-light->yaw) - lightRelative[0] * sins(-light->yaw); - #ifdef VISUAL_DEBUG - Vec3f debugPos[2]; - vec3f_set(debugPos[0], light->pos[0][0], light->pos[0][1], light->pos[0][2]); - vec3f_set(debugPos[1], light->pos[1][0], light->pos[1][1], light->pos[1][2]); - debug_box_color(0x00FFFF00); - debug_box_rot(debugPos[0], debugPos[1], light->yaw, DEBUG_SHAPE_BOX); - #endif - - if (-light->pos[1][0] < lightPos[0] && lightPos[0] < light->pos[1][0] && - -light->pos[1][1] < lightRelative[1] && lightRelative[1] < light->pos[1][1] && - -light->pos[1][2] < lightPos[1] && lightPos[1] < light->pos[1][2]) - { - scale = (lightPos[0] * lightPos[0]) + (lightRelative[1] * lightRelative[1]) + (lightPos[1] * lightPos[1]); - scaleVal = (light->pos[1][0] + light->pos[1][1] + light->pos[1][2])/3; - scaleVal *= scaleVal; - } - else - return; - } - else + #ifdef VISUAL_DEBUG + Vec3f debugPos[2]; + vec3f_set(debugPos[0], light->pos[0][0], light->pos[0][1], light->pos[0][2]); + vec3f_set(debugPos[1], light->pos[1][0], light->pos[1][1], light->pos[1][2]); + debug_box_color(0x00FF00FF); if (light->flags & PUPPYLIGHT_SHAPE_CYLINDER) + debug_box_rot(debugPos[0], debugPos[1], light->yaw, DEBUG_SHAPE_CYLINDER); + else + debug_box_rot(debugPos[0], debugPos[1], light->yaw, DEBUG_SHAPE_BOX); + #endif + //Check if the object is inside the box, after correcting it for rotation. + if (-light->pos[1][0] < lightPos[0] && lightPos[0] < light->pos[1][0] && + -light->pos[1][1] < lightRelative[1] && lightRelative[1] < light->pos[1][1] && + -light->pos[1][2] < lightPos[1] && lightPos[1] < light->pos[1][2]) { - - #ifdef VISUAL_DEBUG - Vec3f debugPos[2]; - vec3f_set(debugPos[0], light->pos[0][0], light->pos[0][1], light->pos[0][2]); - vec3f_set(debugPos[1], light->pos[1][0], light->pos[1][1], light->pos[1][2]); - debug_box_color(0x00FFFF00); - debug_box_rot(debugPos[0], debugPos[1], 0, DEBUG_SHAPE_CYLINDER); - #endif - //First do an AABB check, since it's a fair bit faster than doing just a distance check. - if (-light->pos[1][0] < lightRelative[0] && lightRelative[0] < light->pos[1][0] && - -light->pos[1][1] < lightRelative[1] && lightRelative[1] < light->pos[1][1] && - -light->pos[1][2] < lightRelative[2] && lightRelative[2] < light->pos[1][2]) + //If so, then start making preparations to see how alongside they're in. + //This takes the largest side of the box and multiplies the other axis to match the numbers. + //This way, the colour value will scale correctly, no matter which side is entered. + //Because positions are a vector, and Y is up, it means tempID needs to be multiplied + //By 2 in order to reach the X and Z axis. Thanks SM64. + s32 lightPos2[2]; + lightPos2[0] = light->pos[1][2] * sins(-light->yaw) + light->pos[1][0] * coss(-light->yaw); + lightPos2[1] = light->pos[1][2] * coss(-light->yaw) - light->pos[1][0] * sins(-light->yaw); + s32 tempID; + f32 scaleFac; + if (ABS(lightPos2[0]) > ABS(lightPos2[1])) + tempID = 0; + else + tempID = 1; + scaleFac = (f32)light->pos[1][(tempID^1)*2]/(f32)light->pos[1][tempID*2]; + lightPos[tempID] *= scaleFac; + scale = (lightPos[0] * lightPos[0]) + (lightRelative[1] * lightRelative[1]) + (lightPos[1] * lightPos[1]); + scaleVal = (light->pos[1][0] + light->pos[1][1] + light->pos[1][2])/3; + scaleVal *= scaleVal; + //If it's a cylinder, then bin anything outside it. + if (light->flags & PUPPYLIGHT_SHAPE_CYLINDER) { - //Okay now we can do a distance check - scale = (lightRelative[0] * lightRelative[0]) + (lightRelative[1] * lightRelative[1]) + (lightRelative[2] * lightRelative[2]); - scaleVal = (light->pos[1][0] + light->pos[1][1] + light->pos[1][2])/3; - scaleVal *= scaleVal; if (scale > scaleVal) return; } - else - return; } else return; From 93f8872bd97c66258eb58b5bb43b9c342d39dc57 Mon Sep 17 00:00:00 2001 From: Fazana <52551480+FazanaJ@users.noreply.github.com> Date: Thu, 9 Sep 2021 13:29:13 +0100 Subject: [PATCH 16/18] Society if Faz commented his code any further --- src/game/puppylights.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/game/puppylights.c b/src/game/puppylights.c index cd73ed58..441abf5c 100644 --- a/src/game/puppylights.c +++ b/src/game/puppylights.c @@ -11,6 +11,13 @@ simply set the object flag OBJ_FLAG_EMIT_LIGHT and set some values to o->puppyli For easy light modification, you can call set_light_properties, so set all the attributes of any given loaded puppylight struct. Objects will ignore x, y, z, active and room, as it will set all of these automatically. It will force the PUPPYLIGHT_DYNAMIC flag, too. + +If you're introducing a static light in the level script with PUPPYLIGHT_NODE, ensure it's contained +inside the respective area node it's going to be inside, otherwise it will not show up. If you do not +use rooms in your level, or if you wish for this light to be seen from any room, use -1 for that param. + +If you have visual debug enabled, light nodes will show up as magenta in the world. They will be +shaped and rotated correctly, for accurate representation of their properties. **/ #include @@ -93,7 +100,7 @@ void puppylights_iterate(struct PuppyLight *light, Lights1 *src, struct Object * Vec3f debugPos[2]; vec3f_set(debugPos[0], light->pos[0][0], light->pos[0][1], light->pos[0][2]); vec3f_set(debugPos[1], light->pos[1][0], light->pos[1][1], light->pos[1][2]); - debug_box_color(0x00FF00FF); + debug_box_color(0x00FF0040); if (light->flags & PUPPYLIGHT_SHAPE_CYLINDER) debug_box_rot(debugPos[0], debugPos[1], light->yaw, DEBUG_SHAPE_CYLINDER); else From 2894e3cab8f8e2e93db4407318f5924bc21bcc54 Mon Sep 17 00:00:00 2001 From: Fazana <52551480+FazanaJ@users.noreply.github.com> Date: Thu, 9 Sep 2021 14:05:38 +0100 Subject: [PATCH 17/18] Update spawn_object.c --- src/game/spawn_object.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/game/spawn_object.c b/src/game/spawn_object.c index 04f2e7d8..eb42bd04 100644 --- a/src/game/spawn_object.c +++ b/src/game/spawn_object.c @@ -357,6 +357,8 @@ struct Object *create_object(const BehaviorScript *bhvScript) { */ void mark_obj_for_deletion(struct Object *obj) { //! Same issue as obj_mark_for_deletion +#ifdef PUPPYLIGHTS obj_disable_light(obj); +#endif obj->activeFlags = ACTIVE_FLAG_DEACTIVATED; } From 0afe65462d48ea300ce4851bf40209184dfa8f08 Mon Sep 17 00:00:00 2001 From: Fazana <52551480+FazanaJ@users.noreply.github.com> Date: Thu, 9 Sep 2021 14:06:35 +0100 Subject: [PATCH 18/18] Update object_helpers.c --- src/game/object_helpers.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/game/object_helpers.c b/src/game/object_helpers.c index 84eb45fb..902ce4af 100644 --- a/src/game/object_helpers.c +++ b/src/game/object_helpers.c @@ -1140,7 +1140,9 @@ void obj_mark_for_deletion(struct Object *obj) { // setting it to 0 could potentially enable unexpected behavior. After an // object is marked for deletion, it still updates on that frame (I think), // so this is worth looking into. +#ifdef PUPPYLIGHTS obj_disable_light(obj); +#endif obj->activeFlags = ACTIVE_FLAG_DEACTIVATED; }