forked from itsapi/pycraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender_c_module.c
1173 lines (949 loc) · 34.3 KB
/
render_c_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <Python.h>
#include <math.h>
#include <stdarg.h>
#include "render.h"
#include "colours.c"
#include "data.c"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <signal.h>
PyObject *C_RENDERER_EXCEPTION;
static PrintableChar *last_frame = 0;
static LightingBuffer lighting_buffer = {.current_frame = 0, .screen = 0};
static bool resize;
static bool redraw_all;
static long width;
static long height;
static int frame_id = 1;
#define S_POS_STR_FORMAT L"\033[%ld;%ldH"
#define POS_STR_FORMAT_MAX_LEN (sizeof(S_POS_STR_FORMAT))
static wchar_t *POS_STR_FORMAT = S_POS_STR_FORMAT;
size_t
pos_str(long x, long y, wchar_t *result)
{
return swprintf(result, POS_STR_FORMAT_MAX_LEN, POS_STR_FORMAT, y+1, x+1);
}
// #define DEBUG
#define debug_colour(c) debug(L"%f, %f, %f\n", (c).r, (c).g, (c).b)
#ifdef DEBUG
void
debug(wchar_t *str, ...)
{
static int debug_y = 0;
static wchar_t debug_buff[128];
size_t pos = pos_str(0, height + debug_y++, debug_buff);
debug_buff[pos] = L'\0';
wprintf(debug_buff);
wprintf(L"\033[0K");
va_list aptr;
va_start(aptr, str);
vwprintf(str, aptr);
va_end(aptr);
puts("\033[0K");
if (debug_y > 20)
debug_y = 0;
}
#else
void
debug(wchar_t *str, ...)
{}
#endif
wchar_t
PyString_AsChar(PyObject *str)
{
wchar_t result = 0;
Py_ssize_t size;
wchar_t *chars = PyUnicode_AsWideCharString(str, &size);
if (chars && size > 0)
{
result = *chars;
}
return result;
}
long
get_long_from_PyDict_or(PyObject *dict, char key[], long default_result)
{
long result = default_result;
PyObject *item = PyDict_GetItemString(dict, key);
if (item != NULL)
{
result = PyLong_AsLong(item);
}
return result;
}
wchar_t
get_block(long x, long y, PyObject *map)
{
// TODO: Cache this? (hash map?)
wchar_t result = 0;
PyObject *column = PyDict_GetItem(map, PyLong_FromLong(x));
if (column)
{
PyObject *block = NULL;
if (y < PyList_Size(column))
{
block = PyList_GetItem(column, y);
}
if (block)
{
result = PyString_AsChar(block);
}
}
return result;
}
float
lightness(Colour *rgb)
{
return 0.2126f * rgb->r + 0.7152f * rgb->g + 0.0722f * rgb->b;
}
float
circle_dist(float test_x, float test_y, float x, float y, float r)
{
return ( pow(test_x - x, 2.0f) / pow(r , 2.0f) +
pow(test_y - y, 2.0f) / pow(r*.5f, 2.0f) );
}
float
lit(long x, long y, long lx, long ly, long l_width, long l_height, long l_radius)
{
float result;
float l_center_x = (float)lx + ((float)l_width * 0.5) - 0.5f;
float l_center_y = (float)ly - ((float)l_height * 0.5) + 0.5f;
result = circle_dist(x, y, l_center_x, l_center_y, l_radius);
return fmin(result, 1.0f);
}
Colour
PyColour_AsColour(PyObject *py_colour)
{
Colour rgb;
rgb.r = -1;
if (py_colour)
{
rgb.r = PyFloat_AsDouble(PyTuple_GetItem(py_colour, 0));
rgb.g = PyFloat_AsDouble(PyTuple_GetItem(py_colour, 1));
rgb.b = PyFloat_AsDouble(PyTuple_GetItem(py_colour, 2));
}
return rgb;
}
wchar_t
get_char(long x, long y, PyObject *map, BlockData *pixel)
{
// TODO: Implement a cache in get_block?
wchar_t left_block_key = get_block(x-1, y, map);
wchar_t right_block_key = get_block(x+1, y, map);
wchar_t below_block_key = get_block(x, y+1, map);
wchar_t character = pixel->character;
if (below_block_key == 0 || !(get_block_data(below_block_key)->solid))
{
if (left_block_key != 0 && (get_block_data(left_block_key)->solid) && pixel->character_left != 0)
{
character = pixel->character_left;
}
else if (right_block_key != 0 && (get_block_data(right_block_key)->solid) && pixel->character_right != 0)
{
character = pixel->character_right;
}
}
return character;
}
bool
printable_char_eq(PrintableChar *a, PrintableChar *b)
{
return (a->character == b->character &&
colour_eq(&(a->fg), &(b->fg)) &&
colour_eq(&(a->bg), &(b->bg)) &&
a->style == b->style);
}
void
apply_block_lightness(Colour *result, float lightness)
{
/*
Applies a 0-1 lightness to a block colour
*/
Colour hsv = rgb_to_hsv(result);
hsv.v *= lightness;
*result = hsv_to_rgb(&hsv);
}
void
get_lighting_buffer_pixel(LightingBuffer *lighting_buffer, int x, int y, struct PixelLighting **result)
{
*result = lighting_buffer->screen + y * lighting_buffer->width + x;
}
int
objects_hash_func(long x, long y)
{
int result = (((x + y) * (x + y + 1) / 2) + y) % OBJECTS_MAP_SIZE;
return result;
}
void
get_obj_pixel(long x, long y, ObjectsMap *objects_map, Object *result)
{
long current_pixel_hierarchy = 0;
Object *object = NULL;
int map_hash = objects_hash_func(x, y);
Object *test_obj = objects_map->objects + map_hash;
do
{
if (test_obj->from_frame == frame_id &&
test_obj->hierarchy > current_pixel_hierarchy &&
x == test_obj->x && y == test_obj->y)
{
current_pixel_hierarchy = test_obj->hierarchy;
object = test_obj;
}
test_obj = test_obj->next;
}
while (test_obj != NULL);
if (object != NULL)
{
*result = *object;
}
return;
}
void
create_lit_block(long screen_x, long world_x, long world_y, PyObject *map, wchar_t pixel_f_key, struct PixelLighting *lighting_pixel, ObjectsMap *objects_map, LightingBuffer *lighting_buffer, Settings *settings, PrintableChar *result)
{
bool light_bg = false;
bool light_fg = false;
// Get block bg colour if it isn't transparent
BlockData *pixel_f = get_block_data(pixel_f_key);
if (pixel_f->colours.bg.r >= 0)
{
result->bg = pixel_f->colours.bg;
light_bg = true;
}
// Get object fg colour and character if there is an object, otherwise get block fg colour and character
Object object = {.key = 0};
get_obj_pixel(screen_x, world_y, objects_map, &object);
if (object.key != 0)
{
result->character = object.key;
result->fg = object.rgb;
light_fg = false;
}
else
{
result->character = get_char(world_x, world_y, map, pixel_f);
if (pixel_f->colours.fg.r >= 0)
{
result->fg = pixel_f->colours.fg;
light_fg = true;
}
}
// Light block fg and bg
if ((settings->fancy_lights > 0) &&
(light_bg || light_fg) &&
lighting_buffer->current_frame != 0)
{
// lighting_pixel->lightness is guaranteed to be set for every pixel this frame by add_daylight_lightness_to_lighting_buffer if there hasn't been an error
float lightness = 1;
if (lighting_pixel != NULL)
{
lightness = lighting_pixel->lightness;
}
if (light_bg)
{
apply_block_lightness(&result->bg, lightness);
}
if (light_fg)
{
apply_block_lightness(&result->fg, lightness);
}
}
result->style = pixel_f->colours.style;
}
void
create_pixel(long screen_x, long world_x, long world_y, PyObject *map, wchar_t pixel_f_key, ObjectsMap *objects_map, LightingBuffer *lighting_buffer, bool underground, Colour *sky_colour_rgb, Settings *settings, PrintableChar *result)
{
result->bg.r = -1;
result->fg.r = -1;
result->style = -1;
result->character = ' ';
long lb_x = world_x - lighting_buffer->x;
long lb_y = world_y - lighting_buffer->y;
struct PixelLighting *lighting_pixel = NULL;
if (lb_x >= 0 && lb_x < lighting_buffer->width &&
lb_y >= 0 && lb_y < lighting_buffer->height)
{
get_lighting_buffer_pixel(lighting_buffer, lb_x, lb_y, &lighting_pixel);
}
else
{
debug(L"Error: create_pixel trying to access lighting_buffer out of bounds");
}
create_lit_block(screen_x, world_x, world_y, map, pixel_f_key, lighting_pixel, objects_map, lighting_buffer, settings, result);
// If the block did not set a background colour, add the sky background.
if (result->bg.r == -1 && lighting_buffer->current_frame != 0)
{
// lighting_pixel->background_colour_set_on_frame is only set for lit pixels, set the rest to sky_colour/cave colour.
if (lighting_pixel != NULL && lighting_pixel->background_colour_set_on_frame == lighting_buffer->current_frame)
{
result->bg = lighting_pixel->background_colour;
}
else
{
if (underground)
{
result->bg = cave_colour;
}
else
{
result->bg = *sky_colour_rgb;
}
}
}
}
bool
is_light_behind_a_solid_block(long light_world_x, long light_world_y, long light_height, long light_width, PyObject *map)
{
bool result = true;
long world_x, world_y;
for (world_x = light_world_x; world_x < light_world_x + light_width; ++world_x)
{
for (world_y = light_world_y; world_y > light_world_y - light_height; --world_y)
{
wchar_t block_key = get_block(world_x, world_y, map);
if (block_key == 0 ||
!get_block_data(block_key)->solid)
{
result = false;
break;
}
}
}
return result;
}
bool
check_light_z(Light *light, long top_edge, PyObject *map, PyObject *slice_heights)
{
/*
Lights with z of:
-2 are not added to the lighting buffer as they are just graphical lights, like the moon.
-1 are added to the lighting buffer IF they are above the ground, and not behind a solid block.
0 are always added to the lighting buffer.
*/
bool result = false;
if (light->z == -2)
{
result = false;
}
else if (light->z == -1)
{
long buffer_ly = light->world_y - top_edge;
// Check light source is above ground
float ground_height_world = PyFloat_AsDouble(PyDict_GetItem(slice_heights, PyLong_FromLong(light->world_x)));
float ground_height_buffer = (world_gen_height - ground_height_world) - top_edge;
if (buffer_ly < ground_height_buffer)
{
// Check light source is not behind a solid block
if (!is_light_behind_a_solid_block(light->world_x, light->world_y, light->height, light->width, map))
{
result = true;
}
}
}
else if (light->z == 0)
{
result = true;
}
return result;
}
void
add_light_pixel_lightness_to_lighting_buffer(struct PixelLighting *pixel, float light_distance, Light *light)
{
// TODO: Basic lighting mode: threshold
// TODO: Figure out whether this would be better before (old version) or after inverting the distance?
light_distance *= lightness(&light->rgb);
float this_lightness = 1 - light_distance;
// this_lightness *= lightness(&light->rgb);
if (pixel->lightness < this_lightness ||
pixel->lightness_set_on_frame != lighting_buffer.current_frame)
{
pixel->lightness = this_lightness;
pixel->lightness_set_on_frame = lighting_buffer.current_frame;
}
}
void
add_light_pixel_colour_to_lighting_buffer(Settings *settings, struct PixelLighting *pixel, long x, long y, float light_distance, Light *light, PyObject *map, Colour *sky_colour, long slice_height)
{
/*
Adds the colour of the light's pixel for the light's light-radius' to the lighting buffer.
*/
// Check if the background for this pixel is visible
bool visible = false;
// First, if the background for this pixel has already been set this frame, then the check has already passed.
if (pixel->background_colour_set_on_frame == lighting_buffer.current_frame)
{
visible = true;
}
else
{
// Check if there is no block or a block without a clear background at this position.
wchar_t block_key = get_block(lighting_buffer.x+x, lighting_buffer.y+y, map);
if (block_key == 0 ||
get_block_data(block_key)->colours.bg.r == -1)
{
visible = true;
}
}
if (visible)
{
bool add_to_buffer = true;
Colour rgb;
float pixel_background_colour_lightness;
// Different lighting calculations for above and below ground
long world_top_to_ground = world_gen_height - slice_height;
long ground_height_buffer = world_top_to_ground - lighting_buffer.y;
if (y > ground_height_buffer)
{
// Underground
// How light's Z values apply to underground background:
// -2 invisible
// -1 visible if the source is above ground
// 0 always visible
if (light->z == -2)
{
add_to_buffer = false;
}
else if (light->z == -1)
{
if (light->world_y > world_top_to_ground)
{
// Light source is underground
add_to_buffer = false;
}
}
// Does light's Z value allow this pixel to light?
if (add_to_buffer)
{
if (settings->fancy_lights > 0)
{
// Fancy lighting
// TODO: Fudge this calculation until it looks like the Python renderer!
pixel_background_colour_lightness = 1 - light_distance;
rgb.r = (cave_colour.r + pixel_background_colour_lightness) * .5f;
rgb.g = (cave_colour.g + pixel_background_colour_lightness) * .5f;
rgb.b = (cave_colour.b + pixel_background_colour_lightness) * .5f;
}
else
{
// Basic lighting
rgb = cave_colour;
pixel_background_colour_lightness = 0;
}
}
}
else
{
// Above ground
if (settings->fancy_lights > 0)
{
Colour hsv = lerp_colour(&light->hsv, light_distance, sky_colour);
rgb = hsv_to_rgb(&hsv);
}
else
{
rgb = CYAN;
}
pixel_background_colour_lightness = lightness(&rgb);
}
// Update lighting buffer pixel if it's unset this frame or if it's lightness is less than this lights lightness
if (add_to_buffer &&
(pixel->background_colour_lightness < pixel_background_colour_lightness ||
pixel->background_colour_set_on_frame != lighting_buffer.current_frame))
{
pixel->background_colour = rgb;
pixel->background_colour_lightness = pixel_background_colour_lightness;
pixel->background_colour_set_on_frame = lighting_buffer.current_frame;
}
}
}
void
add_bk_objects_pixels_colour_to_lighting_buffer(PyObject *bk_objects, PyObject *slice_heights)
{
/*
Adds the pixels of the background objects (sun and moon).
- Because the function is only setting ~4 blocks with the current
usage for bk_objects, this function does not mask around solid
blocks (create_pixel won't use background pixels it doesn't need
anyway).
- We do need to mask ground height so the objects don't show up
underground.
- We do not need to check brightness as we are not setting light
pixels, this is just for the actual pixels of the background
objects.
*/
PyObject *iter = PyObject_GetIter(bk_objects);
PyObject *bk_object;
while ((bk_object = PyIter_Next(iter)))
{
long o_world_x = PyLong_AsLong(PyDict_GetItemString(bk_object, "x"));
long o_world_y = PyLong_AsLong(PyDict_GetItemString(bk_object, "y"));
long o_width = PyLong_AsLong(PyDict_GetItemString(bk_object, "width"));
long o_height = PyLong_AsLong(PyDict_GetItemString(bk_object, "height"));
Colour o_colour = PyColour_AsColour(PyDict_GetItemString(bk_object, "colour"));
long world_x;
for (world_x = o_world_x; world_x < o_world_x + o_width; ++world_x)
{
long buffer_x = world_x - lighting_buffer.x;
if (buffer_x >= 0 && buffer_x < lighting_buffer.width)
{
long ground_height_world = PyFloat_AsDouble(PyDict_GetItem(slice_heights, PyLong_FromLong(world_x)));
long world_top_to_ground = world_gen_height - ground_height_world;
long world_y;
for (world_y = o_world_y; world_y > o_world_y - o_height; --world_y)
{
long buffer_y = world_y - lighting_buffer.y;
if (world_y < world_top_to_ground &&
buffer_y >= 0 && buffer_y < lighting_buffer.height)
{
struct PixelLighting *pixel;
get_lighting_buffer_pixel(&lighting_buffer, buffer_x, buffer_y, &pixel);
pixel->background_colour = o_colour;
pixel->background_colour_set_on_frame = lighting_buffer.current_frame;
}
}
}
}
}
}
void
add_daylight_lightness_to_lighting_buffer(PyObject *lights, PyObject *slice_heights, float day)
{
/*
Fills in all the gaps of the lightness lighting buffer with daylight, also overwrites darker than daylight parts.
*/
long x, y;
for (x = 0; x < lighting_buffer.width; ++x)
{
long ground_height_world = PyFloat_AsDouble(PyDict_GetItem(slice_heights, PyLong_FromLong(lighting_buffer.x+x)));
long ground_height_buffer = (world_gen_height - ground_height_world) - lighting_buffer.y;
for (y = 0; y < lighting_buffer.height; ++y)
{
float lightness;
if (y < ground_height_buffer)
{
// Above ground
lightness = day;
}
else if (y < ground_height_buffer + 3)
{
// Surface fade
int d_ground = y - ground_height_buffer;
float ground_fade = fmin(1.0f, ((float)d_ground / 3.0f));
lightness = lerp(day, ground_fade, 0.0f);
}
else
{
// Underground
lightness = 0;
}
struct PixelLighting *pixel;
get_lighting_buffer_pixel(&lighting_buffer, x, y, &pixel);
if (pixel->lightness < lightness ||
pixel->lightness_set_on_frame != lighting_buffer.current_frame)
{
pixel->lightness = lightness;
pixel->lightness_set_on_frame = lighting_buffer.current_frame;
}
// TODO: Assert pixel->lightness_set_on_frame == lighting_buffer.current_frame
}
}
}
void
fill_lighting_buffer(PyObject *lights, PyObject *bk_objects, PyObject *map, Settings *settings, PyObject *slice_heights, float day, Colour *sky_colour)
{
/*
- Store the lightness value for every block, calculated from the max of:
- Lights (passed in from python)
- Including sun (not moon), when sun is above ground height and not behind a block
- Day value, fading to 0 at the ground
- Also stores the background colour for pixels where the block in the map has a clear bg.
- The colour of the light at radius `r` from the pixel is calculated with:
hsv_to_rgb( lerp_colour( rgb_to_hsv(light_colour), r, sky_colour ) )
- The colour is then selected by taking the max lightness of that colour from all the lights reaching this pixel.
*/
PyObject *iter = PyObject_GetIter(lights);
PyObject *py_light;
while ((py_light = PyIter_Next(iter)))
{
Light light = {
.world_x = PyLong_AsLong(PyDict_GetItemString(py_light, "x")),
.world_y = PyLong_AsLong(PyDict_GetItemString(py_light, "y")),
.z = PyLong_AsLong(PyDict_GetItemString(py_light, "z")),
.radius = PyLong_AsLong(PyNumber_Long(PyDict_GetItemString(py_light, "radius"))),
.width = get_long_from_PyDict_or(py_light, "source_width", 1),
.height = get_long_from_PyDict_or(py_light, "source_height", 1),
.rgb = PyColour_AsColour(PyDict_GetItemString(py_light, "colour"))
};
light.hsv = rgb_to_hsv(&light.rgb);
bool add_this_lights_lightness = check_light_z(&light, lighting_buffer.y, map, slice_heights);
long buffer_x_pos = light.world_x - lighting_buffer.x;
long buffer_y_pos = light.world_y - lighting_buffer.y;
long buffer_x, buffer_y;
for (buffer_x = buffer_x_pos - light.radius; buffer_x <= buffer_x_pos + light.radius; ++buffer_x)
{
for (buffer_y = buffer_y_pos - light.radius; buffer_y <= buffer_y_pos + light.radius; ++buffer_y)
{
// Is pixel on screen?
if ((buffer_x >= 0 && buffer_x < lighting_buffer.width) &&
(buffer_y >= 0 && buffer_y < lighting_buffer.height))
{
float light_distance = lit(buffer_x, buffer_y, buffer_x_pos, buffer_y_pos, light.width, light.height, light.radius);
if (light_distance < 1)
{
struct PixelLighting *lighting_pixel;
get_lighting_buffer_pixel(&lighting_buffer, buffer_x, buffer_y, &lighting_pixel);
if (add_this_lights_lightness)
{
add_light_pixel_lightness_to_lighting_buffer(lighting_pixel, light_distance, &light);
}
long slice_height = PyLong_AsLong(PyDict_GetItem(slice_heights, PyLong_FromLong(lighting_buffer.x + buffer_x)));
add_light_pixel_colour_to_lighting_buffer(settings, lighting_pixel, buffer_x, buffer_y, light_distance, &light, map, sky_colour, slice_height);
}
}
}
}
}
add_bk_objects_pixels_colour_to_lighting_buffer(bk_objects, slice_heights);
add_daylight_lightness_to_lighting_buffer(lights, slice_heights, day);
}
void
calculate_object_pixel_colour(PyObject *object, Object *map_obj)
{
// Apply effect colour if it exists
Colour colour = PyColour_AsColour(PyDict_GetItemString(object, "colour"));
if (colour.r == -1)
{
colour = get_block_data(map_obj->key)->colours.fg;
}
Colour effect_colour = PyColour_AsColour(PyDict_GetItemString(object, "effect_colour"));
float effect_strength = -1;
PyObject *effect_strength_obj = PyDict_GetItemString(object, "effect_strength");
if (effect_strength_obj != NULL)
{
effect_strength = (PyFloat_AsDouble(effect_strength_obj));
}
if (effect_colour.r != -1 &&
effect_strength >= 0 && effect_strength <= 1)
{
map_obj->rgb = lerp_colour(&colour, effect_strength, &effect_colour);
}
else
{
map_obj->rgb = colour;
}
}
void
filter_objects(PyObject *objects, ObjectsMap *objects_map, long left_edge, long right_edge, long top_edge, long bottom_edge)
{
++frame_id;
static Object zero_obj = {0};
PyObject *iter = PyObject_GetIter(objects);
PyObject *object;
while ((object = PyIter_Next(iter)))
{
long ox = PyLong_AsLong(PyDict_GetItemString(object, "x"));
long oy = PyLong_AsLong(PyDict_GetItemString(object, "y"));
PyObject *model = PyDict_GetItemString(object, "model");
long width = PySequence_Length(model);
long height = PySequence_Length(PySequence_GetItem(model, 0));
int dx, dy;
for (dx = 0; dx < width; ++dx)
{
for (dy = 0; dy < height; ++dy)
{
long mx = ox + dx;
long my = oy - dy;
if ((mx >= 0 && mx <= (right_edge - left_edge)) &&
(my >= top_edge && my <= bottom_edge))
{
int map_hash = objects_hash_func(mx, my);
Object *map_obj = objects_map->objects + map_hash;
while (map_obj->from_frame == frame_id)
{
if (map_obj->next == NULL)
{
map_obj->next = (Object *)malloc(sizeof(Object));
*(map_obj->next) = zero_obj;
}
map_obj = map_obj->next;
}
map_obj->from_frame = frame_id;
map_obj->x = mx;
map_obj->y = my;
map_obj->hierarchy = PyLong_AsLong(PyDict_GetItemString(object, "hierarchy"));
map_obj->key = PyString_AsChar(PySequence_GetItem(PySequence_GetItem(model, dx), height - 1 - dy));
calculate_object_pixel_colour(object, map_obj);
}
}
}
}
}
bool
terminal_out(ScreenBuffer *frame, PrintableChar *c, long x, long y, Settings *settings)
{
size_t frame_pos = y * width + x;
if (!printable_char_eq(last_frame + frame_pos, c) || resize || redraw_all)
{
last_frame[frame_pos] = *c;
size_t old_cur_pos = frame->cur_pos;
frame->cur_pos += pos_str(x, y, frame->buffer + frame->cur_pos);
frame->cur_pos += colour_str(c, frame->buffer + frame->cur_pos, settings);
if (frame->cur_pos >= frame->size)
{
PyErr_SetString(C_RENDERER_EXCEPTION, "Exceeded frame buffer size in terminal_out!");
return false;
}
if (frame->cur_pos - old_cur_pos >= (COLOUR_CODE_MAX_LEN + POS_STR_FORMAT_MAX_LEN))
{
PyErr_SetString(C_RENDERER_EXCEPTION, "Block string length exceeded allocated space!");
return false;
}
}
return true;
}
bool
setup_frame(ScreenBuffer *frame, long new_width, long new_height)
{
resize = false;
if (new_width != width)
{
resize = true;
width = new_width;
}
if (new_height != height)
{
resize = true;
height = new_height;
}
if (resize)
{
frame->size = width * height * (POS_STR_FORMAT_MAX_LEN + COLOUR_CODE_MAX_LEN);
frame->buffer = (wchar_t *)realloc(frame->buffer, frame->size * sizeof(wchar_t));
if (!frame->buffer)
{
PyErr_SetString(C_RENDERER_EXCEPTION, "Could not allocate frame buffer!");
return false;
}
last_frame = (PrintableChar *)realloc(last_frame, width * height * sizeof(PrintableChar));
if (!last_frame)
{
PyErr_SetString(C_RENDERER_EXCEPTION, "Could not allocate last frame buffer!");
return false;
}
}
frame->cur_pos = 0;
return true;
}
static PyObject *
render_map(PyObject *self, PyObject *args)
{
static ScreenBuffer frame = {.buffer = 0};
static ObjectsMap objects_map = {{{0}}};
long left_edge,
right_edge,
top_edge,
bottom_edge;
PyObject *map,
*slice_heights,
*objects,
*py_sky_colour,
*py_settings;
if (!PyArg_ParseTuple(args, "OO(ll)(ll)OOOl:render_map", &map, &slice_heights,
&left_edge, &right_edge, &top_edge, &bottom_edge,
&objects, &py_sky_colour, &py_settings, &redraw_all))
{
PyErr_SetString(C_RENDERER_EXCEPTION, "Could not parse arguments!");
return NULL;
}
Colour sky_colour_hsv = PyColour_AsColour(py_sky_colour);
Colour sky_colour_rgb = hsv_to_rgb(&sky_colour_hsv);
Settings settings = {
.terminal_output = PyLong_AsLong(PyDict_GetItemString(py_settings, "terminal_output")),
.fancy_lights = PyLong_AsLong(PyDict_GetItemString(py_settings, "fancy_lights")),
.colours = PyLong_AsLong(PyDict_GetItemString(py_settings, "colours"))
};
long cur_width = right_edge - left_edge;
long cur_height = bottom_edge - top_edge;
if (!setup_frame(&frame, cur_width, cur_height))
return NULL;
if (!PyDict_Check(map))
{
PyErr_SetString(C_RENDERER_EXCEPTION, "Map is not a dict!");
return NULL;
}
filter_objects(objects, &objects_map, left_edge, right_edge, top_edge, bottom_edge);
Py_ssize_t i = 0;
PyObject *world_x, *column;
while (PyDict_Next(map, &i, &world_x, &column))
{
long world_x_l = PyLong_AsLong(world_x);
if (!(world_x_l >= left_edge && world_x_l < right_edge))
continue;
if (!PyList_Check(column))
{
PyErr_SetString(C_RENDERER_EXCEPTION, "Column is not a list!");
return NULL;
}
long screen_x = world_x_l - left_edge;
long slice_height = PyFloat_AsDouble(PyDict_GetItem(slice_heights, PyLong_FromLong(world_x_l)));
PyObject *iter = PyObject_GetIter(column);
PyObject *py_pixel;
long world_y_l = 0;
while ((py_pixel = PyIter_Next(iter)))
{
if (world_y_l >= top_edge && world_y_l < bottom_edge)
{
long screen_y = world_y_l - top_edge;
bool underground = world_y_l > world_gen_height - slice_height;
wchar_t pixel = PyString_AsChar(py_pixel);
if (!pixel)
{
PyErr_SetString(C_RENDERER_EXCEPTION, "Cannot get char from pixel!");