source: abuse/trunk/src/objects.cpp @ 635

Last change on this file since 635 was 635, checked in by Sam Hocevar, 11 years ago

lisp: refactor Lisp spaces so that they are real objects, and get rid
of the unused USER_SPACE.

File size: 36.8 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
9 */
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "common.h"
16
17#include "transimage.h"
18#include "objects.h"
19#include "chars.h"
20
21#include "game.h"
22#include "intsect.h"
23#include "ability.h"
24#include "lisp.h"
25#include "jrand.h"
26#include "light.h"
27#include "dprint.h"
28#include "clisp.h"
29#include "lisp_gc.h"
30#include "profile.h"
31
32char **object_names;
33int total_objects;
34game_object *current_object;
35view *current_view;
36
37game_object *game_object::copy()
38{
39  game_object *o=create(otype,x,y);
40  o->state=state;
41  int i=0;
42  for (; i<TOTAL_OBJECT_VARS; i++)
43    o->set_var(i,get_var(i));
44  memcpy(o->lvars,lvars,4*figures[otype]->tv);
45  for (i=0; i<total_objects(); i++)
46    o->add_object(get_object(i));
47  for (i=0; i<total_lights(); i++)
48    o->add_light(get_light(i));
49  return o;
50}
51
52int simple_object::total_vars() { return TOTAL_OBJECT_VARS; }
53
54
55obj_desc object_descriptions[TOTAL_OBJECT_VARS] =
56{
57    { "fade_dir",      RC_8 },
58    { "frame_dir",     RC_8 },
59    { "direction",     RC_8 },
60    { "gravity_on",    RC_8 },
61    { "fade_count",    RC_8 },
62
63    { "fade_max",      RC_8 },
64    { "active",        RC_8 },
65    { "flags",         RC_8 },
66    { "aitype",        RC_8 },
67    { "xvel",          RC_32 },
68
69    { "fxvel",         RC_8 },
70    { "yvel",          RC_32 },
71    { "fyvel",         RC_8 },
72    { "xacel",         RC_32 },
73    { "fxacel",        RC_8 },
74
75    { "yacel",         RC_32 },
76    { "fyacel",        RC_8 },
77    { "x",             RC_32 },
78    { "fx",            RC_8 },
79    { "y",             RC_32 },
80
81    { "fy",            RC_8 },
82    { "hp",            RC_16 },
83    { "mp",            RC_16 },
84    { "fmp",           RC_16 },
85    { "cur_frame",     RC_16 },
86
87    { "aistate",       RC_16 },
88    { "aistate_time",  RC_16 },
89    { "targetable",    RC_8 }
90};
91
92int32_t game_object::get_var_by_name(char *name, int &error)
93{
94  error=0;
95  int i=0;
96  for (; i<TOTAL_OBJECT_VARS; i++)
97  {
98    if (!strcmp(object_descriptions[i].name,name))
99      return get_var(i);
100  }
101
102  for (i=0; i<figures[otype]->tiv; i++)
103  {
104    if (!strcmp(lstring_value(((LSymbol *)figures[otype]->vars[i])->GetName()),name))
105    {
106      return lvars[figures[otype]->var_index[i]];
107/*      LObjectVar *cobj=(LObjectVar *)symbol_value(figures[otype]->vars[i]);
108      character_type *t=figures[otype];
109      int number=cobj->number;
110      if (t->tiv<=number || !t->vars[number])
111      {
112    lbreak("access : variable does not exsist for this class\n");
113    return 0;
114      }
115      return lvars[t->var_index[number]]; */
116    }
117  }
118  error=1;
119  return 0;
120}
121
122int game_object::set_var_by_name(char *name, int32_t value)
123{
124  int i=0;
125  for (; i<TOTAL_OBJECT_VARS; i++)
126  {
127    if (!strcmp(object_descriptions[i].name,name))
128    {
129      set_var(i,value);
130      return 1;
131    }
132  }
133  for (i=0; i<figures[otype]->tiv; i++)
134    if (!strcmp(lstring_value(((LSymbol *)figures[otype]->vars[i])->GetName()),name))
135    {
136      lvars[figures[otype]->var_index[i]]=value;
137      return 1;
138    }
139  return 0;
140}
141
142
143char const *simple_object::var_name(int x)
144{
145  return object_descriptions[x].name;
146}
147
148int simple_object::var_type(int x)
149{
150  return object_descriptions[x].type;
151}
152
153
154void simple_object::set_var(int xx, uint32_t v)
155{
156  switch (xx)
157  {
158    case 0 : set_fade_dir(v); break;
159    case 1 : set_frame_dir(v); break;
160    case 2 : direction=v; break;
161    case 3 : set_gravity(v); break;
162    case 4 : set_fade_count(v); break;
163    case 5 : set_fade_max(v); break;
164    case 6 : active=v; break;
165    case 7 : set_flags(v); break;
166    case 8 : set_aitype(v); break;
167    case 9 : set_xvel(v); break;
168
169    case 10 : set_fxvel(v); break;
170    case 11 : set_yvel(v); break;
171    case 12 : set_fyvel(v); break;
172    case 13 : set_xacel(v); break;
173    case 14 : set_fxacel(v); break;
174
175    case 15 : set_yacel(v); break;
176    case 16 : set_fyacel(v); break;
177    case 17 : x=v; break;
178    case 18 : set_fx(v); break;
179    case 19 : y=v; break;
180
181    case 20 : set_fy(v); break;
182    case 21 : set_hp(v); break;
183    case 22 : set_mp(v); break;
184    case 23 : set_fmp(v); break;
185
186    case 24 : current_frame=v;  break;
187    case 25 : set_aistate(v); break;
188
189    case 26 : set_aistate_time(v); break;
190    case 27 : set_targetable(v); break;
191  }
192}
193
194int32_t simple_object::get_var(int xx)
195{
196  switch (xx)
197  {
198    case 0 : return fade_dir(); break;
199    case 1 : return frame_dir(); break;
200    case 2 : return direction; break;
201    case 3 : return gravity(); break;
202    case 4 : return fade_count(); break;
203    case 5 : return fade_max(); break;
204    case 6 : return active; break;
205    case 7 : return flags(); break;
206    case 8 : return aitype(); break;
207    case 9 : return xvel(); break;
208    case 10 : return fxvel(); break;
209
210    case 11 : return yvel(); break;
211    case 12 : return fyvel(); break;
212
213    case 13 : return xacel(); break;
214    case 14 : return fxacel(); break;
215
216    case 15 : return yacel(); break;
217    case 16 : return fyacel(); break;
218
219    case 17 : return x; break;
220    case 18 : return fx(); break;
221
222    case 19 : return y; break;
223    case 20 : return fy(); break;
224
225    case 21 : return hp(); break;
226    case 22 : return mp(); break;
227    case 23 : return fmp(); break;
228
229    case 24 : return current_frame;  break;
230    case 25 : return aistate(); break;
231    case 26 : return aistate_time(); break;
232    case 27 : return targetable();
233  }
234  return 0;
235}
236
237
238
239
240int RC_type_size(int type)
241{
242  switch (type)
243  {
244    case RC_8 :
245    { return 1; } break;
246    case RC_16 :
247    { return 2; } break;
248    case RC_32 :
249    { return 4; } break;
250  }
251  CHECK(0);
252  return 1;
253}
254
255void game_object::reload_notify()
256{
257  void *ns=figures[otype]->get_fun(OFUN_RELOAD);
258  if (ns)
259  {
260    game_object *o=current_object;
261    current_object=this;
262
263    void *m = LSpace::Tmp.Mark();
264    ((LSymbol *)ns)->EvalFunction(NULL);
265    LSpace::Tmp.Restore(m);
266
267    current_object=o;
268  }
269}
270
271void game_object::next_sequence()
272{
273    void *ns = figures[otype]->get_fun( OFUN_NEXT_STATE );
274    if( ns )
275    {
276        current_object = this;
277        void *m = LSpace::Tmp.Mark();
278        ((LSymbol *)ns)->EvalFunction(NULL);
279        LSpace::Tmp.Restore(m);
280    }
281    else
282    {
283        switch( state )
284        {
285            case dieing:
286            {
287                set_state( dead );
288            } break;
289            case end_run_jump:
290            {
291                set_state( running );
292            } break;
293            case dead:
294            case run_jump:
295            case run_jump_fall:
296            case running:
297            {
298                set_state(state);
299            } break;
300            case start_run_jump:
301            {
302                set_yvel(get_ability(type(),jump_yvel));
303                if( xvel() > 0 )
304                    set_xvel(get_ability(type(),jump_xvel));
305                else if( xvel() < 0 )
306                    set_xvel(-get_ability(type(),jump_xvel));
307                set_xacel(0);
308                set_fxacel(0);
309                set_gravity(1);
310                set_state(run_jump);
311            } break;
312            case flinch_up:
313            case flinch_down:
314            {
315                if( gravity() )
316                {
317                    if( has_sequence( end_run_jump ) )
318                        set_state(end_run_jump);
319                    else
320                        set_state(stopped);
321                }
322                else
323                {
324                    set_state(stopped);
325                }
326            } break;
327
328            default:
329            {
330                set_state(stopped);
331            } break;
332        }
333    }
334}
335
336
337
338game_object::~game_object()
339{
340  if (lvars) free(lvars);
341  clean_up();
342}
343
344void game_object::add_power(int amount)
345{
346  int max_power=lnumber_value(symbol_value(l_max_power));
347  int n=mp()+amount;
348  if (n<0) n=0;
349  if (n>max_power) n=max_power;
350  set_mp(n);
351}
352
353void game_object::add_hp(int amount)
354{
355  if (controller() && controller()->god) return ;
356  int max_hp=lnumber_value(symbol_value(l_max_hp));
357  int n=hp()+amount;
358  if (n<0) n=0;
359  if (n>max_hp)
360    n=max_hp;
361  set_hp(n);
362}
363
364int game_object::can_morph_into(int type)
365{
366  if (type!=otype && mp()>=figures[type]->morph_power)
367    return 1;
368  else return 0;
369}
370
371void game_object::morph_into(int type, void (*stat_fun)(int), int anneal, int frames)
372{
373  set_morph_status(new morph_char(this,type,stat_fun,anneal,frames));
374  otype=type;
375  set_state(stopped);
376}
377
378void game_object::draw_above(view *v)
379{
380  int32_t x1,y1,x2,y2,sy1,sy2,sx,i;
381  picture_space(x1,y1,x2,y2);
382
383  the_game->game_to_mouse(x1,y1,v,sx,sy2);
384  if (sy2>=v->cy1)
385  {
386    int32_t draw_to=y1-(sy2-v->cy1),tmp=x;
387    current_level->foreground_intersect(x,y1,tmp,draw_to);
388    the_game->game_to_mouse(x1,draw_to,v,i,sy1);     // calculate sy1
389
390    sy1 = Max(v->cy1, sy1);
391    sy2 = Min(v->cy2, sy2);
392    TransImage *p=picture();
393
394    for (i=sy1; i<=sy2; i++)
395      p->PutScanLine(screen,vec2i(sx,i),0);
396  }
397}
398
399int game_object::push_range()
400{
401  return get_ability(otype,push_xrange);
402}
403
404int game_object::decide()
405{
406  if (figures[otype]->get_fun(OFUN_AI))
407  {
408    int old_aistate;
409    old_aistate=aistate();
410
411    current_object=this;
412    void *m = LSpace::Tmp.Mark();
413
414    time_marker *prof1=NULL;
415    if (profiling())
416      prof1=new time_marker;
417
418    LObject *ret = ((LSymbol *)figures[otype]->get_fun(OFUN_AI))->EvalFunction(NULL);
419    if (profiling())
420    {
421      time_marker now;
422      profile_add_time(this->otype,now.diff_time(prof1));
423      delete prof1;
424    }
425
426    LSpace::Tmp.Restore(m);
427
428    if (keep_ai_info())
429    {
430      if (aistate()!=old_aistate)
431      set_aistate_time(0);
432      else set_aistate_time(aistate_time()+1);
433    }
434    if (!NILP(ret)) return 1;
435    else return 0;
436  }
437  else move(0,0,0);
438  return 1;
439}
440
441// collision checking will ask first to see if you
442int game_object::can_hurt(game_object *who)
443{
444    int is_attacker = current_level->is_attacker(this);
445
446    // it's you against them!  Damage only if it you are attacking or they are
447    // attacking you, ie. don't let them hurt themselves. This can change if
448    // you override this virtual function
449
450    if(who->hurtable()
451        && ((_team == -1) || (_team != who->get_team()))
452        && (is_attacker || current_level->is_attacker(who) || hurt_all()))
453        return 1;
454
455    return 0;
456}
457
458void game_object::note_attack(game_object *whom)
459{
460    return; // nevermind
461}
462
463void game_object::do_flinch(game_object *from)
464{
465  if (jrandom(2) && has_sequence(flinch_down))
466    set_state(flinch_down);
467  else if (has_sequence(flinch_up))
468    set_state(flinch_up);
469}
470
471
472void game_object::do_damage(int amount, game_object *from, int32_t hitx, int32_t hity,
473                int32_t push_xvel, int32_t push_yvel)
474{
475  // No friendly fire
476  if((_team != -1) && (_team == from->get_team()))
477    return;
478
479  void *d=figures[otype]->get_fun(OFUN_DAMAGE);
480  if (d)
481  {
482    LList *am, *frm, *hx, *hy, *px, *py;
483    game_object *o = current_object;
484    current_object = this;
485
486    void *m = LSpace::Tmp.Mark();
487
488    am = LList::Create();
489    PtrRef r1(am);
490    am->car = LNumber::Create(amount);
491
492    frm = LList::Create();
493    PtrRef r2(frm);
494    frm->car = LPointer::Create(from);
495
496    hx = LList::Create();
497    PtrRef r3(hx);
498    hx->car = LNumber::Create(hitx);
499
500    hy = LList::Create();
501    PtrRef r4(hy);
502    hy->car = LNumber::Create(hity);
503
504    px = LList::Create();
505    PtrRef r5(px);
506    px->car = LNumber::Create(push_xvel);
507
508    py = LList::Create();
509    PtrRef r6(py);
510    py->car = LNumber::Create(push_yvel);
511
512    px->cdr = py;
513    hy->cdr = px;
514    hx->cdr = hy;
515    frm->cdr = hx;
516    am->cdr = frm;
517
518    time_marker *prof1 = NULL;
519    if (profiling())
520      prof1 = new time_marker;
521
522    ((LSymbol *)d)->EvalUserFunction(am);
523    if (profiling())
524    {
525      time_marker now;
526      profile_add_time(this->otype, now.diff_time(prof1));
527      delete prof1;
528    }
529
530    LSpace::Tmp.Restore(m);
531
532    current_object = o;
533  } else damage_fun(amount,from,hitx,hity,push_xvel,push_yvel);
534}
535
536void game_object::damage_fun(int amount, game_object *from, int32_t hitx, int32_t hity,
537                int32_t push_xvel, int32_t push_yvel)
538{
539  if (!hurtable() || !alive()) return ;
540
541  add_hp(-amount);
542  set_flags(flags()|FLAG_JUST_HIT);
543  do_flinch(from);
544
545
546  set_xvel(xvel()+push_xvel);
547  if (push_yvel<0 && !gravity())
548    set_gravity(1);
549  set_yvel(yvel()+push_yvel);
550
551  view *c=controller();
552  if (c && hp()<=0)
553  {
554    view *v=from->controller();
555    if (v) v->kills++;                       // attack from another player?
556    else if (from->total_objects()>0)
557    {
558      v=from->get_object(0)->controller();   // weapon from another player?
559      if (v && v!=c) v->kills++;
560      else
561      {
562    v=c;                                 // suicide
563    if (v) v->kills--;
564      }
565    }
566  }
567}
568
569
570
571int game_object::facing_attacker(int attackerx)
572{
573  return ((attackerx<x && direction<0) || (attackerx>=x && direction>0));
574
575}
576
577
578void game_object::picture_space(int32_t &x1, int32_t &y1,int32_t &x2, int32_t &y2)
579{
580  int xc=x_center(),w=picture()->Size().x,h=picture()->Size().y;
581  if (direction>0)
582    x1=x-xc;
583  else x1=x-(w-xc-1);
584  x2=x1+w-1;
585  y1=y-h+1;
586  y2=y;
587}
588
589
590int game_object::next_picture()
591{
592  int ret=1;
593  if (frame_dir()>0)
594  {
595    if (!current_sequence()->next_frame(current_frame))
596    {
597      next_sequence();
598      ret=0;
599    }
600  }
601  else
602  {
603    if (!current_sequence()->last_frame(current_frame))
604    {
605      next_sequence();
606      ret=0;
607    }
608  }
609  frame_advance();
610  return ret;
611}
612
613
614int32_t game_object::x_center()
615{
616  return current_sequence()->x_center(current_frame);
617}
618
619
620void game_object::draw()
621{
622  if (figures[otype]->get_fun(OFUN_DRAW))
623  {
624    current_object=this;
625
626    void *m = LSpace::Tmp.Mark();
627    time_marker *prof1=NULL;
628    if (profiling())
629      prof1=new time_marker;
630
631    ((LSymbol *)figures[otype]->get_fun(OFUN_DRAW))->EvalFunction(NULL);
632    if (profiling())
633    {
634      time_marker now;
635      profile_add_time(this->otype,now.diff_time(prof1));
636      delete prof1;
637    }
638
639    LSpace::Tmp.Restore(m);
640
641  } else drawer();
642}
643
644
645void game_object::map_draw()
646{
647  if (figures[otype]->get_fun(OFUN_MAP_DRAW))
648  {
649    current_object=this;
650
651    void *m = LSpace::Tmp.Mark();
652    time_marker *prof1=NULL;
653    if (profiling())
654      prof1=new time_marker;
655
656    ((LSymbol *)figures[otype]->get_fun(OFUN_MAP_DRAW))->EvalFunction(NULL);
657    if (profiling())
658    {
659      time_marker now;
660      profile_add_time(this->otype,now.diff_time(prof1));
661      delete prof1;
662    }
663
664    LSpace::Tmp.Restore(m);
665  }
666}
667
668void game_object::draw_trans(int count, int max)
669{
670  TransImage *cpict=picture();
671  cpict->PutFade(screen,
672          vec2i((direction<0 ? x-(cpict->Size().x-x_center()-1) : x-x_center())-current_vxadd,
673                y-cpict->Size().y+1-current_vyadd),
674          count,max,
675          color_table,the_game->current_palette());
676}
677
678
679void game_object::draw_tint(int tint_id)
680{
681  TransImage *cpict=picture();
682  if (fade_count())
683    cpict->PutFadeTint(screen,
684               vec2i((direction<0 ? x-(cpict->Size().x-x_center()-1) : x-x_center())-current_vxadd,
685                     y-cpict->Size().y+1-current_vyadd),
686               fade_count(),fade_max(),
687               cache.ctint(tint_id)->data,
688               color_table,the_game->current_palette());
689
690
691  else
692    cpict->PutRemap(screen,
693               vec2i((direction<0 ? x-(cpict->Size().x-x_center()-1) : x-x_center())-current_vxadd,
694                     y-cpict->Size().y+1-current_vyadd),
695               cache.ctint(tint_id)->data);
696}
697
698
699void game_object::draw_double_tint(int tint_id, int tint2)
700{
701  TransImage *cpict=picture();
702  if (fade_count())
703    cpict->PutFadeTint(screen,
704               vec2i((direction<0 ? x-(cpict->Size().x-x_center()-1) : x-x_center())-current_vxadd,
705                     y-cpict->Size().y+1-current_vyadd),
706               fade_count(),fade_max(),
707               cache.ctint(tint_id)->data,
708               color_table,the_game->current_palette());
709
710
711  else
712    cpict->PutDoubleRemap(screen,
713               vec2i((direction<0 ? x-(cpict->Size().x-x_center()-1) : x-x_center())-current_vxadd,
714                     y-cpict->Size().y+1-current_vyadd),
715               cache.ctint(tint_id)->data,
716               cache.ctint(tint2)->data);
717}
718
719
720
721void game_object::draw_predator()
722{
723  TransImage *cpict=picture();
724  cpict->PutPredator(screen,
725             vec2i((direction<0 ? x-(cpict->Size().x-x_center()-1) : x-x_center())-current_vxadd,
726                   y-cpict->Size().y+1-current_vyadd));
727
728}
729
730void game_object::drawer()
731{
732  if (morph_status())
733  {
734    morph_status()->draw(this,current_view);
735    if (morph_status()->frames_left()<1)
736      set_morph_status(NULL);
737  }
738  else
739  {
740    //view *v=controller();
741
742    if (fade_count())
743      draw_trans(fade_count(),fade_max());
744    else
745    {
746      TransImage *cpict=picture();
747      cpict->PutImage(screen,
748               vec2i((direction<0 ? x-(cpict->Size().x-x_center()-1) : x-x_center())-current_vxadd,
749                     y-cpict->Size().y+1-current_vyadd));
750    }
751  }
752}
753
754game_object *game_object::try_move(int32_t x, int32_t y, int32_t &xv, int32_t &yv, int checks)
755{
756  if (xv || yv)  // make sure they are suggesting movement
757  {
758    game_object *who1=NULL,*who2=NULL;      // who did we intersect?
759    int32_t x2,y2,h;
760
761    if (checks&1)
762    {
763      x2=x+xv;
764      y2=y+yv;
765      current_level->foreground_intersect(x,y,x2,y2);
766      if (!stoppable())
767        who1=current_level->boundary_setback(this,x,y,x2,y2);
768      else
769        who1=current_level->all_boundary_setback(this,x,y,x2,y2);
770      xv=x2-x;
771      yv=y2-y;
772    }
773
774
775    if (checks&2)
776    {
777      h=picture()->Size().y;
778      x2=x+xv;
779      y2=y-h+1+yv;
780      current_level->foreground_intersect(x,y-h+1,x2,y2);
781      if (!stoppable())
782        who2=current_level->all_boundary_setback(this,x,y-h+1,x2,y2);
783      else
784        who2=current_level->boundary_setback(this,x,y-h+1,x2,y2);
785      xv=x2-x;
786      yv=y2-y+h-1;
787    }
788
789    if (who2) return who2;
790    else return who1;
791  }
792  else return NULL;
793}
794
795void *game_object::float_tick()  // returns 1 if you hit something, 0 otherwise
796{
797  int32_t ret=0;
798  if (hp()<=0)
799  {
800    if (state!=dead)
801    {
802      set_xacel(0);
803      set_fxacel(0);
804
805      if (has_sequence(dieing))
806      {
807    if (state!=dieing)
808    {
809      set_state(dieing);
810      set_xvel(0);
811    }
812      } else
813      { set_xvel(0);
814    set_fxvel(0);
815    if (has_sequence(dead))
816          set_state(dead);
817    else return 0;
818      }
819    }
820  }
821
822  int32_t fxv=sfxvel()+sfxacel(),fyv=sfyvel()+sfyacel();
823  int32_t xv=xvel()+xacel()+(fxv>>8),yv=yvel()+yacel()+(fyv>>8);
824
825  if (xv!=xvel() || yv!=yvel())   // only store vel's if changed so we don't increase object size
826  {
827    set_xvel(xv);
828    set_yvel(yv);
829  }
830
831  if (fxv!=sfxvel() || fyv!=sfyvel())
832  {
833    set_fxvel(fxv&0xff);
834    set_fyvel(fyv&0xff);
835  }
836
837
838  if (fxv || fyv || xv || yv)   // don't even try if there is no velocity
839  {
840    int32_t ffx=fx()+sfxvel(),ffy=fy()+sfyvel();
841    int32_t nxv=xvel()+(ffx>>8);
842    int32_t nyv=yvel()+(ffy>>8);
843    set_fx(ffx&0xff);
844    set_fy(ffy&0xff);
845
846    int32_t old_nxv=nxv,old_nyv=nyv;
847    game_object *hit_object=try_move(x,y,nxv,nyv,3);   // now find out what velocity is safe to use
848
849/*    if (get_cflag(CFLAG_STOPPABLE))
850    {
851      game_object *r=current_level->boundary_setback(exclude,x,y,nxv,nyv,1);
852      if (r) hit_object=r;
853    }*/
854
855    x+=nxv;
856    y+=nyv;
857    if (old_nxv!=nxv || old_nyv!=nyv)
858    {
859      int32_t lx=last_tile_hit_x,ly=last_tile_hit_y;
860      stop();
861      if (old_nxv==0)
862      {
863    if (old_nyv>0) ret|=BLOCKED_DOWN;
864    else if (old_nyv<0) ret|=BLOCKED_UP;
865      } else if (old_nyv==0)
866      {
867    if (old_nxv>0) ret|=BLOCKED_RIGHT;
868    else if (old_nxv<0) ret|=BLOCKED_LEFT;
869      } else
870      {
871    int32_t tx=(old_nxv>0 ? 1 : -1),ty=0;
872    try_move(x,y,tx,ty,3);
873    if (!tx)
874      ret|=(old_nxv>0 ? BLOCKED_RIGHT : BLOCKED_LEFT);
875    else tx=0;
876
877    ty=(old_nyv>0 ? 1 : -1);
878    try_move(x,y,tx,ty,3);
879    if (!ty)
880      ret|=(old_nyv>0 ? BLOCKED_DOWN : BLOCKED_UP);
881
882    if (!ret)
883      ret|=(old_nyv>0 ? BLOCKED_DOWN : BLOCKED_UP) | (old_nxv>0 ? BLOCKED_RIGHT : BLOCKED_LEFT);
884
885      }
886
887      void *rlist=NULL;   // return list
888      PtrRef r1(rlist);
889
890      if (hit_object)
891      {
892    push_onto_list(LPointer::Create(hit_object),rlist);
893    push_onto_list(l_object,rlist);
894      } else
895      {
896    push_onto_list(LNumber::Create(ly),rlist);
897    push_onto_list(LNumber::Create(lx),rlist);
898    push_onto_list(l_tile,rlist);
899      }
900      push_onto_list(LNumber::Create(ret),rlist);
901
902      return rlist;
903    } else return true_symbol;
904  }
905  return true_symbol;
906}
907
908int game_object::tick()      // returns blocked status
909{
910  int blocked=0;
911
912  int32_t xt=0,yt=2;
913  try_move(x,y-2,xt,yt,1);    // make sure we are not falling through the floor
914  y=y-2+yt;
915
916  if (flags()&FLAG_JUST_BLOCKED)
917    set_flags(flags()-FLAG_JUST_BLOCKED);
918
919  if (gravity() && !floating())
920  {
921    int fya;
922    if (yacel()>=0)
923      fya=sfyacel()+200;
924    else
925      fya=sfyacel()-200;
926
927    set_yacel(yacel()+(fya>>8));
928    set_fyacel(fya&255);
929  }
930
931  // first let's move the guy acording to his physics
932  int32_t xa=xacel(),ya=yacel(),fxa=sfxacel(),fya=sfyacel();
933  if (xa || ya || fxa || fya)
934  {
935    int fxv=sfxvel(),fyv=sfyvel();
936    fxv+=fxa;  fyv+=fya;
937    //int32_t xv=xvel()+xa+(fxv>>8);
938    set_xvel(xvel()+xa+(fxv>>8));
939    set_yvel(yvel()+ya+(fyv>>8));
940    set_fxvel(fxv&0xff);
941    set_fyvel(fyv&0xff);
942  }
943
944  // check to see if this advancement causes him to collide with objects
945  int32_t old_vy=yvel(),old_vx=xvel();  // save the correct veloicties
946
947  if (old_vx || old_vy)
948  {
949    int up=0;
950    if (yvel()<=0)  // if we are going up or a strait across check up and down
951    up=2;
952    int32_t xv=xvel(),yv=yvel();
953    game_object *h=try_move(x,y,xv,yv,1|up);       // now find out what velocity is safe to use
954    set_xvel(xv);
955    set_yvel(yv);
956    x+=xv;
957    y+=yv;
958
959    if (h && stoppable()) return BLOCKED_LEFT|BLOCKED_RIGHT;
960
961    if (xv!=old_vx || yv!=old_vy)     // he collided with something
962    {
963      if (gravity())                         // was he going up or down?
964      {
965    int32_t fall_xv=0,old_fall_vy,fall_vy;
966    old_fall_vy=fall_vy=old_vy-yvel();             // make sure he gets all of his yvel
967    try_move(x,y,fall_xv,fall_vy,1|up);
968    if (old_vy>0 && fall_vy<old_fall_vy)       // he was trying to fall, but he hit the ground
969    {
970      if (old_vy>0)
971      {
972        blocked|=BLOCKED_DOWN;
973
974        if (!xvel() && has_sequence(end_run_jump))
975        {
976          set_xvel(old_vx);
977          set_state(end_run_jump);
978        }
979        else set_state(stopped);
980      }
981      else blocked|=BLOCKED_UP;
982
983
984      if (state==run_jump_fall)
985      {
986        if (has_sequence(running))
987        set_state(running);
988        else
989        {
990          stop_x();
991          set_state(stopped);
992        }
993      }
994      else
995      {
996        set_yacel(0);
997        set_fyacel(0);
998        set_yvel(0);
999        set_fyvel(0);
1000        set_gravity(0);
1001      }
1002
1003    } else
1004    {
1005      if (old_vy!=0)
1006      {
1007        int32_t testx=old_vx<0 ? -1 : 1,testy=0;    // see if we were stopped left/right
1008                                                     // or just up down
1009        try_move(x,y,testx,testy,1|up);
1010        if (testx==0)                           // blocked left/right, set flag
1011        {
1012          if (old_vx<0)
1013            blocked|=BLOCKED_LEFT;
1014          else
1015            blocked|=BLOCKED_RIGHT;
1016        }
1017        else if (old_vy<0)
1018          blocked|=BLOCKED_UP;
1019        else if (old_vy>0)
1020          blocked|=BLOCKED_DOWN;
1021
1022      } else if (old_vx<0)   // we can skip left/right test because player wasn't moving up/down
1023            blocked|=BLOCKED_LEFT;
1024      else
1025            blocked|=BLOCKED_RIGHT;
1026
1027      set_xvel(0);
1028      set_fxvel(0);
1029      if (old_vy<0 && fall_vy>0)
1030      {
1031        set_yvel(yvel()+fall_vy);
1032      } else set_yvel(yvel()+fall_vy);
1033    }
1034    y+=fall_vy;
1035      }
1036      else                  // see if we can make him 'climb' the hill
1037      {
1038    int32_t ox=x,oy=y;       // rember orginal position in case climb doesn't work
1039
1040    int32_t climb_xvel=0,climb_yvel=-5;        // try to move up one pixel to step over the
1041    try_move(x,y,climb_xvel,climb_yvel,3);  // jutting polygon line
1042    y+=climb_yvel;
1043
1044    climb_xvel=old_vx-xvel();
1045    climb_yvel=-(abs(climb_xvel));        // now try 45 degree slope
1046    try_move(x,y,climb_xvel,climb_yvel,3);
1047
1048    if (abs(climb_xvel)>0)  // see if he got further by climbing
1049    {
1050      blocked=blocked&(~(BLOCKED_LEFT|BLOCKED_RIGHT));
1051      x+=climb_xvel;
1052      y+=climb_yvel;
1053
1054      set_xvel(xvel()+climb_xvel);
1055      set_yvel(0);
1056      set_fyvel(0);
1057
1058      // now put him back on the ground
1059      climb_yvel=abs(climb_xvel)+5;               // plus one to put him back on the ground
1060      climb_xvel=0;
1061      try_move(x,y,climb_xvel,climb_yvel,1);
1062      if (climb_yvel)
1063          y+=climb_yvel;
1064    }
1065    else
1066    {
1067      if (old_vx<0)
1068          blocked|=BLOCKED_LEFT;
1069      else
1070          blocked|=BLOCKED_RIGHT;
1071      set_state(stopped);      // nope, musta hit a wall, stop the poor fella
1072      x=ox;
1073      y=oy;
1074    }
1075
1076      }
1077
1078      if (xvel()!=old_vx && state!=run_jump_fall && state!=end_run_jump)
1079      {
1080    set_xacel(0);
1081    set_fxacel(0);
1082      }
1083    }
1084  }
1085
1086  if (yacel()==0 && !gravity())       // he is not falling, make sure he can't
1087  {
1088    int32_t nvx=0,nvy=yvel()+12;  // check three pixels below for ground
1089    try_move(x,y,nvx,nvy,1);
1090    if (nvy>11)                    // if he falls more than 2 pixels, then he falls
1091    {
1092      if (state!=run_jump_fall)      // make him fall
1093      {
1094        if (has_sequence(run_jump_fall))
1095          set_state(run_jump_fall);
1096        set_gravity(1);
1097      }
1098    } else if (nvy)   // if he fells less than 3 pixels then let him descend 'hills'
1099    {
1100      y+=nvy;
1101      blocked|=BLOCKED_DOWN;
1102    }
1103  }
1104  return blocked;
1105}
1106
1107void game_object::defaults()
1108{
1109  set_state(state);
1110  if (otype!=0xffff)
1111  {
1112    int s=get_ability(otype,start_hp);
1113    if (s!=default_simple.hp())
1114      set_hp(s);
1115  }
1116}
1117
1118void game_object::frame_advance()
1119{
1120  int ad=current_sequence()->get_advance(current_frame);
1121  if (ad && current_level)
1122  {
1123    int32_t xv;
1124    if (direction>0) xv=ad; else xv=-ad;
1125    int32_t yv=0;
1126    try_move(x,y,xv,yv,3);
1127    x+=xv;
1128  }
1129}
1130
1131void game_object::set_state(character_state s, int frame_direction)
1132{
1133  if (has_sequence(s))
1134    state=s;
1135  else state=stopped;
1136
1137  current_frame=0;
1138  if (frame_direction!=1)
1139    set_frame_dir(frame_direction);
1140
1141  frame_advance();
1142}
1143
1144
1145game_object *create(int type, int32_t x, int32_t y, int skip_constructor, int aitype)
1146{
1147  game_object *g=new game_object(type,skip_constructor);
1148  g->x=x; g->y=y; g->last_x=x; g->last_y=y;
1149  if (aitype)
1150    g->set_aitype(aitype);
1151  if (figures[type]->get_fun(OFUN_CONSTRUCTOR) && !skip_constructor)
1152  {
1153    game_object *o=current_object;
1154    current_object=g;
1155
1156    void *m = LSpace::Tmp.Mark();
1157
1158    time_marker *prof1=NULL;
1159    if (profiling())
1160      prof1=new time_marker;
1161
1162    ((LSymbol *)figures[type]->get_fun(OFUN_CONSTRUCTOR))->EvalFunction(NULL);
1163    if (profiling())
1164    {
1165      time_marker now;
1166      profile_add_time(type,now.diff_time(prof1));
1167      delete prof1;
1168    }
1169
1170    LSpace::Tmp.Restore(m);
1171    current_object = o;
1172  }
1173  return g;
1174}
1175
1176int base_size()
1177{
1178  return 1+
1179         1*8+
1180     2*5+
1181     4*7;
1182}
1183
1184int game_object::size()
1185{
1186  return base_size();
1187}
1188
1189
1190
1191int game_object::move(int cx, int cy, int button)
1192{
1193  int ret=0;
1194
1195  if (figures[otype]->get_fun(OFUN_MOVER))      // is a lisp move function defined?
1196  {
1197    LList *lcx, *lcy, *lb;
1198
1199    game_object *o=current_object;
1200    current_object=this;
1201
1202    // make a list of the parameters, and call the lisp function
1203    lcx = LList::Create();
1204    PtrRef r1(lcx);
1205    lcx->car = LNumber::Create(cx);
1206
1207    lcy = LList::Create();
1208    PtrRef r2(lcy);
1209    lcy->car = LNumber::Create(cy);
1210
1211    lb = LList::Create();
1212    PtrRef r3(lb);
1213    lb->car = LNumber::Create(button);
1214
1215    lcx->cdr = lcy;
1216    lcy->cdr = lb;
1217
1218    void *m = LSpace::Tmp.Mark();
1219
1220    time_marker *prof1 = NULL;
1221    if (profiling())
1222      prof1=new time_marker;
1223
1224    LObject *r = ((LSymbol *)figures[otype]->get_fun(OFUN_MOVER))->EvalFunction(lcx);
1225    if (profiling())
1226    {
1227      time_marker now;
1228      profile_add_time(this->otype,now.diff_time(prof1));
1229      delete prof1;
1230    }
1231
1232    LSpace::Tmp.Restore(m);
1233
1234    if (item_type(r)!=L_NUMBER)
1235    {
1236      ((LObject *)r)->Print();
1237      lbreak("Object %s did not return a number from its mover function!\n"
1238         "It should return a number to indicate its blocked status to the\n"
1239         "ai function.", object_names[otype]);
1240    }
1241    ret |= lnumber_value(r);
1242    current_object = o;
1243  }
1244  else ret |= mover(cx, cy, button);
1245
1246  return ret;
1247}
1248
1249int game_object::mover(int cx, int cy, int button)  // return false if the route is blocked
1250{
1251  if (hp()<=0)
1252    return tick();
1253
1254  if (flinch_state(state))                    // flinching? don't move
1255    cx=cy=button=0;
1256
1257
1258  if (cx)          // x movement suggested?
1259  {
1260    if (state==stopped)   // see if started moving
1261    {
1262      if (has_sequence(running))
1263      {
1264    if (cx>0)
1265    {
1266      direction=1;
1267          set_xvel(get_ability(type(),run_top_speed));
1268    }
1269    else
1270    {
1271      direction=-1;
1272      set_xacel(-get_ability(type(),run_top_speed));
1273    }
1274    set_state(running);
1275      }
1276    } else if (state==run_jump || state==run_jump_fall)
1277    {
1278      if (cx>0)
1279      {
1280    direction=1;
1281    set_xacel(get_ability(type(),start_accel));           // set the appropriate accel
1282      } else
1283      {
1284    direction=-1;
1285    set_xacel(-get_ability(type(),start_accel));           // set the appropriate accel
1286      }
1287    }
1288    else
1289    {
1290      // turn him around if he pressed the other way, he is not walking so a fast turn
1291      // is needed, don't go through the turn sequence
1292      if ((cx>0  && direction<0) || (cx<0 && direction>0))
1293        direction=-direction;
1294    }
1295  }         // not pressing left or right, so slow down or stop
1296  else if (!gravity() && state!=start_run_jump)
1297  {
1298    int32_t stop_acel;
1299    if (xvel()<0)                                    // he was going left
1300    {
1301      stop_acel=get_ability(type(),stop_accel);    // find out how fast he can slow down
1302      if (xvel()+stop_acel>=0)                       // if this acceleration is enough to stop him
1303      {
1304    stop_x();
1305    if (!gravity())
1306      set_state(stopped);
1307      } else { set_xacel(stop_acel); }
1308    } else if (xvel()>0)
1309    {
1310      stop_acel=-get_ability(type(),stop_accel);
1311      if (xvel()+stop_acel<=0)
1312      {
1313    stop_x();
1314    if (!gravity())
1315      set_state(stopped);
1316      } else set_xacel(stop_acel);
1317    } else if (!gravity())                // don't stop in the air
1318    {
1319      set_xacel(0);
1320      set_fxacel(0);
1321      // Check to see if we should go to stop state
1322      if (state==running)
1323        set_state(stopped);
1324    }
1325  }
1326
1327
1328/*  if (state==still_jump || state==still_jump_fall || state==end_still_jump)
1329  {
1330    set_xacel(0);
1331    set_fxacel(0);
1332    if (xvel()>0) set_xvel(get_ability(type(),jump_top_speed));
1333    else if (xvel()<0) set_xvel(-get_ability(type(),jump_top_speed));
1334  } else if (state==run_jump || state==run_jump_fall || state==end_run_jump)
1335  {
1336    set_xacel(0);
1337    set_fxacel(0);
1338    if (xvel()>0) set_xvel(get_ability(type(),jump_top_speed));
1339    else if (xvel()<0) set_xvel(-get_ability(type(),jump_top_speed));
1340  } */
1341
1342  // see if the user said to jump
1343  if (cy<0 && !floating() && !gravity())
1344  {
1345    set_gravity(1);
1346    set_yvel(get_ability(type(),jump_yvel));
1347//    if (cx && has_sequence(run_jump))
1348      set_state(run_jump);
1349    if (xvel()!=0)
1350    {
1351      if (direction>0)
1352        set_xvel(get_ability(type(),jump_top_speed));
1353      else
1354        set_xvel(-get_ability(type(),jump_top_speed));
1355    }
1356    set_xacel(0);
1357
1358
1359/*    if (state==stopped)
1360    {
1361      if (cx && has_sequence(start_run_jump))
1362        set_state(start_run_jump);
1363      else if (has_sequence(start_still_jump))
1364        set_state(start_still_jump);
1365      else
1366      {
1367
1368      }
1369    }
1370    else if (state==running && has_sequence(run_jump))
1371    {
1372      set_state(run_jump);
1373      set_yvel(get_ability(type(),jump_yvel));
1374      set_gravity(1);
1375    }
1376    else if (state==walking || state==turn_around)  // if walking check to see if has a
1377    {
1378      if (has_sequence(start_still_jump))
1379        set_state(start_still_jump);
1380      else
1381      {
1382        set_yvel(get_ability(type(),jump_yvel));
1383        set_gravity(1);
1384        if (has_sequence(run_jump))
1385          set_state(run_jump);
1386      }
1387    }    */
1388  }
1389
1390
1391
1392  if (state==run_jump && yvel()>0)
1393    set_state(run_jump_fall);
1394
1395
1396
1397//  if (state!=end_still_jump && state!=end_run_jump)
1398  {
1399    if (cx>0)
1400      set_xacel(get_ability(type(),start_accel));
1401    else if (cx<0)
1402      set_xacel(-get_ability(type(),start_accel));
1403  }
1404
1405  // make sure they are not going faster than their maximum speed
1406  int top_speed;
1407  if (state==stopped || state==end_run_jump)
1408    top_speed=get_ability(type(),walk_top_speed);
1409  else if (state==running)
1410    top_speed=get_ability(type(),run_top_speed);
1411  else if (state==run_jump || state==run_jump_fall || state==start_run_jump)
1412  {
1413    top_speed=get_ability(type(),jump_top_speed);
1414    if (!cx) top_speed=0;
1415  }
1416  else top_speed=1000;
1417
1418
1419  if (abs(xvel()+xacel())>top_speed)
1420  {
1421    if (xacel()<0) set_xacel(-top_speed-xvel());
1422    else set_xacel(top_speed-xvel());
1423  }
1424
1425  character_state old_state=state;
1426  int old_frame=current_frame;
1427  int tick_stat=tick();
1428
1429  // if he started to jump and slammed into a wall then make sure he stays in this state
1430  // so he can finish the jump
1431  if (!tick_stat && (old_state==start_run_jump))
1432  {
1433    set_state(old_state);
1434    current_frame=old_frame;
1435    next_picture();
1436  }
1437
1438  return tick_stat;
1439
1440}
1441
1442
1443
1444
1445game_object *game_object::bmove(int &whit, game_object *exclude)
1446{
1447
1448  // first let's move the guy acording to his physics
1449  int32_t xa=xacel(),ya=yacel(),fxa=sfxacel(),fya=sfyacel();
1450  if (xa || ya || fxa || fya)
1451  {
1452    int fxv=sfxvel(),fyv=sfyvel();
1453    fxv+=fxa;  fyv+=fya;
1454    //int32_t xv=xvel()+xa+(fxv>>8);
1455    set_xvel(xvel()+xa+(fxv>>8));
1456    set_yvel(yvel()+ya+(fyv>>8));
1457    set_fxvel(fxv&0xff);
1458    set_fyvel(fyv&0xff);
1459  }
1460
1461  int32_t ox2,oy2;
1462
1463  int32_t nx=x+xvel(),nfx=fx()+fxvel(),ny=y+yvel(),nfy=fy()+fyvel();
1464  nx+=nfx>>8;
1465  ny+=nfy>>8;
1466
1467
1468  // check to see if this advancement causes him to collide with objects
1469  ox2=nx;
1470  oy2=ny;  // save the correct veloicties
1471
1472  current_level->foreground_intersect(x,y,nx,ny);  // first see how far we can travel
1473  game_object *ret=current_level->all_boundary_setback(exclude,x,y,nx,ny);
1474  x=nx;
1475  y=ny;
1476  set_fx(nfx&0xff);
1477  set_fy(nfy&0xff);
1478  if (ret)
1479  {
1480    if (!ret->hurtable())   // object is not hurtable, return as if hit wall.
1481    { whit=1;
1482      return NULL;
1483    } else
1484    return ret;
1485  }
1486  else
1487  {
1488    whit=(nx!=ox2 || ny!=oy2);
1489    return NULL;
1490  }
1491}
1492
1493
1494
1495int object_to_number_in_list(game_object *who, object_node *list)
1496{
1497  int x=1;
1498  while (list)
1499  {
1500    if (who==list->me) return x;
1501    else list=list->next;
1502    x++;
1503  }
1504  return 0;
1505}
1506
1507game_object *number_to_object_in_list(int32_t x, object_node *list)
1508{
1509  if (!x) return NULL; x--;
1510  while (x && list) { list=list->next; x--; }
1511  if (list) return list->me;
1512  else return NULL;
1513}
1514
1515
1516void delete_object_list(object_node *first)
1517{
1518  while (first)
1519  {
1520    object_node *n=first;
1521    first=first->next;
1522    delete n;
1523  }
1524}
1525
1526
1527int32_t object_list_length(object_node *list)
1528{
1529  int32_t x=0;
1530  while (list) { list=list->next; x++; }
1531  return x;
1532
1533}
1534
1535
1536
1537game_object::game_object(int Type, int load)
1538{
1539  lvars = NULL;
1540
1541  if (Type<0xffff)
1542  {
1543    int t = figures[Type]->tv;
1544    if (t)
1545    {
1546      lvars = (int32_t *)malloc(t * sizeof(int32_t));
1547      memset(lvars, 0, t * sizeof(int32_t));
1548    }
1549  }
1550
1551  otype=Type;
1552  if (!load) defaults();
1553}
1554
1555
1556int game_object::reduced_state()
1557{
1558  int32_t x=0;
1559  for (int i=0; i<figures[otype]->ts; i++)
1560  {
1561    if (i==state) return x;
1562      else
1563    if (figures[otype]->seq[i]) x++;
1564  }
1565  return 0;
1566}
1567
1568
1569void game_object::change_aitype(int new_type)
1570{
1571  set_aitype(new_type);
1572  if (otype<0xffff)
1573  {
1574    void *f=figures[otype]->get_fun(OFUN_CHANGE_TYPE);
1575    if (f)
1576    {
1577      game_object *o=current_object;
1578      current_object=(game_object *)this;
1579
1580      time_marker *prof1=NULL;
1581      if (profiling())
1582        prof1=new time_marker;
1583
1584      ((LSymbol *)f)->EvalUserFunction(NULL);
1585
1586      if (profiling())
1587      {
1588    time_marker now;
1589    profile_add_time(this->otype,now.diff_time(prof1));
1590    delete prof1;
1591      }
1592
1593
1594      current_object=o;
1595    }
1596  }
1597}
1598
1599
1600void game_object::change_type(int new_type)
1601{
1602  free(lvars);     // free old variable
1603  lvars = NULL;
1604
1605  if (otype<0xffff)
1606  {
1607    int t = figures[new_type]->tv;
1608    if (t)
1609    {
1610      lvars = (int32_t *)malloc(t * sizeof(int32_t));
1611      memset(lvars, 0, t * sizeof(int32_t));
1612    }
1613  }
1614  else return;
1615  otype=new_type;
1616
1617  if (figures[new_type]->get_fun(OFUN_CONSTRUCTOR))
1618  {
1619    game_object *o=current_object;
1620    current_object=this;
1621
1622    void *m = LSpace::Tmp.Mark();
1623
1624    time_marker *prof1=NULL;
1625    if (profiling())
1626      prof1=new time_marker;
1627
1628    ((LSymbol *)figures[new_type]->get_fun(OFUN_CONSTRUCTOR))->EvalFunction(NULL);
1629    if (profiling())
1630    {
1631      time_marker now;
1632      profile_add_time(otype,now.diff_time(prof1));
1633      delete prof1;
1634    }
1635
1636    LSpace::Tmp.Restore(m);
1637    current_object = o;
1638  }
1639}
Note: See TracBrowser for help on using the repository browser.