Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrote several light routines. #954

Merged
merged 31 commits into from Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3b7bbc7
Rewrote DK_light_set_light_position
AdamPlenty Mar 5, 2021
594535e
Update light_data.c
AdamPlenty Mar 5, 2021
15482a0
Rewrote DK_light_remove_light_from_list
AdamPlenty Mar 5, 2021
031282a
Rewrote DK_light_signal_stat_light_update_in_area
AdamPlenty Mar 5, 2021
19c0423
Rewrote DK_light_signal_update_in_area
AdamPlenty Mar 5, 2021
56cd9e4
Rewrote DK_light_stat_light_map_clear_area
AdamPlenty Mar 6, 2021
d4dd317
Rewrote DK_light_set_light_minimum_size_to_cache
AdamPlenty Mar 6, 2021
1bbb430
Fixed light_stat_light_map_clear_area
AdamPlenty Mar 21, 2021
225ea8c
Update light_data.c
AdamPlenty Jul 15, 2021
34a0950
Renamed field_1
AdamPlenty Aug 4, 2021
281e889
Merge remote-tracking branch 'upstream/master' into Lights
AdamPlenty Aug 4, 2021
8b7e2ac
Update light_data.c
AdamPlenty Aug 4, 2021
17e1854
Improve readability
AdamPlenty Aug 4, 2021
b8db4dd
Update light_data.c
AdamPlenty Aug 4, 2021
5d848c3
Update light_data.c
AdamPlenty Aug 4, 2021
68fbcd3
Made next map block retrieval into a function
AdamPlenty Aug 4, 2021
64198d9
Revert "Made next map block retrieval into a function"
AdamPlenty Aug 4, 2021
2522116
Update light_data.c
AdamPlenty Aug 4, 2021
0295689
Made absolutely sure columns aren't invalid.
AdamPlenty Aug 4, 2021
1b8582f
Converted to for loops.
AdamPlenty Aug 5, 2021
a039151
Update light_data.c
AdamPlenty Aug 5, 2021
43e3832
Removed unneeded line
AdamPlenty Aug 5, 2021
b447df5
Update light_data.c
AdamPlenty Dec 5, 2021
231eed4
Fixed warnings
AdamPlenty Dec 5, 2021
8c10fb2
Moved error message.
AdamPlenty Dec 5, 2021
3edbc04
Merge remote-tracking branch 'upstream/master' into Lights
AdamPlenty Dec 8, 2021
e6e07b8
Merge remote-tracking branch 'upstream/master' into Lights
AdamPlenty Dec 14, 2021
0925c9f
Update light_data.c
AdamPlenty Dec 30, 2021
5f05747
split unknown fields from blob of unknown bytes
PieterVdc Apr 11, 2022
ea2299f
Merge branch 'master' into pr/954
Loobinex Apr 11, 2022
6f98fe1
removed file added mysteriously
Loobinex Apr 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
229 changes: 211 additions & 18 deletions src/light_data.c
Expand Up @@ -32,16 +32,8 @@
extern "C" {
#endif
/******************************************************************************/
DLLIMPORT void _DK_light_remove_light_from_list(struct Light *lgt, struct StructureList *list);
DLLIMPORT void _DK_light_signal_stat_light_update_in_area(long x1, long y1, long x2, long y2);
DLLIMPORT void _DK_light_initialise_lighting_tables(void);
DLLIMPORT void _DK_light_set_light_minimum_size_to_cache(long a1, long a2, long a3);
DLLIMPORT void _DK_light_set_light_position(long lgt_id, struct Coord3d *pos);
DLLIMPORT long _DK_light_get_light_intensity(long idx);
DLLIMPORT long _DK_light_set_light_intensity(long a1, long a2);
DLLIMPORT void _DK_light_render_area(int startx, int starty, int endx, int endy);
DLLIMPORT void _DK_light_stat_light_map_clear_area(long x1, long y1, long x2, long y2);
DLLIMPORT void _DK_light_signal_update_in_area(long sx, long sy, long ex, long ey);

/******************************************************************************/
struct Light *light_allocate_light(void)
Expand Down Expand Up @@ -111,13 +103,13 @@ void light_shadow_cache_free(struct ShadowCache *shdc)

TbBool light_add_light_to_list(struct Light *lgt, struct StructureList *list)
{
if ((lgt->field_1 & 0x01) != 0)
if ((lgt->flags2 & 0x01) != 0)
{
ERRORLOG("Light is already in list");
return false;
}
list->count++;
lgt->field_1 |= 0x01;
lgt->flags2 |= 0x01;
lgt->field_26 = list->index;
list->index = lgt->index;
return true;
Expand Down Expand Up @@ -155,7 +147,7 @@ long light_create_light(struct InitLight *ilght)
lgt->radius = ilght->radius;
lgt->intensity = ilght->intensity;
unsigned long k = 2 * ilght->field_3;
lgt->field_1 = k ^ ((k ^ lgt->field_1) & 0x01);
lgt->flags2 = k ^ ((k ^ lgt->flags2) & 0x01);
set_flag_byte(&lgt->flags,LgtF_Dynamic,ilght->is_dynamic);
lgt->field_1A = ilght->field_8;
lgt->field_18 = ilght->field_4;
Expand Down Expand Up @@ -356,22 +348,147 @@ long light_is_light_allocated(long lgt_id)

void light_set_light_position(long lgt_id, struct Coord3d *pos)
{
_DK_light_set_light_position(lgt_id, pos);
// _DK_light_set_light_position(lgt_id, pos);
struct Light *lgt = &game.lish.lights[lgt_id];
if ( lgt->mappos.x.val != pos->x.val
|| pos->y.val != lgt->mappos.y.val
|| pos->z.val != lgt->mappos.z.val )
{
if ( (lgt->flags & LgtF_Dynamic) == 0 )
{
stat_light_needs_updating = 1;
unsigned char range = lgt->range;
long end_y = lgt->mappos.y.stl.num + range;
long end_x = lgt->mappos.x.stl.num + range;
if ( end_y > 255 )
{
end_y = 255;
}
if ( end_x > 255 )
{
end_x = 255;
}
long beg_y = lgt->mappos.y.stl.num - range;
if ( beg_y < 0 )
{
beg_y = 0;
}
long beg_x = lgt->mappos.x.stl.num - range;
if ( beg_x < 0 )
{
beg_x = 0;
}
light_signal_stat_light_update_in_area(beg_x, beg_y, end_x, end_y);
}
lgt->mappos.x.val = pos->x.val;
lgt->mappos.y.val = pos->y.val;
lgt->mappos.z.val = pos->z.val;
lgt->flags |= LgtF_Unkn08;
}
}

void light_remove_light_from_list(struct Light *lgt, struct StructureList *list)
{
_DK_light_remove_light_from_list(lgt, list);
// _DK_light_remove_light_from_list(lgt, list);
if ( list->count == 0 )
{
ERRORLOG("List %d has no structures", list->index);
return;
}
TbBool Removed = false;
struct Light *lgt2;
struct Light *i;
if ( lgt->flags2 & 1 )
{
if ( lgt->index == list->index )
{
Removed = true;
list->count--;
list->index = lgt->field_26;
lgt->field_26 = 0;
lgt->flags2 &= ~1;
}
else
{
lgt2 = &game.lish.lights[list->index];
for ( i = 0; lgt2 != game.lish.lights; lgt2 = &game.lish.lights[lgt2->field_26] )
{
if ( lgt2 == lgt )
{
Removed = true;
if ( i )
{
i->field_26 = lgt->field_26;
lgt->flags2 &= ~1;
list->count--;
lgt->field_26 = 0;
}
else
{
ERRORLOG("No prev when removing light from list");
}
}
i = lgt2;
}
}
if ( !Removed )
{
ERRORLOG("Could not find light %d in list", lgt->index);
}
}
}

void light_signal_stat_light_update_in_area(long x1, long y1, long x2, long y2)
{
_DK_light_signal_stat_light_update_in_area(x1, y1, x2, y2);
// _DK_light_signal_stat_light_update_in_area(x1, y1, x2, y2);
int i = 0;
struct Light *lgt = &game.lish.lights[1];
do
{
if ( lgt->flags & LgtF_Allocated )
{
if ( !(lgt->flags & LgtF_Dynamic) )
{
unsigned char range = lgt->range;
MapSubtlCoord x = lgt->mappos.x.stl.num;
MapSubtlCoord y = lgt->mappos.y.stl.num;
if ( range + x >= x1 && x - range <= x2 && range + y >= y1 && y - range <= y2 )
{
stat_light_needs_updating = 1;
i++;
lgt->flags |= LgtF_Unkn08;
lgt->flags &= 0x7F;
}
}
}
lgt++;
}
while ( lgt < (struct Light *)game.lish.shadow_cache );
if ( i )
light_stat_light_map_clear_area(x1, y1, x2, y2);
}

void light_signal_update_in_area(long sx, long sy, long ex, long ey)
{
_DK_light_signal_update_in_area(sx, sy, ex, ey);
// _DK_light_signal_update_in_area(sx, sy, ex, ey);
struct Light *lgt = &game.lish.lights[1];
do
{
if ( lgt->flags & LgtF_Allocated )
{
if ( lgt->flags & LgtF_Dynamic )
{
unsigned char range = lgt->range;;
MapSubtlCoord x = lgt->mappos.x.stl.num;
MapSubtlCoord y = lgt->mappos.y.stl.num;
if ( range + x >= sx && x - range <= ex && range + y >= sy && y - range <= ey )
lgt->flags |= LgtF_Unkn08;
}
}
lgt++;
}
while ( lgt < (struct Light *)game.lish.shadow_cache );
light_signal_stat_light_update_in_area(sx, sy, ex, ey);
}

void light_signal_stat_light_update_in_own_radius(struct Light *lgt)
Expand Down Expand Up @@ -588,7 +705,55 @@ void light_initialise(void)

void light_stat_light_map_clear_area(long x1, long y1, long x2, long y2)
{
_DK_light_stat_light_map_clear_area(x1, y1, x2, y2);
// _DK_light_stat_light_map_clear_area(x1, y1, x2, y2);
long j,n,x,k,y;
unsigned short *p;
unsigned short *light_map;
if ( y2 >= y1 )
{
y = y1 << 8;
unsigned long i = x1 + (y1 << 8);
struct Map *Mapblk1 = get_map_block_at_pos(i);
light_map = &game.lish.stat_light_map[i];
for (k = y1; k <= y2; k++)
{
if ( x2 >= x1 )
{
p = light_map;
n = k - 1;
if ( k - 1 <= 0 )
{
n = 0;
}
struct Map *Mapblk2 = get_map_block_at_pos((n << 8) + x1);
for (j = x1; j <= x2; j++)
{
x = j - 1;
if ( x < 0 )
{
x = 0;
}
struct Column *Col1 = get_map_column(Mapblk1);
struct Column *Col2 = get_map_column(Mapblk2);
struct Column *Col3 = get_map_column(get_map_block_at_pos(x + y));
struct Column *Col4 = get_map_column(get_map_block_at_pos((n << 8) + x));
if ( (!column_invalid(Col1)) && (!column_invalid(Col2)) && (!column_invalid(Col3)) && (!column_invalid(Col4)) )
{
*p = game.lish.field_46149 << 8;
}
else
{
*p = 0;
}
p++;
Mapblk1++;
Mapblk2++;
}
}
y += 256;
light_map += 256;
}
}
}

void light_set_lights_on(char state)
Expand Down Expand Up @@ -661,9 +826,37 @@ void update_light_render_area(void)
light_render_area(startx, starty, endx, endy);
}

void light_set_light_minimum_size_to_cache(long a1, long a2, long a3)
void light_set_light_minimum_size_to_cache(long lgt_id, long a2, long a3)
{
_DK_light_set_light_minimum_size_to_cache(a1, a2, a3);
struct Light *lgt;
if ( lgt_id )
{
lgt = &game.lish.lights[lgt_id];
if ( lgt->flags & LgtF_Allocated )
{
if ( lgt->flags & LgtF_Unkn02 )
{
lgt->flags &= ~LgtF_Unkn02;
if ( lgt->flags & LgtF_Dynamic )
{
lgt->field_9 = a2;
lgt->field_24 = a3;
}
else
{
ERRORLOG("Attempt to set_minimum light size to cache on non dynamic light");
}
}
}
else
{
ERRORLOG("Attempt to set minimum light size for unallocated light structure");
}
}
else
{
ERRORLOG("Attempt to set minimum light size for light 0");
}
}

/******************************************************************************/
Expand Down
12 changes: 8 additions & 4 deletions src/light_data.h
Expand Up @@ -48,20 +48,22 @@ enum LightFlags {

struct Light { // sizeof = 46
unsigned char flags;
unsigned char field_1;
unsigned char flags2;
unsigned char intensity;
unsigned char field_3[2];
unsigned char range;
unsigned char field_6;
unsigned short field_7;
unsigned char field_9[5];
unsigned char field_9;
unsigned char field_A[4];
unsigned short index;
unsigned short shadow_index;
long field_12;
unsigned short radius;
short field_18;
short field_1A;
unsigned char field_1C[10];
unsigned char field_1C[8];
unsigned short field_24;
unsigned short field_26;
struct Coord3d mappos;
};
Expand Down Expand Up @@ -125,12 +127,14 @@ TbBool light_is_invalid(const struct Light *lgt);
long light_is_light_allocated(long lgt_id);
void light_set_light_position(long lgt_id, struct Coord3d *pos);
void light_set_lights_on(char state);
void light_set_light_minimum_size_to_cache(long a1, long a2, long a3);
void light_set_light_minimum_size_to_cache(long lgt_id, long a2, long a3);
void light_signal_update_in_area(long sx, long sy, long ex, long ey);
long light_get_total_dynamic_lights(void);
void light_export_system_state(struct LightSystemState *lightst);
void light_import_system_state(const struct LightSystemState *lightst);
TbBool lights_stats_debug_dump(void);
void light_signal_stat_light_update_in_area(long x1, long y1, long x2, long y2);
void light_stat_light_map_clear_area(long x1, long y1, long x2, long y2);

/******************************************************************************/
#ifdef __cplusplus
Expand Down