OGENMAP.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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 : OGENMAP.CPP
  21. //Description : World Map generation function part 1
  22. //Ownership : Gilbert
  23. #include <time.h>
  24. #include <stdlib.h>
  25. #include <ALL.h>
  26. #include <OGAME.h>
  27. #include <OVGA.h>
  28. #include <OTERRAIN.h>
  29. #include <OWORLD.h>
  30. #include <OPLASMA.h>
  31. #include <OREGION.h>
  32. #include <OFIRMID.h>
  33. //-------- Begin of function World::generate_map ----------//
  34. //
  35. void World::generate_map()
  36. {
  37. const int dispProgress = 1;
  38. const int maxGenMapSteps = 14;
  39. vga_front.unlock_buf();
  40. int curGenMapSteps = 0;
  41. if( dispProgress )
  42. {
  43. vga_front.lock_buf();
  44. game.disp_gen_map_status( curGenMapSteps, maxGenMapSteps, 0 );
  45. vga_front.unlock_buf();
  46. }
  47. //--- loc_matrix, first store terrain height, then world map icon id ---//
  48. loc_matrix = (Location*) mem_resize( loc_matrix, MAX_WORLD_X_LOC * MAX_WORLD_Y_LOC * sizeof(Location) );
  49. max_x_loc = MAX_WORLD_X_LOC;
  50. max_y_loc = MAX_WORLD_Y_LOC;
  51. /*
  52. #ifdef DEBUG
  53. // testing the completeness of Ocean-DarkGrass joint //
  54. static TerrainTypeCode ta[6] = { TERRAIN_OCEAN,TERRAIN_OCEAN,TERRAIN_OCEAN,
  55. TERRAIN_DARK_GRASS,TERRAIN_DARK_GRASS,TERRAIN_DARK_GRASS };
  56. static SubTerrainMask sta[6] = { BOTTOM_MASK, MIDDLE_MASK, TOP_MASK,
  57. BOTTOM_MASK, MIDDLE_MASK, TOP_MASK };
  58. int failure = 0;
  59. int nw, ne, sw, se;
  60. for( nw = 0; nw < 6; ++nw)
  61. for( ne = 0; ne < 6; ++ne)
  62. for( sw = 0; sw < 6; ++sw)
  63. for( se = 0; se < 6; ++se)
  64. {
  65. if(!terrain_res.scan( ta[nw], sta[nw], ta[ne], sta[ne],
  66. ta[sw], sta[sw], ta[se], sta[se], 1,0,0))
  67. {
  68. TerrainTypeCode nwType=ta[nw], neType=ta[ne], swType=ta[sw], seType=ta[se];
  69. SubTerrainMask nwMask=sta[nw], neMask=sta[ne], swMask=sta[sw], seMask=sta[se];
  70. failure++;
  71. };
  72. }
  73. #endif
  74. */
  75. //----------- start generating -----------//
  76. // ---------- generate plasma map ----------//
  77. Plasma heightMap;
  78. memset( loc_matrix , 0, sizeof(Location) * MAX_WORLD_X_LOC * MAX_WORLD_Y_LOC );
  79. heightMap.init(max_x_loc, max_y_loc);
  80. heightMap.generate( m.random(2), 5, m.rand() );
  81. curGenMapSteps++; // 1
  82. if( dispProgress )
  83. {
  84. vga_front.lock_buf();
  85. game.disp_gen_map_status( curGenMapSteps, maxGenMapSteps, 0 );
  86. vga_front.unlock_buf();
  87. }
  88. // ###### begin Gilbert 27/8 ########//
  89. // ---------- add base level --------//
  90. // heightMap.add_base_level(heightMap.calc_tera_base_level(TerrainRes::min_height(TERRAIN_DARK_GRASS)));
  91. // grouping plasma sample data, find sea or land first
  92. int totalLoc = (max_x_loc+1) * (max_y_loc+1);
  93. short heightLimit[2];
  94. int heightFreq[2];
  95. int minLandCount, maxLandCount;
  96. int initHeightLimit = TerrainRes::min_height(TERRAIN_DARK_GRASS);
  97. switch(config.land_mass)
  98. {
  99. case OPTION_LOW:
  100. minLandCount = totalLoc *4/10;
  101. maxLandCount = totalLoc *6/10;
  102. break;
  103. case OPTION_MODERATE:
  104. minLandCount = totalLoc *6/10;
  105. maxLandCount = totalLoc *8/10;
  106. break;
  107. case OPTION_HIGH:
  108. minLandCount = totalLoc *8 /10;
  109. maxLandCount = totalLoc;
  110. break;
  111. default:
  112. err_here();
  113. }
  114. int avgLandCount = (minLandCount + maxLandCount) /2;
  115. heightLimit[0] = 0;
  116. heightLimit[1] = initHeightLimit;
  117. heightMap.stat(2, heightLimit, heightFreq);
  118. int& landCount = heightFreq[1];
  119. int& seaCount = heightFreq[0];
  120. int loopCount = 0;
  121. while( ++loopCount <= 4 && (landCount<minLandCount || landCount>maxLandCount) )
  122. {
  123. if( landCount < minLandCount )
  124. {
  125. // positive add_base_level to gain more land
  126. // find a level between 0 to TerrainRes::min_height(TERRAIN_DARK_GRASS)
  127. // assume heightlevel below heightLimit[1] is evenly distributed,
  128. // approximate a new heightLimit[1] such that landCount is avgLandCount
  129. // (heightLimit[1] - newheightLimit[1]) * seaCount / (heightLimit[1] - heightLimit[0]) + landCount = avgLandCount
  130. heightLimit[1] = heightLimit[1] - (avgLandCount - landCount) * (heightLimit[1] - heightLimit[0]) / seaCount;
  131. }
  132. else if( landCount > maxLandCount )
  133. {
  134. // negative add_base_level to reduce land
  135. // find a level above TerrainRes::min_height(TERRAIN_DARK_GRASS)
  136. // assume heightlevel above heightLimit[1] is evenly distributed,
  137. // approximate a new heightLimit[1] such that landCount is avgLandCount
  138. const int maxHeightLimit = 255;
  139. // landCount * (maxHeightLimit - newheightLimit[1])/ (maxHeightLimit - heightLimit[1]) = avgLandCount
  140. heightLimit[1] = maxHeightLimit - avgLandCount * (maxHeightLimit - heightLimit[1]) / landCount;
  141. }
  142. heightMap.stat(2, heightLimit, heightFreq);
  143. }
  144. if( abs( heightLimit[1] - initHeightLimit ) > 2 )
  145. {
  146. heightMap.add_base_level(initHeightLimit - heightLimit[1]);
  147. }
  148. curGenMapSteps++; // 2
  149. if( dispProgress )
  150. {
  151. vga_front.lock_buf();
  152. game.disp_gen_map_status( curGenMapSteps, maxGenMapSteps, 0 );
  153. vga_front.unlock_buf();
  154. }
  155. // ###### end Gilbert 27/8 ########//
  156. // --------- remove odd terrain --------//
  157. for(short y = 0; y <= heightMap.max_y; ++y)
  158. for(short x = 0; x <= heightMap.max_x; ++x)
  159. {
  160. remove_odd(heightMap, x, y, 5);
  161. }
  162. curGenMapSteps++; // 3
  163. if( dispProgress )
  164. {
  165. vga_front.lock_buf();
  166. game.disp_gen_map_status( curGenMapSteps, maxGenMapSteps, 0 );
  167. vga_front.unlock_buf();
  168. }
  169. // ------------ shuffle sub-terrain level ---------//
  170. heightMap.shuffle_level(TerrainRes::min_height(TERRAIN_OCEAN),
  171. TerrainRes::max_height(TERRAIN_OCEAN), -3 );
  172. heightMap.shuffle_level(TerrainRes::min_height(TERRAIN_DARK_GRASS),
  173. TerrainRes::max_height(TERRAIN_DARK_GRASS), 3 );
  174. heightMap.shuffle_level(TerrainRes::min_height(TERRAIN_LIGHT_GRASS),
  175. TerrainRes::max_height(TERRAIN_LIGHT_GRASS), 3 );
  176. heightMap.shuffle_level(TerrainRes::min_height(TERRAIN_DARK_DIRT),
  177. TerrainRes::max_height(TERRAIN_DARK_DIRT), 3 );
  178. curGenMapSteps++; // 4
  179. if( dispProgress )
  180. {
  181. vga_front.lock_buf();
  182. game.disp_gen_map_status( curGenMapSteps, maxGenMapSteps, 0 );
  183. vga_front.unlock_buf();
  184. }
  185. set_tera_id(heightMap);
  186. curGenMapSteps++; // 5
  187. if( dispProgress )
  188. {
  189. vga_front.lock_buf();
  190. game.disp_gen_map_status( curGenMapSteps, maxGenMapSteps, 0 );
  191. vga_front.unlock_buf();
  192. }
  193. substitute_pattern();
  194. curGenMapSteps++; // 6
  195. if( dispProgress )
  196. {
  197. vga_front.lock_buf();
  198. game.disp_gen_map_status( curGenMapSteps, maxGenMapSteps, 0 );
  199. vga_front.unlock_buf();
  200. }
  201. set_loc_flags();
  202. //--------- assign the map --------//
  203. assign_map();
  204. curGenMapSteps++; // 7
  205. if( dispProgress )
  206. {
  207. vga_front.lock_buf();
  208. game.disp_gen_map_status( curGenMapSteps, maxGenMapSteps, 0 );
  209. vga_front.unlock_buf();
  210. }
  211. gen_hills(TERRAIN_DARK_DIRT);
  212. curGenMapSteps++; // 8
  213. if( dispProgress )
  214. {
  215. vga_front.lock_buf();
  216. game.disp_gen_map_status( curGenMapSteps, maxGenMapSteps, 0 );
  217. vga_front.unlock_buf();
  218. }
  219. set_region_id();
  220. curGenMapSteps++; // 9
  221. if( dispProgress )
  222. {
  223. vga_front.lock_buf();
  224. game.disp_gen_map_status( curGenMapSteps, maxGenMapSteps, 0 );
  225. vga_front.unlock_buf();
  226. }
  227. gen_dirt(40,30,60);
  228. curGenMapSteps++; // 10
  229. if( dispProgress )
  230. {
  231. vga_front.lock_buf();
  232. game.disp_gen_map_status( curGenMapSteps, maxGenMapSteps, 0 );
  233. vga_front.unlock_buf();
  234. }
  235. gen_rocks(5,10,30);
  236. curGenMapSteps++; // 11
  237. if( dispProgress )
  238. {
  239. vga_front.lock_buf();
  240. game.disp_gen_map_status( curGenMapSteps, maxGenMapSteps, 0 );
  241. vga_front.unlock_buf();
  242. }
  243. set_harbor_bit();
  244. curGenMapSteps++; // 12
  245. if( dispProgress )
  246. {
  247. vga_front.lock_buf();
  248. game.disp_gen_map_status( curGenMapSteps, maxGenMapSteps, 0 );
  249. vga_front.unlock_buf();
  250. }
  251. plant_init();
  252. curGenMapSteps++; // 13
  253. if( dispProgress )
  254. {
  255. vga_front.lock_buf();
  256. game.disp_gen_map_status( curGenMapSteps, maxGenMapSteps, 0 );
  257. vga_front.unlock_buf();
  258. }
  259. init_fire();
  260. curGenMapSteps++; // 14
  261. if( dispProgress )
  262. {
  263. vga_front.lock_buf();
  264. game.disp_gen_map_status( curGenMapSteps, maxGenMapSteps, 0 );
  265. vga_front.unlock_buf();
  266. }
  267. /*
  268. // randomly put a fire
  269. Location *locPtr;
  270. do
  271. {
  272. locPtr = zoom_matrix->get_loc(m.random(MAX_MAP_WIDTH), m.random(MAX_MAP_HEIGHT));
  273. if( locPtr->flammability > 0)
  274. {
  275. locPtr->fire_level = 80;
  276. break;
  277. }
  278. } while(1);
  279. */
  280. vga_front.lock_buf();
  281. //----- debug code: validate terrain_id -----//
  282. #ifdef DEBUG
  283. Location* locPtr = loc_matrix;
  284. for( int i=0 ; i<MAX_WORLD_X_LOC*MAX_WORLD_Y_LOC ; i++, locPtr++ )
  285. {
  286. err_when( locPtr->terrain_id < 1 ||
  287. locPtr->terrain_id > terrain_res.terrain_count );
  288. }
  289. #endif
  290. }
  291. //---------- End of function World::generate_map ------------//
  292. //---------- Begin of function World::set_tera_id -----------//
  293. //
  294. // Set terrain icon id
  295. //
  296. void World::set_tera_id(Plasma &plasma)
  297. {
  298. //------- create a world map based on the terrain map ------//
  299. memset(loc_matrix, 0, sizeof(Location)*max_x_loc*max_y_loc);
  300. for( int y = 0; y < max_y_loc; ++y)
  301. {
  302. for( int x = 0; x < max_x_loc; ++x)
  303. {
  304. int nwType, neType, swType, seType;
  305. int nwSubType, neSubType, swSubType, seSubType;
  306. nwType = TerrainRes::terrain_height(plasma.get_pix(x,y), &nwSubType);
  307. neType = TerrainRes::terrain_height(plasma.get_pix(x+1,y), &neSubType);
  308. swType = TerrainRes::terrain_height(plasma.get_pix(x,y+1), &swSubType);
  309. seType = TerrainRes::terrain_height(plasma.get_pix(x+1,y+1), &seSubType);
  310. if((get_loc(x,y)->terrain_id = terrain_res.scan( nwType, nwSubType,
  311. neType, neSubType, swType, swSubType, seType, seSubType ,0,1,0)) == 0)
  312. {
  313. err.run("Error World::set_tera_id, Cannot find terrain type %d:%d, %d:%d, %d:%d, %d:%d",
  314. nwType, nwSubType, neType, neSubType, swType, swSubType,
  315. seType, seSubType);
  316. }
  317. }
  318. }
  319. }
  320. //---------- End of function World::set_tera_id -----------//
  321. //---------- Begin of function World::set_loc_flags -----------//
  322. //
  323. void World::set_loc_flags()
  324. {
  325. int i;
  326. int totalLoc=MAX_WORLD_X_LOC*MAX_WORLD_Y_LOC;
  327. Location* locPtr=loc_matrix;
  328. //----- set power_off of the map edges -----//
  329. for( int xLoc=0 ; xLoc<MAX_WORLD_X_LOC ; xLoc++ ) // set the top and bottom edges
  330. {
  331. get_loc(xLoc, 0)->set_power_off();
  332. get_loc(xLoc, MAX_WORLD_Y_LOC-1)->set_power_off();
  333. }
  334. for( int yLoc=0 ; yLoc<MAX_WORLD_Y_LOC ; yLoc++ ) // set the left and right edges
  335. {
  336. get_loc(0, yLoc)->set_power_off();
  337. get_loc(MAX_WORLD_X_LOC-1, yLoc)->set_power_off();
  338. }
  339. //-----------------------------------------//
  340. if( config.explore_whole_map )
  341. {
  342. for( i=0 ; i<totalLoc ; i++, locPtr++ )
  343. {
  344. //------- set explored flag ----------//
  345. locPtr->explored_on();
  346. if( terrain_res[locPtr->terrain_id]->is_coast() )
  347. {
  348. locPtr->loc_flag |= LOCATE_COAST;
  349. if(terrain_res[locPtr->terrain_id]->average_type!=TERRAIN_OCEAN)
  350. locPtr->set_power_off();
  351. else
  352. set_surr_power_off(i%MAX_WORLD_X_LOC, i/MAX_WORLD_X_LOC);
  353. }
  354. locPtr->walkable_reset();
  355. }
  356. }
  357. else
  358. {
  359. for( i=0 ; i<totalLoc ; i++, locPtr++ )
  360. {
  361. //------- clear explored flag ----------//
  362. locPtr->explored_off();
  363. if( terrain_res[locPtr->terrain_id]->is_coast() )
  364. {
  365. locPtr->loc_flag |= LOCATE_COAST;
  366. if(terrain_res[locPtr->terrain_id]->average_type!=TERRAIN_OCEAN)
  367. locPtr->set_power_off();
  368. else
  369. set_surr_power_off(i%MAX_WORLD_X_LOC, i/MAX_WORLD_X_LOC);
  370. }
  371. locPtr->walkable_reset();
  372. }
  373. }
  374. }
  375. //---------- End of function World::set_loc_flags -----------//
  376. //---------- Begin of function World::remove_odd --------//
  377. void World::remove_odd(Plasma &plasma, short x, short y, short recur)
  378. {
  379. if( recur < 0)
  380. return;
  381. // -------- compare the TerrainTypeCode of four adjacent square ------//
  382. int center = TerrainRes::terrain_height(plasma.get_pix(x,y));
  383. int same = 0;
  384. int diff = 0;
  385. short diffTerrain = -1;
  386. short diffHeight;
  387. short sameX, sameY;
  388. // ------- compare north square -------//
  389. if( y > 0)
  390. {
  391. if( center == TerrainRes::terrain_height(plasma.get_pix(x,y-1)) )
  392. {
  393. same++;
  394. sameX = x; sameY = y-1;
  395. }
  396. else
  397. {
  398. diff++;
  399. if( diffTerrain < 0)
  400. {
  401. // new diffHeight
  402. diffHeight = plasma.get_pix(x,y-1);
  403. diffTerrain = TerrainRes::terrain_height(diffHeight);
  404. }
  405. else
  406. {
  407. // three terrain types are close, don't change anything
  408. if( diffTerrain != TerrainRes::terrain_height(plasma.get_pix(x,y-1)))
  409. return;
  410. }
  411. }
  412. }
  413. // ------- compare south square -------//
  414. if( y < plasma.max_y)
  415. {
  416. if( center == TerrainRes::terrain_height(plasma.get_pix(x,y+1)) )
  417. {
  418. same++;
  419. sameX = x; sameY = y+1;
  420. }
  421. else
  422. {
  423. diff++;
  424. if( diffTerrain < 0)
  425. {
  426. // new diffHeight
  427. diffHeight = plasma.get_pix(x,y+1);
  428. diffTerrain = TerrainRes::terrain_height(diffHeight);
  429. }
  430. else
  431. {
  432. // three terrain types are close, don't change anything
  433. if( diffTerrain != TerrainRes::terrain_height(plasma.get_pix(x,y+1)))
  434. return;
  435. }
  436. }
  437. }
  438. // ------- compare west square -------//
  439. if( x > 0)
  440. {
  441. if( center == TerrainRes::terrain_height(plasma.get_pix(x-1,y)) )
  442. {
  443. same++;
  444. sameX = x-1; sameY = y;
  445. }
  446. else
  447. {
  448. diff++;
  449. if( diffTerrain < 0)
  450. {
  451. // new diffHeight
  452. diffHeight = plasma.get_pix(x-1,y);
  453. diffTerrain = TerrainRes::terrain_height(diffHeight);
  454. }
  455. else
  456. {
  457. // three terrain types are close, don't change anything
  458. if( diffTerrain != TerrainRes::terrain_height(plasma.get_pix(x-1,y)))
  459. return;
  460. }
  461. }
  462. }
  463. // ------- compare east square -------//
  464. if( x < plasma.max_x)
  465. {
  466. if( center == TerrainRes::terrain_height(plasma.get_pix(x+1,y)) )
  467. {
  468. same++;
  469. sameX = x+1; sameY = y;
  470. }
  471. else
  472. {
  473. diff++;
  474. if( diffTerrain < 0)
  475. {
  476. // new diffHeight
  477. diffHeight = plasma.get_pix(x+1,y);
  478. diffTerrain = TerrainRes::terrain_height(diffHeight);
  479. }
  480. else
  481. {
  482. // three terrain types are close, don't change anything
  483. if( diffTerrain != TerrainRes::terrain_height(plasma.get_pix(x+1,y)))
  484. return;
  485. }
  486. }
  487. }
  488. if( same <= 1 && diff >= 2)
  489. {
  490. // flatten
  491. plasma.plot(x,y, diffHeight);
  492. // propagate to next square
  493. if( same == 1)
  494. {
  495. remove_odd(plasma, sameX, sameY, recur-1);
  496. }
  497. }
  498. }
  499. //---------- End of function World::remove_odd --------//
  500. //---------- Begin of function World::substitute_pattern -----//
  501. void World::substitute_pattern()
  502. {
  503. short terrainId;
  504. int SubFound;
  505. const unsigned int resultArraySize = 20;
  506. TerrainSubInfo *candSub[resultArraySize];
  507. for( short y = 0; y < max_y_loc; ++y)
  508. {
  509. for( short x = 0; x < max_x_loc; ++x)
  510. {
  511. terrainId = get_loc(x,y)->terrain_id;
  512. SubFound = terrain_res.search_pattern(
  513. terrain_res[terrainId]->pattern_id, candSub, resultArraySize);
  514. for( int i = 0; i < SubFound; ++i)
  515. {
  516. short tx = x, ty = y;
  517. char flag = 1;
  518. TerrainSubInfo *terrainSubInfo = candSub[i];
  519. // ----- test if a substitution matches
  520. for(terrainSubInfo = candSub[i]; terrainSubInfo != NULL;
  521. terrainSubInfo = terrainSubInfo->next_step)
  522. {
  523. if( tx < 0 || tx >= max_x_loc || ty < 0 || ty >= max_y_loc ||
  524. terrain_res[get_loc(tx,ty)->terrain_id]->pattern_id
  525. != terrainSubInfo->old_pattern_id)
  526. {
  527. flag = 0;
  528. break;
  529. }
  530. // ----- update tx, ty according to post_move -----//
  531. switch(terrainSubInfo->post_move)
  532. {
  533. case 1: ty--; break; // North
  534. case 2: ty--; tx++; break; // NE
  535. case 3: tx++; break; // East
  536. case 4: tx++; ty++; break; // SE
  537. case 5: ty++; break; // South
  538. case 6: ty++; tx--; break; // SW
  539. case 7: tx--; break; // West
  540. case 8: tx--; ty--; break; // NW
  541. }
  542. }
  543. // ------ replace pattern -------//
  544. if(flag)
  545. {
  546. tx = x; ty = y;
  547. for(terrainSubInfo = candSub[i]; terrainSubInfo != NULL;
  548. terrainSubInfo = terrainSubInfo->next_step)
  549. {
  550. TerrainInfo *oldTerrain = terrain_res[get_loc(tx,ty)->terrain_id];
  551. if( !(get_loc(tx,ty)->terrain_id = terrain_res.scan(oldTerrain->average_type,
  552. oldTerrain->secondary_type + terrainSubInfo->sec_adj,
  553. terrainSubInfo->new_pattern_id, 0,1,0) ))
  554. {
  555. err_here(); // cannot find terrain_id
  556. }
  557. // ----- update tx, ty according to post_move -----//
  558. switch(terrainSubInfo->post_move)
  559. {
  560. case 1: ty--; break; // North
  561. case 2: ty--; tx++; break; // NE
  562. case 3: tx++; break; // East
  563. case 4: tx++; ty++; break; // SE
  564. case 5: ty++; break; // South
  565. case 6: ty++; tx--; break; // SW
  566. case 7: tx--; break; // West
  567. case 8: tx--; ty--; break; // NW
  568. }
  569. }
  570. break;
  571. }
  572. }
  573. }
  574. }
  575. }
  576. //---------- End of function World::substitute_pattern -----//
  577. //---------- Begin of function World::set_region_id -----//
  578. // must be called before any mountain or buildings on the map
  579. static RegionType walkable; // to save stack space
  580. static unsigned char regionId;
  581. void World::set_region_id()
  582. {
  583. int i,x,y;
  584. int totalLoc=max_x_loc * max_y_loc;
  585. Location* locPtr=loc_matrix;
  586. // -------- reset region_id to zero
  587. for( i=0 ; i<totalLoc ; i++, locPtr++ )
  588. {
  589. locPtr->region_id = 0;
  590. }
  591. regionId = 0;
  592. for( y = 0; y < max_y_loc; ++y)
  593. {
  594. locPtr = get_loc(0,y);
  595. for( x = 0; x < max_x_loc; ++x, ++locPtr)
  596. {
  597. if( !locPtr->region_id && locPtr->region_type() != REGION_INPASSABLE)
  598. {
  599. walkable = locPtr->region_type();
  600. ++regionId;
  601. fill_region(x,y);
  602. err_when( regionId == 255);
  603. }
  604. }
  605. }
  606. region_array.init(regionId);
  607. // ------ update adjacency information and region area ------//
  608. regionId = 0;
  609. for( y = 0; y < max_y_loc; ++y)
  610. {
  611. locPtr = get_loc(0,y);
  612. for( x = 0; x < max_x_loc; ++x, ++locPtr)
  613. {
  614. int thisRegionId = locPtr->region_id;
  615. // #### begin Gilbert 19/2 ######//
  616. if( thisRegionId > 0)
  617. {
  618. region_array.inc_size( thisRegionId );
  619. }
  620. // #### end Gilbert 19/2 ######//
  621. if( thisRegionId > regionId)
  622. {
  623. if(thisRegionId == regionId+1)
  624. regionId++;
  625. region_array.set_region( thisRegionId, locPtr->region_type());
  626. }
  627. int adjRegionId;
  628. if( y > 0)
  629. {
  630. if( x > 0 && (adjRegionId = get_loc(x-1,y-1)->region_id) < thisRegionId )
  631. region_array.set_adjacent( thisRegionId, adjRegionId);
  632. if( (adjRegionId = get_loc(x,y-1)->region_id) < thisRegionId )
  633. region_array.set_adjacent( thisRegionId, adjRegionId);
  634. if( x < max_x_loc-1 && (adjRegionId = get_loc(x+1,y-1)->region_id) < thisRegionId )
  635. region_array.set_adjacent( thisRegionId, adjRegionId);
  636. }
  637. if( x > 0 && (adjRegionId = get_loc(x-1,y)->region_id) < thisRegionId )
  638. region_array.set_adjacent( thisRegionId, adjRegionId);
  639. if( x < max_x_loc-1 && (adjRegionId = get_loc(x+1,y)->region_id) < thisRegionId )
  640. region_array.set_adjacent( thisRegionId, adjRegionId);
  641. if( y < max_y_loc-1)
  642. {
  643. if( x > 0 && (adjRegionId = get_loc(x-1,y+1)->region_id) < thisRegionId )
  644. region_array.set_adjacent( thisRegionId, adjRegionId);
  645. if( (adjRegionId = get_loc(x,y+1)->region_id) < thisRegionId )
  646. region_array.set_adjacent( thisRegionId, adjRegionId);
  647. if( x < max_x_loc-1 && (adjRegionId = get_loc(x+1,y+1)->region_id) < thisRegionId )
  648. region_array.set_adjacent( thisRegionId, adjRegionId);
  649. }
  650. }
  651. }
  652. //---- sort the region after setting its size ----//
  653. region_array.sort_region();
  654. //-------- initialize region_stat_array ----------//
  655. region_array.init_region_stat();
  656. }
  657. //---------- End of function World::set_region_id -----//
  658. //---------- Begin of function World::fill_region -----//
  659. void World::fill_region(short x, short y)
  660. {
  661. err_when( x < 0 || x >= max_x_loc || y < 0 || y >= max_y_loc);
  662. short left, right;
  663. // Location *locPtr;
  664. // extent x to left and right
  665. for( left = x; left >= 0 && !get_loc(left,y)->region_id && get_loc(left,y)->region_type() == walkable; --left)
  666. {
  667. get_loc(left,y)->region_id = regionId;
  668. }
  669. ++left;
  670. for( right=x+1; right < max_x_loc && !get_loc(right,y)->region_id && get_loc(right,y)->region_type() == walkable; ++right)
  671. {
  672. get_loc(right,y)->region_id = regionId;
  673. }
  674. --right;
  675. // ------- scan line below ---------//
  676. y++;
  677. if( y < max_y_loc )
  678. {
  679. for( x = left>0?left-1:0 ; x <= right+1 && x < max_x_loc; ++x )
  680. {
  681. if( !get_loc(x,y)->region_id && get_loc(x,y)->region_type() == walkable)
  682. {
  683. fill_region(x,y);
  684. }
  685. }
  686. }
  687. // ------- scan line above -------- //
  688. y -= 2;
  689. if( y >= 0)
  690. {
  691. for( x = left>0?left-1:0 ; x <= right+1 && x < max_x_loc; ++x )
  692. {
  693. if( !get_loc(x,y)->region_id && get_loc(x,y)->region_type() == walkable)
  694. {
  695. fill_region(x,y);
  696. }
  697. }
  698. }
  699. }
  700. //---------- End of function World::fill_region -----//
  701. //---------- Begin of function World::set_harbor_bit -----//
  702. void World::set_harbor_bit()
  703. {
  704. // a pass during genmap to set LOCATE_HARBOR_BIT
  705. // notice this bit is only a necessary condition to build harbor
  706. int x,y;
  707. Location *locPtr;
  708. for( y = 0; y < max_y_loc-2; ++y)
  709. {
  710. locPtr = get_loc(0,y);
  711. for( x = 0; x < max_x_loc-2; ++x, ++locPtr)
  712. {
  713. if( can_build_firm(x, y, FIRM_HARBOR))
  714. {
  715. locPtr->set_harbor_bit();
  716. }
  717. }
  718. }
  719. }
  720. //---------- End of function World::set_harbor_bit -----//