@@ -2694,7 +2694,39 @@ int ObjectRef::l_set_lighting(lua_State *L)
26942694 lighting.bloom_radius = getfloatfield_default (L, -1 , " radius" , lighting.bloom_radius );
26952695 }
26962696 lua_pop (L, 1 ); // bloom
2697- }
2697+
2698+ lua_getfield (L, 2 , " vision_effects" );
2699+ if (!lua_isnoneornil (L, -1 )) {
2700+ if (!lua_istable (L, -1 ))
2701+ throw LuaError (" vision_effects is not a table" );
2702+
2703+ lua_getfield (L, 3 , " color_transform_matrix" );
2704+
2705+ // if none or nil, keep color transform matrix unchanged
2706+ if (!lua_isnoneornil (L, -1 )) {
2707+ if (!lua_istable (L, -1 ))
2708+ throw LuaError (" vision_effects.color_transform_matrix is not a table" );
2709+
2710+ for (int row = 1 ; row <= 3 ; ++row) {
2711+ lua_rawgeti (L, -1 , row);
2712+ if (!lua_istable (L, -1 ))
2713+ throw LuaError (" color_transform_matrix should be in format {{a, b, c},{d, e, f},{g, a, h}}" );
2714+
2715+ for (int col = 1 ; col <= 3 ; ++col) {
2716+ lua_rawgeti (L, -1 , col);
2717+ if (!lua_isnumber (L, -1 ))
2718+ throw LuaError (" color_transform_matrix should be in format {{a, b, c},{d, e, f},{g, a, h}}" );
2719+
2720+ lighting.vision_effects .color_transform_matrix [(row - 1 ) * 3 + (col - 1 )] = (float )lua_tonumber (L, -1 );
2721+ lua_pop (L, 1 ); // Pop the value at [row][col]
2722+ }
2723+ lua_pop (L, 1 ); // Pop the row table
2724+ }
2725+ }
2726+ lua_pop (L, 1 ); // color_transform_matrix
2727+ }
2728+ lua_pop (L, 1 ); // vision_effects
2729+ }
26982730
26992731 getServer (L)->setLighting (player, lighting);
27002732 return 0 ;
@@ -2746,6 +2778,19 @@ int ObjectRef::l_get_lighting(lua_State *L)
27462778 lua_pushnumber (L, lighting.bloom_radius );
27472779 lua_setfield (L, -2 , " radius" );
27482780 lua_setfield (L, -2 , " bloom" );
2781+ lua_newtable (L); // "vision_effects"
2782+ lua_newtable (L); // "color_transform_matrix"
2783+ // Create the nested table structure {{a, b, c}, {d, e, f}, {g, h, i}}
2784+ for (int row = 0 ; row < 3 ; row++) {
2785+ lua_newtable (L); // Create inner row table
2786+ for (int col = 0 ; col < 3 ; col++) {
2787+ lua_pushnumber (L, lighting.vision_effects .color_transform_matrix [row * 3 + col]); // Push the value
2788+ lua_rawseti (L, -2 , col + 1 ); // Set value in inner table with 1-based indexing
2789+ }
2790+ lua_rawseti (L, -2 , row + 1 ); // Set inner table in the outer matrix table
2791+ }
2792+ lua_setfield (L, -2 , " color_transform_matrix" );
2793+ lua_setfield (L, -2 , " vision_effects" );
27492794 return 1 ;
27502795}
27512796
0 commit comments