OWORLD_M.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. //Filename : OWORLD_M.CPP
  21. //Description : Object MapMatrix
  22. #include <OVGA.h>
  23. #include <OMOUSE.h>
  24. #include <OIMGRES.h>
  25. #include <OBUTTON.h>
  26. #include <OPLANT.h>
  27. #include <OTOWN.h>
  28. #include <ONATION.h>
  29. #include <OFIRM.h>
  30. #include <OSYS.h>
  31. #include <OPOWER.h>
  32. #include <OGAME.h>
  33. #include <OWORLD.h>
  34. #include <OTERRAIN.h>
  35. //-------- Begin of function MapMatrix::MapMatrix ----------//
  36. MapMatrix::MapMatrix()
  37. {
  38. init( MAP_X1, MAP_Y1, MAP_X2, MAP_Y2,
  39. MAP_WIDTH, MAP_HEIGHT,
  40. MAP_LOC_WIDTH, MAP_LOC_HEIGHT, 1 ); // 1-create a background buffer
  41. }
  42. //---------- End of function MapMatrix::MapMatrix ----------//
  43. //-------- Begin of function MapMatrix::~MapMatrix ----------//
  44. MapMatrix::~MapMatrix()
  45. {
  46. }
  47. //---------- End of function MapMatrix::~MapMatrix ----------//
  48. //---------- Begin of function MapMatrix::init_para ------------//
  49. void MapMatrix::init_para()
  50. {
  51. last_map_mode = MAP_NORMAL;
  52. map_mode = MAP_MODE_TERRAIN;
  53. power_mode = 0;
  54. }
  55. //---------- End of function MapMatrix::init_para ----------//
  56. //--------- Begin of function MapMatrix::paint -----------//
  57. void MapMatrix::paint()
  58. {
  59. disp_mode_button();
  60. }
  61. //----------- End of function MapMatrix::paint ------------//
  62. //------- Begin of function MapMatrix::disp_mode_button ---------//
  63. //
  64. // Display map mode buttons.
  65. //
  66. // [int] putFront - 1-display the buttons on the front buffer
  67. // 0-display the buttons on the back buffer
  68. // (default: 0)
  69. //
  70. void MapMatrix::disp_mode_button(int putFront)
  71. {
  72. char* iconName;
  73. switch(map_mode)
  74. {
  75. case MAP_MODE_TERRAIN:
  76. iconName = "MAP-1";
  77. break;
  78. case MAP_MODE_POWER:
  79. if( power_mode )
  80. iconName = "MAP-2B";
  81. else
  82. iconName = "MAP-2A";
  83. break;
  84. case MAP_MODE_SPOT:
  85. iconName = "MAP-3";
  86. break;
  87. default:
  88. err_here();
  89. }
  90. if( putFront )
  91. image_button.put_front( 579, 2, iconName, 1 );
  92. else
  93. image_button.put_back( 579, 2, iconName, 1 );
  94. }
  95. //----------- End of function MapMatrix::disp_mode_button ------------//
  96. //--------- Begin of function MapMatrix::detect ------------//
  97. int MapMatrix::detect()
  98. {
  99. int x=586;
  100. #define MAP_MODE_BUTTON_WIDTH 40
  101. for( int i=0 ; i<MAP_MODE_COUNT ; i++, x+=MAP_MODE_BUTTON_WIDTH )
  102. {
  103. if( mouse.single_click( x, 7, x+MAP_MODE_BUTTON_WIDTH-1, 46 ) )
  104. {
  105. toggle_map_mode(i);
  106. return 1;
  107. }
  108. }
  109. //----- detect clicking on the map -------//
  110. return detect_area();
  111. }
  112. //---------- End of function MapMatrix::detect ------------//
  113. //---------- Begin of function MapMatrix::draw ------------//
  114. //
  115. // Draw world map
  116. //
  117. void MapMatrix::draw()
  118. {
  119. draw_map();
  120. //------- save it to the buffer for later reuse ------//
  121. if( save_image_buf )
  122. {
  123. vga_back.read_bitmap( image_x1, image_y1, image_x2, image_y2, save_image_buf );
  124. just_drawn_flag = 1;
  125. }
  126. }
  127. //------------ End of function MapMatrix::draw ------------//
  128. //---------- Begin of function MapMatrix::draw_map ------------//
  129. // see also World::explore
  130. //
  131. void MapMatrix::draw_map()
  132. {
  133. char* writePtr = vga_back.buf_ptr() + vga_back.buf_pitch() * image_y1 + image_x1;
  134. int lineRemain = vga_back.buf_pitch() - image_width;
  135. int x, y;
  136. Location* locPtr = world.loc_matrix;
  137. char* nationColorArray = nation_array.nation_power_color_array;
  138. //----------- draw map now ------------//
  139. sys.yield();
  140. int shadowMapDist = max_x_loc + 1;
  141. int tileYOffset;
  142. char tilePixel;
  143. Location* northWestPtr;
  144. switch(map_mode)
  145. {
  146. case MAP_MODE_TERRAIN:
  147. for( y=image_y1 ; y<=image_y2 ; y++, writePtr+=lineRemain )
  148. {
  149. tileYOffset = (y & TERRAIN_TILE_Y_MASK) * TERRAIN_TILE_WIDTH;
  150. for( x=image_x1 ; x<=image_x2 ; x++, writePtr++, locPtr++ )
  151. {
  152. if( locPtr->explored() )
  153. {
  154. if( locPtr->fire_str() > 0)
  155. *writePtr = (char) FIRE_COLOR;
  156. else if( locPtr->is_plant() )
  157. *writePtr = plant_res.plant_map_color;
  158. else
  159. {
  160. tilePixel = terrain_res.get_map_tile(locPtr->terrain_id)[tileYOffset + (x & TERRAIN_TILE_X_MASK)];
  161. if( y == image_y1 || x == image_x1)
  162. {
  163. *writePtr = tilePixel;
  164. }
  165. else
  166. {
  167. northWestPtr = locPtr - shadowMapDist;
  168. if( terrain_res[locPtr->terrain_id]->average_type >=
  169. terrain_res[northWestPtr->terrain_id]->average_type)
  170. {
  171. *writePtr = tilePixel;
  172. }
  173. else
  174. {
  175. *writePtr = (char) VGA_GRAY;
  176. }
  177. }
  178. }
  179. }
  180. else
  181. {
  182. *writePtr = UNEXPLORED_COLOR;
  183. }
  184. }
  185. }
  186. break;
  187. case MAP_MODE_SPOT:
  188. for( y=image_y1 ; y<=image_y2 ; y++, writePtr+=lineRemain )
  189. {
  190. for( x=image_x1 ; x<=image_x2 ; x++, writePtr++, locPtr++ )
  191. {
  192. if( locPtr->explored() )
  193. {
  194. if( locPtr->sailable() )
  195. *writePtr = (char) 0x32;
  196. else if( locPtr->has_hill() )
  197. *writePtr = (char) V_BROWN;
  198. // else if( locPtr->is_plant() )
  199. // *writePtr = (char) V_DARK_GREEN;
  200. else
  201. *writePtr = (char) VGA_GRAY+10;
  202. }
  203. else
  204. {
  205. *writePtr = UNEXPLORED_COLOR;
  206. }
  207. }
  208. }
  209. break;
  210. case MAP_MODE_POWER:
  211. for( y=image_y1 ; y<=image_y2 ; y++, writePtr+=lineRemain )
  212. {
  213. for( x=image_x1 ; x<=image_x2 ; x++, writePtr++, locPtr++ )
  214. {
  215. if( locPtr->explored() )
  216. {
  217. if( locPtr->sailable() )
  218. *writePtr = (char) 0x32;
  219. else if( locPtr->has_hill() )
  220. *writePtr = (char) V_BROWN;
  221. else if( locPtr->is_plant() )
  222. *writePtr = (char) V_DARK_GREEN;
  223. else
  224. *writePtr = nationColorArray[locPtr->power_nation_recno];
  225. }
  226. else
  227. {
  228. *writePtr = UNEXPLORED_COLOR;
  229. }
  230. }
  231. }
  232. break;
  233. }
  234. sys.yield();
  235. }
  236. //------------ End of function MapMatrix::draw_map ------------//
  237. //----------- Begin of function MapMatrix::disp ------------//
  238. //
  239. // Display the drawn world map on screen, update the location
  240. // of the map-to-zoom area box.
  241. //
  242. void MapMatrix::disp()
  243. {
  244. if( !just_drawn_flag ) // if the map has just been drawn in draw()
  245. {
  246. if( save_image_buf && map_mode==last_map_mode )
  247. vga_back.put_bitmap( image_x1, image_y1, save_image_buf );
  248. else
  249. {
  250. draw();
  251. last_map_mode = map_mode;
  252. }
  253. }
  254. just_drawn_flag=0;
  255. }
  256. //----------- End of function MapMatrix::disp ------------//
  257. //----------- Begin of function MapMatrix::draw_square ------------//
  258. //
  259. // Calling sequences:
  260. //
  261. // 1. MapMatrix::disp()
  262. // 2. SpriteArray::draw()
  263. // 3. MapMatrix::draw_square()
  264. //
  265. void MapMatrix::draw_square()
  266. {
  267. //-------- draw the map-to-zoom highlight box --------//
  268. static int squareFrameCount=0, squareFrameStep=1;
  269. int x1=image_x1+(cur_x_loc-top_x_loc)*loc_width;
  270. int y1=image_y1+(cur_y_loc-top_y_loc)*loc_height;
  271. int x2=x1+cur_cargo_width *loc_width-1;
  272. int y2=y1+cur_cargo_height*loc_height-1;
  273. vga_back.rect( x1, y1, x2, y2, 1, VGA_YELLOW + squareFrameCount );
  274. squareFrameCount += squareFrameStep;
  275. if( squareFrameCount==0 ) // color with smaller number is brighter
  276. squareFrameStep = 1;
  277. if( squareFrameCount==6 ) // bi-directional color shift
  278. squareFrameStep = -1;
  279. }
  280. //----------- End of function MapMatrix::draw_square ------------//
  281. //-------- Begin of function MapMatrix::toggle_map_mode ------------//
  282. void MapMatrix::toggle_map_mode(int modeId)
  283. {
  284. if( map_mode == modeId )
  285. {
  286. if( map_mode == MAP_MODE_POWER ) // clicking on a pressed button unclick the button
  287. power_mode = !power_mode;
  288. }
  289. else
  290. {
  291. map_mode = modeId;
  292. }
  293. disp_mode_button(1); // 1-display the buttons on the front buffer.
  294. refresh();
  295. }
  296. //---------- End of function MapMatrix::toggle_map_mode ------------//
  297. //----------- Begin of function MapMatrix::detect_area -----------//
  298. //
  299. // Detect for click on a new zoom area, update the zoom window as well
  300. //
  301. // return : 0 - no action
  302. // 1 - pressed on new cargo
  303. // 2 - pressed on new cargo and also scrolled window
  304. //
  305. int MapMatrix::detect_area()
  306. {
  307. if( !mouse.press_area( image_x1,image_y1,image_x2,image_y2, 2 ) &&
  308. !mouse.any_click( image_x1,image_y1,image_x2,image_y2, 2 ) )
  309. {
  310. return 0;
  311. }
  312. int rc = 0;
  313. int lastXLoc = cur_x_loc;
  314. int lastYLoc = cur_y_loc;
  315. //--- if press left button, select zoom area ----//
  316. if( mouse.single_click( image_x1,image_y1,image_x2,image_y2 ) ||
  317. mouse.press_area( image_x1,image_y1,image_x2,image_y2, LEFT_BUTTON ) )
  318. {
  319. int xLoc = top_x_loc + (mouse.cur_x-image_x1)/loc_width;
  320. int yLoc = top_y_loc + (mouse.cur_y-image_y1)/loc_height;
  321. //-- if only single click, don't highlight new firm, only new area --//
  322. cur_x_loc = xLoc - world.zoom_matrix->disp_x_loc/2;
  323. cur_y_loc = yLoc - world.zoom_matrix->disp_y_loc/2;
  324. if( !valid_cur_box() ) // valid_cur_box() return 1 if it has refreshed, 0 if didn't
  325. disp();
  326. rc=1;
  327. }
  328. //------- if the view area has been changed -------//
  329. if( cur_x_loc != lastXLoc || cur_y_loc != lastYLoc )
  330. {
  331. world.zoom_matrix->top_x_loc = cur_x_loc;
  332. world.zoom_matrix->top_y_loc = cur_y_loc;
  333. sys.zoom_need_redraw = 1;
  334. }
  335. return rc;
  336. }
  337. //------------ End of function MapMatrix::detect_area ----------//