[56] | 1 | /* |
---|
| 2 | * Abuse - dark 2D side-scrolling platform game |
---|
| 3 | * Copyright (c) 1995 Crack dot Com |
---|
[494] | 4 | * Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net> |
---|
[56] | 5 | * |
---|
| 6 | * This software was released into the Public Domain. As with most public |
---|
[555] | 7 | * domain software, no warranty is made or implied by Crack dot Com, by |
---|
| 8 | * Jonathan Clark, or by Sam Hocevar. |
---|
[56] | 9 | */ |
---|
| 10 | |
---|
[732] | 11 | #if HAVE_CONFIG_H |
---|
[555] | 12 | # include "config.h" |
---|
| 13 | #endif |
---|
[56] | 14 | |
---|
[686] | 15 | #include "common.h" |
---|
| 16 | |
---|
[2] | 17 | #include <stdlib.h> |
---|
| 18 | |
---|
[16] | 19 | void pushback(int32_t x1,int32_t y1,int32_t &x2,int32_t &y2, |
---|
[124] | 20 | int32_t xp1, int32_t yp1, int32_t xp2, int32_t yp2, int xdir, int ydir, int inside) |
---|
[2] | 21 | { |
---|
| 22 | |
---|
| 23 | // determine if the lines are intersecting before we set back |
---|
| 24 | |
---|
| 25 | // if (yp1==yp2) |
---|
| 26 | // { |
---|
| 27 | // if (inside && y2>=yp1) |
---|
[124] | 28 | |
---|
[2] | 29 | } |
---|
| 30 | |
---|
| 31 | |
---|
[16] | 32 | /* int setback_intersect(int32_t x1,int32_t y1,int32_t &x2,int32_t &y2, |
---|
[124] | 33 | int32_t xp1, int32_t yp1, int32_t xp2, int32_t yp2) |
---|
[2] | 34 | { |
---|
[16] | 35 | int32_t mx1,my1,b1,mx2,my2,b2,side1,side2,tx2,ty2; |
---|
[2] | 36 | my1=(y2-y1); |
---|
| 37 | if (!my1) // is the first line strait across? |
---|
| 38 | { |
---|
| 39 | side1=yp1-y2; // if yes, check to see if the other is top/bottom |
---|
| 40 | side2=yp2-y2; |
---|
| 41 | mx1=(x2-x1); // make sure we give this a value |
---|
| 42 | b1=y2; |
---|
| 43 | } |
---|
| 44 | else |
---|
| 45 | { |
---|
| 46 | mx1=(x2-x1); // if the line strait up and down? |
---|
| 47 | if (!mx1) |
---|
| 48 | { |
---|
| 49 | side1=xp1-x1; |
---|
[124] | 50 | side2=xp2-x2; |
---|
[2] | 51 | } else |
---|
| 52 | { |
---|
| 53 | b1=y1-(my1*x1/mx1); // calculate the y intercept |
---|
[124] | 54 | side1=yp1-((my1*xp1)/mx1+b1); |
---|
| 55 | side2=yp2-((my1*xp2)/mx1+b1); |
---|
[2] | 56 | } |
---|
| 57 | } |
---|
| 58 | if ((side1>=0 && side2<=0) || (side1<=0 && side2>=0)) |
---|
| 59 | { |
---|
| 60 | my2=(yp2-yp1); |
---|
| 61 | if (!my2) |
---|
| 62 | { |
---|
| 63 | side1=y1-yp2; |
---|
| 64 | side2=y2-yp2; |
---|
| 65 | b2=yp1; |
---|
| 66 | mx2=(xp2-xp1); |
---|
| 67 | } |
---|
| 68 | else |
---|
| 69 | { |
---|
| 70 | mx2=(xp2-xp1); |
---|
| 71 | if (!mx2) |
---|
| 72 | { |
---|
[124] | 73 | side1=x1-xp2; |
---|
| 74 | side2=x2-xp2; |
---|
[2] | 75 | } else |
---|
| 76 | { |
---|
[124] | 77 | b2=yp1-(my2*xp1/mx2); |
---|
| 78 | side1=y1-((my2*x1)/mx2+b2); |
---|
| 79 | side2=y2-((my2*x2)/mx2+b2); |
---|
[2] | 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | // check two wierd cases where the lines are parallel |
---|
[124] | 84 | if (mx1==mx2 && mx1==0) |
---|
[2] | 85 | if (y2<yp1 || y1>yp2) |
---|
[124] | 86 | side1=side2=1; |
---|
[2] | 87 | else side1=side2=0; |
---|
| 88 | |
---|
| 89 | if (my1==my2 && my2==0) |
---|
| 90 | if (x2<xp1 || x1>xp2) |
---|
| 91 | side1=side2=1; |
---|
| 92 | else side1=side2=0; |
---|
| 93 | |
---|
| 94 | if ((side1>=0 && side2<=0) || (side1<=0 && side2>=0)) |
---|
| 95 | { |
---|
| 96 | tx2=x2; ty2=y2; |
---|
| 97 | int xadd,yadd; |
---|
| 98 | if (x1>x2) xadd=1; else if (x1<x2) xadd=-1; else xadd=0; |
---|
| 99 | if (y1>y2) yadd=1; else if (y1<y2) yadd=-1; else yadd=0; |
---|
[124] | 100 | |
---|
[2] | 101 | // now find the point of intersection |
---|
| 102 | if (mx1==0) // if the first line is strait up & down |
---|
[124] | 103 | { |
---|
| 104 | if (mx2==0) // if they are both strait up then |
---|
| 105 | { tx2=x1; ty2=y1; } // they connect everywhere, don't let it move! |
---|
[2] | 106 | else |
---|
[124] | 107 | ty2=my2*x2/mx2+b2+yadd; |
---|
[2] | 108 | } else if (mx2==0) |
---|
| 109 | { tx2=xp1+xadd; |
---|
| 110 | ty2=my1*tx2/mx1+b1; |
---|
| 111 | } |
---|
| 112 | else |
---|
| 113 | { |
---|
| 114 | if (my1==0) |
---|
| 115 | if (my2==0) |
---|
| 116 | { tx2=x1; ty2=y1; } |
---|
| 117 | else tx2=mx2*(y1-b2)/my2+xadd; |
---|
| 118 | else if (my2==0) |
---|
[494] | 119 | { |
---|
[2] | 120 | ty2=yp1+yadd; |
---|
[494] | 121 | tx2=mx1*(ty2-b1)/my1; |
---|
| 122 | } |
---|
[124] | 123 | else |
---|
[2] | 124 | { |
---|
[686] | 125 | if (lol::abs(mx1)>lol::abs(my1)) |
---|
[2] | 126 | { |
---|
[16] | 127 | int32_t ae_bd=my1*mx2-mx1*my2; |
---|
[687] | 128 | ASSERT(ae_bd,"line intersect fuck up"); |
---|
[2] | 129 | tx2=(mx1*mx2*(b2-b1))/ae_bd+xadd; |
---|
| 130 | ty2=my1*tx2/mx1+b1; |
---|
| 131 | } |
---|
| 132 | else |
---|
| 133 | { |
---|
[16] | 134 | int32_t db_ea=(my2*mx1-mx2*my1); |
---|
[687] | 135 | ASSERT(db_ea,"line intersect fuck up"); |
---|
[2] | 136 | ty2=(mx1*b1*my2-my1*mx2*b2)/db_ea+yadd; |
---|
| 137 | tx2=mx1*(ty2-b1)/my1; |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
[686] | 142 | if (lol::abs(tx2-x1)<lol::abs(x2-x1) || lol::abs(ty2-y1)<lol::abs(y2-y1)) |
---|
[2] | 143 | { |
---|
| 144 | x2=tx2; |
---|
| 145 | y2=ty2; |
---|
| 146 | return 1; |
---|
[124] | 147 | } |
---|
[2] | 148 | } |
---|
| 149 | } |
---|
[124] | 150 | return 0; |
---|
[2] | 151 | } */ |
---|
| 152 | |
---|
| 153 | |
---|
[16] | 154 | int setback_intersect(int32_t x1,int32_t y1,int32_t &x2,int32_t &y2, |
---|
[124] | 155 | int32_t xp1, int32_t yp1, int32_t xp2, int32_t yp2, |
---|
[16] | 156 | int32_t inside) // which side is inside the polygon? (0 always setback) |
---|
[2] | 157 | { |
---|
| 158 | // the line equations will be put in the form |
---|
| 159 | // x(y2-y1)+y(x1-x2)-x1*y2+x2*y1=0 |
---|
[124] | 160 | // A B C |
---|
[2] | 161 | |
---|
[124] | 162 | int32_t a1,b1,c1,a2,b2,c2,r1,r2; |
---|
[2] | 163 | |
---|
[124] | 164 | a1=y2-y1; |
---|
| 165 | b1=x1-x2; |
---|
| 166 | c1=-x1*y2+x2*y1; |
---|
[2] | 167 | |
---|
| 168 | if (yp1<yp2 || (yp1==yp2 && xp1>xp2)) // use only increasing functions |
---|
[124] | 169 | { |
---|
[2] | 170 | r1=yp1; yp1=yp2; yp2=r1; // swap endpoints if wrong order |
---|
| 171 | r1=xp1; xp1=xp2; xp2=r1; |
---|
[124] | 172 | } |
---|
[2] | 173 | |
---|
[16] | 174 | int32_t xdiff,ydiff; |
---|
[686] | 175 | /* int32_t xdiff=lol::abs(xp1-xp2),ydiff=yp1-yp2; |
---|
[124] | 176 | if (xdiff>=ydiff) // increment the endpoints |
---|
[494] | 177 | if (xp2<xp1) { xp2--; xp1++; } |
---|
[124] | 178 | else { xp2++; xp1--; } |
---|
| 179 | |
---|
[2] | 180 | if (xdiff<=ydiff) |
---|
| 181 | { |
---|
[124] | 182 | yp1++; |
---|
[2] | 183 | yp2--; |
---|
| 184 | } */ |
---|
| 185 | |
---|
[124] | 186 | |
---|
[2] | 187 | r1=xp1*a1+yp1*b1+c1; |
---|
| 188 | r2=xp2*a1+yp2*b1+c1; |
---|
| 189 | |
---|
| 190 | if ((r1^r2)<=0 || r1==0 || r2==0) // signs must be different to intersect |
---|
| 191 | { |
---|
[124] | 192 | a2=yp2-yp1; |
---|
| 193 | b2=xp1-xp2; |
---|
| 194 | c2=-xp1*yp2+xp2*yp1; |
---|
[2] | 195 | r1=x1*a2+y1*b2+c2; |
---|
| 196 | r2=x2*a2+y2*b2+c2; |
---|
| 197 | |
---|
| 198 | if ((r1^r2)<=0 || r1==0 || r2==0) |
---|
| 199 | { |
---|
[124] | 200 | if ( ((xp1<xp2) && ((r2^inside)>0)) || |
---|
[2] | 201 | (xp1>=xp2 && ((r2^inside)<0)) || |
---|
[124] | 202 | inside==0 || r2==0) |
---|
| 203 | { |
---|
[494] | 204 | int32_t ae=a1*b2,bd=b1*a2; |
---|
[124] | 205 | if (ae!=bd) // co-linear returns 0 |
---|
| 206 | { |
---|
| 207 | x2=(b1*c2-b2*c1)/(ae-bd); |
---|
[494] | 208 | y2=(a1*c2-a2*c1)/(bd-ae); |
---|
[686] | 209 | xdiff=lol::abs(x2-x1); |
---|
| 210 | ydiff=lol::abs(y2-y1); |
---|
[124] | 211 | // if (xdiff<=ydiff) // push the intersection back one pixel |
---|
| 212 | // { |
---|
| 213 | if (y2!=y1) |
---|
[494] | 214 | { |
---|
[124] | 215 | if (y2>y1) |
---|
| 216 | y2--; |
---|
| 217 | else y2++; |
---|
| 218 | } |
---|
| 219 | // } |
---|
| 220 | // if (xdiff>=ydiff) |
---|
| 221 | // { |
---|
| 222 | if (x2!=x1) |
---|
| 223 | { |
---|
| 224 | if (x2>x1) |
---|
| 225 | x2--; |
---|
| 226 | else x2++; |
---|
| 227 | } |
---|
| 228 | // } |
---|
[2] | 229 | |
---|
[124] | 230 | if (inside) // check to make sure end point is on the |
---|
| 231 | { // right side |
---|
[494] | 232 | r1=x1*a2+y1*b2+c2; |
---|
| 233 | r2=x2*a2+y2*b2+c2; |
---|
[124] | 234 | if ((r2!=0 && ((r1^r2)<0))) |
---|
[494] | 235 | { |
---|
[124] | 236 | x2=x1; |
---|
| 237 | y2=y1; |
---|
[494] | 238 | } |
---|
| 239 | } |
---|
| 240 | return 1; |
---|
[124] | 241 | } |
---|
[2] | 242 | } |
---|
[124] | 243 | } |
---|
| 244 | } |
---|
[2] | 245 | return 0; |
---|
[124] | 246 | |
---|
[2] | 247 | } |
---|
| 248 | |
---|