splash particles
[dd2d.git] / data / scripts / moveapi.dacs
blobeb971b2666748792ae24a40c4df0cc44a22a8cb9
1 module moveapi;
3 import actor, mapapi, stdlib;
6 const MAX_YV = 30;
9 // ////////////////////////////////////////////////////////////////////////// //
10 /// check area, half of height
11 public int Z_checkerRHHalf (int x, int y, int r, int h, int function (int tiletype) ccb) {
12   // start tile
13   int sx = max((x-r)/CELW, 0);
14   int sy = max((y-h+1)/CELH, 0);
15   // end tile
16   x = min((x+r)/CELW, FLDW-1);
17   y = min((y-h/2)/CELH, FLDH-1);
18   if (sx > x || sy > y) return -1; // counts as true anyway
19   while (sx <= x) {
20     for (int j = sy; j <= y; ++j) {
21       int res = ccb(mapGetTypeTile(sx, j));
22       if (res) return res;
23     }
24     ++sx;
25   }
26   return 0;
30 public int Z_checkerRHHalfEx (int x, int y, int r, int h, int function (int tilefg, int tiletype) ccb) {
31   // start tile
32   int sx = max((x-r)/CELW, 0);
33   int sy = max((y-h+1)/CELH, 0);
34   // end tile
35   x = min((x+r)/CELW, FLDW-1);
36   y = min((y-h/2)/CELH, FLDH-1);
37   if (sx > x || sy > y) return -1; // counts as true anyway
38   while (sx <= x) {
39     for (int j = sy; j <= y; ++j) {
40       int res = ccb(mapGetTile(LAYER_FRONT, sx, j), mapGetTypeTile(sx, j));
41       if (res) return res;
42     }
43     ++sx;
44   }
45   return 0;
49 // check "under feet" / "above head"
50 public int Z_checkerRHFeetHead (int x, int y, int r, int h, int function (int tiletype) ccb) {
51   //int origx = x;
52   int i = max((x-r)/CELW, 0);
53   x = min((x+r)/CELW, FLDW-1);
54   y = (y-h+1)/CELH;
55   //if (origx >= 160 && origx <= 168 && h > 2) { writeln("Z_checkerRHFeetHead: i=", i, "; x=", x, "; y=", y); }
56   if (y >= FLDH || y < 0) return 0;
57   for (; i <= x; ++i) {
58     //if (origx >= 160 && origx <= 168 && h > 2) { writeln("  i=", i, "; y=", y, "; tt=", mapGetTypeTile(i, y)); }
59     int res = ccb(mapGetTypeTile(i, y));
60     //writeln("  Z_checkerRHFeetHead(", i, ", ", y, ") = ", res);
61     if (res) return res;
62   }
63   return 0;
67 // check the whole occupied area
68 public int Z_checkerRHWhole (int x, int y, int r, int h, int dy, int function (int tiletype) ccb) {
69   int sx = max((x-r)/CELW, 0);
70   int sy = max((y-h+1)/CELH, 0);
71   x = min((x+r)/CELW, FLDW-1);
72   y = min((y+dy)/CELH, FLDH-1);
73   if (sx > x || sy > y) return -1; // counts as true anyway
74   for (int i = sx; i <= x; ++i) {
75     for (int j = sy; j <= y; ++j) {
76       int res = ccb(mapGetTypeTile(i, j));
77       //writeln("  Z_checkerRHWhole(", i, ", ", j, ") = ", res);
78       if (res) return res;
79     }
80   }
81   return 0;
85 // ////////////////////////////////////////////////////////////////////////// //
86 int Z_canbreatheCCB (int fv) {
87   switch (fv) {
88     case TILE_EMPTY:
89     case TILE_DOORO:
90     case TILE_LIFTU:
91     case TILE_LIFTD:
92       return 1;
93     default:
94   }
95   return 0;
98 public int Z_canbreathe (int x, int y, int r, int h) { return (Z_checkerRHHalf(x, y, r, h, &Z_canbreatheCCB) != 0); }
101 // ////////////////////////////////////////////////////////////////////////// //
102 int Z_canstandCCB (int fv) { return (fv == TILE_WALL || fv == TILE_DOORC || fv == TILE_STEP ? 1 : 0); }
104 public int Z_canstand (int x, int y, int r) { return Z_checkerRHFeetHead(x, y, r, 0, &Z_canstandCCB); }
107 // ////////////////////////////////////////////////////////////////////////// //
108 int Z_cangodownCCB (int fv) { return (fv == TILE_STEP ? 1 : 0); }
110 public int Z_cangodown (int x, int y, int r) { return Z_checkerRHFeetHead(x, y, r, 0, &Z_cangodownCCB); }
113 // ////////////////////////////////////////////////////////////////////////// //
114 int Z_hitceilCCB (int fv) {
115   return (getCheatNoDoors ? (fv == TILE_WALL ? 1 : 0) : (fv == TILE_WALL || fv == TILE_DOORC ? 1 : 0));
118 public int Z_hitceil (int x, int y, int r, int h) {
119   //if (x >= 160 && x <= 168) writeln("=== hitceil: x=", x, "; y=", y, "; r=", r, "; h=", h);
120   return Z_checkerRHFeetHead(x, y, r, h+1, &Z_hitceilCCB); // `+1` due to checker specific
124 // ////////////////////////////////////////////////////////////////////////// //
125 int Z_canfitCCB (int fv) { return (fv == TILE_WALL || fv == TILE_DOORC ? 1 : 0); }
127 public int Z_canfit (int x, int y, int r, int h) { return 1-Z_checkerRHWhole(x, y, r, h, 0, &Z_canfitCCB); }
130 // ////////////////////////////////////////////////////////////////////////// //
131 int Z_inliftCCB (int fv) {
132   if (fv == TILE_LIFTU || fv == TILE_LIFTD) return fv-TILE_LIFTU+1;
133   return 0;
136 public int Z_inlift (int x, int y, int r, int h) { return Z_checkerRHWhole(x, y, r, h, -1, &Z_inliftCCB); }
139 // ////////////////////////////////////////////////////////////////////////// //
140 int Z_isblockedCCB (int fv) { return (fv == TILE_MBLOCK ? 1 : 0); }
142 public int Z_isblocked (int x, int y, int r, int h, int xv) { return Z_checkerRHWhole(x, y, r, h, -1, &Z_isblockedCCB); }
145 // ////////////////////////////////////////////////////////////////////////// //
146 int Z_istrappedCCB (int fv) { return (fv == TILE_ACTTRAP ? 1 : 0); }
148 public int Z_istrapped (int x, int y, int r, int h) { return Z_checkerRHWhole(x, y, r, h, -1, &Z_istrappedCCB); }
151 // ////////////////////////////////////////////////////////////////////////// //
152 //static uint8_t wfront; // for splash
153 int Z_inwaterCCB (int fv) {
154   //return (fv >= TILE_WATER && fv <= TILE_ACID2 ? 1 : 0); /* wfront = fv;*/
155   switch (fv) {
156     case TILE_WATER: return Z_WATER_0;
157     case TILE_ACID1: return Z_WATER_1;
158     case TILE_ACID2: return Z_WATER_2;
159     default:
160   }
161   return 0;
164 public int Z_inwater (int x, int y, int r, int h/*, ref int wfront*/) { return Z_checkerRHHalf(x, y, r, h, &Z_inwaterCCB); }
167 int Z_inwaterCCBEx (int fg, int fv) {
168   //return (fv >= TILE_WATER && fv <= TILE_ACID2 ? 1 : 0); /* wfront = fv;*/
169   int res = 0;
170   switch (fv) {
171     case TILE_WATER: res = Z_WATER_0; break;
172     case TILE_ACID1: res = Z_WATER_1; break;
173     case TILE_ACID2: res = Z_WATER_2; break;
174     default:
175   }
176   if (res) {
177     int wt = mapGetWaterTexture(fg);
178     switch (wt) {
179       case 1: res |= Z_WATER_T0; break;
180       case 2: res |= Z_WATER_T1; break;
181       case 3: res |= Z_WATER_T2; break;
182       default:
183     }
184   }
185   return res;
188 public int Z_inwaterEx (int x, int y, int r, int h/*, ref int wfront*/) { return Z_checkerRHHalfEx(x, y, r, h, &Z_inwaterCCBEx); }
191 // ////////////////////////////////////////////////////////////////////////// //
192 int clamp7 (int v) { return (abs(v) <= 7 ? v : (v > 0 ? 7 : -7)); }
194 int is_monster (Actor me) { return (me.classtype == "monster" && me.classname != "Player"); }
196 //int is_dot (Actor me) { return false; }
197 //int is_ghost_player (Actor me) { return false; }
200 public int Z_dec (int n, int delta) {
201   if (abs(n) > delta) {
202     if (n > 0) return n-delta;
203     if (n < 0) return n+delta;
204   }
205   return 0;
208 //#define wvel(v) if ((xv = abs(v)+1) > 5) v = Z_dec(v, xv/2-2)
211 public int Z_moveobj (Actor me) {
212   int xv, yv, lx, ly;
213   int/*uint8_t*/ inw;
215   int st = 0;
216   int x = me.x;
217   int y = me.y;
218   int r = me.radius;
219   int h = me.height;
221   if (/*!is_ghost_player(me)*/1) {
222     switch (Z_inlift(x, y, r, h)) {
223       case 0:
224         if (++me.yv > MAX_YV) --me.yv;
225         break;
226       case 1:
227         if (--me.yv < -5) ++me.yv;
228         break;
229       case 2:
230         if (me.yv > 5) --me.yv; else ++me.yv;
231         break;
232       default:
233     }
234   }
236   inw = Z_inwater(x, y, r, h);
237   //writeln("x=", x, "; y=", y, "; r=", r, "; h=", h, "; inw=", inw);
238   if (inw != 0) {
239     st |= Z_INWATER;
240     if ((xv = abs(me.xv)+1) > 5) me.xv = Z_dec(me.xv, xv/2-2);
241     if ((xv = abs(me.yv)+1) > 5) me.yv = Z_dec(me.yv, xv/2-2);
242     if ((xv = abs(me.vx)+1) > 5) me.vx = Z_dec(me.vx, xv/2-2);
243     if ((xv = abs(me.vy)+1) > 5) me.vy = Z_dec(me.vy, xv/2-2);
244   }
246   //writeln("  vx=", me.vx, "; vy=", me.vy);
247   me.vx = Z_dec(me.vx, 1);
248   me.vy = Z_dec(me.vy, 1);
249   //writeln("  vx=", me.vx, "; vy=", me.vy);
251   //writeln("  xv=", me.xv, "; yv=", me.yv);
253   xv = me.xv+me.vx;
254   yv = me.yv+me.vy;
255   //writeln("  xv=", xv, "; yv=", yv);
257   while (xv || yv) {
258     //writeln("  *xv=", xv, "; yv=", yv);
259     if (x < -100 || x >= FLDW*CELW+100 || y < -100 || y >= FLDH*CELH+100) {
260       // out of map
261       st |= Z_FALLOUT;
262     }
264     //writeln("  x=", x, "; xv=", xv, "; c7(xv)=", clamp7(xv));
265     lx = x;
266     x += clamp7(xv);
267     //writeln("  x=", x);
269     if (is_monster(me)) {
270       // for monster
271       if (Z_isblocked(x, y, r, h, xv)) st |= Z_BLOCK;
272     }
274     if (!Z_canfit(/*is_dot(me),*/ x, y, r, h)) {
275       if (/*!is_ghost_player(me)*/1) {
276         /*
277         if (gm_climb_stairs && is_player(me)) {
278           // check if we can climb upstairs
279           if (yv == 1 && (xv == 1 || xv == -1)) {
280             if (Z_canfit(me.type, x, y-CELH, r, h) && Z_canfit(me.type, x+Z_sign(xv)*(CELW-1), y-CELH, r, h)) {
281               me.vy = -8;
282               return Z_moveobj(me);
283             }
284           }
285         }
286         */
287         if (xv == 0) x = lx;
288         else if (xv < 0) x = ((lx-r)&0xFFF8)+r;
289         else x = ((lx+r)&0xFFF8)-r+7;
290         xv = me.xv = me.vx = 0;
291         st |= Z_HITWALL;
292       }
293     }
294     xv -= clamp7(xv);
295     //writeln("  xv=", xv);
297     ly = y;
298     y += clamp7(yv);
299     if (yv >= 8) --y;
300     // moving up and hit the ceiling
301     if (/*!is_ghost_player(me)*/1) {
302       if (yv < 0 && Z_hitceil(/*me.type,*/ x, y, r, h)) {
303         //writeln("hitceil: x=", x, "; y=", y);
304         y = ((ly-h+1)&0xFFF8)+h-1;
305         //writeln("  newy=", y);
306         yv = me.vy = 1;
307         me.yv = 0;
308         st |= Z_HITCEIL;
309       }
310       if (yv > 0 && Z_canstand(/*me.type,*/ x, y, r)) {
311         y = ((y+1)&0xFFF8)-1;
312         yv = me.yv = me.vy = 0;
313         st |= Z_HITLAND;
314       }
315     }
316     yv -= clamp7(yv);
317     //writeln("  yv=", yv);
318   }
320   me.x = x;
321   me.y = y;
322   //writeln("  x=", x, "; y=", y);
324   int wtr = Z_inwaterEx(x, y, r, h);
325   if (wtr) {
326     st |= Z_INWATER;
327     if (!inw) st |= Z_HITWATER;
328     st |= wtr;
329   } else if (inw) {
330     st |= Z_HITAIR;
331   }
333   return st;
337 // ////////////////////////////////////////////////////////////////////////// //
338 public void Z_splash (Actor me, int n, int st) {
339   //Z_sound(bulsnd[0], 128);
340   //DOT_water(p->x, p->y-p->h/2, p->xv+p->vx, p->yv+p->vy, n, /*(int)walp[wfront]*/walp[wfront]->wanim-1);
341   //writeln("Z_splash: n=", n, "; wtx=", (st&(Z_WATER_0|Z_WATER_1|Z_WATER_2)));
342   int wtt;
343        if (st&Z_WATER_T0) wtt = 0;
344   else if (st&Z_WATER_T1) wtt = 1;
345   else if (st&Z_WATER_T2) wtt = 2;
346   else return;
347   //writeln("Z_splash: n=", n, "; wtt=", wtt);
348   dotAddWater(me.x, me.y-me.height/2, me.xv+me.vx, me.yv+me.vy, n, wtt);