OGAMESET.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 : OGAMESET.CPP
  21. //Description : Object Game Set
  22. #include <ALL.h>
  23. #include <OSTR.h>
  24. #include <ODIR.h>
  25. #include <OSYS.h>
  26. #include <OGAMESET.h>
  27. //-------- Define constant -------------//
  28. #define SET_HEADER_DB "HEADER"
  29. //--------- Begin of GameSet::init -------------//
  30. void GameSet::init()
  31. {
  32. set_opened_flag=0; // no set has been opened
  33. load_set_header(); // load the info. of all sets
  34. init_flag=1;
  35. }
  36. //----------- End of GameSet::init -------------//
  37. //--------- Begin of GameSet::deinit -------------//
  38. void GameSet::deinit()
  39. {
  40. if( init_flag )
  41. {
  42. mem_del(set_info_array);
  43. init_flag = 0;
  44. }
  45. }
  46. //----------- End of GameSet::deinit -------------//
  47. //--------- Begin of GameSet::load_set_header -------------//
  48. void GameSet::load_set_header()
  49. {
  50. int i;
  51. Directory setDir;
  52. SetRec *setRec;
  53. SetInfo *setInfo;
  54. char *dataPtr;
  55. String str;
  56. setDir.read( DIR_RES"*.SET", 1 ); // read in the file list of all game sets in the directory, 1-Sort file names
  57. set_count = setDir.size(); // no. of game set available to choose
  58. err_when( !set_count ); // error if there is no GameSet
  59. set_info_array = (SetInfo*) mem_add( sizeof(SetInfo) * set_count );
  60. //-------- read in the headers of all game sets -------//
  61. for( i=1 ; i<=setDir.size() ; i++ )
  62. {
  63. str = DIR_RES;
  64. str += setDir[i]->name;
  65. set_res.init( str, 0 ); // open the game set first
  66. dataPtr = set_res.read(SET_HEADER_DB); // get the pointer to the header database
  67. err_when( !dataPtr );
  68. set_db.open_from_buf(dataPtr); // read in the header database
  69. setRec = (SetRec*) set_db.read(1); // the header database only contains one record
  70. setInfo = set_info_array+i-1;
  71. m.rtrim_fld( setInfo->code, setRec->code, setRec->CODE_LEN );
  72. m.rtrim_fld( setInfo->des , setRec->des , setRec->DES_LEN );
  73. }
  74. }
  75. //----------- End of GameSet::load_set_header -------------//
  76. //--------- Begin of GameSet::open_set -------------//
  77. //
  78. // Open the specific set.
  79. // Only one set can be activiated at a time in GameSet.
  80. //
  81. // <int> setId = the id. of the set
  82. //
  83. void GameSet::open_set(int setId)
  84. {
  85. err_if( setId<0 || setId>set_count )
  86. err_here();
  87. cur_set_id = setId;
  88. String str;
  89. str = DIR_RES;
  90. str += set_info_array[setId-1].code;
  91. str += ".SET";
  92. set_res.init( str, 0 ); // 0-don't read all into the buffer
  93. //-----------------------------------------------//
  94. set_opened_flag=1;
  95. }
  96. //----------- End of GameSet::open_set -------------//
  97. //--------- Begin of GameSet::close_set -------------//
  98. //
  99. // Close current set
  100. //
  101. void GameSet::close_set()
  102. {
  103. set_res.deinit();
  104. set_opened_flag=0;
  105. }
  106. //----------- End of GameSet::close_set -------------//
  107. //--------- Begin of GameSet::open_db -------------//
  108. //
  109. // Open a database in the current set
  110. //
  111. // <char*> dbName = the name of the database to be opened
  112. //
  113. Database* GameSet::open_db(char* dbName)
  114. {
  115. err_when(!set_opened_flag);
  116. char* dataPtr = set_res.read(dbName);
  117. err_when( !dataPtr );
  118. set_db.open_from_buf(dataPtr);
  119. return &set_db;
  120. }
  121. //----------- End of GameSet::open_db -------------//
  122. //--------- Begin of GameSet::get_db -------------//
  123. Database* GameSet::get_db()
  124. {
  125. err_when(!set_opened_flag);
  126. return &set_db;
  127. }
  128. //----------- End of GameSet::get_db -------------//
  129. //--------- Begin of GameSet::find_set -------------//
  130. //
  131. // Look for a specified game set
  132. //
  133. // <char*> setCode = the code of the game set.
  134. //
  135. // return : <int> >=1 - the id. of the game set
  136. // 0 - the game set code is not found
  137. //
  138. int GameSet::find_set(char* setCode)
  139. {
  140. int i;
  141. for( i=0 ; i<set_count ; i++ )
  142. {
  143. if( strcmp( set_info_array[i].code, setCode )==0 )
  144. return i+1;
  145. }
  146. return 0;
  147. }
  148. //----------- End of GameSet::find_set -------------//