OPLASMA.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 : OPLASMA.CPP
  21. // Description: plasma map generator
  22. // Ownership : Gilbert
  23. #include <stdlib.h>
  24. #include <ALL.h>
  25. #include <OCONFIG.h>
  26. #include <OPLASMA.h>
  27. // ---------- define constant ----------//
  28. enum { SHIFT_VALUE=18, // shift based on no. of colors
  29. MAX_COLOR=256 // max. no. of colors
  30. };
  31. // --------- Begin of function Plasma::Plasma -----------//
  32. Plasma::Plasma()
  33. {
  34. max_x = max_y = 0;
  35. matrix = NULL;
  36. }
  37. // --------- End of function Plasma::Plasma -----------//
  38. // --------- Begin of function Plasma::~Plasma -----------//
  39. Plasma::~Plasma()
  40. {
  41. deinit();
  42. }
  43. // --------- End of function Plasma::~Plasma -----------//
  44. // --------- Begin of function Plasma::init -----------//
  45. void Plasma::init(short x, short y)
  46. {
  47. deinit();
  48. max_x = x;
  49. max_y = y;
  50. matrix = (short *) mem_add( (x+1)*(y+1)*sizeof(short) );
  51. memset( matrix, 0, (x+1)*(y+1)*sizeof(short) );
  52. }
  53. // --------- End of function Plasma::init -----------//
  54. // --------- Begin of function Plasma::deinit -----------//
  55. void Plasma::deinit()
  56. {
  57. if( matrix)
  58. mem_del(matrix);
  59. matrix = NULL;
  60. max_x = max_y = 0;
  61. }
  62. // --------- End of function Plasma::deinit -----------//
  63. // --------- Begin of function Plasma::get_pix -----------//
  64. short Plasma::get_pix(short x, short y)
  65. {
  66. return matrix[y * (max_x+1) + x];
  67. }
  68. // --------- End of function Plasma::get_pix -----------//
  69. // --------- Begin of function Plasma::plot -----------//
  70. void Plasma::plot(short x, short y, short value)
  71. {
  72. matrix[y * (max_x+1) + x] = value;
  73. }
  74. // --------- End of function Plasma::plot -----------//
  75. // --------- Begin of function Plasma::generate -----------//
  76. void Plasma::generate(int genMethod, int grainFactor, int randomSeed)
  77. {
  78. int i,k, n;
  79. U16 rnd[4];
  80. iparmx = grainFactor * 8;
  81. srand(randomSeed);
  82. for(n = 0; n < 4; n++)
  83. rnd[n] = 1+(((m.rand()/MAX_COLOR)*(MAX_COLOR-1))>>(SHIFT_VALUE-11));
  84. plot( 0, 0, rnd[0]);
  85. plot(max_x, 0, rnd[1]);
  86. plot(max_x, max_y, rnd[2]);
  87. plot( 0, max_y, rnd[3]);
  88. recur_level = 0;
  89. if ( genMethod == 0) // use original method
  90. {
  91. sub_divide(0,0,max_x,max_y);
  92. }
  93. else // use new method
  94. {
  95. recur1 = i = k = 1;
  96. while(new_sub_divide(0,0,max_x,max_y,i)==0)
  97. {
  98. k = k * 2;
  99. if (k > max_x && k > max_y)
  100. break;
  101. i++;
  102. }
  103. }
  104. }
  105. // --------- End of function Plasma::generate -----------//
  106. // --------- Begin of function Plasma::sub_divide -----------//
  107. void Plasma::sub_divide(int x1,int y1,int x2,int y2)
  108. {
  109. int x,y;
  110. S32 v,i;
  111. if(x2-x1<2 && y2-y1<2)
  112. return;
  113. recur_level++;
  114. recur1 = 320L >> recur_level;
  115. x = (x1+x2)>>1;
  116. y = (y1+y2)>>1;
  117. if((v=get_pix(x,y1)) == 0)
  118. v=adjust(x1,y1,x ,y1,x2,y1);
  119. i=v;
  120. if((v=get_pix(x2,y)) == 0)
  121. v=adjust(x2,y1,x2,y ,x2,y2);
  122. i+=v;
  123. if((v=get_pix(x,y2)) == 0)
  124. v=adjust(x1,y2,x ,y2,x2,y2);
  125. i+=v;
  126. if((v=get_pix(x1,y)) == 0)
  127. v=adjust(x1,y1,x1,y ,x1,y2);
  128. i+=v;
  129. if(get_pix(x,y) == 0)
  130. plot(x,y,(U16)((i+2)>>2));
  131. sub_divide(x1,y1,x ,y);
  132. sub_divide(x ,y1,x2,y);
  133. sub_divide(x ,y ,x2,y2);
  134. sub_divide(x1,y ,x ,y2);
  135. recur_level--;
  136. }
  137. // --------- End of function Plasma::sub_divide -----------//
  138. // --------- Begin of function Plasma::new_sub_divide -----------//
  139. int Plasma::new_sub_divide(int x1,int y1,int x2,int y2, int recur)
  140. {
  141. int x,y;
  142. int nx1;
  143. int nx;
  144. int ny1, ny;
  145. S32 i, v;
  146. struct sub
  147. {
  148. BYTE t; // top of stack
  149. int v[16]; // subdivided value
  150. BYTE r[16]; // recursion level
  151. };
  152. static struct sub subx, suby;
  153. /*
  154. recur1=1;
  155. for (i=1;i<=recur;i++)
  156. recur1 = recur1 * 2;
  157. recur1=320/recur1;
  158. */
  159. recur1 = 320L >> recur;
  160. suby.t = 2;
  161. ny = suby.v[0] = y2;
  162. ny1 = suby.v[2] = y1;
  163. suby.r[0] = suby.r[2] = 0;
  164. suby.r[1] = 1;
  165. y = suby.v[1] = (ny1 + ny) >> 1;
  166. while (suby.t >= 1)
  167. {
  168. while (suby.r[suby.t-1] < recur)
  169. {
  170. /* 1. Create new entry at top of the stack */
  171. /* 2. Copy old top value to new top value. */
  172. /* This is largest y value. */
  173. /* 3. Smallest y is now old mid point */
  174. /* 4. Set new mid point recursion level */
  175. /* 5. New mid point value is average */
  176. /* of largest and smallest */
  177. suby.t++;
  178. ny1 = suby.v[suby.t] = suby.v[suby.t-1];
  179. ny = suby.v[suby.t-2];
  180. suby.r[suby.t] = suby.r[suby.t-1];
  181. y = suby.v[suby.t-1] = (ny1 + ny) >> 1;
  182. suby.r[suby.t-1] = (int)max(suby.r[suby.t], suby.r[suby.t-2])+1;
  183. }
  184. subx.t = 2;
  185. nx = subx.v[0] = x2;
  186. nx1 = subx.v[2] = x1;
  187. subx.r[0] = subx.r[2] = 0;
  188. subx.r[1] = 1;
  189. x = subx.v[1] = (nx1 + nx) >> 1;
  190. while (subx.t >= 1)
  191. {
  192. while (subx.r[subx.t-1] < recur)
  193. {
  194. subx.t++; /* move the top ofthe stack up 1 */
  195. nx1 = subx.v[subx.t] = subx.v[subx.t-1];
  196. nx = subx.v[subx.t-2];
  197. subx.r[subx.t] = subx.r[subx.t-1];
  198. x = subx.v[subx.t-1] = (nx1 + nx) >> 1;
  199. subx.r[subx.t-1] = (int)max(subx.r[subx.t],
  200. subx.r[subx.t-2])+1;
  201. }
  202. if ((i = get_pix(nx, y)) == 0)
  203. i = adjust(nx,ny1,nx,y ,nx,ny);
  204. v = i;
  205. if ((i = get_pix(x, ny)) == 0)
  206. i = adjust(nx1,ny,x ,ny,nx,ny);
  207. v += i;
  208. if(get_pix(x,y) == 0)
  209. {
  210. if ((i = get_pix(x, ny1)) == 0)
  211. i = adjust(nx1,ny1,x ,ny1,nx,ny1);
  212. v += i;
  213. if ((i = get_pix(nx1, y)) == 0)
  214. i = adjust(nx1,ny1,nx1,y ,nx1,ny);
  215. v += i;
  216. plot(x,y,(U16)((v + 2) >> 2));
  217. }
  218. if (subx.r[subx.t-1] == recur) subx.t = subx.t - 2;
  219. }
  220. if (suby.r[suby.t-1] == recur) suby.t = suby.t - 2;
  221. }
  222. return(0);
  223. }
  224. // --------- End of function Plasma::new_sub_divide -----------//
  225. // --------- Begin of function Plasma::adjust -----------//
  226. U16 Plasma::adjust(int xa,int ya,int x,int y,int xb,int yb)
  227. {
  228. S32 pseudoRandom;
  229. pseudoRandom = ((S32)iparmx)*((m.rand()-16383));
  230. // pseudoRandom = pseudoRandom*(abs(xa-xb)+abs(ya-yb));
  231. pseudoRandom = pseudoRandom * recur1;
  232. pseudoRandom = pseudoRandom >> SHIFT_VALUE;
  233. pseudoRandom = (((S32)get_pix(xa,ya)+(S32)get_pix(xb,yb)+1)>>1)+pseudoRandom;
  234. if (pseudoRandom >= MAX_COLOR)
  235. pseudoRandom = MAX_COLOR-1;
  236. if(pseudoRandom < 1)
  237. pseudoRandom = 1;
  238. plot(x,y,(U16)pseudoRandom);
  239. return((U16)pseudoRandom);
  240. }
  241. // --------- End of function Plasma::adjust -----------//
  242. // --------- Begin of function Plasma::add_base_level -----------//
  243. void Plasma::add_base_level(short baseLevel)
  244. {
  245. //---------- adjust map baseLevel -----------//
  246. int i;
  247. int totalLoc=(max_x +1) * (max_y +1);
  248. short *locPtr;
  249. err_when( max_x == 0 || max_y == 0 || matrix == NULL);
  250. for( i=0, locPtr=matrix ; i<totalLoc ; i++, locPtr++ )
  251. {
  252. *locPtr += baseLevel;
  253. if(*locPtr < 1)
  254. *locPtr = 1;
  255. if(*locPtr >= MAX_COLOR)
  256. *locPtr = MAX_COLOR-1;
  257. }
  258. }
  259. // --------- End of function Plasma::add_base_level -----------//
  260. // --------- Begin of function Plasma::calc_tera_base_level -----------//
  261. int Plasma::calc_tera_base_level(short minHeight)
  262. {
  263. //------- count the percentage of sea and land -------//
  264. int totalLoc=(max_x+1) * (max_y+1);
  265. int i, locHeight, landCount=0, baseLevel=0;
  266. for( i=0 ; i<totalLoc ; i++ )
  267. {
  268. if( matrix[i] >= minHeight )
  269. landCount++;
  270. }
  271. // ensure that percentage of land won't be less than 1/3
  272. if( landCount < 2*totalLoc/3 ) // if land is less than 2/3 of the map
  273. baseLevel = 50L * (totalLoc-landCount) / totalLoc;
  274. //-------- ensure availability of sea in the map -----------//
  275. int xLoc, yLoc, seaCount=0, lowestGrassHeight=0xFFFF;
  276. err_when(max_x == 0 || max_y == 0);
  277. //---------- scan top & bottom side -----------//
  278. for( xLoc=0 ; xLoc<=max_x ; xLoc++ )
  279. {
  280. //----------- top side ------------//
  281. locHeight = get_pix(xLoc,0) + baseLevel;
  282. if( locHeight < minHeight ) // it's a sea
  283. seaCount++;
  284. else
  285. {
  286. if( locHeight < lowestGrassHeight )
  287. lowestGrassHeight = locHeight;
  288. }
  289. //--------- bottom side -----------//
  290. locHeight = get_pix(xLoc,max_y) + baseLevel;
  291. if( locHeight < minHeight ) // it's a sea
  292. seaCount++;
  293. else
  294. {
  295. if( locHeight < lowestGrassHeight )
  296. lowestGrassHeight = locHeight;
  297. }
  298. }
  299. //---------- scan left & right side -----------//
  300. for( yLoc=0 ; yLoc<=max_y ; yLoc++ )
  301. {
  302. //----------- top side ------------//
  303. locHeight = get_pix(0,yLoc) + baseLevel;
  304. if( locHeight < minHeight) // it's a sea
  305. seaCount++;
  306. else
  307. {
  308. if( locHeight < lowestGrassHeight )
  309. lowestGrassHeight = locHeight;
  310. }
  311. //--------- bottom side -----------//
  312. locHeight = get_pix(max_x,yLoc) + baseLevel;
  313. if( locHeight < minHeight ) // it's a sea
  314. seaCount++;
  315. else
  316. {
  317. if( locHeight < lowestGrassHeight )
  318. lowestGrassHeight = locHeight;
  319. }
  320. }
  321. //------- If there is not enough sea --------//
  322. static short min_sea_count_array[] = { 1600, 700, 200 };
  323. int minSeaCount = min_sea_count_array[config.land_mass-OPTION_LOW];
  324. if( seaCount < minSeaCount )
  325. baseLevel -= lowestGrassHeight - minHeight + (minSeaCount-seaCount) / 15;
  326. return baseLevel;
  327. }
  328. // --------- End of function Plasma::calc_tera_base_level -----------//
  329. // --------- Begin of function Plasma::stat -----------//
  330. //
  331. // classify heights into groups and count the frequency of
  332. // each group
  333. //
  334. // <int> groups (input) no. of groups
  335. // <short *> minHeights (input) lower limit of each group, must be strictly increasing
  336. // <int *> freq (output) frequency of each group
  337. // <int> return value total frequency
  338. //
  339. int Plasma::stat(int groups, short *minHeights, int *freq)
  340. {
  341. int total = (max_x+1)*(max_y+1);
  342. int g;
  343. // -------- initialize freq count to zero ------//
  344. for( g = groups-1; g >= 0; --g)
  345. freq[g] = 0;
  346. // -------- classify and increase counter -------//
  347. for( int i = 0; i < total; ++i)
  348. {
  349. short v = matrix[i];
  350. for( g = groups-1; g >= 0; --g)
  351. {
  352. if( v >= minHeights[g])
  353. {
  354. freq[g]++;
  355. break;
  356. }
  357. }
  358. }
  359. return total;
  360. }
  361. // --------- End of function Plasma::stat -----------//
  362. // --------- Begin of function Plasma::generate2 -----------//
  363. //
  364. // not initialize the corners, so some points should be
  365. // initialized before calling this function
  366. //
  367. void Plasma::generate2(int genMethod, int grainFactor, int randomSeed)
  368. {
  369. int i,k;
  370. iparmx = grainFactor * 8;
  371. srand(randomSeed);
  372. recur_level = 0;
  373. if ( genMethod == 0) // use original method
  374. {
  375. sub_divide(0,0,max_x,max_y);
  376. }
  377. else // use new method
  378. {
  379. recur1 = i = k = 1;
  380. while(new_sub_divide(0,0,max_x,max_y,i)==0)
  381. {
  382. k = k * 2;
  383. if (k > max_x && k > max_y)
  384. break;
  385. i++;
  386. }
  387. }
  388. }
  389. // --------- End of function Plasma::generate2 -----------//
  390. // --------- begin of function Plasma::shuffle_level --------//
  391. // change height of a point, if this point and 8 point around it is within
  392. // minHeight and maxHeight
  393. void Plasma::shuffle_level(short minHeight, short maxHeight, short amplitude)
  394. {
  395. // don't change boundary points
  396. int x,y;
  397. err_when( maxHeight - minHeight < 5 );
  398. for( y = 1; y < max_y; ++y)
  399. {
  400. for( x = 1; x < max_x; ++x)
  401. {
  402. short h;
  403. if( (h = get_pix(x,y)) >= minHeight && h <= maxHeight
  404. && (h = get_pix(x-1,y-1)) >= minHeight && h <= maxHeight
  405. && (h = get_pix(x,y-1)) >= minHeight && h <= maxHeight
  406. && (h = get_pix(x+1,y-1)) >= minHeight && h <= maxHeight
  407. && (h = get_pix(x-1,y)) >= minHeight && h <= maxHeight
  408. && (h = get_pix(x+1,y)) >= minHeight && h <= maxHeight
  409. && (h = get_pix(x-1,y+1)) >= minHeight && h <= maxHeight
  410. && (h = get_pix(x,y+1)) >= minHeight && h <= maxHeight
  411. && (h = get_pix(x+1,y+1)) >= minHeight && h <= maxHeight
  412. )
  413. {
  414. h = get_pix(x,y);
  415. // method 1 - random amplitude
  416. //h += m.random(amplitude*2+1) - amplitude;
  417. //if( h > maxHeight )
  418. // h = maxHeight;
  419. //if( h < minHeight )
  420. // h = minHeight;
  421. // method 2 - folding (amplitude : +1, +3, +5... or -1, -3, -5...)
  422. if( amplitude >= 0)
  423. h = minHeight + (h - minHeight) * amplitude;
  424. else
  425. h = maxHeight + (maxHeight - h) * amplitude;
  426. int loopCount = 20;
  427. while( h < minHeight || h > maxHeight )
  428. {
  429. err_when( --loopCount <= 0 );
  430. if( h > maxHeight )
  431. {
  432. h = maxHeight - (h - maxHeight);
  433. }
  434. else if( h < minHeight )
  435. {
  436. h = minHeight + (minHeight - h);
  437. }
  438. }
  439. plot(x, y, h);
  440. }
  441. }
  442. }
  443. }
  444. // --------- end of function Plasma::shuffle_level --------//