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_BLOCK = 133;
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);
229 vector PieceShape(float pc)
234 return '85 0 0'; // I
236 return '21 1 0'; // L
238 return '1 21 0'; // J
240 return '5 20 0'; // Z
242 return '20 5 0'; // S
244 return '4 21 0'; // T
249 vector PieceCenter(float pc)
252 return '1.5 1.5 0'; // O
254 return '2.5 1.5 0'; // I
269 // do x 1..4 and y 1..4 in case of rotation
270 float PieceMetric(float x, float y, float rot, float pc)
276 // return bits of a piece
277 wid = piece_dat_z + 1;
278 piece_dat = PieceCenter(pc);
279 if (rot == 1) // 90 degrees
281 // x+cx, y+cy -> -y+cx, x+cy
282 // X, Y -> -Y+cy+cx, X-cx+cy
286 y = x - piece_dat_x + piece_dat_y;
287 x = -t + piece_dat_x + piece_dat_y;
289 else if (rot == 2)//180
291 x = 2 * piece_dat_x - x;
292 y = 2 * piece_dat_y - y;
294 else if (rot == 3) // 270
296 // x+cx, y+cy -> y+cx, -x+cy
297 // X, Y -> Y-cy+cx, -X+cx+cy
301 y = -x + piece_dat_y + piece_dat_x;
302 x = t - piece_dat_y + piece_dat_x;
304 if (x < 1 || y < 1 || x > 4 || y > 2)
306 piece_dat = PieceShape(pc);
308 return GetXBlock(x, piece_dat_x); // first row
310 return GetXBlock(x, piece_dat_y); // second row
312 return 0; // illegal parms
315 *********************************
319 *********************************
323 /* some prydon gate functions to make life easier....
325 somewhat modified because we don't need all the fanciness Prydon Gate is capable of
329 void WriteTetrisString(string s)
331 WriteUnterminatedString(MSG_ONE, strconv(0, 2, 2, s));
334 float pnum(float num, float dig)
339 WriteChar(MSG_ONE, 173);
343 num = num - (f * 10);
345 dig = pnum(f, dig+1);
349 for (i = 0; i < (5 - dig); i = i + 1)
350 WriteChar(MSG_ONE, TET_SPACE);
352 WriteChar(MSG_ONE, 176 + num);
356 void DrawLine(float ln)
359 WriteChar(MSG_ONE, TET_BORDER);
361 for (x = 1; x <= TET_WIDTH; x = x + 1)
363 d = GetSquare(x, ln);
366 WriteChar(MSG_ONE, '^');
367 WriteChar(MSG_ONE, d * d - 2 * d + 50); // 1, 2, 5
368 WriteChar(MSG_ONE, TET_BLOCK);
371 WriteChar(MSG_ONE, TET_SPACE);
373 WriteChar(MSG_ONE, '^');
374 WriteChar(MSG_ONE, '7');
375 WriteChar(MSG_ONE, TET_BORDER);
378 void DrawPiece(float pc, float ln)
380 float x, d, piece_ln, pcolor;
382 pcolor = mod(pc, 3) + 1;
383 WriteChar(MSG_ONE, TET_SPACE); // pad to 6
385 piece_dat = PieceShape(pc);
387 piece_ln = piece_dat_x;
389 piece_ln = piece_dat_y;
390 for (x = 1; x <= 4; x = x + 1)
392 d = GetXBlock(x, piece_ln) * pcolor;
395 WriteChar(MSG_ONE, '^');
396 WriteChar(MSG_ONE, d * d - 2 * d + 50); // 1, 2, 5
397 WriteChar(MSG_ONE, TET_BLOCK);
400 WriteChar(MSG_ONE, TET_SPACE);
402 WriteChar(MSG_ONE, TET_SPACE); // pad to 6
409 WriteChar(MSG_ONE, SVC_CENTERPRINTa);
411 for (i = 1; i <= (TET_WIDTH + 2); i = i + 1)
412 WriteChar(MSG_ONE, TET_BORDER);
413 WriteTetrisString(" ");
414 WriteUnterminatedString(MSG_ONE, TET_PADDING_RIGHT);
415 WriteChar(MSG_ONE, 10);
416 for (i = 1; i <= TET_LINES; i = i + 1)
418 if(self.tetris_on == 2)
419 WriteTetrisString(" GAME OVER ");
420 else if(self.tetris_on == 3)
421 WriteTetrisString("PLEASE WAIT");
425 WriteTetrisString(" NEXT ");
427 DrawPiece(self.next_piece, 1);
429 DrawPiece(self.next_piece, 2);
431 WriteTetrisString(" LINES");
433 pnum(self.tet_lines, 0);
435 WriteTetrisString(" SCORE");
437 pnum(self.tet_score, 0);
439 WriteTetrisString(" HIGH ");
441 WriteTetrisString(" SCORE");
443 pnum(tet_high_score, 0);
445 WriteTetrisString(" LEVEL");
447 pnum(Tetris_Level(), 0);
449 WriteTetrisString(" ");
450 WriteUnterminatedString(MSG_ONE, TET_PADDING_RIGHT);
451 WriteChar(MSG_ONE, 10);
455 for (i = 1; i <= (TET_WIDTH + 2); i = i + 1)
456 WriteChar(MSG_ONE, TET_BORDER);
457 WriteTetrisString(" ");
458 WriteUnterminatedString(MSG_ONE, TET_PADDING_RIGHT);
459 WriteChar(MSG_ONE, 10);
464 WriteChar(MSG_ONE, 10);
465 WriteChar(MSG_ONE, 10);
466 if(self.tetris_on == 3)
468 WriteUnterminatedString(MSG_ONE, strcat("WAITING FOR OTHERS (", ftos(ceil(tet_vs_current_timeout - time)), " SEC)\n"));
471 WriteChar(MSG_ONE, 10);
472 FOR_EACH_REALCLIENT(head) if(head.tetris_on) if(head.tet_vs_id == self.tet_vs_id)
475 WriteUnterminatedString(MSG_ONE, ">");
477 WriteUnterminatedString(MSG_ONE, " ");
478 if(head.tetris_on == 2)
479 WriteUnterminatedString(MSG_ONE, " X_X");
481 pnum(head.tet_highest_line, 0);
482 WriteUnterminatedString(MSG_ONE, " ");
483 WriteUnterminatedString(MSG_ONE, head.netname);
484 WriteChar(MSG_ONE, 10);
488 WriteChar(MSG_ONE, 0);
491 *********************************
495 *********************************
503 for (i=1; i<=TET_LINES; i = i + 1)
505 self.piece_pos = '0 0 0';
507 self.next_piece = self.tet_lines = self.tet_score = 0;
513 centerprint(self, " ");
517 self.movetype = MOVETYPE_WALK;
523 *********************************
527 *********************************
531 return floor(random() * PIECES) + 1;
534 void TetAddScore(float n)
536 self.tet_score = self.tet_score + n * Tetris_Level();
537 if (self.tet_score > tet_high_score)
538 tet_high_score = self.tet_score;
540 float CheckMetrics(float piece, float orgx, float orgy, float rot) /*FIXDECL*/
542 // check to see if the piece, if moved to the locations will overlap
545 // why did I start counting from 1, damnit
549 for (y = 0; y < 5; y = y + 1)
551 for (x = 0; x < 5; x = x + 1)
553 if (PieceMetric(x, y, rot, piece))
555 if (GetSquare(x + orgx, y + orgy))
556 return FALSE; // uhoh, gonna hit something.
557 if (x+orgx<1 || x+orgx > TET_WIDTH || y+orgy<1 || y+orgy> TET_LINES)
558 return FALSE; // ouside the level
565 void ClearPiece(float piece, float orgx, float orgy, float rot) /*FIXDECL*/
569 // why did I start counting from 1, damnit
573 for (y = 0; y < 5; y = y + 1)
575 for (x = 0; x < 5; x = x + 1)
577 if (PieceMetric(x, y, rot, piece))
579 SetSquare(x + orgx, y + orgy, 0);
584 void CementPiece(float piece, float orgx, float orgy, float rot) /*FIXDECL*/
588 // why did I start counting from 1, damnit
592 pcolor = mod(piece, 3) + 1;
594 for (y = 0; y < 5; y = y + 1)
596 for (x = 0; x < 5; x = x + 1)
598 if (PieceMetric(x, y, rot, piece))
600 SetSquare(x + orgx, y + orgy, pcolor);
606 float LINE_LOW = 349525;
607 float LINE_HIGH = 699050; // above number times 2
609 void AddLines(float n)
614 FOR_EACH_REALCLIENT(head) if(head != self) if(head.tetris_on) if(head.tet_vs_id == self.tet_vs_id)
615 head.tet_vs_addlines += n;
618 void CompletedLines()
620 float y, cleared, ln, added, pos, i;
627 if (((ln & LINE_LOW) | ((ln & LINE_HIGH)/2)) == LINE_LOW)
628 cleared = cleared + 1;
631 ln = GetLine(y - cleared);
637 else if(cleared >= 1)
638 AddLines(cleared - 1);
640 self.tet_lines = self.tet_lines + cleared;
641 TetAddScore(cleared * cleared * 10);
643 added = self.tet_vs_addlines;
644 self.tet_vs_addlines = 0;
648 for(y = 1; y <= TET_LINES - added; ++y)
650 SetLine(y, GetLine(y + added));
652 for(y = max(1, TET_LINES - added + 1); y <= TET_LINES; ++y)
654 pos = floor(random() * TET_WIDTH);
656 for(i = 1; i <= TET_WIDTH; ++i)
658 ln = SetXBlock(i, ln, floor(random() * 3 + 1));
663 self.tet_highest_line = 0;
664 for(y = 1; y <= TET_LINES; ++y)
667 self.tet_highest_line = TET_LINES + 1 - y;
673 else if(cleared >= 4)
681 void HandleGame(float keyss)
684 // first off, we need to see if we need a new piece
693 if (self.piece_type == 0)
695 self.piece_pos = '5 1 0'; // that's about middle top, we count from 1 ARGH
697 self.piece_type = self.next_piece;
699 self.piece_type = RandomPiece();
700 self.next_piece = RandomPiece();
701 keyss = 0; // no movement first frame
702 self.tet_autodown = time + 0.2;
706 ClearPiece(self.piece_type, self.piece_pos_x, self.piece_pos_y, self.piece_pos_z);
708 // next we need to check the piece metrics against what's on the level
709 // based on the key order
711 old_pos = check_pos = self.piece_pos;
715 if (keyss & TETKEY_RIGHT)
717 check_pos_x = check_pos_x + 1;
720 else if (keyss & TETKEY_LEFT)
722 check_pos_x = check_pos_x - 1;
725 else if (keyss & TETKEY_ROTRIGHT)
727 check_pos_z = check_pos_z + 1;
728 piece_data = PieceShape(self.piece_type);
732 else if (keyss & TETKEY_ROTLEFT)
734 check_pos_z = check_pos_z - 1;
735 piece_data = PieceShape(self.piece_type);
742 else if (check_pos_z < 0)
746 if (CheckMetrics(self.piece_type, check_pos_x, check_pos_y, check_pos_z))
747 self.piece_pos = check_pos;
751 self.tet_gameovertime = time + 5;
756 for(i = 1; i <= nudge; ++i)
758 if(CheckMetrics(self.piece_type, check_pos_x + i, check_pos_y, check_pos_z))
760 self.piece_pos = check_pos + '1 0 0' * i;
763 else if(CheckMetrics(self.piece_type, check_pos_x - i, check_pos_y, check_pos_z))
765 self.piece_pos = check_pos - '1 0 0' * i;
770 check_pos = self.piece_pos;
771 if(keyss & TETKEY_DROP)
773 // drop to bottom, but do NOT cement it yet
774 // this allows sliding it
776 while(CheckMetrics(self.piece_type, check_pos_x, check_pos_y + 1, check_pos_z))
778 self.tet_autodown = time + 2 / (1 + Tetris_Level());
780 else if (keyss & TETKEY_DOWN)
782 check_pos_y = check_pos_y + 1;
783 self.tet_autodown = time + 2 / (1 + Tetris_Level());
785 else if (self.tet_autodown < time)
787 check_pos_y = check_pos_y + 1;
788 self.tet_autodown = time + 2 / (1 + Tetris_Level());
790 if (CheckMetrics(self.piece_type, check_pos_x, check_pos_y, check_pos_z))
792 if(old_pos != check_pos)
793 self.tet_drawtime = 0;
794 self.piece_pos = check_pos;
798 CementPiece(self.piece_type, self.piece_pos_x, self.piece_pos_y, self.piece_pos_z);
802 self.tet_drawtime = 0;
805 CementPiece(self.piece_type, self.piece_pos_x, self.piece_pos_y, self.piece_pos_z);
809 *********************************
811 Important Linking Into Quake stuff
813 *********************************
819 if(self.tetris_on == 0 || self.tetris_on == 2) // from "off" or "game over"
823 if(time < tet_vs_current_timeout)
826 self.tet_vs_id = tet_vs_current_id;
832 tet_vs_current_timeout = time + 15;
833 self.tet_vs_id = tet_vs_current_id;
834 bprint("^1TET^7R^1IS: ", self.netname, "^1 started a new game. Do ^7impulse 100^1 to join.\n");
836 self.tet_highest_line = 0;
838 self.tet_org = self.origin;
839 self.movetype = MOVETYPE_NOCLIP;
841 else if(self.tetris_on == 1) // from "on"
849 float TetrisPreFrame()
854 self.tet_org = self.origin;
855 if (self.tet_drawtime > time)
858 if(self.tetris_on == 3)
859 self.tet_drawtime = ceil(time - tet_vs_current_timeout + 0.1) + tet_vs_current_timeout;
861 self.tet_drawtime = time + 0.5;
864 float frik_anglemoda(float v)
866 return v - floor(v/360) * 360;
868 float angcompa(float y1, float y2)
870 y1 = frik_anglemoda(y1);
871 y2 = frik_anglemoda(y2);
876 answer = answer - 360;
877 else if (answer < -180)
878 answer = answer + 360;
882 .float tetkey_down, tetkey_rotright, tetkey_left, tetkey_right, tetkey_rotleft, tetkey_drop;
884 float TetrisKeyRepeat(.float fld, float f)
888 if(self.fld == 0) // initial key press
890 self.fld = time + 0.3;
893 else if(time > self.fld)
895 self.fld = time + 0.1;
900 // repeating too fast
911 float TetrisPostFrame()
920 if(self.tetris_on == 2 && time > self.tet_gameovertime)
926 if(self.tetris_on == 3 && time > tet_vs_current_timeout)
928 self.tetris_on = 1; // start VS game
929 self.tet_drawtime = 0;
932 self.origin = self.tet_org;
933 self.movetype = MOVETYPE_NONE;
935 if(self.tetris_on == 1)
937 if(TetrisKeyRepeat(tetkey_down, self.movement_x < 0))
938 keysa |= TETKEY_DOWN;
940 if(TetrisKeyRepeat(tetkey_rotright, self.movement_x > 0))
941 keysa |= TETKEY_ROTRIGHT;
943 if(TetrisKeyRepeat(tetkey_left, self.movement_y < 0))
944 keysa |= TETKEY_LEFT;
946 if(TetrisKeyRepeat(tetkey_right, self.movement_y > 0))
947 keysa |= TETKEY_RIGHT;
949 if(TetrisKeyRepeat(tetkey_rotleft, self.BUTTON_CROUCH))
950 keysa |= TETKEY_ROTLEFT;
952 if(TetrisKeyRepeat(tetkey_drop, self.BUTTON_JUMP))
953 keysa |= TETKEY_DROP;