OW_ROCK.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 : OW_ROCK.CPP
  21. // Description : generate random rock on map
  22. #include <OWORLD.h>
  23. #include <OROCKRES.h>
  24. #include <OROCK.h>
  25. #include <OTERRAIN.h>
  26. //--------------- begin of function World::can_add_rock ----------//
  27. int World::can_add_rock(short x1, short y1, short x2, short y2)
  28. {
  29. int x,y;
  30. err_when(x1 > x2 || y1 > y2);
  31. for(y = y1; y <= y2; ++y)
  32. for( x = x1; x <= x2; ++x)
  33. if( !get_loc(x,y)->can_add_rock(3) )
  34. return FALSE;
  35. return TRUE;
  36. }
  37. //--------------- end of function World::can_add_rock ----------//
  38. // --------- begin of function World::add_rock ----------//
  39. //
  40. // note : make sure the location in the area is free
  41. //
  42. void World::add_rock(short rockRecno, short x1, short y1)
  43. {
  44. // ------- get the delay remain count for the first frame -----//
  45. Rock newRock(rockRecno, x1, y1);
  46. int rockArrayRecno = rock_array.add(&newRock);
  47. RockInfo *rockInfo = rock_res.get_rock_info(rockRecno);
  48. for( short dy = 0; dy < rockInfo->loc_height && y1+dy < max_y_loc; ++dy)
  49. {
  50. for( short dx = 0; dx < rockInfo->loc_width && x1+dx < max_x_loc; ++dx)
  51. {
  52. short rockBlockRecno = rock_res.locate_block(rockRecno, dx, dy);
  53. if( rockBlockRecno )
  54. {
  55. Location *locPtr = get_loc(x1+dx, y1+dy);
  56. err_when( !locPtr->can_add_rock(3) );
  57. locPtr->set_rock(rockArrayRecno);
  58. locPtr->set_power_off();
  59. set_surr_power_off(x1, y1);
  60. }
  61. }
  62. }
  63. }
  64. // --------- end of function World::add_rock ----------//
  65. // --------- begin of function World::gen_rocks ----------//
  66. void World::gen_rocks(int nGrouped, int nLarge, int nSmall)
  67. {
  68. // one 'large' (size 1 to 4) at the center
  69. // and a number 'small' (size 1 to 2) at the surroundings
  70. const int GAP=4;
  71. const int HUGE_ROCK_SIZE=6;
  72. const int LARGE_ROCK_SIZE=4;
  73. const int SMALL_ROCK_SIZE=2;
  74. int trial = (nGrouped + nLarge + nSmall) * 2;
  75. while( (nGrouped > 0 || nLarge > 0 || nSmall > 0) && --trial > 0)
  76. {
  77. // generate grouped rocks
  78. if( nGrouped > 0 )
  79. {
  80. short x = (GAP+SMALL_ROCK_SIZE)+m.random( max_x_loc - LARGE_ROCK_SIZE +1 - 2*(GAP+SMALL_ROCK_SIZE));
  81. short y = (GAP+SMALL_ROCK_SIZE)+m.random( max_y_loc - LARGE_ROCK_SIZE +1 - 2*(GAP+SMALL_ROCK_SIZE));
  82. short x2 = x + LARGE_ROCK_SIZE -1;
  83. short y2 = y + LARGE_ROCK_SIZE -1;
  84. if( can_add_rock(x,y, x2,y2) )
  85. {
  86. short rockRecno = rock_res.search("R", 1,LARGE_ROCK_SIZE,1,LARGE_ROCK_SIZE,-1,0,
  87. terrain_res[get_loc(x,y)->terrain_id]->average_type );
  88. if( !rockRecno )
  89. continue;
  90. RockInfo *rockInfo = rock_res.get_rock_info(rockRecno);
  91. x2 = x + rockInfo->loc_width - 1;
  92. y2 = y + rockInfo->loc_height -1;
  93. if( rockInfo->valid_terrain(terrain_res[get_loc(x2, y)->terrain_id]->average_type)
  94. && rockInfo->valid_terrain(terrain_res[get_loc(x, y2)->terrain_id]->average_type)
  95. && rockInfo->valid_terrain(terrain_res[get_loc(x2,y2)->terrain_id]->average_type))
  96. {
  97. add_rock(rockRecno, x, y);
  98. // add other smaller rock
  99. for(int subTrial = m.random(14); subTrial > 0 ; --subTrial )
  100. {
  101. // sx from x-SMALL_ROCK_SIZE to x+4-1+SMALL_ROCK_SIZE
  102. short sx = x - SMALL_ROCK_SIZE - GAP + m.random( LARGE_ROCK_SIZE + SMALL_ROCK_SIZE + 2*GAP);
  103. short sy = y - SMALL_ROCK_SIZE - GAP + m.random( LARGE_ROCK_SIZE + SMALL_ROCK_SIZE + 2*GAP);
  104. short sx2 = sx + SMALL_ROCK_SIZE-1;
  105. short sy2 = sy + SMALL_ROCK_SIZE-1;
  106. if( can_add_rock( sx, sy, sx2, sy2))
  107. {
  108. short rock2Recno = rock_res.search("R", 1,SMALL_ROCK_SIZE,1,SMALL_ROCK_SIZE,-1,0,
  109. terrain_res[get_loc(sx,sy)->terrain_id]->average_type );
  110. if(!rock2Recno)
  111. continue;
  112. RockInfo *rock2Info = rock_res.get_rock_info(rock2Recno);
  113. sx2 = sx + rock2Info->loc_width -1;
  114. sy2 = sy + rock2Info->loc_height -1;
  115. if( rock2Info->valid_terrain(terrain_res[get_loc(sx2,sy)->terrain_id]->average_type)
  116. && rock2Info->valid_terrain(terrain_res[get_loc(sx,sy2)->terrain_id]->average_type)
  117. && rock2Info->valid_terrain(terrain_res[get_loc(sx2,sy2)->terrain_id]->average_type) )
  118. {
  119. add_rock(rock2Recno, sx, sy);
  120. }
  121. }
  122. }
  123. nGrouped--;
  124. }
  125. }
  126. }
  127. // generate stand-alone large rock
  128. if( nLarge > 0 )
  129. {
  130. short x = m.random( max_x_loc - HUGE_ROCK_SIZE);
  131. short y = m.random( max_y_loc - HUGE_ROCK_SIZE);
  132. short x2 = x + HUGE_ROCK_SIZE -1;
  133. short y2 = y + HUGE_ROCK_SIZE -1;
  134. if( can_add_rock( x, y, x2, y2) )
  135. {
  136. short rockRecno = rock_res.search("R", SMALL_ROCK_SIZE+1,HUGE_ROCK_SIZE,LARGE_ROCK_SIZE+1,HUGE_ROCK_SIZE,-1,0,
  137. terrain_res[get_loc(x,y)->terrain_id]->average_type );
  138. if( !rockRecno )
  139. continue;
  140. RockInfo *rockInfo = rock_res.get_rock_info(rockRecno);
  141. x2 = x + rockInfo->loc_width - 1;
  142. y2 = y + rockInfo->loc_height -1;
  143. if( rockInfo->valid_terrain(terrain_res[get_loc(x2, y)->terrain_id]->average_type)
  144. && rockInfo->valid_terrain(terrain_res[get_loc(x, y2)->terrain_id]->average_type)
  145. && rockInfo->valid_terrain(terrain_res[get_loc(x2,y2)->terrain_id]->average_type))
  146. {
  147. add_rock(rockRecno, x, y);
  148. nLarge--;
  149. }
  150. }
  151. }
  152. // generate stand-alone small rock
  153. if( nSmall > 0)
  154. {
  155. short x = m.random( max_x_loc - SMALL_ROCK_SIZE);
  156. short y = m.random( max_y_loc - SMALL_ROCK_SIZE);
  157. short x2 = x + SMALL_ROCK_SIZE -1;
  158. short y2 = y + SMALL_ROCK_SIZE -1;
  159. if( can_add_rock( x, y, x2, y2) )
  160. {
  161. short rockRecno = rock_res.search("R", 1,SMALL_ROCK_SIZE,1,SMALL_ROCK_SIZE,-1,0,
  162. terrain_res[get_loc(x,y)->terrain_id]->average_type );
  163. if( !rockRecno )
  164. continue;
  165. RockInfo *rockInfo = rock_res.get_rock_info(rockRecno);
  166. x2 = x + rockInfo->loc_width - 1;
  167. y2 = y + rockInfo->loc_height -1;
  168. if( rockInfo->valid_terrain(terrain_res[get_loc(x2, y)->terrain_id]->average_type)
  169. && rockInfo->valid_terrain(terrain_res[get_loc(x, y2)->terrain_id]->average_type)
  170. && rockInfo->valid_terrain(terrain_res[get_loc(x2,y2)->terrain_id]->average_type))
  171. {
  172. add_rock(rockRecno, x, y);
  173. nSmall--;
  174. }
  175. }
  176. }
  177. }
  178. }
  179. // --------- end of function World::gen_rocks ----------//
  180. //--------------- begin of function World::can_add_dirt ----------//
  181. int World::can_add_dirt(short x1, short y1, short x2, short y2)
  182. {
  183. int x,y;
  184. err_when(x1 > x2 || y1 > y2);
  185. for(y = y1; y <= y2; ++y)
  186. for( x = x1; x <= x2; ++x)
  187. if( !get_loc(x,y)->can_add_dirt() )
  188. return FALSE;
  189. return TRUE;
  190. }
  191. //--------------- end of function World::can_add_dirt ----------//
  192. // --------- begin of function World::add_dirt ----------//
  193. //
  194. // note : make sure the location in the area is free
  195. //
  196. void World::add_dirt(short dirtRecno, short x1, short y1)
  197. {
  198. if( dirt_array.size() >= 255 )
  199. return;
  200. // ------- get the delay remain count for the first frame -----//
  201. Rock newDirt(dirtRecno, x1, y1);
  202. int dirtArrayRecno = dirt_array.add(&newDirt);
  203. if( dirtArrayRecno >= 255) // Location::extra_para is only BYTE
  204. return;
  205. RockInfo *dirtInfo = rock_res.get_rock_info(dirtRecno);
  206. for( short dy = 0; dy < dirtInfo->loc_height && y1+dy < max_y_loc; ++dy)
  207. {
  208. for( short dx = 0; dx < dirtInfo->loc_width && x1+dx < max_x_loc; ++dx)
  209. {
  210. short dirtBlockRecno = rock_res.locate_block(dirtRecno, dx, dy);
  211. if( dirtBlockRecno )
  212. {
  213. Location *locPtr = get_loc(x1+dx, y1+dy);
  214. err_when( !locPtr->can_add_dirt() );
  215. locPtr->set_dirt(dirtArrayRecno);
  216. if( dirtInfo->rock_type == DIRT_BLOCKING_TYPE )
  217. locPtr->walkable_off();
  218. }
  219. }
  220. }
  221. }
  222. // --------- end of function World::add_rock ----------//
  223. // --------- begin of function World::gen_dirt ----------//
  224. void World::gen_dirt(int nGrouped, int nLarge, int nSmall)
  225. {
  226. // one 'large' (size 1 to 4) at the center
  227. // and a number 'small' (size 1 to 2) at the surroundings
  228. const int GAP=4;
  229. const int HUGE_ROCK_SIZE=6;
  230. const int LARGE_ROCK_SIZE=4;
  231. const int SMALL_ROCK_SIZE=2;
  232. int trial = (nGrouped + nLarge + nSmall) * 2;
  233. while( (nGrouped > 0 || nLarge > 0 || nSmall > 0) && --trial > 0)
  234. {
  235. // generate grouped dirt
  236. if( nGrouped > 0 )
  237. {
  238. short x = (GAP+SMALL_ROCK_SIZE)+m.random( max_x_loc - LARGE_ROCK_SIZE +1 - 2*(GAP+SMALL_ROCK_SIZE));
  239. short y = (GAP+SMALL_ROCK_SIZE)+m.random( max_y_loc - LARGE_ROCK_SIZE +1 - 2*(GAP+SMALL_ROCK_SIZE));
  240. short x2 = x + LARGE_ROCK_SIZE -1;
  241. short y2 = y + LARGE_ROCK_SIZE -1;
  242. if( can_add_dirt(x,y, x2,y2) )
  243. {
  244. short rockRecno = rock_res.search("DE", 1,LARGE_ROCK_SIZE,1,LARGE_ROCK_SIZE,-1,0,
  245. terrain_res[get_loc(x,y)->terrain_id]->average_type );
  246. if( !rockRecno )
  247. continue;
  248. RockInfo *rockInfo = rock_res.get_rock_info(rockRecno);
  249. x2 = x + rockInfo->loc_width - 1;
  250. y2 = y + rockInfo->loc_height -1;
  251. if( rockInfo->valid_terrain(terrain_res[get_loc(x2, y)->terrain_id]->average_type)
  252. && rockInfo->valid_terrain(terrain_res[get_loc(x, y2)->terrain_id]->average_type)
  253. && rockInfo->valid_terrain(terrain_res[get_loc(x2,y2)->terrain_id]->average_type))
  254. {
  255. add_dirt(rockRecno, x, y);
  256. // add other smaller rock
  257. for(int subTrial = m.random(14); subTrial > 0 ; --subTrial )
  258. {
  259. // sx from x-SMALL_ROCK_SIZE to x+4-1+SMALL_ROCK_SIZE
  260. short sx = x - SMALL_ROCK_SIZE - GAP + m.random( LARGE_ROCK_SIZE + SMALL_ROCK_SIZE + 2*GAP);
  261. short sy = y - SMALL_ROCK_SIZE - GAP + m.random( LARGE_ROCK_SIZE + SMALL_ROCK_SIZE + 2*GAP);
  262. short sx2 = sx + SMALL_ROCK_SIZE-1;
  263. short sy2 = sy + SMALL_ROCK_SIZE-1;
  264. if( can_add_dirt( sx, sy, sx2, sy2))
  265. {
  266. short rock2Recno = rock_res.search("DE", 1,SMALL_ROCK_SIZE,1,SMALL_ROCK_SIZE,-1,0,
  267. terrain_res[get_loc(sx,sy)->terrain_id]->average_type );
  268. if(!rock2Recno)
  269. continue;
  270. RockInfo *rock2Info = rock_res.get_rock_info(rock2Recno);
  271. sx2 = sx + rock2Info->loc_width -1;
  272. sy2 = sy + rock2Info->loc_height -1;
  273. if( rock2Info->valid_terrain(terrain_res[get_loc(sx2,sy)->terrain_id]->average_type)
  274. && rock2Info->valid_terrain(terrain_res[get_loc(sx,sy2)->terrain_id]->average_type)
  275. && rock2Info->valid_terrain(terrain_res[get_loc(sx2,sy2)->terrain_id]->average_type) )
  276. {
  277. add_dirt(rock2Recno, sx, sy);
  278. }
  279. }
  280. }
  281. nGrouped--;
  282. }
  283. }
  284. }
  285. // generate stand-alone large dirt
  286. if( nLarge > 0 )
  287. {
  288. short x = m.random( max_x_loc - HUGE_ROCK_SIZE);
  289. short y = m.random( max_y_loc - HUGE_ROCK_SIZE);
  290. short x2 = x + HUGE_ROCK_SIZE -1;
  291. short y2 = y + HUGE_ROCK_SIZE -1;
  292. if( can_add_dirt( x, y, x2, y2) )
  293. {
  294. short rockRecno = rock_res.search("DE", SMALL_ROCK_SIZE+1,HUGE_ROCK_SIZE,SMALL_ROCK_SIZE+1,HUGE_ROCK_SIZE,-1,0,
  295. terrain_res[get_loc(x,y)->terrain_id]->average_type );
  296. if( !rockRecno )
  297. continue;
  298. RockInfo *rockInfo = rock_res.get_rock_info(rockRecno);
  299. x2 = x + rockInfo->loc_width - 1;
  300. y2 = y + rockInfo->loc_height -1;
  301. if( rockInfo->valid_terrain(terrain_res[get_loc(x2, y)->terrain_id]->average_type)
  302. && rockInfo->valid_terrain(terrain_res[get_loc(x, y2)->terrain_id]->average_type)
  303. && rockInfo->valid_terrain(terrain_res[get_loc(x2,y2)->terrain_id]->average_type))
  304. {
  305. add_dirt(rockRecno, x, y);
  306. nLarge--;
  307. }
  308. }
  309. }
  310. // generate stand-alone small dirt
  311. if( nSmall > 0 )
  312. {
  313. short x = m.random( max_x_loc - SMALL_ROCK_SIZE);
  314. short y = m.random( max_y_loc - SMALL_ROCK_SIZE);
  315. short x2 = x + SMALL_ROCK_SIZE -1;
  316. short y2 = y + SMALL_ROCK_SIZE -1;
  317. if( can_add_dirt( x, y, x2, y2) )
  318. {
  319. short rockRecno = rock_res.search("DE", 1,SMALL_ROCK_SIZE,1,SMALL_ROCK_SIZE,-1,0,
  320. terrain_res[get_loc(x,y)->terrain_id]->average_type );
  321. if( !rockRecno )
  322. continue;
  323. RockInfo *rockInfo = rock_res.get_rock_info(rockRecno);
  324. x2 = x + rockInfo->loc_width - 1;
  325. y2 = y + rockInfo->loc_height -1;
  326. if( rockInfo->valid_terrain(terrain_res[get_loc(x2, y)->terrain_id]->average_type)
  327. && rockInfo->valid_terrain(terrain_res[get_loc(x, y2)->terrain_id]->average_type)
  328. && rockInfo->valid_terrain(terrain_res[get_loc(x2,y2)->terrain_id]->average_type))
  329. {
  330. add_dirt(rockRecno, x, y);
  331. nSmall--;
  332. }
  333. }
  334. }
  335. }
  336. }
  337. // --------- end of function World::gen_dirt ----------//