Untitled

From Anonymous, 3 Years ago, written in Plain Text, viewed 56 times.
URL https://paste.bugabuse.net/view/89034563 Embed
Download Paste or View Raw
  1. diff -Naur /home/toil/teeworlds/src/game/client/components/players.cpp src/game/client/components/players.cpp
  2. --- /home/toil/teeworlds/src/game/client/components/players.cpp 2012-06-26 16:53:53.368860821 +1000
  3. +++ src/game/client/components/players.cpp      2012-07-07 23:49:37.941683878 +1000
  4. @@ -272,7 +272,7 @@
  5.         }
  6.  
  7.         bool Stationary = Player.m_VelX <= 1 && Player.m_VelX >= -1;
  8. -       bool InAir = !Collision()->CheckPoint(Player.m_X, Player.m_Y+16);
  9. +       bool InAir = !(Collision()->CheckPoint(Player.m_X-14, Player.m_Y+16) || Collision()->CheckPoint(Player.m_X+14, Player.m_Y+16));
  10.         bool WantOtherDir = (Player.m_Direction == -1 && Vel.x > 0) || (Player.m_Direction == 1 && Vel.x < 0);
  11.  
  12.         // evaluate animation
  13. diff -Naur /home/toil/teeworlds/src/game/collision.cpp src/game/collision.cpp
  14. --- /home/toil/teeworlds/src/game/collision.cpp 2012-06-26 16:53:53.376860790 +1000
  15. +++ src/game/collision.cpp      2012-07-08 15:06:05.856569985 +1000
  16. @@ -26,14 +26,14 @@
  17.         m_Width = m_pLayers->GameLayer()->m_Width;
  18.         m_Height = m_pLayers->GameLayer()->m_Height;
  19.         m_pTiles = static_cast<CTile *>(m_pLayers->Map()->GetData(m_pLayers->GameLayer()->m_Data));
  20. -
  21. +      
  22.         for(int i = 0; i < m_Width*m_Height; i++)
  23.         {
  24.                 int Index = m_pTiles[i].m_Index;
  25. -
  26. +              
  27.                 if(Index > 128)
  28.                         continue;
  29. -
  30. +              
  31.                 switch(Index)
  32.                 {
  33.                 case TILE_DEATH:
  34. @@ -45,35 +45,64 @@
  35.                 case TILE_NOHOOK:
  36.                         m_pTiles[i].m_Index = COLFLAG_SOLID|COLFLAG_NOHOOK;
  37.                         break;
  38. +               case TILE_SLOPE:
  39. +               {
  40. +                       m_pTiles[i].m_Index = COLFLAG_SLOPE;
  41. +                       m_pTiles[i].m_Flags = OrientationTile(m_pTiles[i].m_Flags, 1);
  42. +               }
  43. +                       break;
  44. +               case TILE_SLOPE_NH:
  45. +               {
  46. +                       m_pTiles[i].m_Index = COLFLAG_SLOPE|COLFLAG_NOHOOK;
  47. +                       m_pTiles[i].m_Flags = OrientationTile(m_pTiles[i].m_Flags, 1);
  48. +               }
  49. +                       break;
  50.                 default:
  51.                         m_pTiles[i].m_Index = 0;
  52.                 }
  53.         }
  54.  }
  55.  
  56. -int CCollision::GetTile(int x, int y)
  57. +int CCollision::GetTile(int x, int y, int *pFlags, int *pDx, int *pDy)
  58.  {
  59. -       int Nx = clamp(x/32, 0, m_Width-1);
  60. -       int Ny = clamp(y/32, 0, m_Height-1);
  61. +       int Address = clamp(y/32, 0, m_Height-1)*m_Width + clamp(x/32, 0, m_Width-1);
  62. +       int Index = m_pTiles[Address].m_Index;
  63. +      
  64. +       if(Index&COLFLAG_SLOPE)
  65. +       {
  66. +               int Flags = m_pTiles[Address].m_Flags;
  67. +               int Dx = x < 0 ? 31-abs(x)%32 : x%32;
  68. +               int Dy = y < 0 ? 31-abs(y)%32 : y%32;
  69. +
  70. +               if((Flags == 1 && Dy >= Dx) || (Flags == 3 && Dy+Dx <= 31) || (Flags == 5 && Dy <= Dx) || (Flags == 7 && Dy+Dx >= 31))
  71. +                       Index |= COLFLAG_SOLID;
  72. +               else if(Index&COLFLAG_NOHOOK)
  73. +                       Index ^= COLFLAG_NOHOOK;
  74. +
  75. +               if(pDx) *pDx = Dx;
  76. +               if(pDy) *pDy = Dy;
  77. +               if(pFlags) *pFlags = Flags;
  78. +}
  79. +
  80. +return Index > 128 ? 0 : Index;
  81.  
  82. -       return m_pTiles[Ny*m_Width+Nx].m_Index > 128 ? 0 : m_pTiles[Ny*m_Width+Nx].m_Index;
  83.  }
  84.  
  85.  bool CCollision::IsTileSolid(int x, int y)
  86.  {
  87. -       return GetTile(x, y)&COLFLAG_SOLID;
  88. +       return GetTile(x,y)&COLFLAG_SOLID;
  89.  }
  90.  
  91.  // TODO: rewrite this smarter!
  92.  int CCollision::IntersectLine(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision)
  93.  {
  94. -       float Distance = distance(Pos0, Pos1);
  95. -       int End(Distance+1);
  96. +       float d = distance(Pos0, Pos1);
  97. +       int End(d+1);
  98.         vec2 Last = Pos0;
  99. -
  100. +      
  101.         for(int i = 0; i < End; i++)
  102.         {
  103. -               float a = i/Distance;
  104. +               float a = i/d;
  105.                 vec2 Pos = mix(Pos0, Pos1, a);
  106.                 if(CheckPoint(Pos.x, Pos.y))
  107.                 {
  108. @@ -97,52 +126,127 @@
  109.  {
  110.         if(pBounces)
  111.                 *pBounces = 0;
  112. -
  113. +      
  114.         vec2 Pos = *pInoutPos;
  115. -       vec2 Vel = *pInoutVel;
  116. -       if(CheckPoint(Pos + Vel))
  117. +       vec2 NewPos = Pos + *pInoutVel;
  118. +       int Flags, Index = GetTile(round(NewPos.x), round(NewPos.y), &Flags);
  119. +
  120. +       if(Index&COLFLAG_SOLID)
  121.         {
  122.                 int Affected = 0;
  123. -               if(CheckPoint(Pos.x + Vel.x, Pos.y))
  124. +
  125. +               if(Index&COLFLAG_SLOPE)
  126. +               {
  127. +                       if(round(NewPos.x)/32 == round(Pos.x)/32 && round(NewPos.y)/32 == round(Pos.y)/32)//not correct, only approximative
  128. +                       {
  129. +                               vec2 Reflex = vec2(pInoutVel->y, pInoutVel->x); //inverting axisses values
  130. +                               if(Flags == 3 || Flags == 7)
  131. +                                       Reflex *= -1; //inverting sings
  132. +  
  133. +                               *pInoutVel = Elasticity? Reflex*Elasticity : (*pInoutVel+Reflex)*0.5f;
  134. +                               if(pBounces)
  135. +                                       (*pBounces)++;
  136. +                               Affected++;
  137. +
  138. +                               return;
  139. +                       }
  140. +               }
  141. +
  142. +               if(CheckPoint(NewPos.x, Pos.y))
  143.                 {
  144.                         pInoutVel->x *= -Elasticity;
  145.                         if(pBounces)
  146. -                               (*pBounces)++;
  147. +                               (*pBounces)++;                 
  148.                         Affected++;
  149.                 }
  150.  
  151. -               if(CheckPoint(Pos.x, Pos.y + Vel.y))
  152. +               if(CheckPoint(Pos.x, NewPos.y))
  153.                 {
  154.                         pInoutVel->y *= -Elasticity;
  155.                         if(pBounces)
  156. -                               (*pBounces)++;
  157. +                               (*pBounces)++;                 
  158.                         Affected++;
  159.                 }
  160. -
  161. +              
  162.                 if(Affected == 0)
  163. -               {
  164. -                       pInoutVel->x *= -Elasticity;
  165. -                       pInoutVel->y *= -Elasticity;
  166. -               }
  167. +                       *pInoutVel *= -Elasticity;
  168.         }
  169.         else
  170.         {
  171. -               *pInoutPos = Pos + Vel;
  172. +               *pInoutPos = NewPos;
  173.         }
  174.  }
  175.  
  176. -bool CCollision::TestBox(vec2 Pos, vec2 Size)
  177. +int CCollision::TestBox(vec2 Pos, vec2 Size)
  178.  {
  179. -       Size *= 0.5f;
  180. -       if(CheckPoint(Pos.x-Size.x, Pos.y-Size.y))
  181. -               return true;
  182. -       if(CheckPoint(Pos.x+Size.x, Pos.y-Size.y))
  183. -               return true;
  184. -       if(CheckPoint(Pos.x-Size.x, Pos.y+Size.y))
  185. -               return true;
  186. -       if(CheckPoint(Pos.x+Size.x, Pos.y+Size.y))
  187. -               return true;
  188. -       return false;
  189. +       Pos -= Size*0.5f;
  190. +       int Flags, Dx, Dy, Index = GetTile(round(Pos.x), round(Pos.y), &Flags, &Dx, &Dy);
  191. +       if(Index&COLFLAG_SOLID)
  192. +       {
  193. +               if(Index&COLFLAG_SLOPE && Flags == 3)
  194. +                       return 3;
  195. +               else
  196. +                       return 64;
  197. +       }
  198. +       else if(Index&COLFLAG_SLOPE)
  199. +       {
  200. +               if(Flags == 1 && Dy+Size.y > Dx)
  201. +                       return 64; //16|8
  202. +               else if(Flags == 5 && Dy < Dx+Size.x)
  203. +                       return 64; //32|2
  204. +       }
  205. +  
  206. +       Pos.x += Size.x;
  207. +       Index = GetTile(round(Pos.x), round(Pos.y), &Flags, &Dx, &Dy);
  208. +       if(Index&COLFLAG_SOLID)
  209. +       {
  210. +               if(Index&COLFLAG_SLOPE && Flags == 5)
  211. +                       return 5;
  212. +               else
  213. +                       return 64;
  214. +       }
  215. +       else if(Index&COLFLAG_SLOPE)
  216. +       {
  217. +               if(Flags == 3 && Dy+Dx < 31+Size.x)
  218. +                       return 64; // 32|1
  219. +               else if(Flags == 7 && Dy+Size.y+Dx > 31)
  220. +                       return 64; // 16|4
  221. +       }
  222. +  
  223. +       Pos.y += Size.y;
  224. +       Index = GetTile(round(Pos.x), round(Pos.y), &Flags, &Dx, &Dy);
  225. +       if(Index&COLFLAG_SOLID)
  226. +       {
  227. +               if(Index&COLFLAG_SLOPE && Flags == 7)
  228. +                       return 7;
  229. +               else
  230. +                       return 64;
  231. +       }
  232. +       else if(Index&COLFLAG_SLOPE)
  233. +       {    
  234. +               if(Flags == 1 && Dy+Size.x > Dx)
  235. +                       return 64; // 32|8
  236. +               else if(Flags == 5 && Dy-Size.y < Dx)
  237. +                       return 64; // 16|2
  238. +       }
  239. +       Pos.x -= Size.x;    
  240. +       Index = GetTile(round(Pos.x), round(Pos.y), &Flags, &Dx, &Dy);
  241. +       if(Index&COLFLAG_SOLID)
  242. +       {
  243. +               if(Index&COLFLAG_SLOPE && Flags == 1)
  244. +                       return 1;
  245. +               else
  246. +                       return 64;
  247. +       }
  248. +       else if(Index&COLFLAG_SLOPE)
  249. +       {
  250. +               if(Flags == 3 && Dy+Dx-Size.y < 31)
  251. +                       return 64; // 16|1
  252. +               else if(Flags == 7 && Dy+Dx > 31-Size.x)
  253. +                       return 64; // 32|4
  254. +       }
  255. +
  256. +       return 0;
  257.  }
  258.  
  259.  void CCollision::MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, float Elasticity)
  260. @@ -150,10 +254,10 @@
  261.         // do the move
  262.         vec2 Pos = *pInoutPos;
  263.         vec2 Vel = *pInoutVel;
  264. -
  265. +      
  266.         float Distance = length(Vel);
  267.         int Max = (int)Distance;
  268. -
  269. +      
  270.         if(Distance > 0.00001f)
  271.         {
  272.                 //vec2 old_pos = pos;
  273. @@ -163,42 +267,107 @@
  274.                         //float amount = i/(float)max;
  275.                         //if(max == 0)
  276.                                 //amount = 0;
  277. -
  278. +                      
  279.                         vec2 NewPos = Pos + Vel*Fraction; // TODO: this row is not nice
  280. -
  281. -                       if(TestBox(vec2(NewPos.x, NewPos.y), Size))
  282. +                      
  283. +                       if(TestBox(NewPos, Size))
  284.                         {
  285.                                 int Hits = 0;
  286.  
  287. -                               if(TestBox(vec2(Pos.x, NewPos.y), Size))
  288. +                               int Colliding = TestBox(vec2(NewPos.x, Pos.y), Size);                          
  289. +                               if(Colliding)
  290.                                 {
  291. -                                       NewPos.y = Pos.y;
  292. -                                       Vel.y *= -Elasticity;
  293. +                                       if(Colliding == 64)
  294. +                                               Vel.x *= -Elasticity;
  295. +                                       else if(IntersectSlope(Colliding, &Vel, Elasticity))
  296. +                                               Pos = vec2(round(Pos.x), round(Pos.y));
  297. +
  298.                                         Hits++;
  299.                                 }
  300. -
  301. -                               if(TestBox(vec2(NewPos.x, Pos.y), Size))
  302. +                              
  303. +                               Colliding = TestBox(vec2(Pos.x, NewPos.y), Size);
  304. +                               if(Colliding)
  305.                                 {
  306. -                                       NewPos.x = Pos.x;
  307. -                                       Vel.x *= -Elasticity;
  308. +                                       if(Colliding == 64)
  309. +                                               Vel.y *= -Elasticity;
  310. +                                       else if(IntersectSlope(Colliding, &Vel, Elasticity))
  311. +                                               Pos = vec2(round(Pos.x), round(Pos.y));
  312. +
  313.                                         Hits++;
  314.                                 }
  315. -
  316. +                              
  317.                                 // neither of the tests got a collision.
  318.                                 // this is a real _corner case_!
  319.                                 if(Hits == 0)
  320. -                               {
  321. -                                       NewPos.y = Pos.y;
  322. -                                       Vel.y *= -Elasticity;
  323. -                                       NewPos.x = Pos.x;
  324. -                                       Vel.x *= -Elasticity;
  325. -                               }
  326. -                       }
  327. +                                       Vel *= -Elasticity;
  328.  
  329. -                       Pos = NewPos;
  330. +                       }
  331. +                       else
  332. +                               Pos = NewPos;
  333.                 }
  334.         }
  335. -
  336. +      
  337.         *pInoutPos = Pos;
  338.         *pInoutVel = Vel;
  339.  }
  340. +
  341. +int CCollision::OrientationTile(int Flags, int Dir)
  342. +{  //Converts tiles flags to get effective direction drawn in the map editor
  343. +  // Examples Dir(values)[axisses reference]: (0)=to down[x=0:y=1]; (1)=to bottom-right[x=1:y=1]; (2)=to right[x=1:y=0];...
  344. +       if(Dir == 1)
  345. +       {
  346. +               if(Flags&TILEFLAG_ROTATE)
  347. +               {
  348. +                       if(Flags&TILEFLAG_HFLIP)
  349. +                               Dir = Flags&TILEFLAG_VFLIP? 7 : 5;
  350. +                       else
  351. +                               Dir = Flags&TILEFLAG_VFLIP? 1 : 3;
  352. +               }
  353. +               else
  354. +               {
  355. +                       if(Flags&TILEFLAG_HFLIP)
  356. +                               Dir = Flags&TILEFLAG_VFLIP? 5 : 3;
  357. +                       else
  358. +                               Dir = Flags&TILEFLAG_VFLIP? 7 : 1;
  359. +               }
  360. +       }
  361. +       else if(Dir == 4)//this variant is useless now, but in future coulbe be used
  362. +       {
  363. +               if(Flags&TILEFLAG_ROTATE)
  364. +                       Dir = Flags&TILEFLAG_HFLIP? 6 : 2;
  365. +               else
  366. +                       Dir = Flags&TILEFLAG_HFLIP? 0 : 4;
  367. +       }
  368. +
  369. +       return Dir;
  370. +}
  371. +
  372. +bool CCollision::IntersectSlope(int Flags, vec2 *pVel, int Elasticity)
  373. +{
  374. +       vec2 Vel = *pVel;
  375. +       if((Flags==1 && Vel.y > Vel.x) || (Flags==3 && Vel.y+Vel.x < 0) || (Flags==5 && Vel.y < Vel.x) || (Flags==7 && Vel.y+Vel.x > 0))
  376. +       {
  377. +               vec2 Reaction = vec2(Vel.y, Vel.x); //inverting axisses values
  378. +               if(Flags == 3 || Flags == 7)
  379. +                       Reaction *= -1; //inverting sings
  380. +
  381. +               *pVel = Elasticity? Reaction*Elasticity : (Vel+Reaction)*0.5f;
  382. +               return true;
  383. +       }
  384. +       else
  385. +               return false;
  386. +
  387. +//more general intersection function: maybe useful in future (not formatted)
  388. +/* //equation of the line(AB) in implicit form (ax+by+c=0) analytic geometry
  389. +float a1= Vel.y-Corner.y, b1= Corner.x-Vel.x, c1= -Corner.x*Vel.y; //1 first line: player movement
  390. +float a2= B.y-A.y, b2= A.x-B.x, c2= -A.x*B.y; //2 second line: slope segment
  391. +  
  392. +float d = a1*b2-a2*b1;
  393. +
  394. +if(d)//lines are not parallel
  395. +{
  396. +vec2 HitPoint = vec2((b1*c2-b2*c1)/d, (a2*c1-a1*c2)/d);
  397. +}
  398. +else
  399. +return false*/
  400. +}
  401. diff -Naur /home/toil/teeworlds/src/game/collision.h src/game/collision.h
  402. --- /home/toil/teeworlds/src/game/collision.h   2012-06-26 16:53:53.376860790 +1000
  403. +++ src/game/collision.h        2012-07-07 23:49:37.953689524 +1000
  404. @@ -13,14 +13,16 @@
  405.         class CLayers *m_pLayers;
  406.  
  407.         bool IsTileSolid(int x, int y);
  408. -       int GetTile(int x, int y);
  409. -
  410. +       int GetTile(int x, int y, int *pFlags=0, int *pDx=0, int *pDy=0);
  411. +       int OrientationTile(int Flags, int Dir);
  412. +       bool IntersectSlope(int Flags, vec2 *pVel, int Elasticity);
  413.  public:
  414.         enum
  415.         {
  416.                 COLFLAG_SOLID=1,
  417.                 COLFLAG_DEATH=2,
  418.                 COLFLAG_NOHOOK=4,
  419. +               COLFLAG_SLOPE=8,
  420.         };
  421.  
  422.         CCollision();
  423. @@ -33,7 +35,7 @@
  424.         int IntersectLine(vec2 Pos0, vec2 Pos1, vec2 *pOutCollision, vec2 *pOutBeforeCollision);
  425.         void MovePoint(vec2 *pInoutPos, vec2 *pInoutVel, float Elasticity, int *pBounces);
  426.         void MoveBox(vec2 *pInoutPos, vec2 *pInoutVel, vec2 Size, float Elasticity);
  427. -       bool TestBox(vec2 Pos, vec2 Size);
  428. +       int TestBox(vec2 Pos, vec2 Size);
  429.  };
  430.  
  431.  #endif
  432. diff -Naur /home/toil/teeworlds/src/game/editor/layer_tiles.cpp src/game/editor/layer_tiles.cpp
  433. --- /home/toil/teeworlds/src/game/editor/layer_tiles.cpp        2012-06-26 16:53:53.380861019 +1000
  434. +++ src/game/editor/layer_tiles.cpp     2012-07-08 14:44:12.416572514 +1000
  435. @@ -219,7 +219,6 @@
  436.                         m_pTiles[y*m_Width+x] = m_pTiles[y*m_Width+m_Width-1-x];
  437.                         m_pTiles[y*m_Width+m_Width-1-x] = Tmp;
  438.                 }
  439. -
  440.         if(!m_Game)
  441.                 for(int y = 0; y < m_Height; y++)
  442.                         for(int x = 0; x < m_Width; x++)
  443. @@ -244,13 +243,13 @@
  444.  
  445.  void CLayerTiles::BrushRotate(float Amount)
  446.  {
  447. -       int Rotation = (round(360.0f*Amount/(pi*2))/90)%4;      // 0=0°, 1=90°, 2=180°, 3=270°
  448. +       int Rotation = (round(360.0f*Amount/(pi*2))/90)%4;      // 0=0ᅵ, 1=90ᅵ, 2=180ᅵ, 3=270ᅵ
  449.         if(Rotation < 0)
  450.                 Rotation +=4;
  451.  
  452.         if(Rotation == 1 || Rotation == 3)
  453.         {
  454. -               // 90° rotation
  455. +               // 90ᅵ rotation
  456.                 CTile *pTempData = new CTile[m_Width*m_Height];
  457.                 mem_copy(pTempData, m_pTiles, m_Width*m_Height*sizeof(CTile));
  458.                 CTile *pDst = m_pTiles;
  459. @@ -258,7 +257,7 @@
  460.                         for(int y = m_Height-1; y >= 0; --y, ++pDst)
  461.                         {
  462.                                 *pDst = pTempData[y*m_Width+x];
  463. -                               if(!m_Game)
  464. +                               if(!m_Game || pDst->m_Index == TILE_SLOPE || pDst->m_Index == TILE_SLOPE_NH)
  465.                                 {
  466.                                         if(pDst->m_Flags&TILEFLAG_ROTATE)
  467.                                                 pDst->m_Flags ^= (TILEFLAG_HFLIP|TILEFLAG_VFLIP);
  468. diff -Naur /home/toil/teeworlds/src/game/generated/nethash.c src/game/generated/nethash.c
  469. --- /home/toil/teeworlds/src/game/generated/nethash.c   1970-01-01 10:00:00.000000000 +1000
  470. +++ src/game/generated/nethash.c        2012-07-07 23:45:54.225177199 +1000
  471. @@ -0,0 +1 @@
  472. +#define GAME_NETVERSION_HASH "b67d1f1a1eea234e"
  473. diff -Naur /home/toil/teeworlds/src/game/mapitems.h src/game/mapitems.h
  474. --- /home/toil/teeworlds/src/game/mapitems.h    2012-06-26 16:53:53.384860969 +1000
  475. +++ src/game/mapitems.h 2012-07-08 14:44:12.416572514 +1000
  476. @@ -46,6 +46,8 @@
  477.         TILE_SOLID,
  478.         TILE_DEATH,
  479.         TILE_NOHOOK,
  480. +       TILE_SLOPE=17,
  481. +       TILE_SLOPE_NH,
  482.  
  483.         TILEFLAG_VFLIP=1,
  484.         TILEFLAG_HFLIP=2,
  485. diff -Naur /home/toil/teeworlds/src/game/server/gamemodes/ctf.cpp src/game/server/gamemodes/ctf.cpp
  486. --- /home/toil/teeworlds/src/game/server/gamemodes/ctf.cpp      2012-06-26 16:53:53.396862075 +1000
  487. +++ src/game/server/gamemodes/ctf.cpp   2012-07-07 23:49:37.969176549 +1000
  488. @@ -287,6 +287,9 @@
  489.                                 {
  490.                                         F->m_Vel.y += GameServer()->m_World.m_Core.m_Tuning.m_Gravity;
  491.                                         GameServer()->Collision()->MoveBox(&F->m_Pos, &F->m_Vel, vec2(F->ms_PhysSize, F->ms_PhysSize), 0.5f);
  492. +
  493. +                                       if(GameServer()->Collision()->CheckPoint(F->m_Pos.x, F->m_Pos.y+F->ms_PhysSize/2+5))
  494. +                                               F->m_Vel.x *= GameServer()->m_World.m_Core.m_Tuning.m_GroundFriction;
  495.                                 }
  496.                         }
  497.                 }

Reply to "Untitled"

Here you can reply to the paste above