source: abuse/trunk/src/collide.cpp @ 494

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

style: remove trailing spaces, fix copyright statements.

File size: 5.7 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 or
8 *  Jonathan Clark.
9 */
10
11#include "config.h"
12
13#include "level.h"
14#include "intsect.h"
15
16class collide_patch
17{
18  public :
19  int32_t total,x1,y1,x2,y2;
20  game_object **touch;
21  collide_patch *next;
22  collide_patch(int32_t X1, int32_t Y1, int32_t X2, int32_t Y2, collide_patch *Next)
23  {
24    x1=X1; y1=Y1; x2=X2; y2=Y2;
25    next=Next;
26    total=0;
27    touch=NULL;
28  }
29  void add_collide(int32_t X1, int32_t Y1, int32_t X2, int32_t Y2, game_object *who);
30  collide_patch *copy(collide_patch *Next);
31  ~collide_patch() { if (total) free(touch); }
32} ;
33
34
35collide_patch *collide_patch::copy(collide_patch *Next)
36{
37  collide_patch *p=new collide_patch(x1,y1,x2,y2,Next);
38  p->total=total;
39  if (total)
40  {
41    p->touch=(game_object **)malloc(total*sizeof(game_object *));
42    memcpy(p->touch,touch,total*(sizeof(game_object *)));
43  }
44  else
45    p->touch=NULL;
46  return p;
47}
48
49
50void add_collide(collide_patch *&first, int32_t x1, int32_t y1, int32_t x2, int32_t y2,
51                game_object *who)
52{
53  collide_patch *next;
54  for (collide_patch *p=first; p; p=next)
55  {
56    next=p->next;
57    // first see if light patch we are adding is enclosed entirely by another patch
58    if (x1>=p->x1 && y1>=p->y1 && x2<=p->x2 && y2<=p->y2)
59    {
60      if (x1>p->x1)
61      {
62    first=p->copy(first);
63    first->x2=x1-1;
64      }
65      if (x2<p->x2)
66      {
67    first=p->copy(first);
68    first->x1=x2+1;
69      }
70      if (y1>p->y1)
71      {
72    first=p->copy(first);
73    first->x1=x1;
74    first->x2=x2;
75    first->y2=y1-1;
76      }
77      if (y2<p->y2)
78      {
79    first=p->copy(first);
80    first->x1=x1;
81    first->x2=x2;
82    first->y1=y2+1;
83      }
84      p->x1=x1; p->y1=y1; p->x2=x2; p->y2=y2;
85
86      p->total++;
87      p->touch=(game_object **)realloc(p->touch,sizeof(game_object *)*p->total);
88      p->touch[p->total-1]=who;
89      return ;
90    }
91
92    // see if the patch completly covers another patch.
93    if (x1<=p->x1 && y1<=p->y1 && x2>=p->x2 && y2>=p->y2)
94    {
95      if (x1<p->x1)
96        add_collide(first,x1,y1,p->x1-1,y2,who);
97      if (x2>p->x2)
98        add_collide(first,p->x2+1,y1,x2,y2,who);
99      if (y1<p->y1)
100        add_collide(first,p->x1,y1,p->x2,p->y1-1,who);
101      if (y2>p->y2)
102        add_collide(first,p->x1,p->y2+1,p->x2,y2,who);
103      p->total++;
104      p->touch=(game_object **)realloc(p->touch,sizeof(game_object *)*p->total);
105      p->touch[p->total-1]=who;
106      return ;
107    }
108
109    // see if we intersect another rect
110    if (!(x2<p->x1 || y2<p->y1 || x1>p->x2 || y1>p->y2))
111    {
112      int ax1,ay1,ax2,ay2;
113      if (x1<p->x1)
114      {
115        add_collide(first,x1,Max(y1,p->y1),p->x1-1,Min(y2,p->y2),who);
116    ax1=p->x1;
117      } else
118    ax1=x1;
119
120      if (x2>p->x2)
121      {
122        add_collide(first,p->x2+1,Max(y1,p->y1),x2,Min(y2,p->y2),who);
123    ax2=p->x2;
124      }
125      else
126    ax2=x2;
127
128      if (y1<p->y1)
129      {
130        add_collide(first,x1,y1,x2,p->y1-1,who);
131    ay1=p->y1;
132      } else
133    ay1=y1;
134
135      if (y2>p->y2)
136      {
137        add_collide(first,x1,p->y2+1,x2,y2,who);
138    ay2=p->y2;
139      } else
140    ay2=y2;
141
142
143      add_collide(first,ax1,ay1,ax2,ay2,who);
144
145      return ;
146    }
147
148  }
149
150  first=new collide_patch(x1,y1,x2,y2,first);
151  first->total=1;
152  first->touch=(game_object **)malloc(sizeof(game_object *)*1);
153  first->touch[0]=who;
154}
155
156
157
158
159void level::check_collisions()
160{
161  game_object *target,*rec,*subject;
162  int32_t sx1,sy1,sx2,sy2,tx1,ty1,tx2,ty2,hitx=0,hity=0,t_centerx;
163
164  for (int l=0; l<attack_total; l++)
165  {
166    subject=attack_list[l];
167    subject->picture_space(sx1,sy1,sx2,sy2);
168    rec=NULL;
169
170
171    for (int j=0; j<target_total && !rec; j++)
172    {
173      target=target_list[j];
174      target->picture_space(tx1,ty1,tx2,ty2);
175      if (!(sx2<tx1 || sy2<ty1 || sx1>tx2 || sy1>ty2))  // check to see if picture spaces collide
176      {
177
178    try_pushback(subject,target);
179
180    if (subject->can_hurt(target))    // see if we can hurt him before calculating
181    {
182      t_centerx=target->x_center();
183      point_list *s_hit,*t_damage;
184
185      s_hit=subject->current_figure()->hit;
186
187      if (target->direction>0)
188        t_damage=target->current_figure()->f_damage;
189      else
190        t_damage=target->current_figure()->b_damage;
191
192      unsigned char *s_dat=s_hit->data,*t_dat;
193      int i,j;
194      for (i=(int)s_hit->tot-1; i>0 && !rec; i--)
195      {
196        for (t_dat=t_damage->data,j=(int)t_damage->tot-1; j>0 && !rec; j--)
197        {
198          int32_t x1,y1,x2,y2,          // define the two line segments to check
199          xp1,yp1,xp2,yp2;
200
201          xp1=target->x+target->tx(*t_dat);  t_dat++;
202          yp1=target->y+target->ty(*t_dat);  t_dat++;
203          xp2=target->x+target->tx(*t_dat);
204          yp2=target->y+target->ty(t_dat[1]);
205
206          x1=subject->x+subject->tx(s_dat[0]);
207          y1=subject->y+subject->ty(s_dat[1]);
208          x2=subject->x+subject->tx(s_dat[2]);
209          y2=subject->y+subject->ty(s_dat[3]);
210
211
212          // ok, now we know which line segemnts to check for intersection
213          // now check to see if (x1,y1-x2,y2) intercest with (xp1,yp1-xp2,yp2)
214          int32_t _x2=x2,_y2=y2;
215          setback_intersect(x1, y1, x2, y2, xp1, yp1, xp2, yp2,0);
216
217
218          if (x2!=_x2 || _y2!=y2)
219          {
220        rec=target;
221        hitx=((x1+x2)/2+(xp1+xp2)/2)/2;
222        hity=((y1+y1)/2+(yp1+yp2)/2)/2;
223          }
224        }
225        s_dat+=2;
226      }
227    }
228      }
229    }
230    if (rec)
231    {
232      rec->do_damage((int)subject->current_figure()->hit_damage,subject,hitx,hity,0,0);
233      subject->note_attack(rec);
234    }
235  }
236}
237
Note: See TracBrowser for help on using the repository browser.