MCPcopy Create free account
hub / github.com/assaultcube/AC / lightray

Function lightray

source/src/worldlight.cpp:7–116  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5VAR(lightscale,1,4,100);
6
7void lightray(float bx, float by, const persistent_entity &light, float fade = 1, bool flicker = false) // done in realtime, needs to be fast
8{
9 float lx = light.x+(flicker ? (rnd(21)-10)*0.1f : 0);
10 float ly = light.y+(flicker ? (rnd(21)-10)*0.1f : 0);
11 float dx = bx-lx;
12 float dy = by-ly;
13 float dist = sqrtf(dx*dx+dy*dy);
14 if(dist<1.0f) return;
15 int reach = light.attr1;
16 int steps = (int)(reach*reach*1.6f/dist); // can change this for speedup/quality?
17 const int PRECBITS = 12;
18 const float PRECF = 4096.0f;
19 int x = (int)(lx*PRECF);
20 int y = (int)(ly*PRECF);
21 int fadescale = (int)(fade*PRECF);
22 int l = light.attr2*fadescale;
23 int stepx = (int)(dx/(float)steps*PRECF);
24 int stepy = (int)(dy/(float)steps*PRECF);
25 int stepl = (int)(l/(float)steps); // incorrect: light will fade quicker if near edge of the world
26
27 if(maxtmus)
28 {
29 l /= lightscale;
30 stepl /= lightscale;
31
32 if(light.attr3 || light.attr4) // coloured light version, special case because most lights are white
33 {
34 if(flicker)
35 {
36 int dimness = rnd((((255<<PRECBITS)-(int(light.attr2)+int(light.attr3)+int(light.attr4))*fadescale/3)>>(PRECBITS+4))+1);
37 x += stepx*dimness;
38 y += stepy*dimness;
39 }
40
41 if(OUTBORD(x>>PRECBITS, y>>PRECBITS)) return;
42
43 int g = light.attr3*fadescale;
44 int stepg = (int)(g/(float)steps);
45 int b = light.attr4*fadescale;
46 int stepb = (int)(b/(float)steps);
47 g /= lightscale;
48 stepg /= lightscale;
49 b /= lightscale;
50 stepb /= lightscale;
51 loopi(steps)
52 {
53 sqr *s = S(x>>PRECBITS, y>>PRECBITS);
54 s->r = min((l>>PRECBITS)+s->r, 255);
55 s->g = min((g>>PRECBITS)+s->g, 255);
56 s->b = min((b>>PRECBITS)+s->b, 255);
57 if(SOLID(s)) return;
58 x += stepx;
59 y += stepy;
60 l -= stepl;
61 g -= stepg;
62 b -= stepb;
63 stepl -= 25;
64 stepg -= 25;

Callers 1

calclightsourceFunction · 0.85

Calls 2

minFunction · 0.85
loopiFunction · 0.70

Tested by

no test coverage detected