KILL THE SINGLETON
[SmugglerRL.git] / src / light.d
blob2798778c7af2d4d0d255b75d92e370eacd40e83e
1 import stdlib;
2 import map;
3 import colour;
4 import being;
5 import constants;
7 float sqavg(float[] nums) {
8 float sum;
9 foreach (i; nums) {
10 sum += i * i;
13 return pow(sum, 1. / nums.length);
15 T avg(T)(T[] nums) {
16 T sum;
17 foreach (i; nums) {
18 sum += i;
20 return sum / nums.length;
23 void do_colour(ref Map map, list!Being mons) {
24 import std.stdio;
25 struct Lightcell {
26 float value = 0;
27 uint r, g, b;
28 uint nums;
30 Lightcell[][] base = new Lightcell[][](map_x, map_y);
31 const Lightmap umap = mons.front.lightmap;
33 foreach (mon; mons) {
34 if (!mon.light_source) {
35 continue;
38 auto loc = Vector2n(mon.lightmap.top_edge, mon.lightmap.left_edge);
39 foreach (lightrow; mon.lightmap.get_lights) {
40 foreach (light; lightrow) {
41 if (light.lit && umap[loc].visible) {
42 base[loc.y][loc.x].value += mon.lightmap[loc].light;
43 base[loc.y][loc.x].nums++;
44 base[loc.y][loc.x].r += mon.lightclr.r;
45 base[loc.y][loc.x].g += mon.lightclr.g;
46 base[loc.y][loc.x].b += mon.lightclr.b;
49 loc.x++;
51 loc.y++;
52 loc.x -= 1 + 2*mon.fov_dist;
56 foreach (y; 0 .. map_y) {
57 foreach (x; 0 .. map_x) {
58 if (base[y][x].nums == 0) {
59 map[y, x].bgcolour = RGBColour(0, 0, 0);
60 continue;
63 float f1 = pow(base[y][x].value, .8) / 4.3;
64 float f = f1 / (f1+1); //smaxf;
65 // map[y, x].bgcolour = RGBColour(pow(f/2, 1.5), pow(f/1.4, 3), pow(f/16, 2));
66 //map[y, x].bgcolour = RGBColour(f*1.1, f, f/9);
67 with (base[y][x]) map[y, x].bgcolour = RGBColour(cast(ubyte)(f*r/nums), cast(ubyte)(f*g/nums), cast(ubyte)(f*b/nums)); //f*r/nums, f*g/nums, f*b/nums);
69 //writefln("Set bg to %s", map[y, x].bgcolour);