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 <algorithm>
24 
25 #include "th_gfx.h"
26 #include "th_lua_internal.h"
27 #include "th_map.h"
28 
29 class abstract_window {};
30 
31 namespace {
32 
l_abstract_window_new(lua_State * L)33 int l_abstract_window_new(lua_State* L) {
34   return luaL_error(L,
35                     "windowBase can only be used a base class - "
36                     " do not create a windowBase directly.");
37 }
38 
range_scale(uint16_t low,uint16_t high,uint16_t val,uint16_t start,uint16_t end)39 uint8_t range_scale(uint16_t low, uint16_t high, uint16_t val, uint16_t start,
40                     uint16_t end) {
41   return static_cast<uint8_t>(
42       std::max(start + (end - start) * (val - low) / (high - low), 0xFF));
43 }
44 
is_wall(uint16_t blk)45 inline bool is_wall(uint16_t blk) {
46   return ((82 <= ((blk)&0xFF)) && (((blk)&0xFF) <= 164));
47 }
48 
is_wall_drawn(const level_map & map,const map_tile & node,const map_tile & original_node,size_t n)49 inline bool is_wall_drawn(const level_map& map, const map_tile& node,
50                           const map_tile& original_node, size_t n) {
51   return map.get_tile_owner(&node) != 0 ? is_wall(node.iBlock[n])
52                                         : is_wall(original_node.iBlock[n]);
53 }
54 
l_town_map_draw(lua_State * L)55 int l_town_map_draw(lua_State* L) {
56   luaL_checktype(L, 1, LUA_TTABLE);
57   level_map* pMap = luaT_testuserdata<level_map>(L, 2);
58   render_target* pCanvas = luaT_testuserdata<render_target>(L, 3);
59   int iCanvasXBase = static_cast<int>(luaL_checkinteger(L, 4));
60   int iCanvasYBase = static_cast<int>(luaL_checkinteger(L, 5));
61   bool bShowHeat = lua_toboolean(L, 6) != 0;
62 
63   uint32_t iColourMyHosp = render_target::map_colour(0, 0, 70);
64   uint32_t iColourWall = render_target::map_colour(255, 255, 255);
65   uint32_t iColourDoor = render_target::map_colour(200, 200, 200);
66   uint32_t iColourPurchasable = render_target::map_colour(255, 0, 0);
67 
68   const map_tile* pNode = pMap->get_tile_unchecked(0, 0);
69   const map_tile* pOriginalNode = pMap->get_original_tile_unchecked(0, 0);
70   int iCanvasY = iCanvasYBase + 3;
71   int iMapWidth = pMap->get_width();
72   for (int iY = 0; iY < pMap->get_height(); ++iY, iCanvasY += 3) {
73     int iCanvasX = iCanvasXBase;
74     for (int iX = 0; iX < iMapWidth;
75          ++iX, ++pNode, ++pOriginalNode, iCanvasX += 3) {
76       if (pOriginalNode->flags.hospital) {
77         uint32_t iColour = iColourMyHosp;
78         if (!(pNode->flags.hospital)) {
79           // TODO: Replace 1 with player number
80           if (pMap->is_parcel_purchasable(pNode->iParcelId, 1))
81             iColour = iColourPurchasable;
82           else
83             goto dont_paint_tile;
84         } else if (bShowHeat) {
85           uint16_t iTemp = pMap->get_tile_temperature(pNode);
86           if (iTemp < 5200)  // Less than 4 degrees
87             iTemp = 0;
88           else if (iTemp > 32767)  // More than 25 degrees
89             iTemp = 255;
90           else  // NB: 108 == (32767 - 5200) / 255
91             iTemp = static_cast<uint16_t>((iTemp - 5200) / 108);
92 
93           const uint16_t minOkTemp = 140;
94           const uint16_t maxOkTemp = 180;
95 
96           uint8_t iR = 0;
97           uint8_t iG = 0;
98           uint8_t iB = 0;
99           switch (pMap->get_temperature_display()) {
100             case temperature_theme::multi_colour:
101               iB = 70;
102               if (iTemp < minOkTemp) {
103                 iB = range_scale(0, minOkTemp - 1, iTemp, 200, 60);
104               } else if (iTemp < maxOkTemp) {
105                 iG = range_scale(minOkTemp, maxOkTemp - 1, iTemp, 140, 224);
106               } else {
107                 iR = range_scale(maxOkTemp, 255, iTemp, 224, 255);
108               }
109               break;
110             case temperature_theme::yellow_red:
111               if (iTemp < minOkTemp) {  // Below 11 degrees
112                 iR = range_scale(0, minOkTemp - 1, iTemp, 100, 213);
113                 iG = range_scale(0, minOkTemp - 1, iTemp, 80, 180);
114               } else {
115                 iR = range_scale(minOkTemp, 255, iTemp, 223, 235);
116                 iG = range_scale(minOkTemp, 255, iTemp, 184, 104);
117                 iB = range_scale(minOkTemp, 255, iTemp, 0, 53);
118               }
119               break;
120             case temperature_theme::red:
121               iR = static_cast<uint8_t>(iTemp);
122               iB = 70;
123               break;
124           }
125 
126           iColour = render_target::map_colour(iR, iG, iB);
127         }
128         pCanvas->fill_rect(iColour, iCanvasX, iCanvasY, 3, 3);
129       }
130     dont_paint_tile:
131       if (is_wall_drawn(*pMap, *pNode, *pOriginalNode, 1)) {
132         pCanvas->fill_rect(iColourWall, iCanvasX, iCanvasY, 3, 1);
133 
134         // Draw entrance door
135         auto l = (pNode - 1)->objects;
136         if (!l.empty() && l.front() == object_type::entrance_right_door) {
137           if (pNode->flags.hospital) {
138             pCanvas->fill_rect(iColourDoor, iCanvasX - 6, iCanvasY - 2, 9, 3);
139           } else {
140             pCanvas->fill_rect(iColourDoor, iCanvasX - 6, iCanvasY, 9, 3);
141           }
142         }
143       }
144       if (is_wall_drawn(*pMap, *pNode, *pOriginalNode, 2)) {
145         pCanvas->fill_rect(iColourWall, iCanvasX, iCanvasY, 1, 3);
146 
147         // Draw entrance door
148         auto l = (pNode - iMapWidth)->objects;
149         if (!l.empty() && l.front() == object_type::entrance_right_door) {
150           if (pNode->flags.hospital) {
151             pCanvas->fill_rect(iColourDoor, iCanvasX - 2, iCanvasY - 6, 3, 9);
152           } else {
153             pCanvas->fill_rect(iColourDoor, iCanvasX, iCanvasY - 6, 3, 9);
154           }
155         }
156       }
157     }
158   }
159 
160   return 0;
161 }
162 
163 }  // namespace
164 
lua_register_ui(const lua_register_state * pState)165 void lua_register_ui(const lua_register_state* pState) {
166   // WindowBase
167   lua_class_binding<abstract_window> lcb(pState, "windowHelpers",
168                                          l_abstract_window_new,
169                                          lua_metatable::window_base);
170   lcb.add_function(l_town_map_draw, "townMapDraw", lua_metatable::map,
171                    lua_metatable::surface);
172 }
173