]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/g_tetris.qc
adjust tetris levels to match gameboy tetris better
[divverent/nexuiz.git] / data / qcsrc / server / g_tetris.qc
1 /*
2
3 Installation:
4
5 compile with -DTETRIS
6
7 */
8
9 #ifdef TETRIS
10
11 .vector tet_org;
12
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;
18 .vector piece_pos;
19 .float piece_type, next_piece, tet_score, tet_lines;
20
21 // tetris_on states:
22 //   1 = running
23 //   2 = game over
24 //   3 = waiting for VS players
25
26 var float tet_high_score = 0;
27
28 float TET_LINES = 20;
29 float TET_WIDTH = 10;
30 //character values
31 float TET_BORDER = 139;
32 float TET_BLOCKS = 132; // +1 = first color, +2, +3;
33 float TET_SPACE = 160; // blankness
34
35
36
37 float TETKEY_UP = 1;
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
45
46 float PIECES = 7;
47
48 .float line1, line2, line3, line4, line5, line6, line7,
49 line8, line9, line10, line11, line12, line13, line14, line15,
50 line16, line17, line18, line19, line20;
51
52
53 float   SVC_CENTERPRINTa                = 26;
54
55 float Tetris_Level()
56
57         return ((floor((self.tet_lines / 10)) + 1)); 
58 }; 
59
60 void tetsnd(string snd)
61 {
62         play2(self, strcat("sounds/tetris/", snd));
63 }
64
65 /*
66 *********************************
67
68 Library Functions
69
70 *********************************
71 */
72 void SetLine(float ln, float vl)
73 {
74         if (ln == 1)
75                 self.line1 = vl;
76         else if (ln == 2)
77                 self.line2 = vl;
78         else if (ln == 3)
79                 self.line3 = vl;
80         else if (ln == 4)
81                 self.line4 = vl;
82         else if (ln == 5)
83                 self.line5 = vl;
84         else if (ln == 6)
85                 self.line6 = vl;
86         else if (ln == 7)
87                 self.line7 = vl;
88         else if (ln == 8)
89                 self.line8 = vl;
90         else if (ln == 9)
91                 self.line9 = vl;
92         else if (ln == 10)
93                 self.line10 = vl;
94         else if (ln == 11)
95                 self.line11 = vl;
96         else if (ln == 12)
97                 self.line12 = vl;
98         else if (ln == 13)
99                 self.line13 = vl;
100         else if (ln == 14)
101                 self.line14 = vl;
102         else if (ln == 15)
103                 self.line15 = vl;
104         else if (ln == 16)
105                 self.line16 = vl;
106         else if (ln == 17)
107                 self.line17 = vl;
108         else if (ln == 18)
109                 self.line18 = vl;
110         else if (ln == 19)
111                 self.line19 = vl;
112         else if (ln == 20)
113                 self.line20 = vl;
114 };
115
116 float GetLine(float ln)
117 {
118         if (ln == 1)
119                 return self.line1;
120         else if (ln == 2)
121                 return self.line2;
122         else if (ln == 3)
123                 return self.line3;
124         else if (ln == 4)
125                 return self.line4;
126         else if (ln == 5)
127                 return self.line5;
128         else if (ln == 6)
129                 return self.line6;
130         else if (ln == 7)
131                 return self.line7;
132         else if (ln == 8)
133                 return self.line8;
134         else if (ln == 9)
135                 return self.line9;
136         else if (ln == 10)
137                 return self.line10;
138         else if (ln == 11)
139                 return self.line11;
140         else if (ln == 12)
141                 return self.line12;
142         else if (ln == 13)
143                 return self.line13;
144         else if (ln == 14)
145                 return self.line14;
146         else if (ln == 15)
147                 return self.line15;
148         else if (ln == 16)
149                 return self.line16;
150         else if (ln == 17)
151                 return self.line17;
152         else if (ln == 18)
153                 return self.line18;
154         else if (ln == 19)
155                 return self.line19;
156         else if (ln == 20)
157                 return self.line20;
158         else
159                 return 0;
160 };
161
162 float GetXBlock(float x, float dat)
163 {
164         if (x == 1)
165                 return dat & 3;
166         else if (x == 2)
167                 return (dat & 12) / 4;
168         else if (x == 3)
169                 return (dat & 48) / 16;
170         else if (x == 4)
171                 return (dat & 192) / 64;
172         else if (x == 5)
173                 return (dat & 768) / 256;
174         else if (x == 6)
175                 return (dat & 3072) / 1024;
176         else if (x == 7)
177                 return (dat & 12288) / 4096;
178         else if (x == 8)
179                 return (dat & 49152) / 16384;
180         else if (x == 9)
181                 return (dat & 196608) / 65536;
182         else if (x == 10)
183                 return (dat & 786432) / 262144;
184         else
185                 return 0;
186 };
187
188 float SetXBlock(float x, float dat, float new)
189 {
190         if (x == 1)
191                 return (dat - (dat & 3)) | new;
192         else if (x == 2)
193                 return (dat - (dat & 12)) | (new*4);
194         else if (x == 3)
195                 return (dat - (dat & 48)) | (new*16);
196         else if (x == 4)
197                 return (dat - (dat & 192)) | (new*64);
198         else if (x == 5)
199                 return (dat - (dat & 768)) | (new*256);
200         else if (x == 6)
201                 return (dat - (dat & 3072)) | (new*1024);
202         else if (x == 7)
203                 return (dat - (dat & 12288)) | (new*4096);
204         else if (x == 8)
205                 return (dat - (dat & 49152)) | (new*16384);
206         else if (x == 9)
207                 return (dat - (dat & 196608)) | (new*65536);
208         else if (x == 10)
209                 return (dat - (dat & 786432)) | (new*262144);
210         else
211                 return dat;
212 };
213
214
215 float GetSquare(float x, float y)
216 {
217         return GetXBlock(x,  GetLine(y));
218 };
219
220 void SetSquare(float x, float y, float val)
221 {
222         float dat;
223
224         dat = GetLine(y);
225         dat  = SetXBlock(x, dat, val & 3);
226         SetLine(y, dat);
227 };
228
229
230
231 vector PieceShape(float pc)
232 {
233
234 /*
235 1 =
236  ##
237  ##
238 */
239         if (pc == 1)
240                 return '5 5 2'; // 1 * 4 + 1 * 16
241 /*
242 2 =
243
244 ####
245 */
246         else if (pc == 2)
247                 return '85 0 4';
248
249 /*
250 3 =
251
252 ###
253 #
254 */
255         else if (pc == 3)
256                 return '21 1 3';
257 /*
258 4 =
259
260 #
261 ###
262 */
263         else if (pc == 4)
264                 return '1 21 3';
265 /*
266 5 =
267 ##
268  ##
269 */
270         else if (pc == 5)
271                 return '5 20 3';
272
273 /*
274 6 =
275  ##
276 ##
277 */
278         else if (pc == 6)
279                 return '20 5 3';
280
281 /*
282 7 =
283  #
284 ###
285 */
286         else if (pc == 7)
287                 return '4 21 3';
288
289
290         else
291                 return '0 0 0';
292
293 }
294
295 // do x 1..4 and y 1..4 in case of rotation
296 float PieceMetric(float x, float y, float rot, float pc)
297 {
298         float t;
299         vector piece_dat;
300         float wid;
301
302         // return bits of a piece
303         piece_dat = PieceShape(pc);
304         wid = piece_dat_z + 1;
305         if (rot == 1) // 90 degrees
306         {
307                 t = y;
308                 y = x;
309                 x = wid - t;
310         }
311         else if (rot == 2)//180
312         {
313                 x = wid - x;
314                 y = 3 - y;
315         }
316         else if (rot == 3) // 270
317         {
318                 t = y;
319                 y = wid - x;
320                 x = t;
321         }
322         if (x < 1 || y < 1 || x > 4 || y > 2)
323                 return 0;
324         if (y == 1)
325                 return GetXBlock(x, piece_dat_x); // first row
326         else if (y == 2)
327                 return GetXBlock(x, piece_dat_y); // second row
328         else if (y == 3)
329                 return GetXBlock(x, piece_dat_z); // third row (doesn't exist)
330         else
331                 return 0; // illegal parms
332 };
333 /*
334 *********************************
335
336 Draw
337
338 *********************************
339 */
340
341
342 /* some prydon gate functions to make life easier....
343
344 somewhat modified because we don't need all the fanciness Prydon Gate is capable of
345
346 */
347
348 void WriteTetrisString(string s)
349 {
350         WriteUnterminatedString(MSG_ONE, strconv(0, 2, 2, s));
351 }
352
353 float pnum(float num, float dig)
354 {
355         local float f, i;
356         if (num < 0)
357         {
358                 WriteChar(MSG_ONE, 173);
359                 num = 0 - num;
360         }
361         f = floor(num / 10);
362         num = num - (f * 10);
363         if (f)
364                 dig = pnum(f, dig+1);
365         else
366         {
367                 // pad to 6
368                 for (i = 0; i < (5 - dig); i = i + 1)
369                         WriteChar(MSG_ONE, TET_SPACE);
370         }
371         WriteChar(MSG_ONE, 176 + num);
372         return dig;
373 };
374
375 void DrawLine(float ln)
376 {
377         float x, d;
378         WriteChar(MSG_ONE, TET_BORDER);
379
380         for (x = 1; x <= TET_WIDTH; x = x + 1)
381         {
382                 d = GetSquare(x, ln);
383                 if (d)
384                         WriteChar(MSG_ONE, TET_BLOCKS + d);
385                 else
386                         WriteChar(MSG_ONE, TET_SPACE);
387         }
388         WriteChar(MSG_ONE, TET_BORDER);
389 }
390
391 void DrawPiece(float pc, float ln)
392 {
393         float x, d, piece_ln, pcolor;
394         vector piece_dat;
395         pcolor = pc & 3;
396         if (pcolor == 0) // 4
397                 pcolor = 1;
398         WriteChar(MSG_ONE, TET_SPACE); // pad to 6
399
400         piece_dat = PieceShape(pc);
401         if (ln == 1)
402                 piece_ln = piece_dat_x;
403         else
404                 piece_ln = piece_dat_y;
405         for (x = 1; x <= 4; x = x + 1)
406         {
407                 d = GetXBlock(x, piece_ln) * pcolor;
408                 if (d)
409                         WriteChar(MSG_ONE, TET_BLOCKS + d);
410                 else
411                         WriteChar(MSG_ONE, TET_SPACE);
412         }
413         WriteChar(MSG_ONE, TET_SPACE);  // pad to 6
414 }
415 void Draw_Tetris()
416 {
417         float i;
418         entity head;
419         msg_entity = self;
420         WriteChar(MSG_ONE, SVC_CENTERPRINTa);
421         // decoration
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)
428         {
429                 if(self.tetris_on == 2)
430                         WriteTetrisString(" GAME  OVER ");
431                 else if(self.tetris_on == 3)
432                         WriteTetrisString("PLEASE  WAIT");
433                 else
434                         DrawLine(i);
435                 if (i == 1)
436                         WriteTetrisString(" NEXT ");
437                 else if (i == 3)
438                         DrawPiece(self.next_piece, 1);
439                 else if (i == 4)
440                         DrawPiece(self.next_piece, 2);
441                 else if (i == 6)
442                         WriteTetrisString(" LINES");
443                 else if (i == 7)
444                         pnum(self.tet_lines, 0);
445                 else if (i == 9)
446                         WriteTetrisString(" SCORE");
447                 else if (i == 10)
448                         pnum(self.tet_score, 0);
449                 else if (i == 12)
450                         WriteTetrisString(" HIGH ");
451                 else if (i == 13)
452                         WriteTetrisString(" SCORE");
453                 else if (i == 14)
454                         pnum(tet_high_score, 0);
455                 else if (i == 16)
456                         WriteTetrisString(" LEVEL");
457                 else if (i == 17)
458                         pnum(Tetris_Level(), 0);
459                 else
460                         WriteTetrisString("      ");
461                 WriteUnterminatedString(MSG_ONE, TET_PADDING_RIGHT);
462                 WriteChar(MSG_ONE, 10);
463         }
464         // decoration
465
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);
471
472         // VS game status
473         if(self.tet_vs_id)
474         {
475                 WriteChar(MSG_ONE, 10);
476                 WriteChar(MSG_ONE, 10);
477                 if(self.tetris_on == 3)
478                 {
479                         WriteUnterminatedString(MSG_ONE, strcat("WAITING FOR OTHERS (", ftos(ceil(tet_vs_current_timeout - time)), " SEC)\n"));
480                 }
481
482                 WriteChar(MSG_ONE, 10);
483                 FOR_EACH_REALCLIENT(head) if(head.tetris_on) if(head.tet_vs_id == self.tet_vs_id)
484                 {
485                         if(head == self)
486                                 WriteUnterminatedString(MSG_ONE, ">");
487                         else
488                                 WriteUnterminatedString(MSG_ONE, " ");
489                         if(head.tetris_on == 2)
490                                 WriteUnterminatedString(MSG_ONE, "   X_X");
491                         else
492                                 pnum(head.tet_highest_line, 0);
493                         WriteUnterminatedString(MSG_ONE, " ");
494                         WriteUnterminatedString(MSG_ONE, head.netname);
495                 }
496                 WriteChar(MSG_ONE, 10);
497         }
498
499         WriteChar(MSG_ONE, 0);
500 }
501 /*
502 *********************************
503
504 Game Functions
505
506 *********************************
507 */
508
509 // reset the game
510 void ResetTetris()
511 {
512         float i;
513
514         for (i=1; i<=TET_LINES; i = i + 1)
515                 SetLine(i, 0);
516         self.piece_pos = '0 0 0';
517         self.piece_type = 0;
518         self.next_piece = self.tet_lines = self.tet_score = 0;
519
520 };
521
522 void Tet_GameExit()
523 {
524         centerprint(self, " ");
525         self.tetris_on = 0;
526         self.tet_vs_id = 0;
527         ResetTetris();
528         self.movetype = MOVETYPE_WALK;
529 };
530
531
532
533 /*
534 *********************************
535
536 Game Mechanics
537
538 *********************************
539 */
540 float RandomPiece()
541 {
542         return floor(random() * PIECES) + 1;
543 };
544
545 void TetAddScore(float n)
546 {
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;
550 };
551 float CheckMetrics(float piece, float orgx, float orgy, float rot) /*FIXDECL*/
552 {
553         // check to see if the piece, if moved to the locations will overlap
554
555         float x, y;
556         // why did I start counting from 1, damnit
557         orgx = orgx - 1;
558         orgy = orgy - 1;
559
560         for (y = 1; y < 5; y = y + 1)
561         {
562                 for (x = 1; x < 5; x = x + 1)
563                 {
564                         if (PieceMetric(x, y, rot, piece))
565                         {
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
570                         }
571                 }
572         }
573         return TRUE;
574 }
575
576 void ClearPiece(float piece, float orgx, float orgy, float rot) /*FIXDECL*/
577 {
578
579         float x, y;
580         // why did I start counting from 1, damnit
581         orgx = orgx - 1;
582         orgy = orgy - 1;
583
584         for (y = 1; y < 5; y = y + 1)
585         {
586                 for (x = 1; x < 5; x = x + 1)
587                 {
588                         if (PieceMetric(x, y, rot, piece))
589                         {
590                                 SetSquare(x + orgx, y + orgy, 0);
591                         }
592                 }
593         }
594 }
595 void CementPiece(float piece, float orgx, float orgy, float rot) /*FIXDECL*/
596 {
597         float pcolor;
598         float x, y;
599         // why did I start counting from 1, damnit
600         orgx = orgx - 1;
601         orgy = orgy - 1;
602
603         pcolor = piece & 3;
604         if (pcolor == 0) // 4
605                 pcolor = 1;
606
607         for (y = 1; y < 5; y = y + 1)
608         {
609                 for (x = 1; x < 5; x = x + 1)
610                 {
611                         if (PieceMetric(x, y, rot, piece))
612                         {
613                                 SetSquare(x + orgx, y + orgy, pcolor);
614                         }
615                 }
616         }
617 }
618
619 float LINE_LOW = 349525;
620 float LINE_HIGH = 699050; // above number times 2
621
622 void AddLines(float n)
623 {
624         entity head;
625         if(!self.tet_vs_id)
626                 return;
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;
629 }
630
631 void CompletedLines()
632 {
633         float y, cleared, ln, added, pos, i;
634
635         cleared = 0;
636         y = TET_LINES;
637         while(y >= 1)
638         {
639                 ln = GetLine(y);
640                 if (((ln & LINE_LOW) | ((ln & LINE_HIGH)/2)) == LINE_LOW)
641                         cleared = cleared + 1;
642                 else
643                         y = y - 1;
644                 ln = GetLine(y - cleared);
645                 SetLine(y, ln);
646         }
647
648         if(cleared >= 4)
649                 AddLines(cleared);
650         else if(cleared >= 1)
651                 AddLines(cleared - 1);
652
653         self.tet_lines = self.tet_lines + cleared;
654         TetAddScore(cleared * cleared * 10);
655
656         added = self.tet_vs_addlines;
657         self.tet_vs_addlines = 0;
658
659         if(added)
660         {
661                 for(y = 1; y <= TET_LINES - added; ++y)
662                 {
663                         SetLine(y, GetLine(y + added));
664                 }
665                 for(y = max(1, TET_LINES - added + 1); y <= TET_LINES; ++y)
666                 {
667                         pos = floor(random() * TET_WIDTH);
668                         ln = 0;
669                         for(i = 1; i <= TET_WIDTH; ++i)
670                                 if(i != pos)
671                                         ln = SetXBlock(i, ln, floor(random() * 3 + 1));
672                         SetLine(y, ln);
673                 }
674         }
675
676         self.tet_highest_line = 0;
677         for(y = 1; y <= TET_LINES; ++y)
678                 if(GetLine(y) != 0)
679                 {
680                         self.tet_highest_line = TET_LINES + 1 - y;
681                         break;
682                 }
683
684         if(added)
685                 tetsnd("tetadd");
686         else if(cleared >= 4)
687                 tetsnd("tetris");
688         else if(cleared)
689                 tetsnd("tetline");
690         else
691                 tetsnd("tetland");
692 };
693
694 void HandleGame(float keyss)
695 {
696
697         // first off, we need to see if we need a new piece
698         vector piece_data;
699         vector check_pos;
700         vector old_pos;
701         float brand_new;
702         float i;
703         brand_new = 0;
704
705
706         if (self.piece_type == 0)
707         {
708                 self.piece_pos = '5 1 0'; // that's about middle top, we count from 1 ARGH
709                 if (self.next_piece)
710                         self.piece_type = self.next_piece;
711                 else
712                         self.piece_type = RandomPiece();
713                 self.next_piece =  RandomPiece();
714                 keyss = 0; // no movement first frame
715                 self.tet_autodown = time + 0.2;
716                 brand_new = 1;
717         }
718         else
719                 ClearPiece(self.piece_type, self.piece_pos_x, self.piece_pos_y, self.piece_pos_z);
720
721         // next we need to check the piece metrics against what's on the level
722         // based on the key order
723
724         old_pos = check_pos = self.piece_pos;
725
726         float nudge;
727         nudge = 0;
728         if (keyss & TETKEY_RIGHT)
729         {
730                 check_pos_x = check_pos_x + 1;
731                 tetsnd("tetmove");
732         }
733         else if (keyss & TETKEY_LEFT)
734         {
735                 check_pos_x = check_pos_x - 1;
736                 tetsnd("tetmove");
737         }
738         else if (keyss & TETKEY_ROTRIGHT)
739         {
740                 check_pos_z = check_pos_z + 1;
741                 piece_data = PieceShape(self.piece_type);
742                 nudge = piece_data_z - 2;
743                 tetsnd("tetrot");
744         }
745         else if (keyss & TETKEY_ROTLEFT)
746         {
747                 check_pos_z = check_pos_z - 1;
748                 piece_data = PieceShape(self.piece_type);
749                 nudge = piece_data_z - 2;
750                 tetsnd("tetrot");
751         }
752         // bounds check
753         if (check_pos_z > 3)
754                 check_pos_z = 0;
755         else if (check_pos_z < 0)
756                 check_pos_z = 3;
757
758         // reality check
759         if (CheckMetrics(self.piece_type, check_pos_x, check_pos_y, check_pos_z))
760                 self.piece_pos = check_pos;
761         else if (brand_new)
762         {
763                 self.tetris_on = 2;
764                 self.tet_gameovertime = time + 2;
765                 return;
766         }
767         else
768         {
769                 for(i = 1; i <= nudge; ++i)
770                 {
771                         if(CheckMetrics(self.piece_type, check_pos_x + i, check_pos_y, check_pos_z))
772                         {
773                                 self.piece_pos = check_pos + '1 0 0' * i;
774                                 break;
775                         }
776                         else if(CheckMetrics(self.piece_type, check_pos_x - i, check_pos_y, check_pos_z))
777                         {
778                                 self.piece_pos = check_pos - '1 0 0' * i;
779                                 break;
780                         }
781                 }
782         }
783         check_pos = self.piece_pos;
784         if(keyss & TETKEY_DROP)
785         {
786                 // drop to bottom, but do NOT cement it yet
787                 // this allows sliding it
788                 ++check_pos_y;
789                 while(CheckMetrics(self.piece_type, check_pos_x, check_pos_y + 1, check_pos_z))
790                         ++check_pos_y;
791                 self.tet_autodown = time + 2 / (1 + Tetris_Level());
792         }
793         else if (keyss & TETKEY_DOWN)
794         {
795                 check_pos_y = check_pos_y + 1;
796                 self.tet_autodown = time + 2 / (1 + Tetris_Level());
797         }
798         else if (self.tet_autodown < time)
799         {
800                 check_pos_y = check_pos_y + 1;
801                 self.tet_autodown = time + 2 / (1 + Tetris_Level());
802         }
803         if (CheckMetrics(self.piece_type, check_pos_x, check_pos_y, check_pos_z))
804         {
805                 if(old_pos != check_pos)
806                         self.tet_drawtime = 0;
807                 self.piece_pos = check_pos;
808         }
809         else
810         {
811                 CementPiece(self.piece_type, self.piece_pos_x, self.piece_pos_y, self.piece_pos_z);
812                 TetAddScore(1);
813                 CompletedLines();
814                 self.piece_type = 0;
815                 self.tet_drawtime = 0;
816                 return;
817         }
818         CementPiece(self.piece_type, self.piece_pos_x, self.piece_pos_y, self.piece_pos_z);
819 };
820
821 /*
822 *********************************
823
824 Important Linking Into Quake stuff
825
826 *********************************
827 */
828
829
830 void TetrisImpulse()
831 {
832         if(self.tetris_on != 1 && self.tetris_on != 3)
833         {
834                 self.tetris_on = 3;
835
836                 if(time < tet_vs_current_timeout)
837                 {
838                         // join VS game
839                         self.tet_vs_id = tet_vs_current_id;
840                 }
841                 else
842                 {
843                         // start new VS game
844                         ++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");
848                 }
849                 self.tet_highest_line = 0;
850                 ResetTetris();
851                 self.tet_org = self.origin;
852                 self.movetype = MOVETYPE_NOCLIP;
853         }
854         else
855         {
856                 Tet_GameExit();
857                 self.impulse = 0;
858         }
859 };
860
861
862 float TetrisPreFrame()
863 {
864         if (!self.tetris_on)
865                 return 0;
866
867         self.tet_org = self.origin;
868         if (self.tet_drawtime > time)
869                 return 1;
870         Draw_Tetris();
871         if(self.tetris_on == 3)
872                 self.tet_drawtime = ceil(time - tet_vs_current_timeout + 0.1) + tet_vs_current_timeout;
873         else
874                 self.tet_drawtime = time + 0.5;
875         return 1;
876 };
877 float frik_anglemoda(float v)
878 {
879         return v - floor(v/360) * 360;
880 };
881 float angcompa(float y1, float y2)
882 {
883         y1 = frik_anglemoda(y1);
884         y2 = frik_anglemoda(y2);
885
886         local float answer;
887         answer = y1 - y2;
888         if (answer > 180)
889                 answer = answer - 360;
890         else if (answer < -180)
891                 answer = answer + 360;
892         return answer;
893 };
894
895 .float tetkey_down, tetkey_rotright, tetkey_left, tetkey_right, tetkey_rotleft, tetkey_drop;
896
897 float TetrisKeyRepeat(.float fld, float f)
898 {
899         if(f)
900         {
901                 if(self.fld == 0) // initial key press
902                 {
903                         self.fld = time + 0.3;
904                         return 1;
905                 }
906                 else if(time > self.fld)
907                 {
908                         self.fld = time + 0.1;
909                         return 1;
910                 }
911                 else
912                 {
913                         // repeating too fast
914                         return 0;
915                 }
916         }
917         else
918         {
919                 self.fld = 0;
920                 return 0;
921         }
922 }
923
924 float TetrisPostFrame()
925 {
926         float keysa;
927
928         keysa = 0;
929
930         if (!self.tetris_on)
931                 return 0;
932         if(self.tetris_on == 2 && time > self.tet_gameovertime)
933         {
934                 Tet_GameExit();
935                 return 0;
936         }
937         if(self.tetris_on == 3 && time > tet_vs_current_timeout)
938         {
939                 self.tetris_on = 1; // start VS game
940                 self.tet_drawtime = 0;
941         }
942         
943         self.origin = self.tet_org;
944         self.movetype = MOVETYPE_NONE;
945
946         if(self.tetris_on == 1)
947         {
948                 if(TetrisKeyRepeat(tetkey_down, self.movement_x < 0))
949                         keysa |= TETKEY_DOWN;
950
951                 if(TetrisKeyRepeat(tetkey_rotright, self.movement_x > 0))
952                         keysa |= TETKEY_ROTRIGHT;
953
954                 if(TetrisKeyRepeat(tetkey_left, self.movement_y < 0))
955                         keysa |= TETKEY_LEFT;
956
957                 if(TetrisKeyRepeat(tetkey_right, self.movement_y > 0))
958                         keysa |= TETKEY_RIGHT;
959                 
960                 if(TetrisKeyRepeat(tetkey_rotleft, self.BUTTON_CROUCH))
961                         keysa |= TETKEY_ROTLEFT;
962
963                 if(TetrisKeyRepeat(tetkey_drop, self.BUTTON_JUMP))
964                         keysa |= TETKEY_DROP;
965
966                 HandleGame(keysa);
967         }
968
969         return 1;
970 };
971
972 #endif