OFIRMDIE.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 : OFIRMDIE.CPP
  21. // Description : destruting firm
  22. #include <OSTR.h>
  23. #include <OFIRMRES.h>
  24. #include <OFIRMDIE.h>
  25. #include <OGAMESET.h>
  26. #include <OGAME.h>
  27. #include <ORESDB.h>
  28. #include <ALL.h>
  29. #define FIRM_BUILD_DB "FDBUILD"
  30. #define FIRM_FRAME_DB "FDFRAME"
  31. #define FIRM_BITMAP_DB "FDBITMAP"
  32. struct FirmDieBitmap : public FirmBitmap
  33. {
  34. int bitmap_offset; // fill bitmap_ptr, width and height before draw()
  35. };
  36. FirmDieRes::FirmDieRes()
  37. {
  38. firm_build_count = 0;
  39. firm_bitmap_count = 0;
  40. firm_build_array = NULL;
  41. firm_bitmap_array = NULL;
  42. init_flag = 0;
  43. }
  44. FirmDieRes::~FirmDieRes()
  45. {
  46. deinit();
  47. }
  48. void FirmDieRes::init()
  49. {
  50. deinit();
  51. //----- open firm material bitmap resource file -------//
  52. String str;
  53. str = DIR_RES;
  54. str += "I_FIRMDI.RES";
  55. res_bitmap.init_imported(str,0,1); // 0 - do not read all data into buffer
  56. //------- load database information --------//
  57. load_bitmap_info(); // call load_firm_bitmap() first as load_firm_info() will need info loaded by load_firm_bitmap()
  58. load_build_info();
  59. //----------------------------------------//
  60. init_flag=1;
  61. }
  62. void FirmDieRes::deinit()
  63. {
  64. if(init_flag)
  65. {
  66. mem_del(firm_build_array);
  67. mem_del(firm_bitmap_array);
  68. res_bitmap.deinit();
  69. init_flag = 0;
  70. }
  71. }
  72. //------- Begin of function FirmDieRes::load_firm_bitmap -------//
  73. //
  74. // Read in information of FDBITMAP.DBF into memory array
  75. //
  76. void FirmDieRes::load_bitmap_info()
  77. {
  78. FirmBitmapRec *firmBitmapRec;
  79. FirmDieBitmap *firmBitmap;
  80. int i;
  81. long bitmapOffset;
  82. Database *dbFirmBitmap = game_set.open_db(FIRM_BITMAP_DB);
  83. firm_bitmap_count = (short) dbFirmBitmap->rec_count();
  84. firm_bitmap_array = (FirmDieBitmap*) mem_add( sizeof(FirmDieBitmap)*firm_bitmap_count );
  85. //------ read in firm bitmap info array -------//
  86. memset( firm_bitmap_array, 0, sizeof(FirmDieBitmap) * firm_bitmap_count );
  87. for( i=0 ; i<firm_bitmap_count ; i++ )
  88. {
  89. firmBitmapRec = (FirmBitmapRec*) dbFirmBitmap->read(i+1);
  90. firmBitmap = firm_bitmap_array+i;
  91. memcpy( &bitmapOffset, firmBitmapRec->bitmap_ptr, sizeof(long) );
  92. // BUGHERE : bitmap is not yet loaded into memory, fill them before draw()
  93. firmBitmap->bitmap_ptr = NULL;
  94. firmBitmap->width = 0;
  95. firmBitmap->height = 0;
  96. firmBitmap->offset_x = m.atoi( firmBitmapRec->offset_x, firmBitmapRec->OFFSET_LEN );
  97. firmBitmap->offset_y = m.atoi( firmBitmapRec->offset_y, firmBitmapRec->OFFSET_LEN );
  98. firmBitmap->loc_width = m.atoi( firmBitmapRec->loc_width , firmBitmapRec->LOC_LEN );
  99. firmBitmap->loc_height = m.atoi( firmBitmapRec->loc_height, firmBitmapRec->LOC_LEN );
  100. firmBitmap->display_layer = firmBitmapRec->layer - '0';
  101. firmBitmap->bitmap_offset = bitmapOffset;
  102. }
  103. }
  104. //--------- End of function FirmDieRes::load_firm_bitmap ---------//
  105. //------- Begin of function FirmDieRes::load_firm_build -------//
  106. //
  107. // Read in information of FIRM.DBF into memory array
  108. //
  109. void FirmDieRes::load_build_info()
  110. {
  111. FirmBuildRec *firmBuildRec;
  112. FirmFrameRec *firmFrameRec;
  113. FirmBuild *firmBuild;
  114. FirmBitmap *firmBitmap;
  115. int i, j, k, frameRecno, bitmapRecno;
  116. short *firstFrameArray;
  117. //---- read in firm count and initialize firm info array ----//
  118. Database *dbFirmBuild = game_set.open_db(FIRM_BUILD_DB); // only one database can be opened at a time, so we read FIRM.DBF first
  119. firm_build_count = (short) dbFirmBuild->rec_count();
  120. firm_build_array = (FirmBuild*) mem_add( sizeof(FirmBuild)*firm_build_count );
  121. memset( firm_build_array, 0, sizeof(FirmBuild) * firm_build_count );
  122. //------ allocate an array for storing firstFrameRecno -----//
  123. firstFrameArray = (short*) mem_add( sizeof(short) * firm_build_count );
  124. //---------- read in FDBUILD.DBF ---------//
  125. for( i=0 ; i<firm_build_count ; i++ )
  126. {
  127. firmBuildRec = (FirmBuildRec*) dbFirmBuild->read(i+1);
  128. firmBuild = firm_build_array+i;
  129. m.rtrim_fld( firmBuild->build_code, firmBuildRec->race_code, firmBuild->BUILD_CODE_LEN );
  130. firmBuild->animate_full_size = firmBuildRec->animate_full_size=='1';
  131. firmBuild->race_id = m.atoi( firmBuildRec->race_id , firmBuildRec->RACE_ID_LEN );
  132. firmBuild->frame_count = m.atoi( firmBuildRec->frame_count, firmBuildRec->FRAME_COUNT_LEN );
  133. firmBuild->under_construction_bitmap_recno = m.atoi(firmBuildRec->under_construction_bitmap_recno, firmBuildRec->BITMAP_RECNO_LEN);
  134. firmBuild->idle_bitmap_recno = m.atoi(firmBuildRec->idle_bitmap_recno, firmBuildRec->BITMAP_RECNO_LEN);
  135. firmBuild->ground_bitmap_recno = m.atoi(firmBuildRec->ground_bitmap_recno, firmBuildRec->BITMAP_RECNO_LEN);
  136. err_when( firmBuild->frame_count > MAX_FIRM_FRAME );
  137. firstFrameArray[i] = m.atoi( firmBuildRec->first_frame, firmBuildRec->FIRST_FRAME_LEN );
  138. // BUGHERE : need to compare same Firm name and build code in firm database
  139. }
  140. //-------- read in FDFRAME.DBF --------//
  141. Database *dbFirmFrame = game_set.open_db(FIRM_FRAME_DB);
  142. int minOffsetX, minOffsetY;
  143. int maxX2, maxY2;
  144. for( i=0 ; i<firm_build_count ; i++ )
  145. {
  146. firmBuild = firm_build_array+i;
  147. frameRecno = firstFrameArray[i];
  148. minOffsetX = minOffsetY = 0xFFFF;
  149. maxX2 = maxY2 = 0;
  150. for( j=0 ; j<firmBuild->frame_count ; j++, frameRecno++ )
  151. {
  152. firmFrameRec = (FirmFrameRec*) dbFirmFrame->read(frameRecno);
  153. //------ following animation frames, bitmap sections -----//
  154. firmBuild->first_bitmap_array[j] = m.atoi( firmFrameRec->first_bitmap, firmFrameRec->FIRST_BITMAP_LEN );
  155. firmBuild->bitmap_count_array[j] = m.atoi( firmFrameRec->bitmap_count, firmFrameRec->BITMAP_COUNT_LEN );
  156. firmBuild->frame_delay_array[j] = m.atoi( firmFrameRec->delay, firmFrameRec->DELAY_LEN );
  157. //---- get the min offset_x, offset_y and max width, height ----//
  158. //
  159. // So we can get the largest area of all the frames in this building
  160. // and this will serve as a normal size setting for this building,
  161. // with variation from frame to frame
  162. //
  163. //--------------------------------------------------------------//
  164. firmBitmap = firm_bitmap_array + firmBuild->first_bitmap_array[j] - 1;
  165. for( k=firmBuild->bitmap_count_array[j] ; k>0 ; k--, firmBitmap++ )
  166. {
  167. if( firmBitmap->offset_x < minOffsetX )
  168. minOffsetX = firmBitmap->offset_x;
  169. if( firmBitmap->offset_y < minOffsetY )
  170. minOffsetY = firmBitmap->offset_y;
  171. if( firmBitmap->offset_x + firmBitmap->width > maxX2 )
  172. maxX2 = firmBitmap->offset_x + firmBitmap->width;
  173. if( firmBitmap->offset_y + firmBitmap->height > maxY2 )
  174. maxY2 = firmBitmap->offset_y + firmBitmap->height;
  175. }
  176. }
  177. //------- set FirmBuild Info -------//
  178. bitmapRecno = firmBuild->first_bitmap_array[0];
  179. //----- get the info of the first frame bitmap ----//
  180. firmBitmap = firm_bitmap_array + bitmapRecno - 1;
  181. firmBuild->loc_width = firmBitmap->loc_width;
  182. firmBuild->loc_height = firmBitmap->loc_height;
  183. firmBuild->min_offset_x = minOffsetX;
  184. firmBuild->min_offset_y = minOffsetY;
  185. firmBuild->max_bitmap_width = maxX2 - minOffsetX;
  186. firmBuild->max_bitmap_height = maxY2 - minOffsetY;
  187. //------ set firmBuild's under construction and idle bitmap recno -----//
  188. if( firmBuild->under_construction_bitmap_recno==0 )
  189. firmBuild->under_construction_bitmap_recno = bitmapRecno;
  190. if( firmBuild->idle_bitmap_recno==0 )
  191. firmBuild->idle_bitmap_recno = bitmapRecno;
  192. }
  193. //------ free up the temporary array -------//
  194. mem_del( firstFrameArray );
  195. }
  196. //--------- End of function FirmDieRes::load_firm_build ---------//
  197. //--------- Begin of function FirmDieRes::get_build ---------//
  198. FirmBuild* FirmDieRes::get_build(int buildId)
  199. {
  200. err_when( buildId < 1 || buildId > firm_build_count);
  201. return firm_build_array+buildId-1;
  202. }
  203. //--------- End of function FirmDieRes::get_build ---------//
  204. //--------- Begin of function FirmDieRes::get_bitmap ---------//
  205. FirmDieBitmap* FirmDieRes::get_bitmap(int bitmapId)
  206. {
  207. err_when( bitmapId < 1 || bitmapId > firm_bitmap_count);
  208. return firm_bitmap_array+bitmapId-1;
  209. }
  210. //--------- End of function FirmDieRes::get_bitmap ---------//
  211. //--------- Begin of function FirmDie::init --------//
  212. void FirmDie::init(short firmId, short firmBuildId, short nationRecno,
  213. short locX1, short locY1, short locX2, short locY2)
  214. {
  215. firm_id = firmId;
  216. firm_build_id = firmBuildId;
  217. nation_recno = nationRecno;
  218. loc_x1 = locX1;
  219. loc_y1 = locY1;
  220. loc_x2 = locX2;
  221. loc_y2 = locY2;
  222. frame = 1;
  223. }
  224. // add before delete the firm
  225. void FirmDie::init(Firm *firmPtr)
  226. {
  227. firm_id = firmPtr->firm_id;
  228. firm_build_id = firmPtr->firm_build_id;
  229. nation_recno = firmPtr->nation_recno;
  230. loc_x1 = firmPtr->loc_x1;
  231. loc_y1 = firmPtr->loc_y1;
  232. loc_x2 = firmPtr->loc_x2;
  233. loc_y2 = firmPtr->loc_y2;
  234. frame = 1;
  235. frame_delay_count = 0;
  236. }
  237. void FirmDie::pre_process()
  238. {
  239. //nothing
  240. }
  241. int FirmDie::process()
  242. {
  243. FirmBuild *firmBuild = firm_die_res.get_build(firm_build_id);
  244. if( ++frame_delay_count > firmBuild->frame_delay_array[frame-1])
  245. {
  246. frame_delay_count = 0;
  247. if( ++frame > firmBuild->frame_count)
  248. {
  249. return 1;
  250. }
  251. }
  252. return 0;
  253. }
  254. void FirmDie::draw(int displayLayer)
  255. {
  256. // get ground dirt from firm_res
  257. FirmBuild* firmBuild = firm_res.get_build(firm_build_id);
  258. if( firmBuild->ground_bitmap_recno )
  259. {
  260. // firm_res.get_bitmap(firmBuild->ground_bitmap_recno)
  261. // ->draw_at(loc_x1*ZOOM_LOC_WIDTH, loc_y1*ZOOM_LOC_HEIGHT, NULL, displayLayer);
  262. }
  263. //---------- draw animation now ------------//
  264. firmBuild = firm_die_res.get_build(firm_build_id);
  265. FirmDieBitmap* firmBitmap;
  266. int bitmapRecno, i;
  267. int firstBitmap = firmBuild->first_bitmap(frame);
  268. int bitmapCount = firmBuild->bitmap_count(frame);
  269. char* colorRemapTable = game.get_color_remap_table(nation_recno, 0);
  270. for( i=0, bitmapRecno=firstBitmap ; i<bitmapCount ; i++, bitmapRecno++ )
  271. {
  272. firmBitmap = firm_die_res.get_bitmap(bitmapRecno);
  273. // BUGHERE : need to load bitmap into memory
  274. if( firmBitmap )
  275. {
  276. char *bitmapPtr;
  277. firmBitmap->bitmap_ptr = bitmapPtr = firm_die_res.res_bitmap.read_imported(firmBitmap->bitmap_offset);
  278. firmBitmap->width = *(short *)bitmapPtr;
  279. firmBitmap->height= *(1+(short *)bitmapPtr);
  280. firmBitmap->draw_at(loc_x1*ZOOM_LOC_WIDTH, loc_y1*ZOOM_LOC_HEIGHT, colorRemapTable, displayLayer);
  281. }
  282. }
  283. }
  284. FirmDieArray::FirmDieArray() : DynArrayB(sizeof(FirmDie),10, DEFAULT_REUSE_INTERVAL_DAYS)
  285. {
  286. // nothing
  287. }
  288. FirmDieArray::~FirmDieArray()
  289. {
  290. deinit();
  291. }
  292. void FirmDieArray::init()
  293. {
  294. zap();
  295. }
  296. void FirmDieArray::deinit()
  297. {
  298. // nothing
  299. }
  300. void FirmDieArray::process()
  301. {
  302. int i, j;
  303. for( i=1, j=size(); j; --j, ++i)
  304. {
  305. if( is_deleted(i) )
  306. continue;
  307. FirmDie *firmDiePtr = this->operator[](i);
  308. if( firmDiePtr->process() )
  309. {
  310. del(i);
  311. continue;
  312. }
  313. }
  314. }
  315. int FirmDieArray::add(FirmDie *r)
  316. {
  317. linkin(r);
  318. return recno();
  319. }
  320. void FirmDieArray::del(int i)
  321. {
  322. linkout(i);
  323. }
  324. FirmDie *FirmDieArray::operator[](int recNo)
  325. {
  326. FirmDie* firmDiePtr = (FirmDie*) get(recNo);
  327. if( !firmDiePtr || is_deleted(recNo) )
  328. err.run( "FirmDieArray[] is deleted" );
  329. return firmDiePtr;
  330. }
  331. int FirmDieArray::is_deleted(int recNo)
  332. {
  333. FirmDie* firmDiePtr = (FirmDie*) get(recNo);
  334. if( !firmDiePtr || firmDiePtr->firm_id == 0)
  335. return 1;
  336. return 0;
  337. }