vsand_grid.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /**
  2. * Copyright (c) 2010 Nokia Corporation.
  3. */
  4. #include <math.h>
  5. #include "TMath.h"
  6. #include <memory.h>
  7. #include <stdlib.h>
  8. #include "vsand_grid.h"
  9. #define SELECTIVE_SOFTEN_EPSILON 6000 // 1600 was working quite nicely
  10. CVSand_Grid::CVSand_Grid( int w, int h ) {
  11. m_width = w;
  12. m_height = h;
  13. m_smap = new SVSandNode[ m_width * m_height ];
  14. m_maxFixedWidth = ((m_width-2)<<16)+65535;
  15. m_maxFixedHeight = ((m_height-2)<<16)+65535;
  16. resetGrid( 0 );
  17. };
  18. CVSand_Grid::~CVSand_Grid() {
  19. if (m_smap) delete [] m_smap;
  20. };
  21. void CVSand_Grid::resetGrid( int type ) {
  22. memset( m_smap, 0, m_width * m_height*sizeof(SVSandNode));
  23. for (int f=0; f<m_width*m_height; f++) {
  24. m_smap[f].checksum = 1000;
  25. };
  26. };
  27. #define TRANSMIT_EPSILON 0
  28. SVSandNode* CVSand_Grid::gi( int x, int y) {
  29. if (x<0) x = 0;
  30. if (y<0) y = 0;
  31. if (x>=m_width) x = m_width-1;
  32. if (y>=m_height) y = m_height-1;
  33. return &m_smap[ y*m_width + x ];
  34. };
  35. int CVSand_Grid::getAmount( const int x, const int y) {
  36. return gi(x,y)->amount;
  37. };
  38. void CVSand_Grid::grid_push(SVSandNode *o, int posx, int posy) {
  39. if (posx<0) posx = 0;
  40. if (posy<0) posy = 0;
  41. if (posx>m_maxFixedWidth) posx = m_maxFixedWidth;
  42. if (posy>m_maxFixedHeight) posy = m_maxFixedHeight;
  43. int ofsx = (posx&65535)>>2;
  44. int ofsy = (posy&65535)>>2;
  45. int ul_mul = ((((16384-ofsx) * (16384-ofsy)) >> 14));
  46. int ur_mul = ((((ofsx) * (16384-ofsy)) >> 14));
  47. int ll_mul = ((((16384-ofsx) * (ofsy)) >> 14));
  48. int lr_mul = ((((ofsx) * (ofsy)) >> 14));
  49. posx>>=16;
  50. posy>>=16;
  51. SVSandNode *ul_node = m_smap+posy*m_width + posx;
  52. SVSandNode *ur_node = ul_node+1;
  53. SVSandNode *ll_node = ul_node+m_width;
  54. SVSandNode *lr_node = ll_node+1;
  55. ul_node->new_amount += ((o->amount*ul_mul)>>14);
  56. ur_node->new_amount += ((o->amount*ur_mul)>>14);
  57. ll_node->new_amount += ((o->amount*ll_mul)>>14);
  58. lr_node->new_amount += ((o->amount*lr_mul)>>14);
  59. ul_node->new_dir.m_x += ((o->dir.m_x*ul_mul)>>14);
  60. ul_node->new_dir.m_y += ((o->dir.m_y*ul_mul)>>14);
  61. ur_node->new_dir.m_x += ((o->dir.m_x*ur_mul)>>14);
  62. ur_node->new_dir.m_y += ((o->dir.m_y*ur_mul)>>14);
  63. ll_node->new_dir.m_x += ((o->dir.m_x*ll_mul)>>14);
  64. ll_node->new_dir.m_y += ((o->dir.m_y*ll_mul)>>14);
  65. lr_node->new_dir.m_x += ((o->dir.m_x*lr_mul)>>14);
  66. lr_node->new_dir.m_y += ((o->dir.m_y*lr_mul)>>14);
  67. };
  68. void CVSand_Grid::diffuse_step(int epsilon) {
  69. int dif, dx,dy;
  70. int x,y;
  71. SVSandNode *g;
  72. for (y=0; y<m_height; y++) {
  73. g = m_smap + y*m_width;
  74. for (x=0; x<m_width-1; x++) {
  75. // xdif
  76. dif = g->amount - g[1].amount;
  77. if (abs(dif)>epsilon) {
  78. dif >>= 2;
  79. g->amount -= dif;
  80. g[1].amount += dif;
  81. dif>>=1;
  82. g->dir.m_x += dif;
  83. g[1].dir.m_x += dif;
  84. }
  85. if (y<m_height-1) { // ydif
  86. dif = g->amount - g[m_width].amount;
  87. if (abs(dif)>epsilon) {
  88. dif >>= 2;
  89. g->amount -= dif;
  90. g[m_width].amount += dif;
  91. dif>>=1;
  92. g->dir.m_y += dif;
  93. g[m_width].dir.m_y += dif;
  94. }
  95. }
  96. g++;
  97. };
  98. }
  99. };
  100. void CVSand_Grid::selective_soften(int epsilon ) {
  101. int x,y;
  102. SVSandNode *g;
  103. int dif;
  104. SVSandNode *lineplus, *lineminus;
  105. // selective soften
  106. for (y=0; y<m_height; y++) {
  107. g = m_smap+y*m_width;
  108. if (y>0) lineminus = g-m_width; else lineminus = g;
  109. if (y<m_height-1) lineplus = g+m_width; else lineplus = g;
  110. for (x=0; x<m_width; x++) {
  111. if (x>0 && x<m_width-1) {
  112. dif = ((g[-1].amount+g[1].amount+lineminus->amount+lineplus->amount)>>2); // average
  113. if (abs(g->amount-dif)>epsilon) {
  114. g->amount = (g->amount + dif)>>1;
  115. g->dir.m_x=(g->dir.m_x>>1) + ((g[-1].dir.m_x+g[1].dir.m_x+lineminus->dir.m_x+lineplus->dir.m_x)>>3);
  116. g->dir.m_y=(g->dir.m_y>>1) + ((g[-1].dir.m_y+g[1].dir.m_y+lineminus->dir.m_y+lineplus->dir.m_y)>>3);
  117. };
  118. } else {
  119. dif = ((gi(x-1,y)->amount + gi(x+1,y)->amount +gi(x,y-1)->amount + gi(x, y+1)->amount)>>2);
  120. if (abs(g->amount-dif)>epsilon) {
  121. g->amount = (g->amount + dif)>>1;
  122. g->dir.m_x=(g->dir.m_x>>1) + ((gi(x-1,y)->dir.m_x + gi(x+1,y)->dir.m_x +gi(x,y-1)->dir.m_x + gi(x, y+1)->dir.m_x)>>3);
  123. g->dir.m_y=(g->dir.m_y>>1) + ((gi(x-1,y)->dir.m_y + gi(x+1,y)->dir.m_y +gi(x,y-1)->dir.m_y + gi(x, y+1)->dir.m_y)>>3);
  124. };
  125. };
  126. lineplus++;
  127. lineminus++;
  128. g++;
  129. };
  130. };
  131. };
  132. void CVSand_Grid::run(CT2DVector &gravity, CT2DVector &light, TSDWORD bgLight ) {
  133. #ifndef WIN32
  134. runGridStep(gravity, light, -1 ); // Additional runstep.
  135. #endif
  136. runGridStep(gravity, light, bgLight );
  137. }
  138. void CVSand_Grid::runGridStep( CT2DVector &gravity, CT2DVector &light, TSDWORD bgLight ) { // process sand-cells
  139. int y;
  140. int v,i;
  141. int tx,ty;
  142. SVSandNode *t, *t_target;
  143. for (y=0; y<m_height; y++) {
  144. t = m_smap + (y*m_width);
  145. t_target = t+m_width;
  146. //int x=0;
  147. while (t!=t_target) {
  148. //t->dir.add( t->normal );
  149. if (abs(t->dir.m_x * t->dir.m_y) < 5000 || t->amount<5) { // stop the flow if small enough
  150. t->dir.set(0,0);
  151. } else {
  152. t->dir.m_x = ((t->dir.m_x * 212)>>8) + gravity.m_x; //+ ((gravity.m_x * t->amount)>>16);
  153. t->dir.m_y = ((t->dir.m_y * 212)>>8) + gravity.m_y; //+ ((gravity.m_y * t->amount)>>16);
  154. }
  155. t->new_amount = 0;
  156. t->new_dir.set( 0,0 );
  157. t++;
  158. }
  159. }
  160. #ifdef SANDGRID_DEBUG_ON
  161. m_totalAmount = 0;
  162. m_bleededAmount = 0;
  163. #endif
  164. // exchange.
  165. for (y=0; y<m_height; y++) {
  166. t = m_smap + (y*m_width);
  167. t_target = t+m_width;
  168. int x = 0; // is required?
  169. while (t!=t_target) {
  170. if (t->dir.m_x!=0 || t->dir.m_y!=0) {
  171. grid_push(t, ((int)(x) << 16) + (t->dir.m_x>>1),((int)(y) << 16) + (t->dir.m_y>>1) ); // shr was 1
  172. //grid_push(t, ((int)(x) << 16),((int)(y) << 16) );
  173. } else {
  174. t->new_amount += t->amount;
  175. t->new_dir.add( t->dir );
  176. }
  177. x++;
  178. t++;
  179. }
  180. }
  181. t = m_smap;
  182. t_target = m_smap+m_width*m_height;
  183. while (t!=t_target) {
  184. t->amount = t->new_amount;
  185. t->dir.set(t->new_dir);
  186. #ifdef SANDGRID_DEBUG_ON
  187. m_totalAmount += t->amount;
  188. #endif
  189. t++;
  190. }
  191. CT2DVector d;
  192. //run_pressure();
  193. for (int am = 0; am<4; am ++) diffuse_step( SELECTIVE_SOFTEN_EPSILON );
  194. if (bgLight<0) return; // this is not the finald step
  195. selective_soften( SELECTIVE_SOFTEN_EPSILON/4 );
  196. // calculate normals
  197. for (y=0; y<m_height; y++) {
  198. t = m_smap + (y*m_width);
  199. t_target = t+m_width;
  200. int x=0;
  201. while (t!=t_target) {
  202. if (x>0 && y>0 && x<m_width-1 && y<m_height-1) {
  203. tx = -t[-m_width-1].amount + t[-m_width+1].amount +
  204. t[-1].amount*-2 + t[1].amount*2 +
  205. -t[m_width-1].amount + t[m_width+1].amount;
  206. ty = -t[-m_width-1].amount + -t[-m_width].amount*2 + -t[-m_width+1].amount +
  207. t[m_width-1].amount + t[m_width].amount*2 + t[m_width+1].amount;
  208. } else {
  209. tx = -getAmount(x-1,y-1) + getAmount(x+1,y-1) +
  210. getAmount(x-1,y)*-2 + getAmount(x+1,y)*2 +
  211. -getAmount(x-1,y+1) + getAmount(x+1,y+1);
  212. ty = -getAmount(x-1,y-1) + getAmount(x,y-1)*-2 + -getAmount(x+1,y-1) +
  213. getAmount(x-1, y+1) + getAmount(x, y+1)*2 + getAmount(x+1, y+1);
  214. };
  215. t->normal.set( -(tx>>3),-(ty>>3) );
  216. t->light = ((65536*11)>>4) + (((t->normal.m_x * light.m_x) + (t->normal.m_y * light.m_y))>>13);
  217. t->light = ((t->light*(FP_VAL-bgLight))>>FP_BITS);
  218. t->light += ((((86000-t->amount)>>1)*bgLight)>>14); // >> FP_BITS
  219. //t->light = 16000;
  220. if (t->light<0) t->light =0;
  221. if (t->light>65535) t->light = 65535;
  222. x++;
  223. t++;
  224. }
  225. }
  226. };
  227. int CVSand_Grid::curveMass( int x, int y, int r, int strength ) {
  228. int harvested = 0;
  229. int scanx = x>>FP_BITS;
  230. int scany = y>>FP_BITS;
  231. int scana = (r>>FP_BITS) + 1;
  232. int m;
  233. int dx,dy;
  234. int temp,d;
  235. r>>=6;
  236. for (int yy=scany-scana; yy<=scany+scana; yy++) {
  237. for (int xx=scanx-scana; xx<=scanx+scana; xx++) {
  238. if (xx>=0 && yy>=0 && xx<m_width && yy<m_height) {
  239. dx = (((xx<<FP_BITS) + FP_VAL/2) - x)>>6;
  240. dy = (((yy<<FP_BITS) + FP_VAL/2) - y)>>6;
  241. d = tmath_sqrt(dx*dx+dy*dy);
  242. if (d<r) {
  243. m = (strength * (r-d))/r;
  244. temp = ((m_smap[yy*m_width+xx].amount * m)>>FP_BITS);
  245. harvested += temp;
  246. m_smap[yy*m_width+xx].amount -= temp;
  247. }
  248. };
  249. };
  250. };
  251. return harvested;
  252. };
  253. #define MAX_SPEED 65536/4
  254. void CVSand_Grid::scratch_to_target( int x, int y, int r, int power, TDWORD *target_buffer, int *initial_dir ) {
  255. int scanx = x>>FP_BITS;
  256. int scany = y>>FP_BITS;
  257. int scana = (r>>FP_BITS) + 1;
  258. int m;
  259. int dx,dy;
  260. int d;
  261. int target_amount;
  262. CT2DVector dir;
  263. /*
  264. if (initial_dir) {
  265. dir.set(initial_dir[0], initial_dir[1]);
  266. d = dir.length();
  267. if (d>MAX_SPEED*8)
  268. dir.setLength( MAX_SPEED*8 );
  269. }
  270. */
  271. dir.set(initial_dir[0], initial_dir[1]);
  272. r>>=6;
  273. for (int yy=scany-scana; yy<=scany+scana; yy++) {
  274. for (int xx=scanx-scana; xx<=scanx+scana; xx++) {
  275. if (xx>=0 && yy>=0 && xx<m_width && yy<m_height) {
  276. dx = (((xx<<FP_BITS) + FP_VAL/2) - x)>>6;
  277. dy = (((yy<<FP_BITS) + FP_VAL/2) - y)>>6;
  278. d = tmath_sqrt(dx*dx+dy*dy);
  279. if (d<r) {
  280. target_amount = (int)target_buffer[yy*m_width+xx];
  281. m = ((target_amount-m_smap[yy*m_width+xx].amount) * (r-d))/r;
  282. m = ((m*power)>>FP_BITS);
  283. m_smap[yy*m_width+xx].amount += m;
  284. //m_smap[yy*m_width+xx].amount = target_amount;
  285. if (initial_dir) {
  286. m_smap[yy*m_width+xx].dir.m_x += tmath_fpmul( dir.m_x, m); //((dir.m_x * m)>>14);
  287. m_smap[yy*m_width+xx].dir.m_y += tmath_fpmul( dir.m_y, m); // ((dir.m_y * m)>>14);
  288. };
  289. }
  290. };
  291. };
  292. };
  293. };
  294. #define ADD_MAX_LIMIT (65536*3/4)
  295. int CVSand_Grid::addMass( int x, int y, int r, int amount, int *initial_dir ) {
  296. int added = 0;
  297. int scanx = x>>FP_BITS;
  298. int scany = y>>FP_BITS;
  299. int scana = (r>>FP_BITS) + 1;
  300. int m;
  301. int dx,dy;
  302. int d;
  303. CT2DVector dir;
  304. if (initial_dir) {
  305. dir.set(initial_dir[0], initial_dir[1]);
  306. d = dir.length();
  307. if (d>MAX_SPEED*3)
  308. dir.setLength( MAX_SPEED*3 );
  309. }
  310. r>>=6;
  311. for (int yy=scany-scana; yy<=scany+scana; yy++) {
  312. for (int xx=scanx-scana; xx<=scanx+scana; xx++) {
  313. if (xx>=0 && yy>=0 && xx<m_width && yy<m_height) {
  314. dx = (((xx<<FP_BITS) + FP_VAL/2) - x)>>6;
  315. dy = (((yy<<FP_BITS) + FP_VAL/2) - y)>>6;
  316. d = tmath_sqrt(dx*dx+dy*dy);
  317. if (d<r) {
  318. m = (amount * (r-d))/r;
  319. d = (m_smap[yy*m_width+xx].amount);
  320. if (d+m>ADD_MAX_LIMIT) m = ADD_MAX_LIMIT-d;
  321. if (m<0) m = 0;
  322. added += m;
  323. m_smap[yy*m_width+xx].amount += m;
  324. if (initial_dir) {
  325. m_smap[yy*m_width+xx].dir.m_x += ((dir.m_x * (m>>3))>>7);
  326. m_smap[yy*m_width+xx].dir.m_y += ((dir.m_y * (m>>3))>>7);
  327. };
  328. }
  329. };
  330. };
  331. };
  332. return added;
  333. };
  334. void CVSand_Grid::push( int x, int y,int r, int power, int *add_dir ) {
  335. int scanx = x>>FP_BITS;
  336. int scany = y>>FP_BITS;
  337. int scana = (r>>FP_BITS) + 1;
  338. int m;
  339. int dx,dy;
  340. int d;
  341. int take = 0;
  342. CT2DVector dir;
  343. if (add_dir) {
  344. dir.set(add_dir[0], add_dir[1]);
  345. d = dir.length();
  346. if (d>MAX_SPEED)
  347. dir.setLength( MAX_SPEED );
  348. }
  349. r>>=6;
  350. for (int yy=scany-scana; yy<=scany+scana; yy++) {
  351. for (int xx=scanx-scana; xx<=scanx+scana; xx++) {
  352. if (xx>=0 && yy>=0 && xx<m_width && yy<m_height) {
  353. dx = (((xx<<FP_BITS) + FP_VAL/2) - x)>>6;
  354. dy = (((yy<<FP_BITS) + FP_VAL/2) - y)>>6;
  355. d = tmath_sqrt(dx*dx+dy*dy);
  356. if (d<r) {
  357. m = (power * (r-d))/r;
  358. dx = tmath_fpmuldiv(dx, m*3, d+1); // normalize
  359. dy = tmath_fpmuldiv(dy, m*3, d+1);
  360. m_smap[yy*m_width+xx].dir.m_x += dx;
  361. m_smap[yy*m_width+xx].dir.m_y += dy;
  362. // take mass away from the centre
  363. //take = ((m_smap[yy*m_width+xx].amount*m)>>FP_BITS);
  364. //m_smap[yy*m_width+xx].amount-=take;
  365. if (add_dir) {
  366. m_smap[yy*m_width+xx].dir.m_x += ((dir.m_x * m)>>11);
  367. m_smap[yy*m_width+xx].dir.m_y += ((dir.m_y * m)>>11);
  368. };
  369. }
  370. };
  371. };
  372. };
  373. };
  374. void CVSand_Grid::pull( int x, int y,int r, int power ) {
  375. int scanx = x>>FP_BITS;
  376. int scany = y>>FP_BITS;
  377. int scana = (r>>FP_BITS) + 1;
  378. int m;
  379. int dx,dy;
  380. int d;
  381. r>>=6;
  382. for (int yy=scany-scana; yy<=scany+scana; yy++) {
  383. for (int xx=scanx-scana; xx<=scanx+scana; xx++) {
  384. if (xx>=0 && yy>=0 && xx<m_width && yy<m_height) {
  385. dx = (((xx<<FP_BITS) + FP_VAL/2) - x)>>6;
  386. dy = (((yy<<FP_BITS) + FP_VAL/2) - y)>>6;
  387. d = tmath_sqrt(dx*dx+dy*dy);
  388. if (d<r) {
  389. m = (power * (r-d))/r;
  390. m_smap[yy*m_width+xx].dir.m_x -= (dx*m)>>9;
  391. m_smap[yy*m_width+xx].dir.m_y -= (dy*m)>>9;
  392. m_smap[yy*m_width+xx].dir.m_x -= (dy*m)>>9;
  393. m_smap[yy*m_width+xx].dir.m_y += (dx*m)>>9;
  394. }
  395. };
  396. };
  397. };
  398. };
  399. void CVSand_Grid::push_towards( int x, int y,int dirx, int diry, int r, int power ) {
  400. CT2DVector dir;
  401. dir.set(dirx*8, diry*8);
  402. dir.normalize();
  403. int scanx = x>>FP_BITS;
  404. int scany = y>>FP_BITS;
  405. int scana = (r>>FP_BITS) + 1;
  406. int m;
  407. int dx,dy;
  408. int d;
  409. r>>=6;
  410. for (int yy=scany-scana; yy<=scany+scana; yy++) {
  411. for (int xx=scanx-scana; xx<=scanx+scana; xx++) {
  412. if (xx>=0 && yy>=0 && xx<m_width && yy<m_height) {
  413. dx = (((xx<<FP_BITS) + FP_VAL/2) - x)>>6;
  414. dy = (((yy<<FP_BITS) + FP_VAL/2) - y)>>6;
  415. d = tmath_sqrt(dx*dx+dy*dy);
  416. if (d<r) {
  417. m = (power * (r-d))/r;
  418. m_smap[yy*m_width+xx].dir.m_x += (dir.m_x*m)>>13;
  419. m_smap[yy*m_width+xx].dir.m_y += (dir.m_y*m)>>13;
  420. }
  421. };
  422. };
  423. };
  424. };