1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "ags/engine/ac/region.h"
24 #include "ags/shared/ac/common_defines.h"
25 #include "ags/shared/ac/game_setup_struct.h"
26 #include "ags/engine/ac/game_state.h"
27 #include "ags/engine/ac/global_region.h"
28 #include "ags/engine/ac/room.h"
29 #include "ags/engine/ac/room_status.h"
30 #include "ags/engine/ac/dynobj/cc_region.h"
31 #include "ags/engine/ac/dynobj/script_drawing_surface.h"
32 #include "ags/shared/game/room_struct.h"
33 #include "ags/engine/script/runtime_script_value.h"
34 #include "ags/shared/debugging/out.h"
35 #include "ags/engine/script/script_api.h"
36 #include "ags/engine/script/script_runtime.h"
37 #include "ags/globals.h"
38 
39 namespace AGS3 {
40 
41 using namespace AGS::Shared;
42 
GetRegionAtRoom(int xx,int yy)43 ScriptRegion *GetRegionAtRoom(int xx, int yy) {
44 	return &_G(scrRegion)[GetRegionIDAtRoom(xx, yy)];
45 }
46 
GetRegionAtScreen(int x,int y)47 ScriptRegion *GetRegionAtScreen(int x, int y) {
48 	VpPoint vpt = _GP(play).ScreenToRoomDivDown(x, y);
49 	if (vpt.second < 0)
50 		return nullptr;
51 	return GetRegionAtRoom(vpt.first.X, vpt.first.Y);
52 }
53 
Region_SetLightLevel(ScriptRegion * ssr,int brightness)54 void Region_SetLightLevel(ScriptRegion *ssr, int brightness) {
55 	SetAreaLightLevel(ssr->id, brightness);
56 }
57 
Region_GetLightLevel(ScriptRegion * ssr)58 int Region_GetLightLevel(ScriptRegion *ssr) {
59 	return _GP(thisroom).GetRegionLightLevel(ssr->id);
60 }
61 
Region_GetTintEnabled(ScriptRegion * srr)62 int Region_GetTintEnabled(ScriptRegion *srr) {
63 	if (_GP(thisroom).Regions[srr->id].Tint & 0xFF000000)
64 		return 1;
65 	return 0;
66 }
67 
Region_GetTintRed(ScriptRegion * srr)68 int Region_GetTintRed(ScriptRegion *srr) {
69 
70 	return _GP(thisroom).Regions[srr->id].Tint & 0x000000ff;
71 }
72 
Region_GetTintGreen(ScriptRegion * srr)73 int Region_GetTintGreen(ScriptRegion *srr) {
74 
75 	return (_GP(thisroom).Regions[srr->id].Tint >> 8) & 0x000000ff;
76 }
77 
Region_GetTintBlue(ScriptRegion * srr)78 int Region_GetTintBlue(ScriptRegion *srr) {
79 
80 	return (_GP(thisroom).Regions[srr->id].Tint >> 16) & 0x000000ff;
81 }
82 
Region_GetTintSaturation(ScriptRegion * srr)83 int Region_GetTintSaturation(ScriptRegion *srr) {
84 
85 	return (_GP(thisroom).Regions[srr->id].Tint >> 24) & 0xFF;
86 }
87 
Region_GetTintLuminance(ScriptRegion * srr)88 int Region_GetTintLuminance(ScriptRegion *srr) {
89 	return _GP(thisroom).GetRegionTintLuminance(srr->id);
90 }
91 
Region_Tint(ScriptRegion * srr,int red,int green,int blue,int amount,int luminance)92 void Region_Tint(ScriptRegion *srr, int red, int green, int blue, int amount, int luminance) {
93 	SetRegionTint(srr->id, red, green, blue, amount, luminance);
94 }
95 
Region_TintNoLum(ScriptRegion * srr,int red,int green,int blue,int amount)96 void Region_TintNoLum(ScriptRegion *srr, int red, int green, int blue, int amount) {
97 	SetRegionTint(srr->id, red, green, blue, amount);
98 }
99 
Region_SetEnabled(ScriptRegion * ssr,int enable)100 void Region_SetEnabled(ScriptRegion *ssr, int enable) {
101 	if (enable)
102 		EnableRegion(ssr->id);
103 	else
104 		DisableRegion(ssr->id);
105 }
106 
Region_GetEnabled(ScriptRegion * ssr)107 int Region_GetEnabled(ScriptRegion *ssr) {
108 	return _G(croom)->region_enabled[ssr->id];
109 }
110 
Region_GetID(ScriptRegion * ssr)111 int Region_GetID(ScriptRegion *ssr) {
112 	return ssr->id;
113 }
114 
Region_RunInteraction(ScriptRegion * ssr,int mood)115 void Region_RunInteraction(ScriptRegion *ssr, int mood) {
116 	RunRegionInteraction(ssr->id, mood);
117 }
118 
119 //=============================================================================
120 
generate_light_table()121 void generate_light_table() {
122 	if (_GP(game).color_depth == 1 && _G(color_map) == nullptr) {
123 		create_light_table(&_GP(maincoltable), _G(palette), 0, 0, 0, nullptr);
124 		_G(color_map) = &_GP(maincoltable);
125 	}
126 }
127 
128 //=============================================================================
129 //
130 // Script API Functions
131 //
132 //=============================================================================
133 
134 // ScriptRegion *(int xx, int yy)
Sc_GetRegionAtRoom(const RuntimeScriptValue * params,int32_t param_count)135 RuntimeScriptValue Sc_GetRegionAtRoom(const RuntimeScriptValue *params, int32_t param_count) {
136 	API_SCALL_OBJ_PINT2(ScriptRegion, _GP(ccDynamicRegion), GetRegionAtRoom);
137 }
138 
Sc_GetRegionAtScreen(const RuntimeScriptValue * params,int32_t param_count)139 RuntimeScriptValue Sc_GetRegionAtScreen(const RuntimeScriptValue *params, int32_t param_count) {
140 	API_SCALL_OBJ_PINT2(ScriptRegion, _GP(ccDynamicRegion), GetRegionAtScreen);
141 }
142 
Sc_Region_GetDrawingSurface(const RuntimeScriptValue * params,int32_t param_count)143 RuntimeScriptValue Sc_Region_GetDrawingSurface(const RuntimeScriptValue *params, int32_t param_count) {
144 	ScriptDrawingSurface *ret_obj = Room_GetDrawingSurfaceForMask(kRoomAreaRegion);
145 	return RuntimeScriptValue().SetDynamicObject(ret_obj, ret_obj);
146 }
147 
Sc_Region_Tint(void * self,const RuntimeScriptValue * params,int32_t param_count)148 RuntimeScriptValue Sc_Region_Tint(void *self, const RuntimeScriptValue *params, int32_t param_count) {
149 	API_OBJCALL_VOID_PINT5(ScriptRegion, Region_Tint);
150 }
151 
152 // void (ScriptRegion *srr, int red, int green, int blue, int amount)
Sc_Region_TintNoLum(void * self,const RuntimeScriptValue * params,int32_t param_count)153 RuntimeScriptValue Sc_Region_TintNoLum(void *self, const RuntimeScriptValue *params, int32_t param_count) {
154 	API_OBJCALL_VOID_PINT4(ScriptRegion, Region_TintNoLum);
155 }
156 
157 // void (ScriptRegion *ssr, int mood)
Sc_Region_RunInteraction(void * self,const RuntimeScriptValue * params,int32_t param_count)158 RuntimeScriptValue Sc_Region_RunInteraction(void *self, const RuntimeScriptValue *params, int32_t param_count) {
159 	API_OBJCALL_VOID_PINT(ScriptRegion, Region_RunInteraction);
160 }
161 
162 // int (ScriptRegion *ssr)
Sc_Region_GetEnabled(void * self,const RuntimeScriptValue * params,int32_t param_count)163 RuntimeScriptValue Sc_Region_GetEnabled(void *self, const RuntimeScriptValue *params, int32_t param_count) {
164 	API_OBJCALL_INT(ScriptRegion, Region_GetEnabled);
165 }
166 
167 // void (ScriptRegion *ssr, int enable)
Sc_Region_SetEnabled(void * self,const RuntimeScriptValue * params,int32_t param_count)168 RuntimeScriptValue Sc_Region_SetEnabled(void *self, const RuntimeScriptValue *params, int32_t param_count) {
169 	API_OBJCALL_VOID_PINT(ScriptRegion, Region_SetEnabled);
170 }
171 
172 // int (ScriptRegion *ssr)
Sc_Region_GetID(void * self,const RuntimeScriptValue * params,int32_t param_count)173 RuntimeScriptValue Sc_Region_GetID(void *self, const RuntimeScriptValue *params, int32_t param_count) {
174 	API_OBJCALL_INT(ScriptRegion, Region_GetID);
175 }
176 
177 // int (ScriptRegion *ssr)
Sc_Region_GetLightLevel(void * self,const RuntimeScriptValue * params,int32_t param_count)178 RuntimeScriptValue Sc_Region_GetLightLevel(void *self, const RuntimeScriptValue *params, int32_t param_count) {
179 	API_OBJCALL_INT(ScriptRegion, Region_GetLightLevel);
180 }
181 
182 // void (ScriptRegion *ssr, int brightness)
Sc_Region_SetLightLevel(void * self,const RuntimeScriptValue * params,int32_t param_count)183 RuntimeScriptValue Sc_Region_SetLightLevel(void *self, const RuntimeScriptValue *params, int32_t param_count) {
184 	API_OBJCALL_VOID_PINT(ScriptRegion, Region_SetLightLevel);
185 }
186 
187 // int (ScriptRegion *srr)
Sc_Region_GetTintEnabled(void * self,const RuntimeScriptValue * params,int32_t param_count)188 RuntimeScriptValue Sc_Region_GetTintEnabled(void *self, const RuntimeScriptValue *params, int32_t param_count) {
189 	API_OBJCALL_INT(ScriptRegion, Region_GetTintEnabled);
190 }
191 
192 // int (ScriptRegion *srr)
Sc_Region_GetTintBlue(void * self,const RuntimeScriptValue * params,int32_t param_count)193 RuntimeScriptValue Sc_Region_GetTintBlue(void *self, const RuntimeScriptValue *params, int32_t param_count) {
194 	API_OBJCALL_INT(ScriptRegion, Region_GetTintBlue);
195 }
196 
197 // int (ScriptRegion *srr)
Sc_Region_GetTintGreen(void * self,const RuntimeScriptValue * params,int32_t param_count)198 RuntimeScriptValue Sc_Region_GetTintGreen(void *self, const RuntimeScriptValue *params, int32_t param_count) {
199 	API_OBJCALL_INT(ScriptRegion, Region_GetTintGreen);
200 }
201 
202 // int (ScriptRegion *srr)
Sc_Region_GetTintRed(void * self,const RuntimeScriptValue * params,int32_t param_count)203 RuntimeScriptValue Sc_Region_GetTintRed(void *self, const RuntimeScriptValue *params, int32_t param_count) {
204 	API_OBJCALL_INT(ScriptRegion, Region_GetTintRed);
205 }
206 
207 // int (ScriptRegion *srr)
Sc_Region_GetTintSaturation(void * self,const RuntimeScriptValue * params,int32_t param_count)208 RuntimeScriptValue Sc_Region_GetTintSaturation(void *self, const RuntimeScriptValue *params, int32_t param_count) {
209 	API_OBJCALL_INT(ScriptRegion, Region_GetTintSaturation);
210 }
211 
Sc_Region_GetTintLuminance(void * self,const RuntimeScriptValue * params,int32_t param_count)212 RuntimeScriptValue Sc_Region_GetTintLuminance(void *self, const RuntimeScriptValue *params, int32_t param_count) {
213 	API_OBJCALL_INT(ScriptRegion, Region_GetTintLuminance);
214 }
215 
216 
RegisterRegionAPI()217 void RegisterRegionAPI() {
218 	ccAddExternalStaticFunction("Region::GetAtRoomXY^2", Sc_GetRegionAtRoom);
219 	ccAddExternalStaticFunction("Region::GetAtScreenXY^2", Sc_GetRegionAtScreen);
220 	ccAddExternalStaticFunction("Region::GetDrawingSurface", Sc_Region_GetDrawingSurface);
221 	ccAddExternalObjectFunction("Region::Tint^4", Sc_Region_TintNoLum);
222 	ccAddExternalObjectFunction("Region::Tint^5", Sc_Region_Tint);
223 	ccAddExternalObjectFunction("Region::RunInteraction^1", Sc_Region_RunInteraction);
224 	ccAddExternalObjectFunction("Region::get_Enabled", Sc_Region_GetEnabled);
225 	ccAddExternalObjectFunction("Region::set_Enabled", Sc_Region_SetEnabled);
226 	ccAddExternalObjectFunction("Region::get_ID", Sc_Region_GetID);
227 	ccAddExternalObjectFunction("Region::get_LightLevel", Sc_Region_GetLightLevel);
228 	ccAddExternalObjectFunction("Region::set_LightLevel", Sc_Region_SetLightLevel);
229 	ccAddExternalObjectFunction("Region::get_TintEnabled", Sc_Region_GetTintEnabled);
230 	ccAddExternalObjectFunction("Region::get_TintBlue", Sc_Region_GetTintBlue);
231 	ccAddExternalObjectFunction("Region::get_TintGreen", Sc_Region_GetTintGreen);
232 	ccAddExternalObjectFunction("Region::get_TintRed", Sc_Region_GetTintRed);
233 	ccAddExternalObjectFunction("Region::get_TintSaturation", Sc_Region_GetTintSaturation);
234 	ccAddExternalObjectFunction("Region::get_TintLuminance", Sc_Region_GetTintLuminance);
235 }
236 
237 } // namespace AGS3
238