Advertisement
ijontichy

zdoomtest.c

Dec 2nd, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 29.83 KB | None | 0 0
  1. #include "zcommon.acs"
  2. #library "zdoomtest"
  3.  
  4. #include "commonFuncs.h"
  5. #include "tetDefs.h"
  6.  
  7. int PlayingTetris[PLAYERMAX];
  8. int KeysPressed[KEYCOUNT];
  9.  
  10. int TetrisField[MATRIX_Y][MATRIX_X];
  11. int LinesDeleted[MATRIX_Y] = {0};
  12.  
  13. int ComboLevel, LastLineType, LastLineSpin;
  14. int GameOver, TetrisPaused;
  15. int Gravity;
  16. int TetrisScore, TetrisLines, TetrisLevel, TetrisPlaced, TetrisTics;
  17. int PieceX, PieceY, PieceType, PieceRot, PieceLocking, PieceLocking2;
  18. int HoldType, HoldRot, DidHold;
  19. int PieceDrop;
  20. int OldX, OldY, OldRot, OldPaused;
  21.  
  22. int TetrisFont;
  23.  
  24. int BagTemp[BLOCKCOUNT];
  25. int NextPieces[NEXTCOUNT];
  26.  
  27. function void SetKeysPressed(void)
  28. {
  29.     int i, j, down;
  30.  
  31.     for (i = 0; i < KEYCOUNT; i++)
  32.     {
  33.         j = Keymaps[i];
  34.         down = GetPlayerInput(-1, INPUT_BUTTONS) & j;
  35.  
  36.         KeysPressed[i] = cond(down, KeysPressed[i]+1, 0);
  37.     }
  38. }
  39.  
  40. function int TetKey(int key)
  41. {
  42.     return KeysPressed[key];
  43. }
  44.  
  45. function int AddScore(int add)
  46. {
  47.     TetrisScore += (add * TetrisLevel);
  48.  
  49.     return TetrisScore;
  50. }
  51.  
  52. function int GetBlock(int bX, int bY)
  53. {
  54.     if (bX < 0 || bX >= MATRIX_X
  55.      || bY >= MATRIX_Y) { return -1; }
  56.  
  57.     if (bY < 0) { return 0; }
  58.  
  59.     return TetrisField[bY][bX];
  60. }
  61.  
  62. function void SetBlock(int bX, int bY, int type)
  63. {
  64.     if (bX < 0 || bX >= MATRIX_X
  65.      || bY < 0 || bY >= MATRIX_Y) { return; }
  66.  
  67.     TetrisField[bY][bX] = type;
  68. }
  69.  
  70. function int PieceBlock(int x, int y, int type, int rotation)
  71. {
  72.     return TetrisBlocks[type-1][rotation][y][x];
  73. }
  74.  
  75. function int PieceCollides(int pX, int pY, int type, int rotation)
  76. {
  77.     int tx, ty;
  78.     int block;
  79.     int fieldBlock;
  80.  
  81.     for (ty = BLOCK_YMAX-1; ty >= 0; ty--)
  82.     {
  83.         for (tx = BLOCK_XMAX-1; tx >= 0; tx--)
  84.         {
  85.             block = PieceBlock(tx, ty, type, rotation);
  86.             if (!block) { continue; }
  87.  
  88.             fieldBlock = GetBlock(tx + pX, ty + pY);
  89.  
  90.             if (fieldBlock != 0)
  91.             {
  92.                 return 1;
  93.             }
  94.         }
  95.     }
  96.  
  97.     return 0;
  98. }
  99.  
  100. function int LowestDrop(int x, int y, int type, int rot)
  101. {
  102.     int i, j, collided, collides;
  103.  
  104.     collides = 0; collided = 0;
  105.     y = cond(y, y, -BLOCK_YMAX);
  106.  
  107.     for (i = y; i < MATRIX_Y; i++)
  108.     {
  109.         collided = collides;
  110.         collides = PieceCollides(x, i, type, rot);
  111.  
  112.         if (collides && !collided)
  113.         {
  114.             return i-1;
  115.         }
  116.     }
  117.  
  118.     return -0x80000000;
  119. }
  120.  
  121. function int MovePieceX(int x, int y, int type, int rot)
  122. {
  123.     int i, j;
  124.  
  125.     int goleft, goright;
  126.     int dasdelay = GetCVar("tetris_dasdelay");
  127.     int das   = GetCVar("tetris_das");
  128.  
  129.     goright = TetKey(KEY_RIGHT) % 4 == 1;
  130.  
  131.     int baseleft  = TetKey(KEY_LEFT);
  132.     int baseright = TetKey(KEY_RIGHT);
  133.     int dasleft, dasright;
  134.  
  135.     if (das == 0)
  136.     {
  137.         dasleft  = cond(baseleft  > dasdelay, MATRIX_X, 0);
  138.         dasright = cond(baseright > dasdelay, MATRIX_X, 0);
  139.     }
  140.     else
  141.     {
  142.         if (baseleft > dasdelay) { dasleft = ((baseleft - dasdelay) % das) == 0; }
  143.         else { dasleft = 0; }
  144.  
  145.         if (baseright > dasdelay) { dasright = ((baseright - dasdelay) % das) == 0; }
  146.         else { dasright = 0; }
  147.     }
  148.  
  149.     goleft = dasleft;
  150.     goright = dasright;
  151.  
  152.     if (baseleft == 1 && dasdelay != 0) { goleft = 1; }
  153.     if (baseright == 1 && dasdelay != 0) { goright = 1; }
  154.  
  155.     int ret = (goright - goleft);
  156.  
  157.     for (i = 0; i <= abs(ret); i++)
  158.     {
  159.         j = i * sign(ret);
  160.  
  161.         if (PieceCollides(x+j, y, type, rot))
  162.         {
  163.             ret = max(0, (i-1)) * sign(ret);
  164.             break;
  165.         }
  166.  
  167.         if (PieceCollides(x+j, y+1, type, rot) && i != 0)
  168.         {
  169.             LocalAmbientSound("acstris/pieceland", 127);
  170.         }
  171.     }
  172.  
  173.     if (ret) { LocalAmbientSound("acstris/movepiece", 127); }
  174.  
  175.     return ret+x;
  176. }
  177.  
  178. function int MovePieceY(int x, int y, int type, int rot)
  179. {
  180.     int ret;
  181.     int drop;
  182.     int dropping = TetKey(KEY_CROUCH);
  183.     int i, j;
  184.  
  185.     //PieceDrop += (TetrisLevel * sqrt(itof(TetrisLevel))) / 35;
  186.     PieceDrop += Gravity;
  187.  
  188.     ret = ftoi(PieceDrop);
  189.  
  190.     PieceDrop %= 1.0;
  191.    
  192.     if (dropping)
  193.     {
  194.         if (GetCVar("tetris_dropspeed") == 0)
  195.         {
  196.             ret = LowestDrop(x, y, type, rot) - y;
  197.         }
  198.         else
  199.         {
  200.             if (dropping % GetCVar("tetris_dropspeed") == 0)
  201.             {
  202.                 ret += 1;
  203.                 if (PieceCollides(x, y+ret, type, rot)) { ret -= 1; }
  204.             }
  205.         }
  206.  
  207.         AddScore(ret);
  208.     }
  209.  
  210.     for (i = 0; i <= abs(ret); i++)
  211.     {
  212.         j = i * sign(ret);
  213.  
  214.         if (PieceCollides(x, y+j, type, rot))
  215.         {
  216.             ret = max(0, (i-1)) * sign(ret);
  217.             break;
  218.         }
  219.     }
  220.  
  221.     return ret + y;
  222. }
  223.  
  224. // http://tetrisconcept.net/wiki/SRS#How_Guideline_SRS_Really_Works
  225.  
  226. function int Kick(void)
  227. {
  228.     int kickmod, kx1, ky1, kx2, ky2, kx, ky, i;
  229.  
  230.     if (!PieceCollides(PieceX, PieceY, PieceType, PieceRot)) { return 0; }
  231.  
  232.     /*
  233.     for (i = 0; i < KICKCOUNT_X; i++)
  234.     {
  235.         for (j = 0; j < KICKCOUNT_Y; j++)
  236.         {
  237.             kx = XKicks[i];
  238.             ky = YKicks[j];
  239.  
  240.             if (PieceRot - OldRot == 1) { kx = -kx; }
  241.  
  242.             if (!PieceCollides(PieceX+kx, PieceY+ky, PieceType, PieceRot))
  243.             {
  244.                 PieceX += kx;
  245.                 PieceY += ky;
  246.                 return 1;
  247.             }
  248.         }
  249.     }
  250.     */
  251.  
  252.     for (i = 0; i < KICKCOUNT; i++)
  253.     {
  254.         if (mod(PieceRot - OldRot, ROTCOUNT) == 2)
  255.         {
  256.             kx =  SRS180Kicks[PieceType-1][i][0];
  257.             ky = -SRS180Kicks[PieceType-1][i][1];
  258.         }
  259.         /*
  260.         else
  261.         {
  262.             kx1 =  SRSBases[PieceType-1][OldRot][i][0];
  263.             ky1 = -SRSBases[PieceType-1][OldRot][i][1];
  264.  
  265.             kx2 =  SRSBases[PieceType-1][PieceRot][i][0];
  266.             ky2 = -SRSBases[PieceType-1][PieceRot][i][1];
  267.  
  268.             kx = kx1 - kx2;
  269.             ky = ky1 - ky2;
  270.         }
  271.         */
  272.         else
  273.         {
  274.             // CW if 0, CCW is 1
  275.             kickmod = (mod(PieceRot - OldRot, ROTCOUNT) != 1);
  276.  
  277.             if (PieceType == B_I)
  278.             {
  279.                 kx = SRSKicks_I[PieceRot + kickmod][i][0];
  280.                 ky = SRSKicks_I[PieceRot + kickmod][i][1];
  281.             }
  282.             else
  283.             {
  284.                 kx = SRSKicks[PieceRot + kickmod][i][0];
  285.                 ky = SRSKicks[PieceRot + kickmod][i][1];
  286.             }
  287.         }
  288.  
  289.         //Log(s:"Trying kick (", d:kx, s:", ", d:ky, s:") (", d:OldRot, s:" -> ", d:PieceRot, s:")");
  290.  
  291.         if (!PieceCollides(PieceX+kx, PieceY+ky, PieceType, PieceRot))
  292.         {
  293.             PieceX += kx;
  294.             PieceY += ky;
  295.             return 1;
  296.         }
  297.     }
  298.    
  299.     return 0;
  300. }
  301.  
  302. function void HardDrop(int x, int y, int type, int rot)
  303. {
  304.     int dY = LowestDrop(x, y, type, rot);
  305.     AddPiece(x, dY, type, rot);
  306.     AddScore(2 * (dY - y));
  307.  
  308.     LocalAmbientSound("acstris/pieceland", 127);
  309. }
  310.  
  311.  
  312. function void AddPiece(int pX, int pY, int type, int rotation)
  313. {
  314.     int tx, ty, x, y;
  315.     int block, isspin, cleared = 1;
  316.     int deleted, linetype, combo, score, dogameover = 1;
  317.  
  318.     TetrisPlaced++;
  319.  
  320.     if (!PieceCollides(pX,   pY, type, rotation) &&
  321.         PieceCollides(pX+1, pY, type, rotation) &&
  322.         PieceCollides(pX-1, pY, type, rotation) &&
  323.         PieceCollides(pX, pY-1, type, rotation))
  324.     {
  325.         isspin = 1;
  326.     }
  327.  
  328.     for (ty = BLOCK_YMAX-1; ty >= 0; ty--)
  329.     {
  330.         for (tx = BLOCK_XMAX-1; tx >= 0; tx--)
  331.         {
  332.             block = PieceBlock(tx, ty, type, rotation);
  333.             if (!block) { continue; }
  334.  
  335.             SetBlock(tx + pX, ty + pY, type);
  336.            
  337.             if (ty + pY >= 0) { dogameover = 0; }
  338.         }
  339.     }
  340.  
  341.     if (dogameover) { GameOver = 1; }
  342.  
  343.     deleted = CheckLines();
  344.  
  345.     x = MATRIX_DRAWX + ((GRAPHIC_SIZE / 2) * (MATRIX_X - 1));
  346.     y = MATRIX_DRAWY + GRAPHIC_SIZE;
  347.  
  348.     x = intFloat(x * 0.75) + 0.4;
  349.     y = intFloat(y * 0.75) + 0.1;
  350.  
  351.     for (ty = MATRIX_Y-1; ty >= 0; ty--)
  352.     {
  353.         for (tx = 0; tx < MATRIX_X; tx++)
  354.         {
  355.             if (GetBlock(tx, ty)) { cleared = 0; break; }
  356.         }
  357.        
  358.         if (cleared == 0) { break; }
  359.     }
  360.  
  361.     if (deleted)
  362.     {
  363.         linetype = middle(0, deleted - 1, LINETYPES);
  364.         combo = middle(0, ComboLevel-2, COMBOSOUNDS);
  365.  
  366.         score = itof(50 * pow(2, deleted));
  367.         score = FixedMul(score, 0.5 * (ComboLevel+1));
  368.         score = ftoi(score);
  369.  
  370.         SetHudSize(480, 360, 1);
  371.         SetFont("SMALLFONT");
  372.  
  373.         if (isspin)
  374.         {
  375.             HudMessage(s:PieceLetters[type], s:"-Spin ", s:LineNames[linetype][0]; HUDMSG_PLAIN | HUDMSG_COLORSTRING, 401,
  376.                        LineNames[linetype][1], x, y, 1.5);
  377.             LocalAmbientSound(SpinBreaks[linetype], 127);
  378.  
  379.  
  380.             if (LastLineSpin)
  381.             {
  382.                 LocalAmbientSound("acstris/tetriscont", 127);
  383.                 score *= 6;
  384.             }
  385.             else
  386.             {
  387.                 LocalAmbientSound("acstris/tetrisstart", 127);
  388.                 score *= 3;
  389.             }
  390.  
  391.             LastLineSpin = 1;
  392.         }
  393.         else
  394.         {
  395.             HudMessage(s:LineNames[linetype][0]; HUDMSG_PLAIN | HUDMSG_COLORSTRING, 401,
  396.                        LineNames[linetype][1], x, y, 1.5);
  397.             LocalAmbientSound(LineBreaks[linetype], 127);
  398.  
  399.             if (LastLineSpin)
  400.             {
  401.                 LocalAmbientSound("acstris/tetrisend", 127);
  402.             }
  403.  
  404.             LastLineSpin = 0;
  405.         }
  406.  
  407.         if (ComboLevel > 1)
  408.         {
  409.             LocalAmbientSound(Combos[combo], 127);
  410.  
  411.  
  412.             SetHudSize(480, 360, 1);
  413.             SetFont("SMALLFONT");
  414.             HudMessage(d:ComboLevel-1, s:"-Combo"; HUDMSG_PLAIN, 402,
  415.                        CR_CYAN, x, y+12.0, 1.5);
  416.         }
  417.         else
  418.         {
  419.             HudMessage(s:""; HUDMSG_PLAIN, 402, 0, 0, 0, 1);
  420.         }
  421.  
  422.         if (linetype == 3)
  423.         {
  424.             if (LastLineType != 3) { LocalAmbientSound("acstris/tetrisstart", 127); }
  425.             else { score *= 2; LocalAmbientSound("acstris/tetriscont", 127); }
  426.         }
  427.         else if (LastLineType == 3)
  428.         {
  429.             LocalAmbientSound("acstris/tetrisend", 127);
  430.         }
  431.  
  432.         if (cleared)
  433.         {
  434.             LocalAmbientSound("acstris/clear", 127);
  435.             score += 2000;
  436.         }
  437.  
  438.         LastLineType = linetype;
  439.     }
  440.     else if (isspin)
  441.     {
  442.         score = 400;
  443.  
  444.         SetHudSize(480, 360, 1);
  445.         SetFont("SMALLFONT");
  446.         HudMessage(s:PieceLetters[type], s:"-Spin"; HUDMSG_PLAIN, 401,
  447.                    CR_LIGHTBLUE, x, y, 1.5);
  448.  
  449.         LocalAmbientSound("acstris/spin", 127);
  450.     }
  451.  
  452.     AddScore(score);
  453.  
  454.     NewPiece();
  455. }
  456.  
  457. function void SubtractPiece(int pX, int pY, int type, int rotation)
  458. {
  459.     int tx, ty;
  460.     int block;
  461.  
  462.     for (ty = BLOCK_YMAX-1; ty >= 0; ty--)
  463.     {
  464.         for (tx = BLOCK_XMAX-1; tx >= 0; tx--)
  465.         {
  466.             block = PieceBlock(tx, ty, type, rotation);
  467.             if (!block) { continue; }
  468.  
  469.             SetBlock(tx + pX, ty + pY, 0);
  470.         }
  471.     }
  472. }
  473.  
  474. function int CheckLines(void)
  475. {
  476.     int x, y;
  477.     int deleted;
  478.  
  479.     for (y = 0; y < MATRIX_Y; y++)
  480.     {
  481.         LinesDeleted[y] = 0;
  482.         for (x = 0; x < MATRIX_X; x++)
  483.         {
  484.             if (GetBlock(x, y) == 0) { break; }
  485.  
  486.             if (x == (MATRIX_X - 1))
  487.             {
  488.                 deleted++;
  489.                 DeleteLine(y);
  490.                 ShiftLines(y);
  491.             }
  492.         }
  493.     }
  494.  
  495.     if (deleted)
  496.     {
  497.         ComboLevel++;
  498.     }
  499.     else
  500.     {
  501.         ComboLevel = 0;
  502.     }
  503.  
  504.     return deleted;
  505. }
  506.  
  507. function void DeleteLine(int y)
  508. {
  509.     int x, i;
  510.     TetrisLines++;
  511.  
  512.     int drawy = itof(MATRIX_DRAWY + ((y - MATRIX_Y) * GRAPHIC_SIZE));
  513.  
  514.     for (x = 0; x < MATRIX_X; x++)
  515.     {
  516.         SetBlock(x, y, 0);
  517.  
  518.         SetFont("TETFLASH");
  519.         SetHudSize(640, 480, 1);
  520.         HudMessage(s:"A"; HUDMSG_FADEOUT, 22000 + (y * MATRIX_Y) + x, CR_UNTRANSLATED,
  521.                     itof(MATRIX_DRAWX + (x * GRAPHIC_SIZE)), drawy, 0.0, 0.15);
  522.     }
  523.  
  524.     for (i = 0; i < y; i++)
  525.     {
  526.         LinesDeleted[i] += 1;
  527.     }
  528. }
  529.  
  530. function void ShiftLines(int final)
  531. {
  532.     int x, y, toShift;
  533.  
  534.     for (y = final; y >= 0; y--)
  535.     {
  536.         toShift = LinesDeleted[y];
  537.  
  538.         for (x = 0; x < MATRIX_X; x++)
  539.         {
  540.             SetBlock(x, y, GetBlock(x, y-1));
  541.         }
  542.     }
  543. }
  544.  
  545. function void Hold(void)
  546. {
  547.     int type = PieceType, rot = PieceRot;
  548.  
  549.     if (DidHold)
  550.     {
  551.         LocalAmbientSound("acstris/holdfail", 127);
  552.         return;
  553.     }
  554.  
  555.     PieceX = (MATRIX_X / 2) - 2;
  556.     PieceY = 0;
  557.  
  558.     if (HoldType == 0)
  559.     {
  560.         PieceType = NextPiece();
  561.         PieceRot = 0;
  562.  
  563.         LocalAmbientSound("acstris/firsthold", 127);
  564.     }
  565.     else
  566.     {
  567.         PieceType = HoldType;
  568.         PieceRot  = HoldRot;
  569.  
  570.         LocalAmbientSound("acstris/hold", 127);
  571.     }
  572.  
  573.     LocalAmbientSound(NextSounds[NextPieces[0] - 1], 127);
  574.  
  575.     HoldType = type;
  576.     HoldRot  = rot;
  577.     DidHold  = 1;
  578.  
  579.     PieceLocking = 0;
  580.     PieceLocking2 = 0;
  581.     PieceDrop = 0;
  582. }
  583.  
  584. function void NewPiece(void)
  585. {
  586.     int oldlevel = TetrisLevel;
  587.     int drop;
  588.  
  589.     PieceX = (MATRIX_X / 2) - 2;
  590.     PieceY = 0;
  591.  
  592.     PieceType = NextPiece();
  593.     PieceRot = 0;
  594.     PieceLocking = 0;
  595.     PieceLocking2 = 0;
  596.  
  597.     TetrisLevel = (TetrisLines / 10) + 1;
  598.     PieceDrop = 0;
  599.     DidHold = 0;
  600.  
  601.     drop = itof(max(1, TetrisLevel)) / max(1, 40 - (TetrisLevel * 2));
  602.     Gravity = FixedMul(drop, 60.0/35);
  603.  
  604.     LocalAmbientSound(NextSounds[NextPieces[0] - 1], 127);
  605.  
  606.     if (oldlevel != TetrisLevel && TetrisLevel != 1)
  607.     {
  608.         LocalAmbientSound("acstris/levelup", 127);
  609.     }
  610.  
  611.     if (PieceCollides(PieceX, PieceY, PieceType, PieceRot))
  612.     {
  613.         GameOver = 1;
  614.     }
  615. }
  616.  
  617. function int NextPiece(void)
  618. {
  619.     int i, next;
  620.  
  621.     if (NextPieces[NEXTCOUNT-BLOCKCOUNT] == 0)
  622.     {
  623.         PopulateBag(0);
  624.     }
  625.  
  626.     next = NextPieces[0];
  627.  
  628.     for (i = 0; i < NEXTCOUNT-1; i++)
  629.     {
  630.         NextPieces[i] = NextPieces[i+1];
  631.     }
  632.  
  633.     NextPieces[NEXTCOUNT-1] = 0;
  634.  
  635.     return next;
  636. }
  637.  
  638. function void PopulateBag(int rebuild)
  639. {
  640.     int i, j, k, topop, next;
  641.  
  642.     for (i = 0; i < BLOCKCOUNT; i++)
  643.     {
  644.         if (BagTemp[i] == 0)
  645.         {
  646.             j = i;
  647.             break;
  648.         }
  649.     }
  650.     if (j == 0) { j = BLOCKCOUNT; }
  651.  
  652.     for (i = 0; i < NEXTCOUNT; i++)
  653.     {
  654.         if (NextPieces[i] != 0 && !rebuild) { continue; }
  655.  
  656.         if (BagTemp[0] == 0)
  657.         {
  658.             for (k = 0; k < BLOCKCOUNT; k++)
  659.             {
  660.                 BagTemp[k] = k+1;
  661.             }
  662.             j = BLOCKCOUNT;
  663.         }
  664.  
  665.         j -= 1;
  666.  
  667.         topop = random(0, j);
  668.  
  669.         NextPieces[i] = BagTemp[topop];
  670.         BagTemp[topop] = BagTemp[j];
  671.         BagTemp[j] = 0;
  672.     }
  673. }
  674.    
  675.  
  676. function void DrawBlock(int x, int y, int type, int id)
  677. {
  678.     DrawBlockColor(x, y, type, id, 0);
  679. }
  680.  
  681. function void DrawBlockColor(int bX, int bY, int blockType, int printID, int color)
  682. {
  683.     int ghost;
  684.     int explicit = !!color;
  685.  
  686.     if (bX < 0 || bX >= MATRIX_X || bY >= MATRIX_Y) { return; }
  687.     if (printID == 0) { printID = 61000 + (bX * MATRIX_Y) + bY; }
  688.  
  689.     if (blockType < 0) { blockType *= -1; ghost = 1; }
  690.     if (blockType == 0) { blockType = GetBlock(bX, bY); }
  691.  
  692.     if (color == 0)
  693.     {
  694.         if (ghost) { color = GhostColors[blockType]; }
  695.         else { color = TetrisColors[blockType]; }
  696.     }
  697.  
  698.     if (blockType == 0 || (MATRIX_Y - bY) > 20)
  699.     {
  700.         HudMessage(s:" "; HUDMSG_PLAIN, printID, CR_UNTRANSLATED, 0, 0, 1.0);
  701.     }
  702.     else
  703.     {
  704.         SetFont(TetrisFont);
  705.         SetHudSize(640, 480, 1);
  706.         HudMessage(s:"A"; HUDMSG_PLAIN | HUDMSG_COLORSTRING, printID, color,
  707.                     itof(MATRIX_DRAWX + (bX * GRAPHIC_SIZE)),
  708.                     itof(MATRIX_DRAWY + ((bY - MATRIX_Y) * GRAPHIC_SIZE)),
  709.                     1.0);
  710.     }
  711. }
  712.  
  713. function void DrawPiece(int x, int y, int type, int rotation, int id)
  714. {
  715.     int i, j, block;
  716.     id = cond(id, id, 71000);
  717.  
  718.     for (i = 0; i < BLOCK_XMAX; i++)
  719.     {
  720.         for (j = 0; j < BLOCK_YMAX; j++)
  721.         {
  722.             block = PieceBlock(i, j, abs(type), rotation);
  723.  
  724.             if (block)
  725.             {
  726.                 DrawBlock(x+i, y+j, type, id++);
  727.             }
  728.         }
  729.     }
  730. }
  731.  
  732. function void DrawGhost(int x, int y, int type, int rot)
  733. {
  734.     int i, j, collided, collides;
  735.  
  736.     collides = 0; collided = 0;
  737.  
  738.     i = LowestDrop(x, y, type, rot);
  739.  
  740.     DrawPiece(x, i, -type, rot, 72000);
  741. }
  742.  
  743. function void ClearPiece(void)
  744. {
  745.     int i;
  746.  
  747.     for (i = 0; i < (BLOCK_XMAX * BLOCK_YMAX); i++)
  748.     {
  749.         HudMessage(s:""; HUDMSG_PLAIN, 71000+i, 0, 0, 0, 1);
  750.     }
  751. }
  752.  
  753. function void ClearGhost(void)
  754. {
  755.     int i;
  756.  
  757.     for (i = 0; i < (BLOCK_XMAX * BLOCK_YMAX); i++)
  758.     {
  759.         HudMessage(s:""; HUDMSG_PLAIN, 72000+i, 0, 0, 0, 1);
  760.     }
  761. }
  762.  
  763. function void DrawField(void)
  764. {
  765.     DrawFieldBorder();
  766.     DrawBlocks();
  767.     DrawHold();
  768.     DrawNextPieces();
  769.     DrawStats();
  770.  
  771.     DrawGhost(PieceX, PieceY, PieceType, PieceRot);
  772.     DrawPiece(PieceX, PieceY, PieceType, PieceRot, 0);
  773. }
  774.  
  775. function void DrawFieldGameOver(int level)
  776. {
  777.     int x, y, y2;
  778.  
  779.     DrawFieldBorder();
  780.     DrawBlocks();
  781.     DrawNextPieces();
  782.     DrawStats();
  783.  
  784.     ClearGhost();
  785.     ClearPiece();
  786.  
  787.     for (y = 0; y < level; y++)
  788.     {
  789.         y2 = MATRIX_Y - (y + 1);
  790.  
  791.         for (x = 0; x < MATRIX_X; x++)
  792.         {
  793.             DrawBlockColor(x, y2, 0, 21000 + (y2 * MATRIX_Y) + x, "TetrisBlack");
  794.         }
  795.     }
  796. }
  797.  
  798. function void DrawStats(void)
  799. {
  800.     SetHudSize(640, 480, 1);
  801.     SetFont("CONFONT");
  802.  
  803.     int x = itof(MATRIX_DRAWX - 60);
  804.     int y = itof(MATRIX_DRAWY) - 39.8;
  805.     int bpm, lpm;
  806.     int mins, secs;
  807.  
  808.     if (TetrisTics == 0) { bpm = 0; }
  809.     else
  810.     {
  811.         lpm = itof(TetrisLines) / TetrisTics;
  812.         lpm *= 60 * 35;
  813.         lpm = ftoi(lpm);
  814.     }
  815.  
  816.     if (TetrisTics == 0) { bpm = 0; }
  817.     else
  818.     {
  819.         bpm = itof(TetrisPlaced) / TetrisTics;
  820.         bpm *= 60 * 35;
  821.         bpm = ftoi(bpm);
  822.     }
  823.  
  824.     mins = TetrisTics / (60 * 35);
  825.     secs = (TetrisTics / 35) % 60;
  826.  
  827.     HudMessage(s:"SCORE"; HUDMSG_PLAIN,     3500, CR_GOLD,  x, y - 120.0, 1.0);
  828.     HudMessage(d:TetrisScore; HUDMSG_PLAIN, 3501, CR_WHITE, x, y - 110.0, 1.0);
  829.  
  830.     HudMessage(s:"LEVEL"; HUDMSG_PLAIN,     3502, CR_GOLD,  x, y - 90.0, 1.0);
  831.     HudMessage(d:TetrisLevel; HUDMSG_PLAIN, 3503, CR_WHITE, x, y - 80.0, 1.0);
  832.  
  833.     HudMessage(s:"LINES"; HUDMSG_PLAIN,     3504, CR_GOLD,  x, y - 60.0, 1.0);
  834.     HudMessage(d:TetrisLines; HUDMSG_PLAIN, 3505, CR_WHITE, x, y - 50.0, 1.0);
  835.  
  836.     HudMessage(s:"BPM";   HUDMSG_PLAIN,     3506, CR_GOLD,  x, y - 30.0, 1.0);
  837.     HudMessage(d:bpm;     HUDMSG_PLAIN,     3507, CR_WHITE, x, y - 20.0, 1.0);
  838.  
  839.     HudMessage(s:"LPM";   HUDMSG_PLAIN,     3508, CR_GOLD,  x, y, 1.0);
  840.     HudMessage(d:lpm;     HUDMSG_PLAIN,     3509, CR_WHITE, x, y + 10.0, 1.0);
  841.  
  842.     mins = padStringL(StrParam(d:mins), "0", 2);
  843.     secs = padStringL(StrParam(d:secs), "0", 2);
  844.  
  845.     HudMessage(s:mins, s:"\cf:\c-", s:secs; HUDMSG_PLAIN, 3510, CR_WHITE, x, y + 35.0, 1.0);
  846. }
  847.  
  848. function void DrawHold(void)
  849. {
  850.     int x = itof(MATRIX_DRAWX - 60) + 0.4;
  851.     int y = itof(MATRIX_DRAWY - (GRAPHIC_SIZE * MATRIX_Y));
  852.  
  853.     int x2 = intFloat(1.5 * (MATRIX_DRAWX - 60)) + 0.4;
  854.     int y2 = intFloat(1.5 * (MATRIX_DRAWY - (GRAPHIC_SIZE * MATRIX_Y)));
  855.  
  856.     int dx, dy, tx, ty;
  857.     int block;
  858.     int printid = 2600;  // omg hackers
  859.  
  860.     SetHudSize(640, 480, 1);
  861.     SetFont("CONFONT");
  862.  
  863.     HudMessage(s:"HOLD"; HUDMSG_PLAIN, printid++, CR_GOLD, x, y, 1.0);
  864.    
  865.     if (HoldType == 0) { return; }
  866.  
  867.     for (ty = 0; ty < BLOCK_YMAX; ty++)
  868.     {
  869.         for (tx = 0; tx < BLOCK_XMAX; tx++)
  870.         {
  871.             block = TetrisBlocks[HoldType-1][HoldRot][ty][tx];
  872.  
  873.             if (block == 0)
  874.             {
  875.                 HudMessage(s:""; HUDMSG_PLAIN, printid++, 0, 0, 0, 1);
  876.                 continue;
  877.             }
  878.  
  879.             SetHudSize(960, 720, 1);
  880.             SetFont(TetrisFont);
  881.            
  882.             dx = x2 + itof((tx - 1) * GRAPHIC_SIZE);
  883.             dy = y2 + itof(ty * GRAPHIC_SIZE) + 48.0;
  884.  
  885.             HudMessage(s:"A"; HUDMSG_PLAIN | HUDMSG_COLORSTRING, printid++,
  886.                     TetrisColors[HoldType], dx, dy, 1.0);
  887.         }
  888.     }
  889. }
  890.  
  891.  
  892. function void DrawBlocks(void)
  893. {
  894.     int x, y;
  895.  
  896.     for (x = 0; x < MATRIX_X; x++)
  897.     {
  898.         for (y = 0; y < MATRIX_Y; y++)
  899.         {
  900.             DrawBlock(x, y, 0, 0);
  901.         }
  902.     }
  903. }
  904.  
  905. function void DrawFieldBorder(void)
  906. {
  907.     int printID = 75000;
  908.     int i;
  909.     int y;
  910.  
  911.     for (i = -1; i <= MATRIX_X; i++)
  912.     {
  913.         SetFont(TetrisFont);
  914.         SetHudSize(640, 480, 1);
  915.         HudMessage(s:"A"; HUDMSG_PLAIN | HUDMSG_COLORSTRING, printID++,
  916.                     "TetrisBlack",
  917.                     itof(MATRIX_DRAWX + (i * GRAPHIC_SIZE)),
  918.                     itof(MATRIX_DRAWY),
  919.                     1.0);
  920.     }
  921.  
  922.     for (i = 0; i < 20; i++)
  923.     {
  924.         y = itof(MATRIX_DRAWY - ((i+1) * GRAPHIC_SIZE));
  925.  
  926.         SetFont(TetrisFont);
  927.         SetHudSize(640, 480, 1);
  928.         HudMessage(s:"A"; HUDMSG_PLAIN | HUDMSG_COLORSTRING, printID++,
  929.                     "TetrisBlack",
  930.                     itof(MATRIX_DRAWX - GRAPHIC_SIZE), y,
  931.                     1.0);
  932.  
  933.         HudMessage(s:"A"; HUDMSG_PLAIN | HUDMSG_COLORSTRING, printID++,
  934.                     "TetrisBlack",
  935.                     itof(MATRIX_DRAWX + (MATRIX_X * GRAPHIC_SIZE)), y,
  936.                     1.0);
  937.     }
  938. }
  939.  
  940. function void DrawNextPieces(void)
  941. {
  942.     int printID = 12500;
  943.     int i;
  944.    
  945.     int x = itof(MATRIX_DRAWX + (GRAPHIC_SIZE * (MATRIX_X + 5))) + 0.4;
  946.     int y = itof(MATRIX_DRAWY - (GRAPHIC_SIZE * MATRIX_Y)) + 0.2;
  947.  
  948.     SetHudSize(640, 480, 1);
  949.     SetFont("CONFONT");
  950.  
  951.     HudMessage(s:"NEXT"; HUDMSG_PLAIN, printID++, CR_GOLD,
  952.             x, y, 1.0);
  953.  
  954.     for (i = 0; i < 7; i++)
  955.     {
  956.         DrawNextPiece(i);
  957.     }
  958. }
  959.  
  960. function void DrawNextPiece(int offset)
  961. {
  962.     int type = NextPieces[offset];
  963.     int printid = 13500 + (offset * BLOCK_XMAX * BLOCK_YMAX);
  964.     int block;
  965.     int x, y;
  966.     int dx, dy;
  967.  
  968.     offset *= GRAPHIC_SIZE * (BLOCK_YMAX + 1);
  969.  
  970.     dx = intFloat(1.5 * (MATRIX_DRAWX + (GRAPHIC_SIZE * MATRIX_X))) + itof(GRAPHIC_SIZE * 6);
  971.     dy = intFloat(1.5 * (MATRIX_DRAWY - (GRAPHIC_SIZE * MATRIX_Y))) + itof(GRAPHIC_SIZE * 2) + itof(offset);
  972.    
  973.     for (y = 0; y < BLOCK_YMAX; y++)
  974.     {
  975.         for (x = 0; x < BLOCK_XMAX; x++)
  976.         {
  977.             block = TetrisBlocks[type-1][0][y][x];
  978.  
  979.             if (block == 0)
  980.             {
  981.                 HudMessage(s:""; HUDMSG_PLAIN, printid++, 0, 0, 0, 1);
  982.                 continue;
  983.             }
  984.  
  985.             SetHudSize(960, 720, 1);
  986.             SetFont(TetrisFont);
  987.  
  988.             HudMessage(s:"A"; HUDMSG_PLAIN | HUDMSG_COLORSTRING, printid++,
  989.                     TetrisColors[type],
  990.                     dx + itof(x * GRAPHIC_SIZE),
  991.                     dy + itof(y * GRAPHIC_SIZE), 1.0);
  992.         }
  993.     }
  994. }
  995.  
  996. function void BeginGame(void)
  997. {
  998.     int x, y;
  999.  
  1000.     for (x = 0; x < MATRIX_X; x++)
  1001.     {
  1002.         for (y = 0; y < MATRIX_Y; y++)
  1003.         {
  1004.             SetBlock(x, y, 0);
  1005.         }
  1006.     }
  1007.  
  1008.     PopulateBag(1);
  1009.  
  1010.     GameOver = 0;
  1011.     TetrisScore = 0;
  1012.     TetrisLines = 0;
  1013.     TetrisPlaced = 0;
  1014.     TetrisTics = 0;
  1015.     ComboLevel = 0;
  1016.     LastLineType = 0;
  1017.  
  1018.     HoldType = 0;
  1019.     HoldRot  = 0;
  1020.     DidHold  = 0;
  1021. }
  1022.  
  1023. script 123 (void) net
  1024. {
  1025.     int pln;
  1026.  
  1027.     PlayingTetris[pln] = !PlayingTetris[pln];
  1028.  
  1029.     ACS_ExecuteAlways(124, 0, PlayingTetris[pln],0,0);
  1030.     SetPlayerProperty(0, PlayingTetris[pln], PROP_TOTALLYFROZEN);
  1031. }
  1032.  
  1033. script 124 (int play) clientside
  1034. {
  1035.     int pln = PlayerNumber();
  1036.     if (pln != ConsolePlayerNumber()) { terminate; }
  1037.  
  1038.     PlayingTetris[pln] = play;
  1039.  
  1040.     int x, y, type, rot;
  1041.     int tolog, i;
  1042.     int gameoverY, gameovertic;
  1043.    
  1044.     x = itof(MATRIX_DRAWX + ((GRAPHIC_SIZE / 2) * (MATRIX_X - 1))) + 0.4;
  1045.     y = itof(MATRIX_DRAWY - ((GRAPHIC_SIZE / 2) * MATRIX_Y));
  1046.  
  1047.     if (GetCVar("tetris_varsexist") != 1)
  1048.     {
  1049.         saveCVar("tetris_varsexist", 1);
  1050.         saveCVar("tetris_das", 3);
  1051.         saveCVar("tetris_dasdelay", 6);
  1052.         saveCVar("tetris_dropspeed", 2);
  1053.     }
  1054.  
  1055.     if (!PlayingTetris[pln]) { terminate; }
  1056.  
  1057.     if (!TetrisPaused)
  1058.     {
  1059.         while (!keyPressed(BT_USE))
  1060.         {
  1061.             DrawFieldBorder();
  1062.  
  1063.             SetHudSize(640, 480, 1);
  1064.             SetFont("SMALLFONT");
  1065.             HudMessage(s:"Move left - \cj", k:"+moveleft", s:"\c- or \cj", k:"+left";
  1066.                     HUDMSG_PLAIN, 701, CR_GREEN, x, y - 50.0, 1.0);
  1067.             HudMessage(s:"Move right - \cj", k:"+moveright", s:"\c- or \cj", k:"+right";
  1068.                     HUDMSG_PLAIN, 702, CR_GREEN, x, y - 40.0, 1.0);
  1069.  
  1070.             HudMessage(s:"Rotate clockwise - \cj", k:"+forward", s:"\c- or \cj", k:"+lookup";
  1071.                     HUDMSG_PLAIN, 703, CR_GREEN, x, y - 28.0, 1.0);
  1072.             HudMessage(s:"Rotate counterclockwise - \cj", k:"+back", s:"\c- or \cj", k:"+lookdown";
  1073.                     HUDMSG_PLAIN, 704, CR_GREEN, x, y - 18.0, 1.0);
  1074.  
  1075.             HudMessage(s:"Soft drop - \cj", k:"+crouch";
  1076.                     HUDMSG_PLAIN, 705, CR_GREEN, x, y-6.0, 1.0);
  1077.             HudMessage(s:"Hard drop - \cj", k:"+jump";
  1078.                     HUDMSG_PLAIN, 706, CR_GREEN, x, y+4.0, 1.0);
  1079.  
  1080.             HudMessage(s:"180 degree turn - \cj", k:"+speed";
  1081.                     HUDMSG_PLAIN, 707, CR_GREEN, x, y + 16.0, 1.0);
  1082.  
  1083.             HudMessage(s:"Hold piece - \cj", k:"+user1";
  1084.                     HUDMSG_PLAIN, 708, CR_GREEN, x, y + 28.0, 1.0);
  1085.  
  1086.             HudMessage(s:"Pause - \cj", k:"+use";
  1087.                     HUDMSG_PLAIN, 709, CR_GREEN, x, y + 40.0, 1.0);
  1088.  
  1089.             HudMessage(s:"Hit \cf", k:"+use", s:"\c- to begin.";
  1090.                     HUDMSG_PLAIN, 716, CR_GREEN, x, y + 55.0, 1.0);
  1091.  
  1092.             HudMessage(s:"Puke script 123 again to kill ACSTris.";
  1093.                     HUDMSG_PLAIN, 717, CR_BRICK, x, y + 70.0, 1.0);
  1094.  
  1095.             HudMessage(s:"This will reset your game unless you paused.";
  1096.                     HUDMSG_PLAIN, 718, CR_BRICK, x, y + 78.0, 1.0);
  1097.  
  1098.             Delay(1);
  1099.         }
  1100.  
  1101.         HudMessage(s:""; HUDMSG_PLAIN, 702, CR_UNTRANSLATED, 0, 0, 1);
  1102.         HudMessage(s:""; HUDMSG_PLAIN, 703, CR_UNTRANSLATED, 0, 0, 1);
  1103.         HudMessage(s:""; HUDMSG_PLAIN, 704, CR_UNTRANSLATED, 0, 0, 1);
  1104.         HudMessage(s:""; HUDMSG_PLAIN, 705, CR_UNTRANSLATED, 0, 0, 1);
  1105.         HudMessage(s:""; HUDMSG_PLAIN, 706, CR_UNTRANSLATED, 0, 0, 1);
  1106.         HudMessage(s:""; HUDMSG_PLAIN, 707, CR_UNTRANSLATED, 0, 0, 1);
  1107.         HudMessage(s:""; HUDMSG_PLAIN, 708, CR_UNTRANSLATED, 0, 0, 1);
  1108.         HudMessage(s:""; HUDMSG_PLAIN, 709, CR_UNTRANSLATED, 0, 0, 1);
  1109.         HudMessage(s:""; HUDMSG_PLAIN, 716, CR_UNTRANSLATED, 0, 0, 1);
  1110.         HudMessage(s:""; HUDMSG_PLAIN, 717, CR_UNTRANSLATED, 0, 0, 1);
  1111.         HudMessage(s:""; HUDMSG_PLAIN, 718, CR_UNTRANSLATED, 0, 0, 1);
  1112.  
  1113.         BeginGame();
  1114.  
  1115.         for (i = 0; i < 60; i++)
  1116.         {
  1117.             DrawFieldBorder();
  1118.             DrawNextPieces();
  1119.             DrawHold();
  1120.             DrawStats();
  1121.             SetKeysPressed();
  1122.  
  1123.             if (i == 0)
  1124.             {
  1125.                 LocalAmbientSound("acstris/ready", 127);
  1126.                 SetFont("BIGFONT");
  1127.                 HudMessage(s:"Ready..."; HUDMSG_PLAIN, 701, CR_WHITE, x, y, 1.0);
  1128.             }
  1129.             else if (i == 30)
  1130.             {
  1131.                 LocalAmbientSound("acstris/go", 127);
  1132.                 SetFont("BIGFONT");
  1133.                 HudMessage(s:"GO!"; HUDMSG_PLAIN, 701, CR_WHITE, x, y, 1.0);
  1134.             }
  1135.             Delay(1);
  1136.         }
  1137.  
  1138.         NewPiece();
  1139.     }
  1140.     else
  1141.     {
  1142.         TetrisPaused = 0;
  1143.     }
  1144.  
  1145.     while (!GameOver && PlayingTetris[pln])
  1146.     {
  1147.         SetKeysPressed();
  1148.  
  1149.         OldPaused = TetrisPaused;
  1150.         TetrisPaused = !!TetrisPaused ^ !!keyPressed(BT_USE);
  1151.  
  1152.         if (OldPaused != TetrisPaused) { LocalAmbientSound(PauseSounds[TetrisPaused], 127); }
  1153.  
  1154.         if (TetrisPaused)
  1155.         {
  1156.             DrawField();
  1157.  
  1158.             SetFont("BIGFONT");
  1159.             HudMessage(s:"Paused"; HUDMSG_PLAIN, 701, CR_GREEN, x, y, 1.0);
  1160.             Delay(1);
  1161.             continue;
  1162.         }
  1163.  
  1164.         HudMessage(s:""; HUDMSG_PLAIN, 701, CR_UNTRANSLATED, 0, 0, 1);
  1165.  
  1166.         if (TetKey(KEY_HOLD) == 1) { Hold(); }
  1167.  
  1168.         TetrisTics++;
  1169.  
  1170.         OldX   = PieceX;
  1171.         OldY   = PieceY;
  1172.         OldRot = PieceRot;
  1173.  
  1174.         PieceRot  += (TetKey(KEY_UP) == 1) - (TetKey(KEY_DOWN) == 1) + (2 * (TetKey(KEY_SPEED) == 1));
  1175.  
  1176.         PieceType = mod((PieceType-1), BLOCKCOUNT) + 1;
  1177.         PieceRot  = mod(PieceRot, ROTCOUNT);
  1178.  
  1179.         Kick();
  1180.  
  1181.         PieceX = MovePieceX(PieceX, PieceY, PieceType, PieceRot);
  1182.         PieceY = MovePieceY(PieceX, PieceY, PieceType, PieceRot);
  1183.  
  1184.         if (PieceCollides(PieceX, PieceY, PieceType, PieceRot))
  1185.         {
  1186.             PieceX   = OldX;
  1187.             PieceY   = OldY;
  1188.             PieceRot = OldRot;
  1189.         }
  1190.  
  1191.         if (PieceRot != OldRot)
  1192.         {
  1193.             LocalAmbientSound("acstris/rotate", 127);
  1194.         }
  1195.  
  1196.         if (PieceCollides(PieceX, PieceY+1, PieceType, PieceRot))
  1197.         {
  1198.             PieceLocking += 1;
  1199.             PieceLocking2 += 1;
  1200.  
  1201.             if (PieceLocking2 == 1)
  1202.             {
  1203.                 LocalAmbientSound("acstris/pieceland", 127);
  1204.             }
  1205.  
  1206.             if (PieceLocking >= LOCKTIME || PieceLocking2 >= LOCKTIME2)
  1207.             {
  1208.                 AddPiece(PieceX, PieceY, PieceType, PieceRot);
  1209.             }
  1210.         }
  1211.         else
  1212.         {
  1213.             PieceLocking = 0;
  1214.         }
  1215.  
  1216.         if (OldX != PieceX || OldY != PieceY || OldRot != PieceRot)
  1217.         {
  1218.             PieceLocking = 0;
  1219.         }
  1220.  
  1221.         if (TetKey(KEY_JUMP) == 1) { HardDrop(PieceX, PieceY, PieceType, PieceRot); }
  1222.  
  1223.         /*
  1224.         tolog = "";
  1225.  
  1226.         for (i = 0; i < NEXTCOUNT; i++)
  1227.         {
  1228.             tolog = StrParam(s:tolog, d:NextPieces[i], s:" ");
  1229.         }
  1230.         Log(s:tolog);
  1231.         */
  1232.  
  1233.         DrawField();
  1234.         Delay(1);
  1235.     }
  1236.  
  1237.     while (PlayingTetris[pln])
  1238.     {
  1239.         gameOverY = min(gameOverY + 1, MATRIX_Y);
  1240.  
  1241.         DrawFieldGameOver(gameOverY);
  1242.         gameovertic++;
  1243.  
  1244.         if (gameovertic > 35)
  1245.         {
  1246.             SetHudSize(640, 480, 1);
  1247.             SetFont("BIGFONT");
  1248.  
  1249.             HudMessage(s:"GAME OVER."; HUDMSG_FADEOUT, 1500, CR_WHITE, x, y, 1.0, 0.5);
  1250.         }
  1251.  
  1252.         if (gameovertic == 35) { LocalAmbientSound("acstris/gameover", 127); }
  1253.  
  1254.         if (gameovertic > 105)
  1255.         {
  1256.             SetHudSize(640, 480, 1);
  1257.             SetFont("SMALLFONT");
  1258.  
  1259.             HudMessage(s:"Puke script 123 to exit."; HUDMSG_FADEOUT, 1501, CR_WHITE, x, y+32.0, 1.0, 0.5);
  1260.         }
  1261.        
  1262.         Delay(1);
  1263.     }
  1264. }
  1265.  
  1266. script 133 ENTER
  1267. {
  1268.     Print(s:"Puke script 123 to play ACSTris.");
  1269. }
  1270.  
  1271. script 134 OPEN clientside
  1272. {
  1273.     while (1)
  1274.     {
  1275.         TetrisFont = TetrisFonts[middle(1, defaultCVar("tetris_skin", 1), FONTCOUNT)-1];
  1276.         Delay(1);
  1277.     }
  1278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement