11 float autocvar_g_bastet;
14 float tet_vs_current_id;
15 float tet_vs_current_timeout;
16 .float tet_vs_id, tet_vs_addlines;
17 .float tet_highest_line;
18 .float tetris_on, tet_gameovertime, tet_drawtime, tet_autodown;
20 .float piece_type, next_piece, tet_score, tet_lines;
21 .float tet_piece_bucket;
26 // 3 = waiting for VS players
28 var float tet_high_score = 0;
30 vector TET_START_PIECE_POS = '5 1 0';
32 float TET_DISPLAY_LINES = 20;
35 float TET_BORDER = 139;
36 float TET_BLOCK = 133;
37 float TET_SPACE = 160; // blankness
42 float TETKEY_DOWN = 2;
43 float TETKEY_LEFT = 4;
44 float TETKEY_RIGHT = 8;
45 float TETKEY_ROTLEFT = 16;
46 float TETKEY_ROTRIGHT = 32;
47 float TETKEY_DROP = 64;
48 string TET_PADDING_RIGHT = "\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0"; // get away from crosshair
52 .float line1, line2, line3, line4, line5, line6, line7,
53 line8, line9, line10, line11, line12, line13, line14, line15,
54 line16, line17, line18, line19, line20, line21, line22;
57 float SVC_CENTERPRINTa = 26;
61 return ((floor((self.tet_lines / 10)) + 1));
64 void tetsnd(string snd)
66 play2(self, strcat("sounds/tetris/", snd));
70 *********************************
74 *********************************
76 void SetLine(float ln, float vl)
124 float GetLine(float ln)
174 float GetXBlock(float x, float dat)
179 return (dat & 12) / 4;
181 return (dat & 48) / 16;
183 return (dat & 192) / 64;
185 return (dat & 768) / 256;
187 return (dat & 3072) / 1024;
189 return (dat & 12288) / 4096;
191 return (dat & 49152) / 16384;
193 return (dat & 196608) / 65536;
195 return (dat & 786432) / 262144;
200 float SetXBlock(float x, float dat, float new)
203 return (dat - (dat & 3)) | new;
205 return (dat - (dat & 12)) | (new*4);
207 return (dat - (dat & 48)) | (new*16);
209 return (dat - (dat & 192)) | (new*64);
211 return (dat - (dat & 768)) | (new*256);
213 return (dat - (dat & 3072)) | (new*1024);
215 return (dat - (dat & 12288)) | (new*4096);
217 return (dat - (dat & 49152)) | (new*16384);
219 return (dat - (dat & 196608)) | (new*65536);
221 return (dat - (dat & 786432)) | (new*262144);
227 float GetSquare(float x, float y)
229 return GetXBlock(x, GetLine(y));
232 void SetSquare(float x, float y, float val)
237 dat = SetXBlock(x, dat, val & 3);
241 vector PieceShape(float pc)
244 return '20 20 0'; // O
246 return '1 21 0'; // J
248 return '16 21 0'; // L
250 return '85 0 0'; // I
252 return '5 20 0'; // Z
254 return '20 5 0'; // S
256 return '4 21 0'; // T
260 vector PieceSize(float pc)
279 vector PieceCenter(float pc)
282 return '2.5 1.5 0'; // O
288 return '2.5 1.5 0'; // I
299 // do x 1..4 and y 1..4 in case of rotation
300 float PieceMetric(float x, float y, float rot, float pc)
305 // return bits of a piece
306 ce = PieceCenter(pc);
307 if (rot == 1) // 90 degrees
309 // x+cx, y+cy -> -y+cx, x+cy
310 // X, Y -> -Y+cy+cx, X-cx+cy
315 x = -t + ce_x + ce_y;
317 else if (rot == 2)//180
322 else if (rot == 3) // 270
324 // x+cx, y+cy -> y+cx, -x+cy
325 // X, Y -> Y-cy+cx, -X+cx+cy
329 y = -x + ce_y + ce_x;
332 if (x < 1 || y < 1 || x > 4 || y > 2)
336 return GetXBlock(x, ce_x); // first row
338 return GetXBlock(x, ce_y); // second row
340 return 0; // illegal parms
342 vector tet_piecemins;
343 vector tet_piecemaxs;
344 void PieceMinsMaxs(float rot, float pc)
351 ce = PieceCenter(pc);
356 tet_piecemins_x = floor(2.5 - sz_x * 0.5);
357 tet_piecemaxs_x = floor(2.0 + sz_x * 0.5);
359 tet_piecemaxs_y = sz_y;
360 if (rot == 1) // 90 degrees
363 tet_piecemins_y = -tet_piecemins_x + ce_y + ce_x;
364 tet_piecemins_x = t - ce_y + ce_x;
366 tet_piecemaxs_y = -tet_piecemaxs_x + ce_y + ce_x;
367 tet_piecemaxs_x = t - ce_y + ce_x;
368 // swap mins_y, maxs_y
370 tet_piecemins_y = tet_piecemaxs_y;
374 else if (rot == 2)//180
377 tet_piecemins = 2 * ce - tet_piecemaxs;
378 tet_piecemaxs = 2 * ce - v;
380 else if (rot == 3) // 270
383 tet_piecemins_y = tet_piecemins_x - ce_x + ce_y;
384 tet_piecemins_x = -t + ce_x + ce_y;
386 tet_piecemaxs_y = tet_piecemaxs_x - ce_x + ce_y;
387 tet_piecemaxs_x = -t + ce_x + ce_y;
388 // swap mins_x, maxs_x
390 tet_piecemins_x = tet_piecemaxs_x;
395 print(vtos(tet_piecemins), "-");
396 print(vtos(tet_piecemaxs), "\n");
397 if(tet_piecemins_x > tet_piecemaxs_x)
398 error("inconsistent mins/maxs");
399 if(tet_piecemins_y > tet_piecemaxs_y)
400 error("inconsistent mins/maxs");
402 for(i = 1; i <= 4; ++i)
403 for(j = 1; j <= 4; ++j)
404 if(PieceMetric(i, j, rot, pc))
405 if(i < tet_piecemins_x || i > tet_piecemaxs_x || j < tet_piecemins_y || j > tet_piecemaxs_y)
406 error(sprintf("incorrect mins/maxs: %d %d in %d rot %d mins %v maxs %v\n", i, j, rot, pc, tet_piecemins, tet_piecemaxs));
410 *********************************
414 *********************************
418 /* some prydon gate functions to make life easier....
420 somewhat modified because we don't need all the fanciness Prydon Gate is capable of
424 void WriteTetrisString(string s)
426 WriteUnterminatedString(MSG_ONE, strconv(0, 2, 2, s));
429 float pnum(float num, float dig)
434 WriteChar(MSG_ONE, 173);
438 num = num - (f * 10);
440 dig = pnum(f, dig+1);
444 for (i = 0; i < (5 - dig); i = i + 1)
445 WriteChar(MSG_ONE, TET_SPACE);
447 WriteChar(MSG_ONE, 176 + num);
451 void DrawLine(float ln)
454 WriteChar(MSG_ONE, TET_BORDER);
456 for (x = 1; x <= TET_WIDTH; x = x + 1)
458 d = GetSquare(x, ln + TET_LINES - TET_DISPLAY_LINES);
461 WriteChar(MSG_ONE, '^');
462 WriteChar(MSG_ONE, d * d - 2 * d + 50); // 1, 2, 5
463 WriteChar(MSG_ONE, TET_BLOCK);
466 WriteChar(MSG_ONE, TET_SPACE);
468 WriteChar(MSG_ONE, '^');
469 WriteChar(MSG_ONE, '7');
470 WriteChar(MSG_ONE, TET_BORDER);
473 void DrawPiece(float pc, float ln)
475 float x, d, piece_ln, pcolor;
477 pcolor = mod(pc, 3) + 1;
478 WriteChar(MSG_ONE, TET_SPACE); // pad to 6
480 piece_dat = PieceShape(pc);
482 piece_ln = piece_dat_x;
484 piece_ln = piece_dat_y;
485 for (x = 1; x <= 4; x = x + 1)
487 d = GetXBlock(x, piece_ln) * pcolor;
490 WriteChar(MSG_ONE, '^');
491 WriteChar(MSG_ONE, d * d - 2 * d + 50); // 1, 2, 5
492 WriteChar(MSG_ONE, TET_BLOCK);
495 WriteChar(MSG_ONE, TET_SPACE);
497 WriteChar(MSG_ONE, TET_SPACE); // pad to 6
504 WriteChar(MSG_ONE, SVC_CENTERPRINTa);
505 if(autocvar_g_bastet)
507 WriteTetrisString("NEVER GONNA GIVE YOU");
508 WriteChar(MSG_ONE, 10);
511 for (i = 1; i <= (TET_WIDTH + 2); i = i + 1)
512 WriteChar(MSG_ONE, TET_BORDER);
513 WriteTetrisString(" ");
514 WriteUnterminatedString(MSG_ONE, TET_PADDING_RIGHT);
515 WriteChar(MSG_ONE, 10);
516 for (i = 1; i <= TET_DISPLAY_LINES; i = i + 1)
518 if(self.tetris_on == 2)
519 WriteTetrisString(" GAME OVER ");
520 else if(self.tetris_on == 3)
521 WriteTetrisString("PLEASE WAIT");
525 WriteTetrisString(autocvar_g_bastet ? " THAT " : " NEXT ");
527 DrawPiece(self.next_piece, 1);
529 DrawPiece(self.next_piece, 2);
531 WriteTetrisString(" LINES");
533 pnum(self.tet_lines, 0);
535 WriteTetrisString(" SCORE");
537 pnum(self.tet_score, 0);
539 WriteTetrisString(" HIGH ");
541 WriteTetrisString(" SCORE");
543 pnum(tet_high_score, 0);
545 WriteTetrisString(" LEVEL");
547 pnum(Tetris_Level(), 0);
549 WriteTetrisString(" ");
550 WriteUnterminatedString(MSG_ONE, TET_PADDING_RIGHT);
551 WriteChar(MSG_ONE, 10);
555 for (i = 1; i <= (TET_WIDTH + 2); i = i + 1)
556 WriteChar(MSG_ONE, TET_BORDER);
557 WriteTetrisString(" ");
558 WriteUnterminatedString(MSG_ONE, TET_PADDING_RIGHT);
559 WriteChar(MSG_ONE, 10);
564 WriteChar(MSG_ONE, 10);
565 WriteChar(MSG_ONE, 10);
566 if(self.tetris_on == 3)
568 WriteUnterminatedString(MSG_ONE, strcat("WAITING FOR OTHERS (", ftos(ceil(tet_vs_current_timeout - time)), " SEC)\n"));
571 WriteChar(MSG_ONE, 10);
572 FOR_EACH_REALCLIENT(head) if(head.tetris_on) if(head.tet_vs_id == self.tet_vs_id)
575 WriteUnterminatedString(MSG_ONE, ">");
577 WriteUnterminatedString(MSG_ONE, " ");
578 if(head.tetris_on == 2)
579 WriteUnterminatedString(MSG_ONE, " X_X");
581 pnum(head.tet_highest_line, 0);
582 WriteUnterminatedString(MSG_ONE, " ");
583 WriteUnterminatedString(MSG_ONE, head.netname);
584 WriteChar(MSG_ONE, 10);
588 WriteChar(MSG_ONE, 0);
591 *********************************
595 *********************************
603 for (i=1; i<=TET_LINES; i = i + 1)
605 self.piece_pos = '0 0 0';
607 self.next_piece = self.tet_lines = self.tet_score = 0;
608 self.tet_piece_bucket = 0;
613 centerprint(self, " ");
617 self.movetype = MOVETYPE_WALK;
624 for(r = 1; r <= TET_LINES; ++r)
628 for(c = 1; c <= TET_WIDTH; ++c)
630 print(ftos(GetXBlock(c, l)));
636 float BastetEvaluate()
641 float occupied_count;
643 float score_save, occupied_save, occupied_count_save;
648 // adds a bonus for each free dot above the occupied blocks profile
649 occupied_count = TET_WIDTH;
652 for(i = 1; i <= TET_LINES; ++i)
661 occupied_save = occupied;
662 occupied_count_save = occupied_count;
664 for(j = 1; j <= TET_WIDTH; ++j)
668 if(!GetXBlock(j, occupied))
670 occupied = SetXBlock(j, occupied, 1);
676 score += 10000 * occupied_count;
680 occupied = occupied_save;
681 occupied_count = occupied_count_save;
682 score = score_save + 100000000 + 10000 * TET_WIDTH + 1000;
687 score += 1000 * height;
692 float CheckMetrics(float piece, float orgx, float orgy, float rot);
693 void ClearPiece(float piece, float orgx, float orgy, float rot);
694 void CementPiece(float piece, float orgx, float orgy, float rot);
695 float bastet_profile_evaluate_time;
696 float bastet_profile_checkmetrics_time;
697 float BastetSearch(float buf, float pc, float x, float y, float rot, float move_bias)
698 // returns best score, or -1 if position is impossible
706 return 0; // DO NOT WANT
708 if(x < 1 || x > TET_WIDTH || y < 1 || y > TET_LINES)
709 return -1; // impossible
713 // did we already try?
714 b = x + (TET_WIDTH+2) * (y + (TET_LINES+2) * rot);
715 r = bufstr_get(buf, b);
716 if(r != "") // already tried
719 bufstr_set(buf, b, "0"); // in case we READ that, not that bad - we already got that value in another branch then anyway
723 t1 = gettime(GETTIME_HIRES);
724 if(CheckMetrics(pc, x, y, rot))
726 t2 = gettime(GETTIME_HIRES);
727 bastet_profile_checkmetrics_time += (t2 - t1);
730 s = BastetSearch(buf, pc, x-1, y, rot, move_bias - 1); if(s > sm) sm = s;
731 s = BastetSearch(buf, pc, x+1, y, rot, move_bias - 1); if(s > sm) sm = s;
732 s = BastetSearch(buf, pc, x, y, rot+1, move_bias - 1); if(s > sm) sm = s;
733 s = BastetSearch(buf, pc, x, y, rot-1, move_bias - 1); if(s > sm) sm = s;
735 s = BastetSearch(buf, pc, x, y+1, rot, move_bias + 2); if(s > sm) sm = s;
738 //print(sprintf("MAY CEMENT AT: %d %d %d\n", x, y, rot));
739 // moving down did not work - that means we can fixate the block here
740 t1 = gettime(GETTIME_HIRES);
742 CementPiece(pc, x, y, rot);
743 s = BastetEvaluate();
744 ClearPiece(pc, x, y, rot);
746 t2 = gettime(GETTIME_HIRES);
747 bastet_profile_evaluate_time += (t2 - t1);
754 t2 = gettime(GETTIME_HIRES);
755 bastet_profile_checkmetrics_time += (t2 - t1);
756 sm = -1; // impassible
759 bufstr_set(buf, b, ftos(sm));
764 float bastet_piece[7];
765 float bastet_score[7];
766 float bastet_piecetime[7];
771 bastet_profile_evaluate_time = 0;
772 bastet_profile_checkmetrics_time = 0;
773 var float t1 = gettime(GETTIME_HIRES);
775 b = buf_create(); bastet_piece[0] = 1; bastet_score[0] = BastetSearch(b, 1, TET_START_PIECE_POS_x, 1+TET_START_PIECE_POS_y, TET_START_PIECE_POS_y, TET_WIDTH) + 100 * random() + bastet_piecetime[0]; buf_del(b);
776 b = buf_create(); bastet_piece[1] = 2; bastet_score[1] = BastetSearch(b, 2, TET_START_PIECE_POS_x, 1+TET_START_PIECE_POS_y, TET_START_PIECE_POS_y, TET_WIDTH) + 100 * random() + bastet_piecetime[1]; buf_del(b);
777 b = buf_create(); bastet_piece[2] = 3; bastet_score[2] = BastetSearch(b, 3, TET_START_PIECE_POS_x, 1+TET_START_PIECE_POS_y, TET_START_PIECE_POS_y, TET_WIDTH) + 100 * random() + bastet_piecetime[2]; buf_del(b);
778 b = buf_create(); bastet_piece[3] = 4; bastet_score[3] = BastetSearch(b, 4, TET_START_PIECE_POS_x, 1+TET_START_PIECE_POS_y, TET_START_PIECE_POS_y, TET_WIDTH) + 100 * random() + bastet_piecetime[3]; buf_del(b);
779 b = buf_create(); bastet_piece[4] = 5; bastet_score[4] = BastetSearch(b, 5, TET_START_PIECE_POS_x, 1+TET_START_PIECE_POS_y, TET_START_PIECE_POS_y, TET_WIDTH) + 100 * random() + bastet_piecetime[4]; buf_del(b);
780 b = buf_create(); bastet_piece[5] = 6; bastet_score[5] = BastetSearch(b, 6, TET_START_PIECE_POS_x, 1+TET_START_PIECE_POS_y, TET_START_PIECE_POS_y, TET_WIDTH) + 100 * random() + bastet_piecetime[5]; buf_del(b);
781 b = buf_create(); bastet_piece[6] = 7; bastet_score[6] = BastetSearch(b, 7, TET_START_PIECE_POS_x, 1+TET_START_PIECE_POS_y, TET_START_PIECE_POS_y, TET_WIDTH) + 100 * random() + bastet_piecetime[6]; buf_del(b);
783 var float t2 = gettime(GETTIME_HIRES);
784 dprint(sprintf("Time taken: %.6f seconds (of this, ev = %.2f%%, cm = %.2f%%)\n", t2 - t1, 100 * bastet_profile_evaluate_time / (t2 - t1), 100 * bastet_profile_checkmetrics_time / (t2 - t1)));
790 for(i = 0; i < 7; ++i)
792 print(sprintf("piece %s value = %d\n", substring("OJLIZST", bastet_piece[i]-1, 1), bastet_score[i]));
796 for(i = 0; i < 7; ++i)
801 for(j = i + 1; j < 7; ++j)
803 if(bastet_score[j] < s)
812 bastet_score[k] = bastet_score[i];
813 bastet_piece[k] = bastet_piece[i];
830 for(i = 0; i < 7; ++i)
833 bastet_piecetime[i] = 0.2 * bastet_piecetime[i];
835 bastet_piecetime[i] = 1.8 * bastet_piecetime[i] + 1000;
843 *********************************
847 *********************************
849 .float tet_piece_bucket;
857 if(self.tet_piece_bucket > 1)
859 p = mod(self.tet_piece_bucket, 7);
860 self.tet_piece_bucket = floor(self.tet_piece_bucket / 7);
865 p = floor(random() * 7);
868 for(i = 6; i > 0; --i)
870 q = floor(random() * i);
871 for(j = 0; j <= q; ++j)
882 self.tet_piece_bucket = b;
887 void TetAddScore(float n)
889 self.tet_score = self.tet_score + n * Tetris_Level();
890 if (self.tet_score > tet_high_score)
891 tet_high_score = self.tet_score;
893 float CheckMetrics(float piece, float orgx, float orgy, float rot) /*FIXDECL*/
895 // check to see if the piece, if moved to the locations will overlap
898 // why did I start counting from 1, damnit
902 PieceMinsMaxs(rot, piece);
903 for (y = tet_piecemins_y; y <= tet_piecemaxs_y; y = y + 1)
905 l = GetLine(y + orgy);
906 for (x = tet_piecemins_x; x <= tet_piecemaxs_x; x = x + 1)
908 if (PieceMetric(x, y, rot, piece))
910 if (GetXBlock(x + orgx, l))
911 return FALSE; // uhoh, gonna hit something.
912 if (x+orgx<1 || x+orgx > TET_WIDTH || y+orgy<1 || y+orgy> TET_LINES)
913 return FALSE; // ouside the level
920 void ClearPiece(float piece, float orgx, float orgy, float rot) /*FIXDECL*/
923 // why did I start counting from 1, damnit
927 PieceMinsMaxs(rot, piece);
928 for (y = tet_piecemins_y; y <= tet_piecemaxs_y; y = y + 1)
930 for (x = tet_piecemins_x; x <= tet_piecemaxs_x; x = x + 1)
932 if (PieceMetric(x, y, rot, piece))
934 SetSquare(x + orgx, y + orgy, 0);
939 void CementPiece(float piece, float orgx, float orgy, float rot) /*FIXDECL*/
943 // why did I start counting from 1, damnit
947 pcolor = mod(piece, 3) + 1;
949 PieceMinsMaxs(rot, piece);
950 for (y = tet_piecemins_y; y <= tet_piecemaxs_y; y = y + 1)
952 for (x = tet_piecemins_x; x <= tet_piecemaxs_x; x = x + 1)
954 if (PieceMetric(x, y, rot, piece))
956 SetSquare(x + orgx, y + orgy, pcolor);
962 float LINE_LOW = 349525;
963 float LINE_HIGH = 699050; // above number times 2
965 void AddLines(float n)
970 FOR_EACH_REALCLIENT(head) if(head != self) if(head.tetris_on) if(head.tet_vs_id == self.tet_vs_id)
971 head.tet_vs_addlines += n;
974 void CompletedLines()
976 float y, cleared, ln, added, pos, i;
983 if (((ln & LINE_LOW) | ((ln & LINE_HIGH)/2)) == LINE_LOW)
984 cleared = cleared + 1;
987 ln = GetLine(y - cleared);
993 else if(cleared >= 1)
994 AddLines(cleared - 1);
996 self.tet_lines = self.tet_lines + cleared;
997 TetAddScore(cleared * cleared * 10);
999 added = self.tet_vs_addlines;
1000 self.tet_vs_addlines = 0;
1004 for(y = 1; y <= TET_LINES - added; ++y)
1006 SetLine(y, GetLine(y + added));
1008 for(y = max(1, TET_LINES - added + 1); y <= TET_LINES; ++y)
1010 pos = floor(random() * TET_WIDTH);
1012 for(i = 1; i <= TET_WIDTH; ++i)
1014 ln = SetXBlock(i, ln, floor(random() * 3 + 1));
1019 self.tet_highest_line = 0;
1020 for(y = 1; y <= TET_LINES; ++y)
1023 self.tet_highest_line = TET_LINES + 1 - y;
1029 else if(cleared >= 4)
1037 void HandleGame(float keyss)
1040 // first off, we need to see if we need a new piece
1049 if (self.piece_type == 0)
1051 self.piece_pos = TET_START_PIECE_POS; // that's about middle top, we count from 1 ARGH
1053 if(autocvar_g_bastet)
1055 self.piece_type = BastetPiece();
1056 self.next_piece = bastet_piece[6];
1060 if (self.next_piece)
1061 self.piece_type = self.next_piece;
1063 self.piece_type = RandomPiece();
1064 self.next_piece = RandomPiece();
1066 keyss = 0; // no movement first frame
1067 self.tet_autodown = time + 0.2;
1071 ClearPiece(self.piece_type, self.piece_pos_x, self.piece_pos_y, self.piece_pos_z);
1073 // next we need to check the piece metrics against what's on the level
1074 // based on the key order
1076 old_pos = check_pos = self.piece_pos;
1080 if (keyss & TETKEY_RIGHT)
1082 check_pos_x = check_pos_x + 1;
1085 else if (keyss & TETKEY_LEFT)
1087 check_pos_x = check_pos_x - 1;
1090 else if (keyss & TETKEY_ROTRIGHT)
1092 check_pos_z = check_pos_z + 1;
1093 piece_data = PieceShape(self.piece_type);
1097 else if (keyss & TETKEY_ROTLEFT)
1099 check_pos_z = check_pos_z - 1;
1100 piece_data = PieceShape(self.piece_type);
1105 if (check_pos_z > 3)
1107 else if (check_pos_z < 0)
1111 if (CheckMetrics(self.piece_type, check_pos_x, check_pos_y, check_pos_z))
1112 self.piece_pos = check_pos;
1116 self.tet_gameovertime = time + 5;
1121 for(i = 1; i <= nudge; ++i)
1123 if(CheckMetrics(self.piece_type, check_pos_x + i, check_pos_y, check_pos_z))
1124 self.piece_pos = check_pos + '1 0 0' * i;
1125 else if(CheckMetrics(self.piece_type, check_pos_x - i, check_pos_y, check_pos_z))
1126 self.piece_pos = check_pos - '1 0 0' * i;
1132 check_pos = self.piece_pos;
1133 if(keyss & TETKEY_DROP)
1135 // drop to bottom, but do NOT cement it yet
1136 // this allows sliding it
1138 while(CheckMetrics(self.piece_type, check_pos_x, check_pos_y + 1, check_pos_z))
1140 self.tet_autodown = time + 2 / (1 + Tetris_Level());
1142 else if (keyss & TETKEY_DOWN)
1144 check_pos_y = check_pos_y + 1;
1145 self.tet_autodown = time + 2 / (1 + Tetris_Level());
1147 else if (self.tet_autodown < time)
1149 check_pos_y = check_pos_y + 1;
1150 self.tet_autodown = time + 2 / (1 + Tetris_Level());
1152 if (CheckMetrics(self.piece_type, check_pos_x, check_pos_y, check_pos_z))
1154 if(old_pos != check_pos)
1155 self.tet_drawtime = 0;
1156 self.piece_pos = check_pos;
1160 CementPiece(self.piece_type, self.piece_pos_x, self.piece_pos_y, self.piece_pos_z);
1163 self.piece_type = 0;
1164 self.tet_drawtime = 0;
1167 CementPiece(self.piece_type, self.piece_pos_x, self.piece_pos_y, self.piece_pos_z);
1171 *********************************
1173 Important Linking Into Quake stuff
1175 *********************************
1179 void TetrisImpulse()
1181 if(self.tetris_on == 0 || self.tetris_on == 2) // from "off" or "game over"
1185 if(time < tet_vs_current_timeout)
1188 self.tet_vs_id = tet_vs_current_id;
1192 // start new VS game
1193 ++tet_vs_current_id;
1194 tet_vs_current_timeout = time + 15;
1195 self.tet_vs_id = tet_vs_current_id;
1196 bprint("^1TET^7R^1IS: ", self.netname, "^1 started a new game. Do ^7impulse 100^1 to join.\n");
1198 self.tet_highest_line = 0;
1200 self.tet_org = self.origin;
1201 self.movetype = MOVETYPE_NOCLIP;
1203 else if(self.tetris_on == 1) // from "on"
1211 float TetrisPreFrame()
1213 if (!self.tetris_on)
1216 self.tet_org = self.origin;
1217 if (self.tet_drawtime > time)
1220 if(self.tetris_on == 3)
1221 self.tet_drawtime = ceil(time - tet_vs_current_timeout + 0.1) + tet_vs_current_timeout;
1223 self.tet_drawtime = time + 0.5;
1226 float frik_anglemoda(float v)
1228 return v - floor(v/360) * 360;
1230 float angcompa(float y1, float y2)
1232 y1 = frik_anglemoda(y1);
1233 y2 = frik_anglemoda(y2);
1238 answer = answer - 360;
1239 else if (answer < -180)
1240 answer = answer + 360;
1244 .float tetkey_down, tetkey_rotright, tetkey_left, tetkey_right, tetkey_rotleft, tetkey_drop;
1246 float TetrisKeyRepeat(.float fld, float f)
1250 if(self.fld == 0) // initial key press
1252 self.fld = time + 0.3;
1255 else if(time > self.fld)
1257 self.fld = time + 0.1;
1262 // repeating too fast
1273 float TetrisPostFrame()
1279 if (!self.tetris_on)
1282 if(self.tetris_on == 2 && time > self.tet_gameovertime)
1288 if(self.tetris_on == 3 && time > tet_vs_current_timeout)
1290 self.tetris_on = 1; // start VS game
1291 self.tet_drawtime = 0;
1294 setorigin(self, self.tet_org);
1295 self.movetype = MOVETYPE_NONE;
1297 if(self.tetris_on == 1)
1299 if(TetrisKeyRepeat(tetkey_down, self.movement_x < 0))
1300 keysa |= TETKEY_DOWN;
1302 if(TetrisKeyRepeat(tetkey_rotright, self.movement_x > 0))
1303 keysa |= TETKEY_ROTRIGHT;
1305 if(TetrisKeyRepeat(tetkey_left, self.movement_y < 0))
1306 keysa |= TETKEY_LEFT;
1308 if(TetrisKeyRepeat(tetkey_right, self.movement_y > 0))
1309 keysa |= TETKEY_RIGHT;
1311 if(TetrisKeyRepeat(tetkey_rotleft, self.BUTTON_CROUCH))
1312 keysa |= TETKEY_ROTLEFT;
1314 if(TetrisKeyRepeat(tetkey_drop, self.BUTTON_JUMP))
1315 keysa |= TETKEY_DROP;