blob: 2b34de8412950cad092e58463e822b9f21020a32 [file] [log] [blame]
MG Mud User88f12472016-06-24 23:31:02 +02001// MorgenGrauen MUDlib
2//
3// container/light.c -- Lichtsystem fuer Container
4//
5// $Id: description.c 6986 2008-08-22 21:32:15Z Zesstra $
6
7inherit "/std/thing/light";
8
9#pragma strict_types
10#pragma save_types,rtt_checks
11#pragma range_check
12#pragma no_clone
MG Mud User88f12472016-06-24 23:31:02 +020013
14#define NEED_PROTOTYPES
15
16#include <container.h>
17#include <properties.h>
18
19// BTW: _query_last_content_change() sollte bloss nie auch -1 fuer
20// uninitialisiert zurueckliefern...
21private nosave int last_light_calc = -1;
22
23protected void create()
24{
25 ::create();
26 // wieviel Licht schluckt der Container _muss_ > 0 sein!
27 SetProp(P_LIGHT_TRANSPARENCY, 2);
28}
29
30protected void create_super() {set_next_reset(-1);}
31
32
33static mixed _query_light_transparency()
34{
35 if (QueryProp(P_TRANSPARENT))
36 return Query(P_LIGHT_TRANSPARENCY,F_VALUE);
37 return 999;
38}
39
Zesstra303c6f62018-11-28 21:56:38 +010040protected int add_light_sources(int * sources) {
MG Mud User88f12472016-06-24 23:31:02 +020041 float light = 0.0;
42 //printf("als(%O): %O\n",this_object(),sources);
43 // Alle Lichtlevel werden als Exponent von e aufgefasst und die Summe dieser
44 // Potenzen gebildet.
45 foreach(int l : sources) {
46 if (l > 0)
47 light += exp(l);
48 else if (l < 0)
49 light -= exp(abs(l));
50 // l==0 muss ignoriert werden
51 }
52 // anschliessend wird aus dieser Summe der natuerliche Logarithmus
53 // berechnet.
54 // auf diese Weise haben hoehere Lichtlevel ueberproportional viel Einfluss
55 // auf die Helligkeit im Raum, kleine Lichtlevel fallen aber nicht komplett
56 // raus.
57 if (light > 0) {
58 light = log(light);
59 }
60 else if (light < 0) {
61 light = -log(abs(light));
62 }
63
64 //printf("als(): %O\n",light);
65 // runden und als int zurueckgeben.
66 if (light >= 0)
67 return to_int(light+0.5);
68 //else
69 return to_int(light-0.5);
70}
71
72static int _query_total_light()
73{
74 int totallight;
75
76 if ( _query_last_content_change() == last_light_calc )
77 return Query(P_TOTAL_LIGHT);
78
79 // eigenes P_LIGHT und P_TOTAL_LIGHT der enthaltenen Objekte verrechnen
80 int intlight = add_light_sources(
bugfixd94d0932020-04-08 11:27:13 +020081 ({int*})all_inventory()->QueryProp(P_TOTAL_LIGHT) + ({QueryProp(P_LIGHT)}));
MG Mud User88f12472016-06-24 23:31:02 +020082 // P_INT_LIGHT (gerundet) abspeichern
83 Set(P_INT_LIGHT, intlight, F_VALUE);
84
85 // Licht nach aussen muss Containertransparenz beruecksichtigen.
86 if (intlight > 0) {
87 totallight = intlight - QueryProp(P_LIGHT_TRANSPARENCY);
88 if (totallight < 0)
89 totallight = 0;
90 }
91 else {
92 totallight = intlight + QueryProp(P_LIGHT_TRANSPARENCY);
93 if (totallight > 0)
94 totallight = 0;
95 }
96 //printf("_query_total_light(%O): %O\n",this_object(),totallight);
97
98 last_light_calc = _query_last_content_change();
99
100 // Runden und Konversion nach int nicht vergessen...
101 if (totallight > 0)
102 return SetProp(P_TOTAL_LIGHT, to_int(totallight + 0.5));
103 //else
104 return SetProp(P_TOTAL_LIGHT, to_int(totallight - 0.5));
105}
106
107static int _query_int_light()
108{
109 int intlight, envlight;
110
111 if ( _query_last_content_change() != last_light_calc )
112 _query_total_light();
113
114 // P_INT_LIGHT des environments kann sich natuerlich aendern _ohne_ das
115 // etwas an einem container geaendert wird. Daher Auswertung jedes mal
116 // neu aktualisieren.
117 if (!environment()
Vanion50652322020-03-10 21:13:25 +0100118 || !(envlight=({int})environment()->QueryProp(P_INT_LIGHT)))
MG Mud User88f12472016-06-24 23:31:02 +0200119 return Query(P_INT_LIGHT, F_VALUE);
120 else {
121 intlight = Query(P_INT_LIGHT, F_VALUE);
122 int transparency = QueryProp(P_LIGHT_TRANSPARENCY);
123
124 // bei einem transparentem Container kann natuerlich das Licht des
125 // environments den Container auch ausleuchten....
126 // (Anmerkung: in dem Licht von draussen ist unser Lichtanteil
127 // natuerlich auch drin, gedaempft um die Transparenz (raus und rein).
128 // Ich vernachlaessige das.)
129 if (abs(envlight) > transparency) {
130 if (envlight > 0)
131 envlight -= transparency;
132 else
133 envlight += transparency;
134 // jetzt Licht von aussen und innen verrechnen
135 intlight = add_light_sources(({intlight, envlight}));
136 }
137 // else: nur P_INT_LIGHT, Licht des Environments keine Bedeutung
138 }
139 return intlight;
140}
141
142static int _set_light(int light)
143{
144 last_light_calc=-1;
145
146 return ::_set_light(light);
147}
148