OSPRTRES.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 : OSPRTRES.CPP
  21. //Description : Object Sprite resource
  22. #include <ALL.h>
  23. #include <OSYS.h>
  24. #include <OWORLD.h>
  25. #include <OGAMESET.h>
  26. #include <OSPRTRES.h>
  27. #include <OWEATHER.h>
  28. //-------- define file name -----------//
  29. #define SPRITE_DB "SPRITE"
  30. #define SPRITE_ACTION_DB "SACTION"
  31. #define SUB_SPRITE_DB "SUB_SPR"
  32. //-------- Begin of function SpriteRes::init ---------//
  33. void SpriteRes::init()
  34. {
  35. deinit();
  36. load_sprite_info();
  37. load_sub_sprite_info();
  38. init_flag=1;
  39. }
  40. //--------- End of function SpriteRes::init ----------//
  41. //-------- Begin of function SpriteRes::deinit ---------//
  42. void SpriteRes::deinit()
  43. {
  44. if( init_flag )
  45. {
  46. delete[] sprite_info_array;
  47. mem_del(sub_sprite_info_array);
  48. init_flag=0;
  49. }
  50. }
  51. //--------- End of function SpriteRes::deinit ----------//
  52. //-------- Begin of function SpriteRes::load_sprite_info ---------//
  53. void SpriteRes::load_sprite_info()
  54. {
  55. Database *dbSprite = game_set.open_db(SPRITE_DB);
  56. SpriteRec *spriteRec;
  57. SpriteInfo *spriteInfo;
  58. SpriteActionRec *spriteActionRec;
  59. SpriteMove *spriteMove;
  60. SpriteAttack *spriteAttack;
  61. int i, j, actionRecno, dirId;
  62. sprite_info_count = dbSprite->rec_count();
  63. sprite_info_array = new SpriteInfo[sprite_info_count];
  64. memset( sprite_info_array, 0, sizeof(SpriteInfo)*sprite_info_count );
  65. short* first_dir_recno_array = (short*) mem_add( sizeof(short) * sprite_info_count ); // allocate temporary arrays for temporary storage
  66. short* dir_count_array = (short*) mem_add( sizeof(short) * sprite_info_count );
  67. //------------ read in sprite info -------------//
  68. for( i=0 ; i<sprite_info_count ; i++ )
  69. {
  70. spriteRec = (SpriteRec*) dbSprite->read(i+1);
  71. spriteInfo = sprite_info_array+i;
  72. m.rtrim_fld( spriteInfo->sprite_code, spriteRec->sprite_code, spriteRec->CODE_LEN );
  73. spriteInfo->sprite_type = spriteRec->sprite_type;
  74. if(spriteInfo->sprite_type == ' ')
  75. spriteInfo->sprite_type = 0;
  76. spriteInfo->sprite_sub_type = spriteRec->sprite_sub_type;
  77. if(spriteInfo->sprite_sub_type == ' ')
  78. spriteInfo->sprite_sub_type = 0;
  79. if( spriteRec->need_turning != ' ' )
  80. spriteInfo->need_turning = spriteRec->need_turning-'0';
  81. spriteInfo->turn_resolution = m.atoi(spriteRec->turn_resolution, spriteRec->TURN_RES_LEN);
  82. spriteInfo->loc_width = m.atoi(spriteRec->loc_width , spriteRec->SPRITE_PARA_LEN);
  83. spriteInfo->loc_height = m.atoi(spriteRec->loc_height, spriteRec->SPRITE_PARA_LEN);
  84. spriteInfo->speed = m.atoi(spriteRec->speed , spriteRec->SPRITE_PARA_LEN);
  85. spriteInfo->max_speed = m.atoi(spriteRec->speed , spriteRec->SPRITE_PARA_LEN);
  86. //### begin alex 2/6 ###//
  87. /*#ifdef DEBUG2
  88. spriteInfo->speed *=2;
  89. spriteInfo->max_speed *=2;
  90. #endif*/
  91. //#### end alex 2/6 ####//
  92. spriteInfo->frames_per_step = m.atoi(spriteRec->frames_per_step, spriteRec->SPRITE_PARA_LEN);
  93. spriteInfo->max_rain_slowdown = m.atoi(spriteRec->max_rain_slowdown, spriteRec->SPRITE_PARA_LEN);
  94. spriteInfo->max_snow_slowdown = m.atoi(spriteRec->max_snow_slowdown, spriteRec->SPRITE_PARA_LEN);
  95. spriteInfo->lightning_damage = m.atoi(spriteRec->lightning_damage, spriteRec->DAMAGE_LEN);
  96. // ###### begin Gilbert 21/8 ########//
  97. if( spriteRec->remap_bitmap_flag == '\0'
  98. || spriteRec->remap_bitmap_flag == ' '
  99. || spriteRec->remap_bitmap_flag == '0' )
  100. spriteInfo->remap_bitmap_flag = 0;
  101. else
  102. spriteInfo->remap_bitmap_flag = 1;
  103. // ###### end Gilbert 21/8 ########//
  104. first_dir_recno_array[i] = m.atoi(spriteRec->first_move_recno, spriteRec->RECNO_LEN);
  105. dir_count_array[i] = m.atoi(spriteRec->move_count , spriteRec->COUNT_LEN);
  106. }
  107. //------- read in sprite action info ---------//
  108. Database *dbSpriteMove = game_set.open_db(SPRITE_ACTION_DB);
  109. for( i=0 ; i<sprite_info_count ; i++ )
  110. {
  111. spriteInfo = sprite_info_array+i;
  112. for( j=0, actionRecno=first_dir_recno_array[i] ; j<dir_count_array[i] ; j++, actionRecno++ )
  113. {
  114. spriteActionRec = (SpriteActionRec*) dbSpriteMove->read(actionRecno);
  115. dirId = m.atoi(spriteActionRec->dir_id, spriteActionRec->DIR_ID_LEN);
  116. //--------- move motion --------//
  117. if( spriteActionRec->action[0] == 'M' )
  118. {
  119. spriteMove = spriteInfo->move_array+dirId;
  120. spriteMove->first_frame_recno = m.atoi(spriteActionRec->first_frame_recno, spriteActionRec->RECNO_LEN);
  121. spriteMove->frame_count = m.atoi(spriteActionRec->frame_count, spriteActionRec->COUNT_LEN);
  122. //--- the first movement frame is the default stop frame ---//
  123. if( spriteInfo->stop_array[dirId].frame_recno == 0)
  124. {
  125. spriteInfo->stop_array[dirId].frame_recno = spriteMove->first_frame_recno;
  126. spriteInfo->stop_array[dirId].frame_count = 1;
  127. }
  128. }
  129. //-------- attacking motion or weapon motion --------//
  130. else if( spriteActionRec->action[0] == 'A' )
  131. {
  132. err_when( spriteActionRec->action[1] < '1' || spriteActionRec->action[1] > '9' );
  133. spriteAttack = spriteInfo->attack_array[spriteActionRec->action[1]-'1'] + dirId;
  134. spriteAttack->first_frame_recno = m.atoi(spriteActionRec->first_frame_recno, spriteActionRec->RECNO_LEN);
  135. spriteAttack->frame_count = m.atoi(spriteActionRec->frame_count , spriteActionRec->COUNT_LEN);
  136. }
  137. //--------- stop bitmap ---------//
  138. else if( spriteActionRec->action[0] == 'S' )
  139. {
  140. spriteInfo->stop_array[dirId].frame_recno = m.atoi(spriteActionRec->first_frame_recno, spriteActionRec->RECNO_LEN);
  141. spriteInfo->stop_array[dirId].frame_count = m.atoi(spriteActionRec->frame_count, spriteActionRec->COUNT_LEN);
  142. }
  143. //-------- dying motion ---------//
  144. else if( spriteActionRec->action[0] == 'D' )
  145. {
  146. spriteInfo->die.first_frame_recno = m.atoi(spriteActionRec->first_frame_recno, spriteActionRec->RECNO_LEN);
  147. spriteInfo->die.frame_count = m.atoi(spriteActionRec->frame_count , spriteActionRec->COUNT_LEN);
  148. }
  149. //--------- guarding motion --------//
  150. else if( spriteActionRec->action[0] == 'G' )
  151. {
  152. if( spriteActionRec->action[1] == 'M' )
  153. {
  154. // moving guard
  155. SpriteGuardMove *spriteGuardMove = spriteInfo->guard_move_array+dirId;
  156. spriteGuardMove->first_frame_recno = m.atoi(spriteActionRec->first_frame_recno, spriteActionRec->RECNO_LEN);
  157. spriteGuardMove->frame_count = m.atoi(spriteActionRec->frame_count, spriteActionRec->COUNT_LEN);
  158. // set can_guard_flag
  159. spriteInfo->can_guard_flag |= 2;
  160. }
  161. else
  162. {
  163. // standing guard
  164. SpriteGuardStop *spriteGuardStop = spriteInfo->guard_stop_array+dirId;
  165. spriteGuardStop->first_frame_recno = m.atoi(spriteActionRec->first_frame_recno, spriteActionRec->RECNO_LEN);
  166. spriteGuardStop->frame_count = m.atoi(spriteActionRec->frame_count, spriteActionRec->COUNT_LEN);
  167. // set can_guard_flag
  168. spriteInfo->can_guard_flag |= 1;
  169. }
  170. }
  171. }
  172. }
  173. //----------- delete temp arrays -------------//
  174. mem_del( first_dir_recno_array );
  175. mem_del( dir_count_array );
  176. }
  177. //-------- End of function SpriteRes::load_sprite_info ---------//
  178. //-------- Begin of function SpriteRes::load_sub_sprite_info ---------//
  179. void SpriteRes::load_sub_sprite_info()
  180. {
  181. //------------ read in sub sprite info -------------//
  182. Database *dbSubSprite = game_set.open_db(SUB_SPRITE_DB);
  183. int subSpriteCount = dbSubSprite->rec_count();
  184. sub_sprite_info_array = (SubSpriteInfo*) mem_add( sizeof(SubSpriteInfo) * subSpriteCount );
  185. memset( sub_sprite_info_array, 0, sizeof(SubSpriteInfo) * subSpriteCount);
  186. for( int i=0 ; i<subSpriteCount; i++ )
  187. {
  188. SubSpriteRec *subSpriteRec = (SubSpriteRec *) dbSubSprite->read(i+1);
  189. SubSpriteInfo *subSpriteInfo = sub_sprite_info_array+i;
  190. subSpriteInfo->sprite_id = m.atoi(subSpriteRec->sub_sprite_id, subSpriteRec->RECNO_LEN);
  191. err_when( subSpriteInfo->sprite_id > sprite_info_count );
  192. subSpriteInfo->sprite_info = sprite_info_array + subSpriteInfo->sprite_id -1;
  193. subSpriteInfo->offset_x = m.atoi(subSpriteRec->offset_x, subSpriteRec->OFFSET_LEN);
  194. subSpriteInfo->offset_y = m.atoi(subSpriteRec->offset_y, subSpriteRec->OFFSET_LEN);
  195. // set link from parent
  196. // assume SUB_SPR database is sorted by sprite_name and sub_no
  197. int subNo = m.atoi(subSpriteRec->sub_no, subSpriteRec->SUB_NO_LEN);
  198. SpriteInfo *parentSprite = sprite_res[m.atoi(subSpriteRec->sprite_id, subSpriteRec->RECNO_LEN)];
  199. if( subNo == 1)
  200. parentSprite->sub_sprite_info = subSpriteInfo;
  201. parentSprite->sub_sprite_count = subNo;
  202. }
  203. }
  204. //-------- End of function SpriteRes::load_sub_sprite_info ---------//
  205. //-------- Begin of function SpriteRes::update_speed -------//
  206. void SpriteRes::update_speed()
  207. {
  208. SpriteInfo *spriteInfo;
  209. short rainScale = weather.rain_scale();
  210. short snowScale = weather.snow_scale();
  211. short speedDrop;
  212. rainScale = rainScale > 7 ? 7 : rainScale;
  213. snowScale = snowScale > 7 ? 7 : snowScale;
  214. for( int i=0 ; i<sprite_info_count ; i++ )
  215. {
  216. speedDrop = 0;
  217. spriteInfo = sprite_info_array+i;
  218. if( rainScale > 0 && spriteInfo->max_rain_slowdown > 0 )
  219. {
  220. speedDrop += rainScale*spriteInfo->max_rain_slowdown/8 + 1;
  221. }
  222. if( snowScale > 0 && spriteInfo->max_snow_slowdown > 0 )
  223. {
  224. speedDrop += snowScale*spriteInfo->max_snow_slowdown/8 + 1;
  225. }
  226. spriteInfo->speed = spriteInfo->max_speed - speedDrop;
  227. }
  228. }
  229. //-------- End of function SpriteRes::update_speed -------//
  230. #ifdef DEBUG
  231. //-------- Begin of function SpriteRes::operator[] -------//
  232. SpriteInfo* SpriteRes::operator[](int recNo)
  233. {
  234. if( recNo<1 || recNo>sprite_info_count )
  235. err.run( "SpriteRes::operator[%d]", recNo );
  236. return sprite_info_array+recNo-1;
  237. }
  238. //--------- End of function SpriteRes::operator[] --------//
  239. #endif
  240. //------- Begin of function SpriteInfo::~SpriteInfo -------//
  241. SpriteInfo::~SpriteInfo()
  242. {
  243. res_bitmap.deinit();
  244. }
  245. //--------- End of function SpriteInfo::~SpriteInfo -------//
  246. //------- Begin of function SpriteInfo::load_bitmap_res -------//
  247. void SpriteInfo::load_bitmap_res()
  248. {
  249. if( ++loaded_count > 1 ) // if bitmaps of this sprite has been loaded
  250. return;
  251. //----- open sprite bitmap resource file -------//
  252. String str;
  253. str = DIR_SPRITE;
  254. str += sprite_code;
  255. str += ".SPR";
  256. res_bitmap.init_imported(str, 1); // 1-read all into buffer
  257. }
  258. //-------- End of function SpriteInfo::load_bitmap_res -------//
  259. //------- Begin of function SpriteInfo::free_bitmap_res -------//
  260. void SpriteInfo::free_bitmap_res()
  261. {
  262. loaded_count--;
  263. err_when( loaded_count < 0 );
  264. if( loaded_count==0 ) // if this bitmap is still needed by other sprites
  265. res_bitmap.deinit();
  266. }
  267. //-------- End of function SpriteInfo::free_bitmap_res -------//
  268. //------- Begin of function SpriteInfo::get_sub_sprite -------//
  269. SpriteInfo *SpriteInfo::get_sub_sprite(int i)
  270. {
  271. if( i < 1 || i > sub_sprite_count)
  272. return NULL;
  273. else
  274. return (sub_sprite_info+i-1)->sprite_info;
  275. }
  276. //------- End of function SpriteInfo::get_sub_sprite -------//
  277. //------- Begin of function SpriteInfo::get_sub_sprite_info -------//
  278. SubSpriteInfo *SpriteInfo::get_sub_sprite_info(int i)
  279. {
  280. if( i < 1 || i > sub_sprite_count)
  281. return NULL;
  282. else
  283. return sub_sprite_info+i-1;
  284. }
  285. //------- End of function SpriteInfo::get_sub_sprite_info -------//
  286. //-------- Begin of function SpriteInfo::travel_days ---------//
  287. //
  288. // <int> travelDistance - total distance in location units.
  289. //
  290. // return: <int> the no. of days it will take to travel the distance.
  291. //
  292. int SpriteInfo::travel_days(int travelDistance)
  293. {
  294. int travelFrames = ZOOM_LOC_WIDTH * travelDistance / speed;
  295. return travelFrames / FRAMES_PER_DAY * 110 / 100; // + 10% for circumstances that the units are blocked and needed to wait and turning, etc.
  296. }
  297. //--------- End of function SpriteInfo::travel_days ----------//