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;
34 string TET_EMPTY_LINE = "0000000000"; // must match TET_WIDTH
36 float TET_BORDER = 139;
37 float TET_BLOCK = 133;
38 float TET_SPACE = 160; // blankness
43 float TETKEY_DOWN = 2;
44 float TETKEY_LEFT = 4;
45 float TETKEY_RIGHT = 8;
46 float TETKEY_ROTLEFT = 16;
47 float TETKEY_ROTRIGHT = 32;
48 float TETKEY_DROP = 64;
49 string TET_PADDING_RIGHT = "\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0"; // get away from crosshair
55 float SVC_CENTERPRINTa = 26;
59 return ((floor((self.tet_lines / 10)) + 1));
62 void tetsnd(string snd)
64 play2(self, strcat("sounds/tetris/", snd));
68 *********************************
72 *********************************
74 void SetLine(float ln, string vl)
76 if(ln < 1 || ln > TET_LINES)
78 bufstr_set(tet_line_buf, ln + TET_LINES * num_for_edict(self), vl);
81 string GetLine(float ln)
83 if(ln < 1 || ln > TET_LINES)
85 if(ln < 1 || ln > TET_LINES)
86 return TET_EMPTY_LINE;
87 return bufstr_get(tet_line_buf, ln + TET_LINES * num_for_edict(self));
90 float GetXBlock(float x, string dat)
92 if(x < 1 || x > TET_WIDTH)
94 return stof(substring(dat, x-1, 1));
97 string SetXBlock(float x, string dat, float new)
100 substring(dat, 0, x-1),
102 substring(dat, x, -1)
107 float GetSquare(float x, float y)
109 return GetXBlock(x, GetLine(y));
112 void SetSquare(float x, float y, float val)
116 dat = SetXBlock(x, dat, val);
120 float PieceColor(float pc)
127 return 7; // L // we don't have orange, let's use white instead!
139 vector PieceShape(float pc)
142 return '20 20 0'; // O
144 return '1 21 0'; // J
146 return '16 21 0'; // L
148 return '0 85 0'; // I
150 return '5 20 0'; // Z
152 return '20 5 0'; // S
154 return '4 21 0'; // T
158 vector PieceSize(float pc)
177 vector PieceCenter(float pc)
180 return '2.5 1.5 0'; // O
186 return '2.5 2.5 0'; // I
197 // do x 1..4 and y 1..4 in case of rotation
198 float PieceMetric(float x, float y, float rot, float pc)
203 // return bits of a piece
204 ce = PieceCenter(pc);
205 if (rot == 1) // 90 degrees
207 // x+cx, y+cy -> -y+cx, x+cy
208 // X, Y -> -Y+cy+cx, X-cx+cy
213 x = -t + ce_x + ce_y;
215 else if (rot == 2)//180
220 else if (rot == 3) // 270
222 // x+cx, y+cy -> y+cx, -x+cy
223 // X, Y -> Y-cy+cx, -X+cx+cy
227 y = -x + ce_y + ce_x;
230 if (x < 1 || y < 1 || x > 4 || y > 2)
234 return !!(ce_x & pow(4, x-1)); // first row
236 return !!(ce_y & pow(4, x-1)); // second row
238 return 0; // illegal parms
240 vector tet_piecemins;
241 vector tet_piecemaxs;
242 void PieceMinsMaxs(float rot, float pc)
249 ce = PieceCenter(pc);
254 tet_piecemins_x = floor(3.0 - sz_x * 0.5);
255 tet_piecemaxs_x = floor(2.0 + sz_x * 0.5);
258 // special case for "I"
259 tet_piecemins_y = tet_piecemaxs_y = 2;
264 tet_piecemaxs_y = sz_y;
266 //print(sprintf("ce%v sz%v mi%v ma%v\n", ce, sz, tet_piecemins, tet_piecemaxs));
267 if (rot == 1) // 90 degrees
270 tet_piecemins_y = -tet_piecemins_x + ce_y + ce_x;
271 tet_piecemins_x = t - ce_y + ce_x;
273 tet_piecemaxs_y = -tet_piecemaxs_x + ce_y + ce_x;
274 tet_piecemaxs_x = t - ce_y + ce_x;
275 // swap mins_y, maxs_y
277 tet_piecemins_y = tet_piecemaxs_y;
281 else if (rot == 2)//180
284 tet_piecemins = 2 * ce - tet_piecemaxs;
285 tet_piecemaxs = 2 * ce - v;
287 else if (rot == 3) // 270
290 tet_piecemins_y = tet_piecemins_x - ce_x + ce_y;
291 tet_piecemins_x = -t + ce_x + ce_y;
293 tet_piecemaxs_y = tet_piecemaxs_x - ce_x + ce_y;
294 tet_piecemaxs_x = -t + ce_x + ce_y;
295 // swap mins_x, maxs_x
297 tet_piecemins_x = tet_piecemaxs_x;
302 print(vtos(tet_piecemins), "-");
303 print(vtos(tet_piecemaxs), "\n");
304 if(tet_piecemins_x > tet_piecemaxs_x)
305 error("inconsistent mins/maxs");
306 if(tet_piecemins_y > tet_piecemaxs_y)
307 error("inconsistent mins/maxs");
309 vector realmins, realmaxs;
312 for(i = 1; i <= 4; ++i)
313 for(j = 1; j <= 4; ++j)
314 if(PieceMetric(i, j, rot, pc))
316 realmins_x = min(realmins_x, i);
317 realmins_y = min(realmins_y, j);
318 realmaxs_x = max(realmaxs_x, i);
319 realmaxs_y = max(realmaxs_y, j);
321 if(realmins != tet_piecemins || realmaxs != tet_piecemaxs)
322 error(sprintf("incorrect mins/maxs: %v %v in %d rot %d mins %v maxs %v\n", realmins, realmaxs, rot, pc, tet_piecemins, tet_piecemaxs));
326 *********************************
330 *********************************
334 /* some prydon gate functions to make life easier....
336 somewhat modified because we don't need all the fanciness Prydon Gate is capable of
340 void WriteTetrisString(string s)
342 WriteUnterminatedString(MSG_ONE, strconv(0, 2, 2, s));
345 float pnum(float num, float dig)
350 WriteChar(MSG_ONE, 173);
354 num = num - (f * 10);
356 dig = pnum(f, dig+1);
360 for (i = 0; i < (5 - dig); i = i + 1)
361 WriteChar(MSG_ONE, TET_SPACE);
363 WriteChar(MSG_ONE, 176 + num);
367 void DrawLine(float ln)
370 WriteChar(MSG_ONE, TET_BORDER);
372 for (x = 1; x <= TET_WIDTH; x = x + 1)
374 d = GetSquare(x, ln + TET_LINES - TET_DISPLAY_LINES);
377 WriteChar(MSG_ONE, '^');
378 WriteChar(MSG_ONE, d + '0');
379 WriteChar(MSG_ONE, TET_BLOCK);
382 WriteChar(MSG_ONE, TET_SPACE);
384 WriteChar(MSG_ONE, '^');
385 WriteChar(MSG_ONE, '7');
386 WriteChar(MSG_ONE, TET_BORDER);
389 void DrawPiece(float pc, float ln)
391 float x, d, piece_ln, pcolor;
393 pcolor = PieceColor(pc);
394 WriteChar(MSG_ONE, TET_SPACE); // pad to 6
396 piece_dat = PieceShape(pc);
398 piece_ln = piece_dat_x;
400 piece_ln = piece_dat_y;
401 for (x = 1; x <= 4; x = x + 1)
403 if (piece_ln & pow(4, x-1))
405 WriteChar(MSG_ONE, '^');
406 WriteChar(MSG_ONE, pcolor + '0');
407 WriteChar(MSG_ONE, TET_BLOCK);
410 WriteChar(MSG_ONE, TET_SPACE);
412 WriteChar(MSG_ONE, TET_SPACE); // pad to 6
419 WriteChar(MSG_ONE, SVC_CENTERPRINTa);
420 if(autocvar_g_bastet)
422 WriteTetrisString("NEVER GONNA GIVE YOU");
423 WriteChar(MSG_ONE, 10);
426 for (i = 1; i <= (TET_WIDTH + 2); i = i + 1)
427 WriteChar(MSG_ONE, TET_BORDER);
428 WriteTetrisString(" ");
429 WriteUnterminatedString(MSG_ONE, TET_PADDING_RIGHT);
430 WriteChar(MSG_ONE, 10);
431 for (i = 1; i <= TET_DISPLAY_LINES; i = i + 1)
433 if(self.tetris_on == 2)
434 WriteTetrisString(" GAME OVER ");
435 else if(self.tetris_on == 3)
436 WriteTetrisString("PLEASE WAIT");
440 WriteTetrisString(autocvar_g_bastet ? " THAT " : " NEXT ");
442 DrawPiece(self.next_piece, 1);
444 DrawPiece(self.next_piece, 2);
446 WriteTetrisString(" LINES");
448 pnum(self.tet_lines, 0);
450 WriteTetrisString(" SCORE");
452 pnum(self.tet_score, 0);
454 WriteTetrisString(" HIGH ");
456 WriteTetrisString(" SCORE");
458 pnum(tet_high_score, 0);
460 WriteTetrisString(" LEVEL");
462 pnum(Tetris_Level(), 0);
464 WriteTetrisString(" ");
465 WriteUnterminatedString(MSG_ONE, TET_PADDING_RIGHT);
466 WriteChar(MSG_ONE, 10);
470 for (i = 1; i <= (TET_WIDTH + 2); i = i + 1)
471 WriteChar(MSG_ONE, TET_BORDER);
472 WriteTetrisString(" ");
473 WriteUnterminatedString(MSG_ONE, TET_PADDING_RIGHT);
474 WriteChar(MSG_ONE, 10);
479 WriteChar(MSG_ONE, 10);
480 WriteChar(MSG_ONE, 10);
481 if(self.tetris_on == 3)
483 WriteUnterminatedString(MSG_ONE, strcat("WAITING FOR OTHERS (", ftos(ceil(tet_vs_current_timeout - time)), " SEC)\n"));
486 WriteChar(MSG_ONE, 10);
487 FOR_EACH_REALCLIENT(head) if(head.tetris_on) if(head.tet_vs_id == self.tet_vs_id)
490 WriteUnterminatedString(MSG_ONE, ">");
492 WriteUnterminatedString(MSG_ONE, " ");
493 if(head.tetris_on == 2)
494 WriteUnterminatedString(MSG_ONE, " X_X");
496 pnum(head.tet_highest_line, 0);
497 WriteUnterminatedString(MSG_ONE, " ");
498 WriteUnterminatedString(MSG_ONE, head.netname);
499 WriteChar(MSG_ONE, 10);
503 WriteChar(MSG_ONE, 0);
506 *********************************
510 *********************************
519 tet_line_buf = buf_create();
521 for (i=1; i<=TET_LINES; i = i + 1)
522 SetLine(i, TET_EMPTY_LINE);
523 self.piece_pos = '0 0 0';
525 self.next_piece = self.tet_lines = self.tet_score = 0;
526 self.tet_piece_bucket = 0;
531 centerprint(self, " ");
535 self.movetype = MOVETYPE_WALK;
542 for(r = 1; r <= TET_LINES; ++r)
546 for(c = 1; c <= TET_WIDTH; ++c)
548 print(ftos(GetXBlock(c, l)));
554 float BastetEvaluate()
559 float score, score_save;
560 string occupied, occupied_save;
561 float occupied_count, occupied_count_save;
566 // adds a bonus for each free dot above the occupied blocks profile
567 occupied = TET_EMPTY_LINE;
568 occupied_count = TET_WIDTH;
571 for(i = 1; i <= TET_LINES; ++i)
574 if(l == TET_EMPTY_LINE)
580 occupied_save = occupied;
581 occupied_count_save = occupied_count;
583 for(j = 1; j <= TET_WIDTH; ++j)
587 if(!GetXBlock(j, occupied))
589 occupied = SetXBlock(j, occupied, 1);
595 score += 10000 * occupied_count;
599 occupied = occupied_save;
600 occupied_count = occupied_count_save;
601 score = score_save + 100000000 + 10000 * TET_WIDTH + 1000;
606 score += 1000 * height;
611 float CheckMetrics(float piece, float orgx, float orgy, float rot);
612 void ClearPiece(float piece, float orgx, float orgy, float rot);
613 void CementPiece(float piece, float orgx, float orgy, float rot);
614 float bastet_profile_evaluate_time;
615 float bastet_profile_checkmetrics_time;
616 float BastetSearch(float buf, float pc, float x, float y, float rot, float move_bias)
617 // returns best score, or -1 if position is impossible
625 return 0; // DO NOT WANT
627 if(x < 1 || x > TET_WIDTH || y < 1 || y > TET_LINES)
628 return -1; // impossible
632 // did we already try?
633 b = x + (TET_WIDTH+2) * (y + (TET_LINES+2) * rot);
634 r = bufstr_get(buf, b);
635 if(r != "") // already tried
638 bufstr_set(buf, b, "0"); // in case we READ that, not that bad - we already got that value in another branch then anyway
642 t1 = gettime(GETTIME_HIRES);
643 if(CheckMetrics(pc, x, y, rot))
645 t2 = gettime(GETTIME_HIRES);
646 bastet_profile_checkmetrics_time += (t2 - t1);
649 s = BastetSearch(buf, pc, x-1, y, rot, move_bias - 1); if(s > sm) sm = s;
650 s = BastetSearch(buf, pc, x+1, y, rot, move_bias - 1); if(s > sm) sm = s;
651 s = BastetSearch(buf, pc, x, y, rot+1, move_bias - 1); if(s > sm) sm = s;
652 s = BastetSearch(buf, pc, x, y, rot-1, move_bias - 1); if(s > sm) sm = s;
654 s = BastetSearch(buf, pc, x, y+1, rot, move_bias + 2); if(s > sm) sm = s;
657 //print(sprintf("MAY CEMENT AT: %d %d %d\n", x, y, rot));
658 // moving down did not work - that means we can fixate the block here
659 t1 = gettime(GETTIME_HIRES);
661 CementPiece(pc, x, y, rot);
662 s = BastetEvaluate();
663 ClearPiece(pc, x, y, rot);
665 t2 = gettime(GETTIME_HIRES);
666 bastet_profile_evaluate_time += (t2 - t1);
673 t2 = gettime(GETTIME_HIRES);
674 bastet_profile_checkmetrics_time += (t2 - t1);
675 sm = -1; // impassible
678 bufstr_set(buf, b, ftos(sm));
683 float bastet_piece[7];
684 float bastet_score[7];
685 float bastet_piecetime[7];
690 bastet_profile_evaluate_time = 0;
691 bastet_profile_checkmetrics_time = 0;
692 var float t1 = gettime(GETTIME_HIRES);
694 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);
695 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);
696 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);
697 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);
698 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);
699 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);
700 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);
702 var float t2 = gettime(GETTIME_HIRES);
703 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)));
709 for(i = 0; i < 7; ++i)
711 print(sprintf("piece %s value = %d\n", substring("OJLIZST", bastet_piece[i]-1, 1), bastet_score[i]));
715 for(i = 0; i < 7; ++i)
720 for(j = i + 1; j < 7; ++j)
722 if(bastet_score[j] < s)
731 bastet_score[k] = bastet_score[i];
732 bastet_piece[k] = bastet_piece[i];
749 for(i = 0; i < 7; ++i)
752 bastet_piecetime[i] = 0.2 * bastet_piecetime[i];
754 bastet_piecetime[i] = 1.8 * bastet_piecetime[i] + 1000;
762 *********************************
766 *********************************
768 .float tet_piece_bucket;
776 if(self.tet_piece_bucket > 1)
778 p = mod(self.tet_piece_bucket, 7);
779 self.tet_piece_bucket = floor(self.tet_piece_bucket / 7);
784 p = floor(random() * 7);
787 for(i = 6; i > 0; --i)
789 q = floor(random() * i);
790 for(j = 0; j <= q; ++j)
801 self.tet_piece_bucket = b;
806 void TetAddScore(float n)
808 self.tet_score = self.tet_score + n * Tetris_Level();
809 if (self.tet_score > tet_high_score)
810 tet_high_score = self.tet_score;
812 float CheckMetrics(float piece, float orgx, float orgy, float rot) /*FIXDECL*/
814 // check to see if the piece, if moved to the locations will overlap
818 // why did I start counting from 1, damnit
822 PieceMinsMaxs(rot, piece);
823 if (tet_piecemins_x+orgx<1 || tet_piecemaxs_x+orgx > TET_WIDTH || tet_piecemins_y+orgy<1 || tet_piecemaxs_y+orgy> TET_LINES)
824 return FALSE; // ouside the level
825 for (y = tet_piecemins_y; y <= tet_piecemaxs_y; y = y + 1)
827 l = GetLine(y + orgy);
828 if(l != TET_EMPTY_LINE)
829 for (x = tet_piecemins_x; x <= tet_piecemaxs_x; x = x + 1)
830 if (PieceMetric(x, y, rot, piece))
831 if (GetXBlock(x + orgx, l))
832 return FALSE; // uhoh, gonna hit something.
837 void ClearPiece(float piece, float orgx, float orgy, float rot) /*FIXDECL*/
840 // why did I start counting from 1, damnit
844 PieceMinsMaxs(rot, piece);
845 for (y = tet_piecemins_y; y <= tet_piecemaxs_y; y = y + 1)
847 for (x = tet_piecemins_x; x <= tet_piecemaxs_x; x = x + 1)
849 if (PieceMetric(x, y, rot, piece))
851 SetSquare(x + orgx, y + orgy, 0);
856 void CementPiece(float piece, float orgx, float orgy, float rot) /*FIXDECL*/
860 // why did I start counting from 1, damnit
864 pcolor = PieceColor(piece);
866 PieceMinsMaxs(rot, piece);
867 for (y = tet_piecemins_y; y <= tet_piecemaxs_y; y = y + 1)
869 for (x = tet_piecemins_x; x <= tet_piecemaxs_x; x = x + 1)
871 if (PieceMetric(x, y, rot, piece))
873 SetSquare(x + orgx, y + orgy, pcolor);
879 float LINE_LOW = 349525;
880 float LINE_HIGH = 699050; // above number times 2
882 void AddLines(float n)
887 FOR_EACH_REALCLIENT(head) if(head != self) if(head.tetris_on) if(head.tet_vs_id == self.tet_vs_id)
888 head.tet_vs_addlines += n;
891 void CompletedLines()
893 float y, cleared, added, pos, i;
901 if(strstrofs(ln, "0", 0) < 0)
902 cleared = cleared + 1;
910 ln = GetLine(y - cleared);
916 else if(cleared >= 1)
917 AddLines(cleared - 1);
919 self.tet_lines = self.tet_lines + cleared;
920 TetAddScore(cleared * cleared * 10);
922 added = self.tet_vs_addlines;
923 self.tet_vs_addlines = 0;
927 for(y = 1; y <= TET_LINES - added; ++y)
929 SetLine(y, GetLine(y + added));
931 for(y = max(1, TET_LINES - added + 1); y <= TET_LINES; ++y)
933 pos = floor(random() * TET_WIDTH);
935 for(i = 1; i <= TET_WIDTH; ++i)
937 ln = SetXBlock(i, ln, floor(random() * 7 + 1));
942 self.tet_highest_line = 0;
943 for(y = 1; y <= TET_LINES; ++y)
944 if(GetLine(y) != TET_EMPTY_LINE)
946 self.tet_highest_line = TET_LINES + 1 - y;
952 else if(cleared >= 4)
960 void HandleGame(float keyss)
963 // first off, we need to see if we need a new piece
972 if (self.piece_type == 0)
974 self.piece_pos = TET_START_PIECE_POS; // that's about middle top, we count from 1 ARGH
976 if(autocvar_g_bastet)
978 self.piece_type = BastetPiece();
979 self.next_piece = bastet_piece[6];
984 self.piece_type = self.next_piece;
986 self.piece_type = RandomPiece();
987 self.next_piece = RandomPiece();
989 keyss = 0; // no movement first frame
990 self.tet_autodown = time + 0.2;
994 ClearPiece(self.piece_type, self.piece_pos_x, self.piece_pos_y, self.piece_pos_z);
996 // next we need to check the piece metrics against what's on the level
997 // based on the key order
999 old_pos = check_pos = self.piece_pos;
1003 if (keyss & TETKEY_RIGHT)
1005 check_pos_x = check_pos_x + 1;
1008 else if (keyss & TETKEY_LEFT)
1010 check_pos_x = check_pos_x - 1;
1013 else if (keyss & TETKEY_ROTRIGHT)
1015 check_pos_z = check_pos_z + 1;
1016 piece_data = PieceShape(self.piece_type);
1020 else if (keyss & TETKEY_ROTLEFT)
1022 check_pos_z = check_pos_z - 1;
1023 piece_data = PieceShape(self.piece_type);
1028 if (check_pos_z > 3)
1030 else if (check_pos_z < 0)
1034 if (CheckMetrics(self.piece_type, check_pos_x, check_pos_y, check_pos_z))
1035 self.piece_pos = check_pos;
1039 self.tet_gameovertime = time + 5;
1044 for(i = 1; i <= nudge; ++i)
1046 if(CheckMetrics(self.piece_type, check_pos_x + i, check_pos_y, check_pos_z))
1047 self.piece_pos = check_pos + '1 0 0' * i;
1048 else if(CheckMetrics(self.piece_type, check_pos_x - i, check_pos_y, check_pos_z))
1049 self.piece_pos = check_pos - '1 0 0' * i;
1055 check_pos = self.piece_pos;
1056 if(keyss & TETKEY_DROP)
1058 // drop to bottom, but do NOT cement it yet
1059 // this allows sliding it
1061 while(CheckMetrics(self.piece_type, check_pos_x, check_pos_y + 1, check_pos_z))
1063 self.tet_autodown = time + 2 / (1 + Tetris_Level());
1065 else if (keyss & TETKEY_DOWN)
1067 check_pos_y = check_pos_y + 1;
1068 self.tet_autodown = time + 2 / (1 + Tetris_Level());
1070 else if (self.tet_autodown < time)
1072 check_pos_y = check_pos_y + 1;
1073 self.tet_autodown = time + 2 / (1 + Tetris_Level());
1075 if (CheckMetrics(self.piece_type, check_pos_x, check_pos_y, check_pos_z))
1077 if(old_pos != check_pos)
1078 self.tet_drawtime = 0;
1079 self.piece_pos = check_pos;
1083 CementPiece(self.piece_type, self.piece_pos_x, self.piece_pos_y, self.piece_pos_z);
1086 self.piece_type = 0;
1087 self.tet_drawtime = 0;
1090 CementPiece(self.piece_type, self.piece_pos_x, self.piece_pos_y, self.piece_pos_z);
1094 *********************************
1096 Important Linking Into Quake stuff
1098 *********************************
1102 void TetrisImpulse()
1104 if(self.tetris_on == 0 || self.tetris_on == 2) // from "off" or "game over"
1108 if(time < tet_vs_current_timeout)
1111 self.tet_vs_id = tet_vs_current_id;
1115 // start new VS game
1116 ++tet_vs_current_id;
1117 tet_vs_current_timeout = time + 15;
1118 self.tet_vs_id = tet_vs_current_id;
1119 bprint("^1TET^7R^1IS: ", self.netname, "^1 started a new game. Do ^7impulse 100^1 to join.\n");
1121 self.tet_highest_line = 0;
1123 self.tet_org = self.origin;
1124 self.movetype = MOVETYPE_NOCLIP;
1126 else if(self.tetris_on == 1) // from "on"
1134 float TetrisPreFrame()
1136 if (!self.tetris_on)
1139 self.tet_org = self.origin;
1140 if (self.tet_drawtime > time)
1143 if(self.tetris_on == 3)
1144 self.tet_drawtime = ceil(time - tet_vs_current_timeout + 0.1) + tet_vs_current_timeout;
1146 self.tet_drawtime = time + 0.5;
1149 float frik_anglemoda(float v)
1151 return v - floor(v/360) * 360;
1153 float angcompa(float y1, float y2)
1155 y1 = frik_anglemoda(y1);
1156 y2 = frik_anglemoda(y2);
1161 answer = answer - 360;
1162 else if (answer < -180)
1163 answer = answer + 360;
1167 .float tetkey_down, tetkey_rotright, tetkey_left, tetkey_right, tetkey_rotleft, tetkey_drop;
1169 float TetrisKeyRepeat(.float fld, float f)
1173 if(self.fld == 0) // initial key press
1175 self.fld = time + 0.3;
1178 else if(time > self.fld)
1180 self.fld = time + 0.1;
1185 // repeating too fast
1196 float TetrisPostFrame()
1202 if (!self.tetris_on)
1205 if(self.tetris_on == 2 && time > self.tet_gameovertime)
1211 if(self.tetris_on == 3 && time > tet_vs_current_timeout)
1213 self.tetris_on = 1; // start VS game
1214 self.tet_drawtime = 0;
1217 setorigin(self, self.tet_org);
1218 self.movetype = MOVETYPE_NONE;
1220 if(self.tetris_on == 1)
1222 if(TetrisKeyRepeat(tetkey_down, self.movement_x < 0))
1223 keysa |= TETKEY_DOWN;
1225 if(TetrisKeyRepeat(tetkey_rotright, self.movement_x > 0))
1226 keysa |= TETKEY_ROTRIGHT;
1228 if(TetrisKeyRepeat(tetkey_left, self.movement_y < 0))
1229 keysa |= TETKEY_LEFT;
1231 if(TetrisKeyRepeat(tetkey_right, self.movement_y > 0))
1232 keysa |= TETKEY_RIGHT;
1234 if(TetrisKeyRepeat(tetkey_rotleft, self.BUTTON_CROUCH))
1235 keysa |= TETKEY_ROTLEFT;
1237 if(TetrisKeyRepeat(tetkey_drop, self.BUTTON_JUMP))
1238 keysa |= TETKEY_DROP;