13 float tet_vs_current_id;
14 float tet_vs_current_timeout;
15 .float tet_vs_id, tet_vs_addlines;
16 .float tet_highest_line;
17 .float tetris_on, tet_gameovertime, tet_drawtime, tet_autodown;
19 .float piece_type, next_piece, tet_score, tet_lines;
24 // 3 = waiting for VS players
26 var float tet_high_score = 0;
31 float TET_BORDER = 139;
32 float TET_BLOCKS = 132; // +1 = first color, +2, +3;
33 float TET_SPACE = 160; // blankness
38 float TETKEY_DOWN = 2;
39 float TETKEY_LEFT = 4;
40 float TETKEY_RIGHT = 8;
41 float TETKEY_ROTLEFT = 16;
42 float TETKEY_ROTRIGHT = 32;
43 float TETKEY_DROP = 64;
44 string TET_PADDING_RIGHT = "\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0"; // get away from crosshair
48 .float line1, line2, line3, line4, line5, line6, line7,
49 line8, line9, line10, line11, line12, line13, line14, line15,
50 line16, line17, line18, line19, line20;
53 float SVC_CENTERPRINTa = 26;
57 return ((floor((self.tet_lines / 10)) + 1));
60 void tetsnd(string snd)
62 play2(self, strcat("sounds/tetris/", snd));
66 *********************************
70 *********************************
72 void SetLine(float ln, float vl)
116 float GetLine(float ln)
162 float GetXBlock(float x, float dat)
167 return (dat & 12) / 4;
169 return (dat & 48) / 16;
171 return (dat & 192) / 64;
173 return (dat & 768) / 256;
175 return (dat & 3072) / 1024;
177 return (dat & 12288) / 4096;
179 return (dat & 49152) / 16384;
181 return (dat & 196608) / 65536;
183 return (dat & 786432) / 262144;
188 float SetXBlock(float x, float dat, float new)
191 return (dat - (dat & 3)) | new;
193 return (dat - (dat & 12)) | (new*4);
195 return (dat - (dat & 48)) | (new*16);
197 return (dat - (dat & 192)) | (new*64);
199 return (dat - (dat & 768)) | (new*256);
201 return (dat - (dat & 3072)) | (new*1024);
203 return (dat - (dat & 12288)) | (new*4096);
205 return (dat - (dat & 49152)) | (new*16384);
207 return (dat - (dat & 196608)) | (new*65536);
209 return (dat - (dat & 786432)) | (new*262144);
215 float GetSquare(float x, float y)
217 return GetXBlock(x, GetLine(y));
220 void SetSquare(float x, float y, float val)
225 dat = SetXBlock(x, dat, val & 3);
231 vector PieceShape(float pc)
240 return '5 5 2'; // 1 * 4 + 1 * 16
295 // do x 1..4 and y 1..4 in case of rotation
296 float PieceMetric(float x, float y, float rot, float pc)
302 // return bits of a piece
303 piece_dat = PieceShape(pc);
304 wid = piece_dat_z + 1;
305 if (rot == 1) // 90 degrees
311 else if (rot == 2)//180
316 else if (rot == 3) // 270
322 if (x < 1 || y < 1 || x > 4 || y > 2)
325 return GetXBlock(x, piece_dat_x); // first row
327 return GetXBlock(x, piece_dat_y); // second row
329 return GetXBlock(x, piece_dat_z); // third row (doesn't exist)
331 return 0; // illegal parms
334 *********************************
338 *********************************
342 /* some prydon gate functions to make life easier....
344 somewhat modified because we don't need all the fanciness Prydon Gate is capable of
348 void WriteTetrisString(string s)
350 WriteUnterminatedString(MSG_ONE, strconv(0, 2, 2, s));
353 float pnum(float num, float dig)
358 WriteChar(MSG_ONE, 173);
362 num = num - (f * 10);
364 dig = pnum(f, dig+1);
368 for (i = 0; i < (5 - dig); i = i + 1)
369 WriteChar(MSG_ONE, TET_SPACE);
371 WriteChar(MSG_ONE, 176 + num);
375 void DrawLine(float ln)
378 WriteChar(MSG_ONE, TET_BORDER);
380 for (x = 1; x <= TET_WIDTH; x = x + 1)
382 d = GetSquare(x, ln);
384 WriteChar(MSG_ONE, TET_BLOCKS + d);
386 WriteChar(MSG_ONE, TET_SPACE);
388 WriteChar(MSG_ONE, TET_BORDER);
391 void DrawPiece(float pc, float ln)
393 float x, d, piece_ln, pcolor;
396 if (pcolor == 0) // 4
398 WriteChar(MSG_ONE, TET_SPACE); // pad to 6
400 piece_dat = PieceShape(pc);
402 piece_ln = piece_dat_x;
404 piece_ln = piece_dat_y;
405 for (x = 1; x <= 4; x = x + 1)
407 d = GetXBlock(x, piece_ln) * pcolor;
409 WriteChar(MSG_ONE, TET_BLOCKS + d);
411 WriteChar(MSG_ONE, TET_SPACE);
413 WriteChar(MSG_ONE, TET_SPACE); // pad to 6
420 WriteChar(MSG_ONE, SVC_CENTERPRINTa);
422 for (i = 1; i <= (TET_WIDTH + 2); i = i + 1)
423 WriteChar(MSG_ONE, TET_BORDER);
424 WriteTetrisString(" ");
425 WriteUnterminatedString(MSG_ONE, TET_PADDING_RIGHT);
426 WriteChar(MSG_ONE, 10);
427 for (i = 1; i <= TET_LINES; i = i + 1)
429 if(self.tetris_on == 2)
430 WriteTetrisString(" GAME OVER ");
431 else if(self.tetris_on == 3)
432 WriteTetrisString("PLEASE WAIT");
436 WriteTetrisString(" NEXT ");
438 DrawPiece(self.next_piece, 1);
440 DrawPiece(self.next_piece, 2);
442 WriteTetrisString(" LINES");
444 pnum(self.tet_lines, 0);
446 WriteTetrisString(" SCORE");
448 pnum(self.tet_score, 0);
450 WriteTetrisString(" HIGH ");
452 WriteTetrisString(" SCORE");
454 pnum(tet_high_score, 0);
456 WriteTetrisString(" LEVEL");
458 pnum(Tetris_Level(), 0);
460 WriteTetrisString(" ");
461 WriteUnterminatedString(MSG_ONE, TET_PADDING_RIGHT);
462 WriteChar(MSG_ONE, 10);
466 for (i = 1; i <= (TET_WIDTH + 2); i = i + 1)
467 WriteChar(MSG_ONE, TET_BORDER);
468 WriteTetrisString(" ");
469 WriteUnterminatedString(MSG_ONE, TET_PADDING_RIGHT);
470 WriteChar(MSG_ONE, 10);
475 WriteChar(MSG_ONE, 10);
476 WriteChar(MSG_ONE, 10);
477 if(self.tetris_on == 3)
479 WriteUnterminatedString(MSG_ONE, strcat("WAITING FOR OTHERS (", ftos(ceil(tet_vs_current_timeout - time)), " SEC)\n"));
482 WriteChar(MSG_ONE, 10);
483 FOR_EACH_REALCLIENT(head) if(head.tetris_on) if(head.tet_vs_id == self.tet_vs_id)
486 WriteUnterminatedString(MSG_ONE, ">");
488 WriteUnterminatedString(MSG_ONE, " ");
489 if(head.tetris_on == 2)
490 WriteUnterminatedString(MSG_ONE, " X_X");
492 pnum(head.tet_highest_line, 0);
493 WriteUnterminatedString(MSG_ONE, " ");
494 WriteUnterminatedString(MSG_ONE, head.netname);
496 WriteChar(MSG_ONE, 10);
499 WriteChar(MSG_ONE, 0);
502 *********************************
506 *********************************
514 for (i=1; i<=TET_LINES; i = i + 1)
516 self.piece_pos = '0 0 0';
518 self.next_piece = self.tet_lines = self.tet_score = 0;
524 centerprint(self, " ");
528 self.movetype = MOVETYPE_WALK;
534 *********************************
538 *********************************
542 return floor(random() * PIECES) + 1;
545 void TetAddScore(float n)
547 self.tet_score = self.tet_score + n * Tetris_Level();
548 if (self.tet_score > tet_high_score)
549 tet_high_score = self.tet_score;
551 float CheckMetrics(float piece, float orgx, float orgy, float rot) /*FIXDECL*/
553 // check to see if the piece, if moved to the locations will overlap
556 // why did I start counting from 1, damnit
560 for (y = 1; y < 5; y = y + 1)
562 for (x = 1; x < 5; x = x + 1)
564 if (PieceMetric(x, y, rot, piece))
566 if (GetSquare(x + orgx, y + orgy))
567 return FALSE; // uhoh, gonna hit something.
568 if (x+orgx<1 || x+orgx > TET_WIDTH || y+orgy<1 || y+orgy> TET_LINES)
569 return FALSE; // ouside the level
576 void ClearPiece(float piece, float orgx, float orgy, float rot) /*FIXDECL*/
580 // why did I start counting from 1, damnit
584 for (y = 1; y < 5; y = y + 1)
586 for (x = 1; x < 5; x = x + 1)
588 if (PieceMetric(x, y, rot, piece))
590 SetSquare(x + orgx, y + orgy, 0);
595 void CementPiece(float piece, float orgx, float orgy, float rot) /*FIXDECL*/
599 // why did I start counting from 1, damnit
604 if (pcolor == 0) // 4
607 for (y = 1; y < 5; y = y + 1)
609 for (x = 1; x < 5; x = x + 1)
611 if (PieceMetric(x, y, rot, piece))
613 SetSquare(x + orgx, y + orgy, pcolor);
619 float LINE_LOW = 349525;
620 float LINE_HIGH = 699050; // above number times 2
622 void AddLines(float n)
627 FOR_EACH_REALCLIENT(head) if(head != self) if(head.tetris_on) if(head.tet_vs_id == self.tet_vs_id)
628 head.tet_vs_addlines += n;
631 void CompletedLines()
633 float y, cleared, ln, added, pos, i;
640 if (((ln & LINE_LOW) | ((ln & LINE_HIGH)/2)) == LINE_LOW)
641 cleared = cleared + 1;
644 ln = GetLine(y - cleared);
650 else if(cleared >= 1)
651 AddLines(cleared - 1);
653 self.tet_lines = self.tet_lines + cleared;
654 TetAddScore(cleared * cleared * 10);
656 added = self.tet_vs_addlines;
657 self.tet_vs_addlines = 0;
661 for(y = 1; y <= TET_LINES - added; ++y)
663 SetLine(y, GetLine(y + added));
665 for(y = max(1, TET_LINES - added + 1); y <= TET_LINES; ++y)
667 pos = floor(random() * TET_WIDTH);
669 for(i = 1; i <= TET_WIDTH; ++i)
671 ln = SetXBlock(i, ln, floor(random() * 3 + 1));
676 self.tet_highest_line = 0;
677 for(y = 1; y <= TET_LINES; ++y)
680 self.tet_highest_line = TET_LINES + 1 - y;
686 else if(cleared >= 4)
694 void HandleGame(float keyss)
697 // first off, we need to see if we need a new piece
706 if (self.piece_type == 0)
708 self.piece_pos = '5 1 0'; // that's about middle top, we count from 1 ARGH
710 self.piece_type = self.next_piece;
712 self.piece_type = RandomPiece();
713 self.next_piece = RandomPiece();
714 keyss = 0; // no movement first frame
715 self.tet_autodown = time + 0.2;
719 ClearPiece(self.piece_type, self.piece_pos_x, self.piece_pos_y, self.piece_pos_z);
721 // next we need to check the piece metrics against what's on the level
722 // based on the key order
724 old_pos = check_pos = self.piece_pos;
728 if (keyss & TETKEY_RIGHT)
730 check_pos_x = check_pos_x + 1;
733 else if (keyss & TETKEY_LEFT)
735 check_pos_x = check_pos_x - 1;
738 else if (keyss & TETKEY_ROTRIGHT)
740 check_pos_z = check_pos_z + 1;
741 piece_data = PieceShape(self.piece_type);
742 nudge = piece_data_z - 2;
745 else if (keyss & TETKEY_ROTLEFT)
747 check_pos_z = check_pos_z - 1;
748 piece_data = PieceShape(self.piece_type);
749 nudge = piece_data_z - 2;
755 else if (check_pos_z < 0)
759 if (CheckMetrics(self.piece_type, check_pos_x, check_pos_y, check_pos_z))
760 self.piece_pos = check_pos;
764 self.tet_gameovertime = time + 2;
769 for(i = 1; i <= nudge; ++i)
771 if(CheckMetrics(self.piece_type, check_pos_x + i, check_pos_y, check_pos_z))
773 self.piece_pos = check_pos + '1 0 0' * i;
776 else if(CheckMetrics(self.piece_type, check_pos_x - i, check_pos_y, check_pos_z))
778 self.piece_pos = check_pos - '1 0 0' * i;
783 check_pos = self.piece_pos;
784 if(keyss & TETKEY_DROP)
786 // drop to bottom, but do NOT cement it yet
787 // this allows sliding it
789 while(CheckMetrics(self.piece_type, check_pos_x, check_pos_y + 1, check_pos_z))
791 self.tet_autodown = time + 2 / (1 + Tetris_Level());
793 else if (keyss & TETKEY_DOWN)
795 check_pos_y = check_pos_y + 1;
796 self.tet_autodown = time + 2 / (1 + Tetris_Level());
798 else if (self.tet_autodown < time)
800 check_pos_y = check_pos_y + 1;
801 self.tet_autodown = time + 2 / (1 + Tetris_Level());
803 if (CheckMetrics(self.piece_type, check_pos_x, check_pos_y, check_pos_z))
805 if(old_pos != check_pos)
806 self.tet_drawtime = 0;
807 self.piece_pos = check_pos;
811 CementPiece(self.piece_type, self.piece_pos_x, self.piece_pos_y, self.piece_pos_z);
815 self.tet_drawtime = 0;
818 CementPiece(self.piece_type, self.piece_pos_x, self.piece_pos_y, self.piece_pos_z);
822 *********************************
824 Important Linking Into Quake stuff
826 *********************************
832 if(self.tetris_on != 1 && self.tetris_on != 3)
836 if(time < tet_vs_current_timeout)
839 self.tet_vs_id = tet_vs_current_id;
845 tet_vs_current_timeout = time + 15;
846 self.tet_vs_id = tet_vs_current_id;
847 bprint("^2TET^4R^2IS: ", self.netname, "^2 started a new game. Do 'impulse 100' to join.\n");
849 self.tet_highest_line = 0;
851 self.tet_org = self.origin;
852 self.movetype = MOVETYPE_NOCLIP;
862 float TetrisPreFrame()
867 self.tet_org = self.origin;
868 if (self.tet_drawtime > time)
871 if(self.tetris_on == 3)
872 self.tet_drawtime = ceil(time - tet_vs_current_timeout + 0.1) + tet_vs_current_timeout;
874 self.tet_drawtime = time + 0.5;
877 float frik_anglemoda(float v)
879 return v - floor(v/360) * 360;
881 float angcompa(float y1, float y2)
883 y1 = frik_anglemoda(y1);
884 y2 = frik_anglemoda(y2);
889 answer = answer - 360;
890 else if (answer < -180)
891 answer = answer + 360;
895 .float tetkey_down, tetkey_rotright, tetkey_left, tetkey_right, tetkey_rotleft, tetkey_drop;
897 float TetrisKeyRepeat(.float fld, float f)
901 if(self.fld == 0) // initial key press
903 self.fld = time + 0.3;
906 else if(time > self.fld)
908 self.fld = time + 0.1;
913 // repeating too fast
924 float TetrisPostFrame()
932 if(self.tetris_on == 2 && time > self.tet_gameovertime)
937 if(self.tetris_on == 3 && time > tet_vs_current_timeout)
939 self.tetris_on = 1; // start VS game
940 self.tet_drawtime = 0;
943 self.origin = self.tet_org;
944 self.movetype = MOVETYPE_NONE;
946 if(self.tetris_on == 1)
948 if(TetrisKeyRepeat(tetkey_down, self.movement_x < 0))
949 keysa |= TETKEY_DOWN;
951 if(TetrisKeyRepeat(tetkey_rotright, self.movement_x > 0))
952 keysa |= TETKEY_ROTRIGHT;
954 if(TetrisKeyRepeat(tetkey_left, self.movement_y < 0))
955 keysa |= TETKEY_LEFT;
957 if(TetrisKeyRepeat(tetkey_right, self.movement_y > 0))
958 keysa |= TETKEY_RIGHT;
960 if(TetrisKeyRepeat(tetkey_rotleft, self.BUTTON_CROUCH))
961 keysa |= TETKEY_ROTLEFT;
963 if(TetrisKeyRepeat(tetkey_drop, self.BUTTON_JUMP))
964 keysa |= TETKEY_DROP;