OTOWNDRW.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 : OTOWNDRW.CPP
  21. //Description : Town drawing routines
  22. #include <OVGA.h>
  23. #include <OSYS.h>
  24. #include <OFONT.h>
  25. #include <OMOUSE.h>
  26. #include <OBUTTON.h>
  27. #include <OINFO.h>
  28. #include <OIMGRES.h>
  29. #include <OPOWER.h>
  30. #include <OWORLD.h>
  31. #include <OGAME.h>
  32. #include <OREMOTE.h>
  33. #include <OANLINE.h>
  34. #include <OPLANT.h>
  35. #include <ONATION.h>
  36. #include <OFIRM.h>
  37. #include <OTOWN.h>
  38. #include <OSE.h>
  39. //------- Begin of function Town::draw -----------//
  40. //
  41. // Draw the town section on the zoom map
  42. //
  43. // [int] displayLayer : 1 = normal layer (default : 1)
  44. // : 2 = layer above the town
  45. // : 4 = layer below the town
  46. //
  47. void Town::draw(int displayLayer)
  48. {
  49. TownLayout* townLayout = town_res.get_layout(layout_id);
  50. TownSlot* townSlot = town_res.get_slot(townLayout->first_slot_recno);
  51. int sectionX1 = loc_x1 * ZOOM_LOC_WIDTH;
  52. int sectionY1 = loc_y1 * ZOOM_LOC_HEIGHT;
  53. if( displayLayer==4 )
  54. {
  55. int townX1 = ZOOM_X1 + (loc_x1-world.zoom_matrix->top_x_loc) * ZOOM_LOC_WIDTH;
  56. int townY1 = ZOOM_Y1 + (loc_y1-world.zoom_matrix->top_y_loc) * ZOOM_LOC_HEIGHT;
  57. townX1 += (STD_TOWN_LOC_WIDTH * ZOOM_LOC_WIDTH - get_bitmap_width(townLayout->ground_bitmap_ptr))/2; // adjust offset
  58. townY1 += (STD_TOWN_LOC_HEIGHT * ZOOM_LOC_HEIGHT - get_bitmap_height(townLayout->ground_bitmap_ptr))/2; // adjust offset
  59. world.zoom_matrix->put_bitmap_remap_clip( townX1, townY1, townLayout->ground_bitmap_ptr );
  60. return;
  61. }
  62. //-------- draw plants, buildings and flags --------//
  63. for( int i=0 ; i<townLayout->slot_count ; i++, townSlot++ )
  64. {
  65. //----- build_type==0 if plants -----//
  66. switch(townSlot->build_type)
  67. {
  68. //----- build_type>0 if town buildings -----//
  69. case TOWN_OBJECT_HOUSE:
  70. town_res.get_build( slot_object_id_array[i] )
  71. ->draw(town_recno, sectionX1+townSlot->base_x, sectionY1+townSlot->base_y );
  72. break;
  73. case TOWN_OBJECT_PLANT:
  74. plant_res.get_bitmap( slot_object_id_array[i] )
  75. ->draw_at(sectionX1+townSlot->base_x, sectionY1+townSlot->base_y);
  76. break;
  77. case TOWN_OBJECT_FARM:
  78. draw_farm(sectionX1+townSlot->base_x, sectionY1+townSlot->base_y, townSlot->build_code );
  79. break;
  80. case TOWN_OBJECT_FLAG:
  81. if( nation_recno )
  82. draw_flag(sectionX1+townSlot->base_x, sectionY1+townSlot->base_y);
  83. break;
  84. }
  85. }
  86. }
  87. //-------- End of function Town::draw -----------//
  88. //------- Begin of function Town::draw_flag -----------//
  89. //
  90. // Draw the town section on the zoom map
  91. //
  92. void Town::draw_flag(int absBaseX, int absBaseY)
  93. {
  94. char flagName[] = "FLAG-1";
  95. flagName[5] = '1' + (char) ((sys.frame_count+town_recno)%8) / 2;
  96. char* colorRemapTable = game.get_color_remap_table(nation_recno, 0);
  97. int drawX = absBaseX - world.view_top_x + ZOOM_X1 - 9;
  98. int drawY = absBaseY - world.view_top_y + ZOOM_Y1 - 97;
  99. world.zoom_matrix->put_bitmap_remap_clip(drawX, drawY, image_spict.get_ptr(flagName), colorRemapTable, 1); // 1-the bitmap is compressed
  100. }
  101. //-------- End of function Town::draw_flag -----------//
  102. //------- Begin of function Town::draw_farm -----------//
  103. //
  104. // Draw farming field.
  105. //
  106. void Town::draw_farm(int absBaseX, int absBaseY, int farmId)
  107. {
  108. err_when( farmId<1 || farmId>9 );
  109. char farmName[] = "FARM-1";
  110. farmName[5] = '0' + farmId;
  111. int drawX = absBaseX - world.view_top_x + ZOOM_X1;
  112. int drawY = absBaseY - world.view_top_y + ZOOM_Y1;
  113. world.zoom_matrix->put_bitmap_clip(drawX, drawY, image_spict.get_ptr(farmName), 1); // 1-the bitmap is compressed
  114. }
  115. //-------- End of function Town::draw_farm -----------//
  116. //------- Begin of function Town::is_in_zoom_win -----------//
  117. //
  118. // Whether the town section is in the current zoom window.
  119. //
  120. int Town::is_in_zoom_win()
  121. {
  122. int x1=world.zoom_matrix->top_x_loc;
  123. int y1=world.zoom_matrix->top_y_loc;
  124. int x2=x1+world.zoom_matrix->disp_x_loc-1;
  125. int y2=y1+world.zoom_matrix->disp_y_loc-1;
  126. return m.is_touch( x1, y1, x2, y2, loc_x1, loc_y1, loc_x2, loc_y2 );
  127. }
  128. //--------- End of function Town::is_in_zoom_win -----------//
  129. //------- Begin of function Town::draw_selected -----------//
  130. //
  131. // Draw a square around the town section on the map.
  132. //
  133. void Town::draw_selected()
  134. {
  135. draw_detect_link_line(0); // 0-the action is draw only, not detecting
  136. }
  137. //--------- End of function Town::draw_selected -----------//
  138. //------- Begin of function Town::draw_detect_link_line ---------//
  139. //
  140. // [int] actionDetect - 0 - this is a draw action
  141. // 1 - this is a detect action
  142. // (default: 0)
  143. //
  144. // return: <int> 1 - detected
  145. // 0 - not detected
  146. //
  147. int Town::draw_detect_link_line(int actionDetect)
  148. {
  149. int i, firmX, firmY, townX, townY;
  150. Firm* firmPtr;
  151. Town* townPtr;
  152. //-------- set source points ----------//
  153. int srcX = ( ZOOM_X1 + (loc_x1-world.zoom_matrix->top_x_loc) * ZOOM_LOC_WIDTH
  154. + ZOOM_X1 + (loc_x2-world.zoom_matrix->top_x_loc+1) * ZOOM_LOC_WIDTH ) / 2;
  155. int srcY = ( ZOOM_Y1 + (loc_y1-world.zoom_matrix->top_y_loc) * ZOOM_LOC_HEIGHT
  156. + ZOOM_Y1 + (loc_y2-world.zoom_matrix->top_y_loc+1) * ZOOM_LOC_HEIGHT ) / 2;
  157. //------ draw lines to linked firms ---------//
  158. char* bitmapPtr;
  159. char goodLinkName[9] = "GOODLINK";
  160. goodLinkName[7] = '1'+(char)(sys.frame_count%3);
  161. for( i=0 ; i<linked_firm_count ; i++ )
  162. {
  163. firmPtr = firm_array[linked_firm_array[i]];
  164. firmX = ( ZOOM_X1 + (firmPtr->loc_x1-world.zoom_matrix->top_x_loc) * ZOOM_LOC_WIDTH
  165. + ZOOM_X1 + (firmPtr->loc_x2-world.zoom_matrix->top_x_loc+1) * ZOOM_LOC_WIDTH ) / 2;
  166. firmY = ( ZOOM_Y1 + (firmPtr->loc_y1-world.zoom_matrix->top_y_loc) * ZOOM_LOC_HEIGHT
  167. + ZOOM_Y1 + (firmPtr->loc_y2-world.zoom_matrix->top_y_loc+1) * ZOOM_LOC_HEIGHT ) / 2;
  168. anim_line.draw_line(&vga_back, srcX, srcY, firmX, firmY, linked_firm_enable_array[i]==LINK_EE );
  169. if( !can_toggle_firm_link(linked_firm_array[i]) )
  170. continue;
  171. //------ draw the link icon and detect mouse action -----//
  172. bitmapPtr = power.get_link_icon( linked_firm_enable_array[i], nation_recno==firmPtr->nation_recno );
  173. if( actionDetect )
  174. {
  175. int rc = world.zoom_matrix->detect_bitmap_clip( firmX-11, firmY-11, bitmapPtr );
  176. if( rc )
  177. mouse.reset_click(); // reset queued mouse click for fast single clicking
  178. //------ left clicking to toggle link -------//
  179. if( rc==1 && nation_recno==nation_array.player_recno )
  180. {
  181. if( linked_firm_enable_array[i] & LINK_ED )
  182. {
  183. toggle_firm_link( i+1, 0, COMMAND_PLAYER );
  184. // ###### begin Gilbert 25/9 #######//
  185. se_ctrl.immediate_sound("TURN_OFF");
  186. // ###### end Gilbert 25/9 #######//
  187. }
  188. else
  189. {
  190. toggle_firm_link( i+1, 1, COMMAND_PLAYER );
  191. // ###### begin Gilbert 25/9 #######//
  192. se_ctrl.immediate_sound("TURN_ON");
  193. // ###### end Gilbert 25/9 #######//
  194. }
  195. // ######## begin Gilbert 23/9 ##########//
  196. if( firmPtr->firm_id == FIRM_CAMP && !remote.is_enable())
  197. // ######## end Gilbert 23/9 ##########//
  198. {
  199. if( nation_recno )
  200. update_target_loyalty();
  201. else
  202. update_target_resistance();
  203. update_camp_link();
  204. }
  205. return 1;
  206. }
  207. //------ right clicking to recruit soldiers ------//
  208. else if( rc==2 )
  209. {
  210. if( firmPtr->nation_recno == nation_recno && !firmPtr->under_construction &&
  211. firmPtr->worker_array && firmPtr->worker_count < MAX_WORKER )
  212. {
  213. firmPtr->pull_town_people(town_recno, COMMAND_PLAYER, browse_selected_race_id(), 1); // last 1-force pulling people from the town to the firm
  214. // ###### begin Gilbert 25/9 #######//
  215. se_ctrl.immediate_sound("PULL_MAN");
  216. // ###### end Gilbert 25/9 #######//
  217. }
  218. }
  219. }
  220. else
  221. {
  222. if( nation_recno == nation_array.player_recno )
  223. world.zoom_matrix->put_bitmap_clip( firmX-11, firmY-11, bitmapPtr );
  224. }
  225. }
  226. //------ draw lines to linked towns ---------//
  227. int townRecno;
  228. for( i=0 ; i<linked_town_count ; i++ )
  229. {
  230. townRecno = linked_town_array[i];
  231. townPtr = town_array[townRecno];
  232. townX = ( ZOOM_X1 + (townPtr->loc_x1-world.zoom_matrix->top_x_loc) * ZOOM_LOC_WIDTH
  233. + ZOOM_X1 + (townPtr->loc_x2-world.zoom_matrix->top_x_loc+1) * ZOOM_LOC_WIDTH ) / 2;
  234. townY = ( ZOOM_Y1 + (townPtr->loc_y1-world.zoom_matrix->top_y_loc) * ZOOM_LOC_HEIGHT
  235. + ZOOM_Y1 + (townPtr->loc_y2-world.zoom_matrix->top_y_loc+1) * ZOOM_LOC_HEIGHT ) / 2;
  236. anim_line.draw_line(&vga_back, srcX, srcY, townX, townY, linked_town_enable_array[i]==LINK_EE );
  237. //------- detect on the migration icon -------//
  238. if( nation_recno == nation_array.player_recno &&
  239. nation_recno == townPtr->nation_recno &&
  240. can_migrate(townRecno) )
  241. {
  242. bitmapPtr = image_icon.get_ptr("MIGRATE");
  243. if( actionDetect )
  244. {
  245. if( world.zoom_matrix->detect_bitmap_clip( townX-11, townY-11, bitmapPtr ) )
  246. {
  247. mouse.reset_click(); // reset queued mouse click for fast single clicking
  248. err_when(town_array[townRecno]->population>MAX_TOWN_POPULATION);
  249. migrate_to(townRecno, COMMAND_PLAYER);
  250. // ###### begin Gilbert 25/9 #######//
  251. se_ctrl.immediate_sound("PULL_MAN");
  252. // ###### end Gilbert 25/9 #######//
  253. return 1;
  254. }
  255. }
  256. else
  257. {
  258. world.zoom_matrix->put_bitmap_clip( townX-11, townY-11, bitmapPtr );
  259. }
  260. }
  261. }
  262. return 0;
  263. }
  264. //------- End of function Town::draw_detect_link_line ---------//
  265. //------- Begin of function Town::can_toggle_firm_link ---------//
  266. //
  267. // Return whether this town can toggle the link to the given
  268. // firm.
  269. //
  270. int Town::can_toggle_firm_link(int firmRecno)
  271. {
  272. if( !nation_recno ) // cannot toggle for independent town
  273. return 0;
  274. //------------------------------------//
  275. Firm* firmPtr;
  276. for( int i=0 ; i<linked_firm_count ; i++ )
  277. {
  278. if( linked_firm_array[i] != firmRecno )
  279. continue;
  280. firmPtr = firm_array[linked_firm_array[i]];
  281. switch( firmPtr->firm_id )
  282. {
  283. //-- you can only toggle a link to a camp if the camp is yours --//
  284. case FIRM_CAMP:
  285. return firmPtr->nation_recno == nation_recno;
  286. //--- town to market link is governed by trade treaty and cannot be toggled ---//
  287. case FIRM_MARKET:
  288. return 0; // !nation_array[nation_recno]->get_relation(firmPtr->nation_recno)->trade_treaty;
  289. default:
  290. return firm_res[firmPtr->firm_id]->is_linkable_to_town;
  291. }
  292. }
  293. return 0;
  294. }
  295. //------- Begin of function Town::can_toggle_firm_link ---------//