Debian Codegraph @

CorsixTH/Src/th_lua_ui.cpp

raw
1 /*
2 Copyright (c) 2010 Peter "Corsix" Cawley
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23 #include "th_lua_ui.h"
24
25 #include "config.h"
26
27 #ifdef WITH_TRACY
28 #include <tracy/Tracy.hpp>
29 #endif
30
31 #include <list>
32
33 #include "lua.hpp"
34 #include "th_gfx_sdl.h"
35 #include "th_lua.h"
36 #include "th_lua_internal.h"
37 #include "th_map.h"
38
39 class abstract_window {};
40
41 namespace {
42
43 int l_abstract_window_new(lua_State* L) {
44 return luaL_error(L,
45 "windowBase can only be used a base class - "
46 " do not create a windowBase directly.");
47 }
48
49 inline bool is_wall(uint16_t blk) {
50 return 82 <= (blk & 0xFF) && (blk & 0xFF) <= 164;
51 }
52
53 inline bool is_wall_drawn(const level_map& map, const map_tile& node,
54 const map_tile& original_node, tile_layer n) {
55 return map.get_tile_owner(&node) != 0 ? is_wall(node.tile_layers[n])
56 : is_wall(original_node.tile_layers[n]);
57 }
58
59 int l_town_map_draw(lua_State* L) {
60 ZoneScoped;
61
62 luaL_checktype(L, 1, LUA_TTABLE);
63 level_map* pMap = luaT_testuserdata<level_map>(L, 2);
64 render_target* pCanvas = luaT_testuserdata<render_target>(L, 3);
65 int iCanvasXBase = static_cast<int>(luaL_checkinteger(L, 4));
66 int iCanvasYBase = static_cast<int>(luaL_checkinteger(L, 5));
67 bool bShowHeat = lua_toboolean(L, 6) != 0;
68 int scale = static_cast<int>(luaL_optinteger(L, 7, 1));
69
70 uint32_t iColourMyHosp = render_target::map_colour(0, 0, 70);
71 uint32_t iColourWall = render_target::map_colour(255, 255, 255);
72 uint32_t iColourDoor = render_target::map_colour(200, 200, 200);
73 uint32_t iColourPurchasable = render_target::map_colour(255, 0, 0);
74
75 const map_tile* pNode = pMap->get_tile_unchecked(0, 0);
76 const map_tile* pOriginalNode = pMap->get_original_tile_unchecked(0, 0);
77 int iCanvasY = iCanvasYBase + 3 * scale;
78 int iMapWidth = pMap->get_width();
79 for (int iY = 0; iY < pMap->get_height(); ++iY, iCanvasY += 3 * scale) {
80 int iCanvasX = iCanvasXBase;
81 for (int iX = 0; iX < iMapWidth;
82 ++iX, ++pNode, ++pOriginalNode, iCanvasX += 3 * scale) {
83 if (pOriginalNode->flags.hospital) {
84 uint32_t iColour = iColourMyHosp;
85 if (!(pNode->flags.hospital)) {
86 // TODO: Replace 1 with player number
87 if (pMap->is_parcel_purchasable(pNode->iParcelId, 1))
88 iColour = iColourPurchasable;
89 else
90 goto dont_paint_tile;
91 } else if (bShowHeat) {
92 uint16_t iTemp = pMap->get_tile_temperature(pNode);
93 if (iTemp < 5200) // Less than 4 degrees
94 iTemp = 0;
95 else if (iTemp > 32767) // More than 25 degrees
96 iTemp = 255;
97 else // NB: 108 == (32767 - 5200) / 255
98 iTemp = static_cast<uint16_t>((iTemp - 5200) / 108);
99
100 constexpr uint16_t minOkTemp = 140;
101 constexpr uint16_t maxOkTemp = 180;
102
103 uint8_t iR = 0;
104 uint8_t iG = 0;
105 uint8_t iB = 0;
106 switch (pMap->get_temperature_display()) {
107 case temperature_theme::multi_colour:
108 iB = 70;
109 if (iTemp < minOkTemp) {
110 iB = map_color_channel(0, minOkTemp - 1, iTemp, 200, 60);
111 } else if (iTemp < maxOkTemp) {
112 iG = map_color_channel(minOkTemp, maxOkTemp - 1, iTemp, 140,
113 224);
114 } else {
115 iR = map_color_channel(maxOkTemp, 255, iTemp, 224, 255);
116 }
117 break;
118 case temperature_theme::yellow_red:
119 if (iTemp < minOkTemp) { // Below 11 degrees
120 iR = map_color_channel(0, minOkTemp - 1, iTemp, 100, 213);
121 iG = map_color_channel(0, minOkTemp - 1, iTemp, 80, 180);
122 } else {
123 iR = map_color_channel(minOkTemp, 255, iTemp, 223, 235);
124 iG = map_color_channel(minOkTemp, 255, iTemp, 184, 104);
125 iB = map_color_channel(minOkTemp, 255, iTemp, 0, 53);
126 }
127 break;
128 case temperature_theme::red:
129 iR = static_cast<uint8_t>(iTemp);
130 iB = 70;
131 break;
132 }
133
134 iColour = render_target::map_colour(iR, iG, iB);
135 }
136 pCanvas->fill_rect(iColour, iCanvasX, iCanvasY, 3 * scale, 3 * scale);
137 }
138 dont_paint_tile:
139 if (is_wall_drawn(*pMap, *pNode, *pOriginalNode,
140 tile_layer::north_wall)) {
141 pCanvas->fill_rect(iColourWall, iCanvasX, iCanvasY, 3 * scale, scale);
142
143 // Draw entrance door
144 auto l = (pNode - 1)->objects;
145 if (!l.empty() && l.front() == object_type::entrance_right_door) {
146 if (pNode->flags.hospital) {
147 pCanvas->fill_rect(iColourDoor, iCanvasX - 6 * scale,
148 iCanvasY - 2 * scale, 9 * scale, 3 * scale);
149 } else {
150 pCanvas->fill_rect(iColourDoor, iCanvasX - 6 * scale, iCanvasY,
151 9 * scale, 3 * scale);
152 }
153 }
154 }
155 if (is_wall_drawn(*pMap, *pNode, *pOriginalNode, tile_layer::west_wall)) {
156 pCanvas->fill_rect(iColourWall, iCanvasX, iCanvasY, scale, 3 * scale);
157
158 // Draw entrance door
159 auto l = (pNode - iMapWidth)->objects;
160 if (!l.empty() && l.front() == object_type::entrance_right_door) {
161 if (pNode->flags.hospital) {
162 pCanvas->fill_rect(iColourDoor, iCanvasX - 2 * scale,
163 iCanvasY - 6 * scale, 3 * scale, 9 * scale);
164 } else {
165 pCanvas->fill_rect(iColourDoor, iCanvasX, iCanvasY - 6 * scale,
166 3 * scale, 9 * scale);
167 }
168 }
169 }
170 }
171 }
172
173 return 0;
174 }
175
176 } // namespace
177
178 void lua_register_ui(const lua_register_state* pState) {
179 // WindowBase
180 lua_class_binding<abstract_window> lcb(pState, "windowHelpers",
181 l_abstract_window_new,
182 lua_metatable::window_base);
183 lcb.add_function(l_town_map_draw, "townMapDraw", lua_metatable::map,
184 lua_metatable::surface);
185 }