├── .gitignore ├── src ├── dos │ ├── video.inc │ ├── inttypes.h │ ├── timer.asm │ ├── video.asm │ ├── pcbios.asm │ ├── main.c │ └── scoredb.c ├── game.h ├── scoredb.h ├── term.h ├── adm3.c ├── term.c ├── pieces.h ├── vt52.c ├── win │ ├── main.c │ └── scoredb.c ├── unix │ ├── scoredb.c │ └── main.c ├── ansi.c └── game.c ├── tools ├── Makefile ├── font ├── bevelfnt └── fontconv.c ├── Makefile.vc ├── .github └── workflows │ ├── build_gnulinux.yml │ └── build_dos_watcom.yml ├── Makefile.sgi ├── Makefile ├── Makefile.wat ├── doc └── softfonts.md ├── README.md └── COPYING /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.d 3 | *.swp 4 | *.log 5 | termtris 6 | fontconv 7 | 8 | *.obj 9 | *.OBJ 10 | *.lnk 11 | *.LNK 12 | *.com 13 | *.COM 14 | *.map 15 | *.MAP 16 | -------------------------------------------------------------------------------- /src/dos/video.inc: -------------------------------------------------------------------------------- 1 | ; video hardware detection 2 | 3 | VIDTYPE_UNK equ 0 4 | VIDTYPE_MDA equ 1 5 | VIDTYPE_CGA equ 2 6 | VIDTYPE_EGA equ 3 7 | VIDTYPE_VGA equ 4 8 | 9 | ; vi:ft=nasm ts=8 sts=8 sw=8: 10 | -------------------------------------------------------------------------------- /tools/Makefile: -------------------------------------------------------------------------------- 1 | obj = fontconv.o 2 | bin = fontconv 3 | 4 | CFLAGS = -pedantic -Wall -g 5 | 6 | $(bin): $(obj) 7 | $(CC) -o $@ $(obj) $(LDFLAGS) 8 | 9 | .PHONY: clean 10 | clean: 11 | rm -f $(bin) $(obj) 12 | -------------------------------------------------------------------------------- /Makefile.vc: -------------------------------------------------------------------------------- 1 | obj = src/win/main.obj src/win/scoredb.obj src/game.obj src/term.obj \ 2 | src/ansi.obj src/vt52.obj src/adm3.obj 3 | bin = termtris.exe 4 | 5 | CC = cl 6 | LD = link 7 | CFLAGS = -Zi -Isrc 8 | 9 | $(bin): $(obj) 10 | $(LD) -out:$@ $(obj) $(LDFLAGS) 11 | 12 | .c.obj: 13 | $(CC) $(CFLAGS) -Fo$@ -c $** 14 | 15 | clean: 16 | del $(obj) $(bin) 17 | -------------------------------------------------------------------------------- /src/dos/inttypes.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef INT_TYPES_H_ 3 | #define INT_TYPES_H_ 4 | 5 | #if defined(__WATCOMC__) && __WATCOMC__ < 1200 6 | typedef char int8_t; 7 | typedef short int16_t; 8 | typedef long int32_t; 9 | 10 | typedef unsigned char uint8_t; 11 | typedef unsigned short uint16_t; 12 | typedef unsigned long uint32_t; 13 | 14 | typedef long intptr_t; 15 | typedef unsigned long uintptr_t; 16 | #else 17 | #include 18 | #endif 19 | 20 | #endif /* INT_TYPES_H_ */ 21 | -------------------------------------------------------------------------------- /.github/workflows/build_gnulinux.yml: -------------------------------------------------------------------------------- 1 | name: GNU/Linux build 2 | 3 | on: 4 | push: 5 | pull_request: 6 | workflow_dispatch: 7 | 8 | jobs: 9 | build: 10 | 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v3 15 | 16 | - name: build 17 | run: | 18 | make 19 | 20 | - uses: actions/upload-artifact@v4 21 | with: 22 | name: termtris-gnulinux 23 | path: | 24 | termtris 25 | 26 | # vi:ts=2 sts=2 sw=2 expandtab: 27 | -------------------------------------------------------------------------------- /Makefile.sgi: -------------------------------------------------------------------------------- 1 | # --- build options --- 2 | PREFIX = /usr/local 3 | BINDIR = bin 4 | SCOREDIR = /var/games/termtris 5 | # --------------------- 6 | 7 | obj = src/unix/main.o src/unix/scoredb.o src/game.o src/term.o src/ansi.o \ 8 | src/vt52.o src/adm3.o 9 | bin = termtris 10 | 11 | CFLAGS = -O2 -g3 -DSCOREDIR=\"$(SCOREDIR)\" -DNO_INTTYPES_H -Isrc 12 | 13 | $(bin): $(obj) 14 | $(CC) -o $@ $(obj) $(LDFLAGS) 15 | 16 | .c.o: 17 | $(CC) -o $@ $(CFLAGS) -c $< 18 | 19 | .PHONY: clean 20 | clean: 21 | rm -f $(obj) $(bin) 22 | 23 | .PHONY: install 24 | install: $(bin) 25 | mkdir -p $(DESTDIR)$(PREFIX)/$(BINDIR) 26 | cp $(bin) $(DESTDIR)$(PREFIX)/$(BINDIR)/$(bin) 27 | mkdir -p $(DESTDIR)$(SCOREDIR) 28 | chmod 0777 $(DESTDIR)$(SCOREDIR) 29 | 30 | .PHONY: uninstall 31 | uninstall: 32 | rm -f $(DESTDIR)$(PREFIX)/$(BINDIR)/$(bin) 33 | -------------------------------------------------------------------------------- /tools/font: -------------------------------------------------------------------------------- 1 | glyph 7x10 0x5b 2 | ####### 3 | ##..... 4 | ##..... 5 | ##....# 6 | ##...#. 7 | ##...#. 8 | ##....# 9 | ##..... 10 | ##..... 11 | ####### 12 | 13 | glyph 7x10 0x5d 14 | ####### 15 | .....## 16 | .....## 17 | #....## 18 | .#...## 19 | .#...## 20 | #....## 21 | .....## 22 | .....## 23 | ####### 24 | 25 | glyph 10x16 0x5b 26 | ########## 27 | ##........ 28 | ##........ 29 | ##........ 30 | ##........ 31 | ##.......# 32 | ##......#. 33 | ##.....#.. 34 | ##.....#.. 35 | ##......#. 36 | ##.......# 37 | ##........ 38 | ##........ 39 | ##........ 40 | ##........ 41 | ########## 42 | 43 | glyph 10x16 0x5d 44 | ########## 45 | ........## 46 | ........## 47 | ........## 48 | ........## 49 | #.......## 50 | .#......## 51 | ..#.....## 52 | ..#.....## 53 | .#......## 54 | #.......## 55 | ........## 56 | ........## 57 | ........## 58 | ........## 59 | ########## 60 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # --- build options --- 2 | PREFIX = /usr/local 3 | BINDIR = bin 4 | SCOREDIR = /var/games/termtris 5 | # --------------------- 6 | 7 | src = $(wildcard src/unix/*.c) $(wildcard src/*.c) 8 | obj = $(src:.c=.o) 9 | dep = $(obj:.o=.d) 10 | bin = termtris 11 | 12 | CFLAGS = -pedantic -Wall -O2 -g -Isrc -DSCOREDIR=\"$(SCOREDIR)\" -MMD 13 | 14 | $(bin): $(obj) 15 | $(CC) -o $@ $(obj) $(LDFLAGS) 16 | 17 | -include $(dep) 18 | 19 | .PHONY: clean 20 | clean: 21 | rm -f $(obj) $(bin) 22 | 23 | .PHONY: cleandep 24 | cleandep: 25 | rm -f $(dep) 26 | 27 | .PHONY: install 28 | install: $(bin) 29 | mkdir -p $(DESTDIR)$(PREFIX)/$(BINDIR) 30 | cp $(bin) $(DESTDIR)$(PREFIX)/$(BINDIR)/$(bin) 31 | mkdir -p $(DESTDIR)$(SCOREDIR) 32 | chmod 0777 $(DESTDIR)$(SCOREDIR) 33 | touch $(DESTDIR)$(SCOREDIR)/scores 34 | chmod 0666 $(DESTDIR)$(SCOREDIR)/scores 35 | 36 | .PHONY: uninstall 37 | uninstall: 38 | rm -f $(DESTDIR)$(PREFIX)/$(BINDIR)/$(bin) 39 | -------------------------------------------------------------------------------- /.github/workflows/build_dos_watcom.yml: -------------------------------------------------------------------------------- 1 | name: DOS build 2 | 3 | on: 4 | push: 5 | pull_request: 6 | workflow_dispatch: 7 | 8 | jobs: 9 | build: 10 | 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v3 15 | 16 | - name: cache toolchain 17 | id: cache-tc 18 | uses: actions/cache@v3 19 | with: 20 | path: ow 21 | key: openwatcom-cache1 22 | 23 | - name: install watcom C toolchain 24 | if: steps.cache-tc.outputs.cache-hit != 'true' 25 | run: | 26 | wget http://mutantstargoat.com/~nuclear/ow_bundle.tar.gz 27 | tar xzvf ow_bundle.tar.gz 28 | sed -i 's/HOME/GITHUB_WORKSPACE/g' ow/owsetenv-dos.sh 29 | 30 | - name: install nasm assembler 31 | run: | 32 | sudo apt-get install nasm 33 | 34 | - name: build 35 | run: | 36 | source ow/owsetenv-dos.sh 37 | wmake -f Makefile.wat 38 | 39 | - uses: actions/upload-artifact@v4 40 | with: 41 | name: termtris-dos 42 | path: | 43 | termtris.com 44 | 45 | # vi:ts=2 sts=2 sw=2 expandtab: 46 | -------------------------------------------------------------------------------- /src/game.h: -------------------------------------------------------------------------------- 1 | /* 2 | Termtris - a tetris game for ANSI/VT100 terminals 3 | Copyright (C) 2019-2023 John Tsiombikas 4 | 5 | This program is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | This program is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with this program. If not, see . 17 | */ 18 | #ifndef GAME_H_ 19 | #define GAME_H_ 20 | 21 | extern int quit; 22 | extern long tick_interval; 23 | extern int use_bell; 24 | extern int monochrome; 25 | extern int use_gfxchar; 26 | extern int onlyascii; 27 | extern int blackpf; 28 | extern int rotstep; 29 | 30 | extern int term_width, term_height; 31 | 32 | int init_game(void); 33 | void cleanup_game(void); 34 | 35 | long update(long msec); 36 | void game_input(int c); 37 | 38 | /* wait for any pending drawing to be completed before proceeding 39 | * implemented in main.c 40 | */ 41 | void wait_display(void); 42 | 43 | #endif /* GAME_H_ */ 44 | -------------------------------------------------------------------------------- /Makefile.wat: -------------------------------------------------------------------------------- 1 | !ifdef __UNIX__ 2 | obj = src/dos/main.obj src/dos/timer.obj src/dos/video.obj src/dos/pcbios.obj & 3 | src/dos/scoredb.obj src/game.obj src/term.obj src/ansi.obj & 4 | src/vt52.obj src/adm3.obj 5 | inc = -Isrc -Isrc/dos 6 | asminc = -i src/dos/ 7 | !else 8 | obj = src\dos\main.obj src\dos\timer.obj src\dos\video.obj src\dos\pcbios.obj & 9 | src\dos\scoredb.obj src\game.obj src\term.obj src\ansi.obj & 10 | src\vt52.obj src\adm3.obj 11 | inc = -Isrc -Isrc\dos 12 | asminc = -i src\dos\ 13 | !endif 14 | 15 | bin = termtris.com 16 | 17 | #opt = -otexan 18 | #opt = -od 19 | dbg = -d3 20 | 21 | AS = nasm 22 | CC = wcc 23 | LD = wlink 24 | ASFLAGS = -fobj $(asminc) 25 | CFLAGS = $(dbg) $(opt) $(inc) $(def) -ms -s -zq -bt=com $(incpath) 26 | LDFLAGS = option map $(libpath) 27 | 28 | $(bin): $(obj) 29 | %write objects.lnk $(obj) 30 | %write ldflags.lnk $(LDFLAGS) 31 | $(LD) name $@ system com file { @objects } @ldflags 32 | 33 | # $(LD) debug all name $@ system com file { @objects } @ldflags 34 | 35 | .c: src;src/dos 36 | .asm: src;src/dos 37 | 38 | .c.obj: .autodepend 39 | $(CC) -fo=$@ $(CFLAGS) $[* 40 | 41 | .asm.obj: 42 | $(AS) $(ASFLAGS) -o $@ $[*.asm 43 | 44 | !ifdef __UNIX__ 45 | clean: .symbolic 46 | rm -f $(obj) 47 | rm -f $(bin) 48 | rm -f *.lnk 49 | rm -f termtris.map 50 | !else 51 | clean: .symbolic 52 | del src\*.obj 53 | del src\dos\*.obj 54 | del $(bin) 55 | del *.lnk 56 | del termtris.map 57 | !endif 58 | -------------------------------------------------------------------------------- /src/scoredb.h: -------------------------------------------------------------------------------- 1 | /* 2 | Termtris - a tetris game for ANSI/VT100 terminals 3 | Copyright (C) 2019-2023 John Tsiombikas 4 | 5 | This program is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | This program is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with this program. If not, see . 17 | */ 18 | #ifndef SCOREDB_H_ 19 | #define SCOREDB_H_ 20 | 21 | #include 22 | 23 | #if defined(MSDOS) || defined(__COM__) 24 | /* the DOS version of score_entry is simpler, because scores are embedded in 25 | * the executable itself in binary format. 26 | */ 27 | #define MAX_NAME 36 28 | struct score_entry { 29 | char user[MAX_NAME]; 30 | long score, lines, level; 31 | struct score_entry *next; 32 | }; 33 | #else 34 | struct score_entry { 35 | char *user; 36 | long score, lines, level; 37 | struct score_entry *next; 38 | }; 39 | #endif 40 | 41 | struct score_entry *read_scores(FILE *fp, int max_scores); 42 | int save_score(struct score_entry *sc); 43 | int print_scores(int num); 44 | 45 | #endif /* SCOREDB_H_ */ 46 | -------------------------------------------------------------------------------- /src/dos/timer.asm: -------------------------------------------------------------------------------- 1 | ; Termtris - a tetris game for ANSI/VT100 terminals 2 | ; Copyright (C) 2019-2023 John Tsiombikas 3 | ; 4 | ; This program is free software: you can redistribute it and/or modify 5 | ; it under the terms of the GNU General Public License as published by 6 | ; the Free Software Foundation, either version 3 of the License, or 7 | ; (at your option) any later version. 8 | ; 9 | ; This program is distributed in the hope that it will be useful, 10 | ; but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | ; GNU General Public License for more details. 13 | ; 14 | ; You should have received a copy of the GNU General Public License 15 | ; along with this program. If not, see . 16 | 17 | bits 16 18 | cpu 8086 19 | 20 | segment _TEXT class=CODE 21 | 22 | global init_timer_ 23 | init_timer_: 24 | push bx 25 | push dx 26 | push es 27 | mov ax, 351ch 28 | int 21h 29 | mov [prev_isr], bx 30 | mov [prev_isr+2], es 31 | 32 | mov ax, 251ch 33 | mov dx, timer_isr ; ds is already equal to cs 34 | int 21h 35 | pop es 36 | pop dx 37 | pop bx 38 | ret 39 | 40 | align 4 41 | prev_isr dd 0 42 | 43 | global cleanup_timer_ 44 | cleanup_timer_: 45 | push dx 46 | push ds 47 | mov dx, [prev_isr] 48 | mov ds, [prev_isr+2] 49 | mov ax, 251ch 50 | int 21h 51 | pop ds 52 | pop dx 53 | ret 54 | 55 | extern _timer_ticks 56 | 57 | timer_isr: 58 | inc word [cs:_timer_ticks] 59 | jnz .done 60 | inc word [cs:_timer_ticks+2] 61 | .done: iret 62 | 63 | ; vi:ft=nasm ts=8 sts=8 sw=8: 64 | -------------------------------------------------------------------------------- /src/term.h: -------------------------------------------------------------------------------- 1 | /* 2 | Termtris - a tetris game for TERM/VT100 terminals 3 | Copyright (C) 2019-2022 John Tsiombikas 4 | 5 | This program is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | This program is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with this program. If not, see . 17 | */ 18 | #ifndef TERM_H_ 19 | #define TERM_H_ 20 | 21 | enum { 22 | G_DIAMOND = 0x04, 23 | G_CHECKER = 0xb1, 24 | G_LR_CORNER = 0xd9, 25 | G_UR_CORNER = 0xbf, 26 | G_UL_CORNER = 0xda, 27 | G_LL_CORNER = 0xc0, 28 | G_CROSS = 0xc5, 29 | G_HLINE = 0xc4, 30 | G_L_TEE = 0xc3, 31 | G_R_TEE = 0xb4, 32 | G_B_TEE = 0xc1, 33 | G_T_TEE = 0xc2, 34 | G_VLINE = 0xb3, 35 | G_CDOT = 0xf8 36 | }; 37 | 38 | enum { BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, YELLOW, WHITE }; 39 | #define BOLD 8 40 | 41 | enum { 42 | TERM_ANSI, 43 | TERM_VT52, 44 | TERM_ADM3, 45 | TERM_PCBIOS = 0xb105 46 | }; 47 | 48 | extern char *termenv; 49 | extern int term_type; 50 | 51 | extern void (*term_reset)(void); 52 | extern void (*term_clearscr)(void); 53 | extern void (*term_setcursor)(int row, int col); 54 | extern void (*term_cursor)(int show); 55 | extern void (*term_setcolor)(int fg, int bg); 56 | /* convert a PC cga/ega/vga char+attr to an TERM sequence and write it to stdout */ 57 | extern void (*term_ibmchar)(unsigned char c, unsigned char attr); 58 | 59 | void term_init(void); 60 | void term_putstr(const char *s, unsigned char attr); 61 | 62 | #endif /* TERM_H_ */ 63 | -------------------------------------------------------------------------------- /src/adm3.c: -------------------------------------------------------------------------------- 1 | /* 2 | Termtris - a tetris game for ANSI/VT100 terminals 3 | Copyright (C) 2019-2023 John Tsiombikas 4 | 5 | This program is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | This program is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with this program. If not, see . 17 | */ 18 | #include 19 | #include "game.h" 20 | #include "term.h" 21 | 22 | void adm3_reset(void); 23 | void adm3_clearscr(void); 24 | void adm3_setcursor(int row, int col); 25 | void adm3_cursor(int show); 26 | void adm3_setcolor(int fg, int bg); 27 | void adm3_ibmchar(unsigned char c, unsigned char attr); 28 | 29 | void adm3_init(void) 30 | { 31 | /* TODO: allow line drawing characters for ADM31 with the appropriate option 32 | * ROM installed 33 | */ 34 | onlyascii = 1; 35 | 36 | term_type = TERM_ADM3; 37 | term_reset = adm3_reset; 38 | term_clearscr = adm3_clearscr; 39 | term_setcursor = adm3_setcursor; 40 | term_cursor = adm3_cursor; 41 | term_setcolor = adm3_setcolor; 42 | term_ibmchar = adm3_ibmchar; 43 | } 44 | 45 | void adm3_reset(void) 46 | { 47 | adm3_clearscr(); 48 | } 49 | 50 | void adm3_clearscr(void) 51 | { 52 | fputc(032, stdout); /* SUB: clearscr */ 53 | } 54 | 55 | void adm3_setcursor(int row, int col) 56 | { 57 | if(!(row | col)) { 58 | fputc(036, stdout); /* RS: home */ 59 | } else { 60 | printf("\033=%c%c", row + 32, col + 32); 61 | } 62 | } 63 | 64 | void adm3_cursor(int show) 65 | { 66 | } 67 | 68 | void adm3_setcolor(int fg, int bg) 69 | { 70 | } 71 | 72 | void adm3_ibmchar(unsigned char c, unsigned char attr) 73 | { 74 | fputc(c, stdout); 75 | } 76 | -------------------------------------------------------------------------------- /src/term.c: -------------------------------------------------------------------------------- 1 | /* 2 | Termtris - a tetris game for ANSI/VT100 terminals 3 | Copyright (C) 2019-2023 John Tsiombikas 4 | 5 | This program is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | This program is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with this program. If not, see . 17 | */ 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | void vt52_init(void); 24 | void ansi_init(void); 25 | void adm3_init(void); 26 | #if defined(MSDOS) || defined(__COM__) 27 | void pcbios_init(void); 28 | #endif 29 | 30 | char *termenv; 31 | int term_type; 32 | 33 | void (*term_reset)(void); 34 | void (*term_clearscr)(void); 35 | void (*term_setcursor)(int row, int col); 36 | void (*term_cursor)(int show); 37 | void (*term_setcolor)(int fg, int bg); 38 | void (*term_ibmchar)(unsigned char c, unsigned char attr); 39 | 40 | 41 | 42 | void term_init(void) 43 | { 44 | char *env; 45 | int vtnum; 46 | 47 | if((env = getenv("TERM"))) { 48 | int len = strlen(env); 49 | if((termenv = calloc(1, len < 8 ? 8 : len + 1))) { 50 | strcpy(termenv, env); 51 | 52 | env = termenv; 53 | while(*env) { 54 | *env = tolower(*env); 55 | env++; 56 | } 57 | } 58 | } 59 | 60 | #if defined(MSDOS) || defined(__COM__) 61 | if(!termenv) { 62 | pcbios_init(); 63 | return; 64 | } 65 | #else 66 | if(!termenv) { 67 | goto ansi; /* if TERM is unset, assume ANSI */ 68 | } 69 | #endif 70 | 71 | if(termenv[0] == 'v' && termenv[1] == 't') { 72 | vtnum = atoi(termenv + 2); 73 | 74 | if(vtnum >= 52 && vtnum < 100) { 75 | vt52_init(); 76 | } else { 77 | ansi_init(); 78 | } 79 | return; 80 | } 81 | 82 | if(memcmp(termenv, "adm3", 4) == 0) { 83 | adm3_init(); 84 | return; 85 | } 86 | 87 | ansi: 88 | /* for unknown terminals, try to use ANSI */ 89 | ansi_init(); 90 | } 91 | 92 | void term_putstr(const char *s, unsigned char attr) 93 | { 94 | while(*s) { 95 | term_ibmchar(*s++, attr); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /tools/bevelfnt: -------------------------------------------------------------------------------- 1 | glyph 10x16 0x5b 2 | ########## 3 | ########## 4 | ####.#.##. 5 | ##.##.#.## 6 | ##.#.##.#. 7 | ###.#.##.# 8 | ##.##.#.#. 9 | ###.##.### 10 | ###.#.##.. 11 | ####.#.### 12 | ##.#.##..# 13 | ###.#.###. 14 | ###.##.#.# 15 | ##.#.#.##. 16 | ###....... 17 | ....#.#..# 18 | 19 | glyph 10x16 0x5d 20 | ########## 21 | #########. 22 | #.##.#.#.. 23 | .#.##.#... 24 | ###.#.###. 25 | ..#.##.#.. 26 | ####.#.#.. 27 | ..#.#.#..# 28 | ##.#.###.. 29 | .##.#.#.#. 30 | .#.###.#.. 31 | #.#.#.##.. 32 | .###.#.#.. 33 | #.#.##.#.# 34 | .......... 35 | .#..#.#... 36 | 37 | glyph 15x12 0x5b 38 | ############### 39 | #######.####.## 40 | #####.#..#.#..# 41 | #####.#..##.#.# 42 | ####.#.#.#..#.# 43 | #####..#..#.#.# 44 | ####.##.##.##.. 45 | ####.#.##..#.#. 46 | ####.#..#..#.## 47 | ######..#..#..# 48 | ###............ 49 | ............... 50 | 51 | glyph 15x12 0x5d 52 | #############.. 53 | ##.####.###.... 54 | .##..#.#..#.... 55 | #.##.#.###..... 56 | ..#..#..###.... 57 | .##.#.#..#..... 58 | ##.#..#.###.... 59 | #.##..#..#..... 60 | ..#..##.###.... 61 | ..##.#..#.#.... 62 | ............... 63 | ............... 64 | 65 | glyph 8x10 0x5b 66 | ######## 67 | ####.#.# 68 | ##.#.##. 69 | ###.#.## 70 | ##.##.#. 71 | ####.#.# 72 | ##.#.##. 73 | ###.##.# 74 | ##.#.#.# 75 | #....... 76 | 77 | glyph 8x10 0x5d 78 | ######## 79 | .##.#.#. 80 | ##.#.##. 81 | .#.##.#. 82 | ###.#.#. 83 | ##.#.#.. 84 | #.###.#. 85 | ###.#.#. 86 | .#.##.#. 87 | ........ 88 | 89 | glyph 8x16 0xdb 90 | ######## 91 | ######## 92 | ####.#.# 93 | ##.##.#. 94 | ##.#.##. 95 | ###.#.## 96 | ##.##.#. 97 | ###.##.# 98 | ###.#.## 99 | ####.#.# 100 | ##.#.##. 101 | ###.#.## 102 | ###.##.# 103 | ##.#.#.# 104 | ###..... 105 | ....#... 106 | 107 | glyph 8x16 0xdd 108 | ######## 109 | #######. 110 | .##.#.#. 111 | #.##.#.. 112 | ##.#.##. 113 | .#.##.#. 114 | ###.#.#. 115 | .#.#.#.. 116 | #.#.###. 117 | ##.#.#.. 118 | #.###.#. 119 | .#.#.##. 120 | ###.#.#. 121 | .#.##.#. 122 | ........ 123 | #....#.. 124 | 125 | glyph 8x14 0xdb 126 | ######## 127 | ######## 128 | ####.#.# 129 | ##.#.##. 130 | ###.#.## 131 | ##.##.#. 132 | ###.##.# 133 | ###.#.## 134 | ####.#.# 135 | ##.#.##. 136 | ###.##.# 137 | ##.#.#.# 138 | ###..... 139 | ....#... 140 | 141 | glyph 8x14 0xdd 142 | ######## 143 | #######. 144 | .##.#.#. 145 | ##.#.##. 146 | .#.##.#. 147 | ###.#.#. 148 | .#.#.#.. 149 | #.#.###. 150 | ##.#.#.. 151 | #.###.#. 152 | ###.#.#. 153 | .#.##.#. 154 | ........ 155 | #....#.. 156 | -------------------------------------------------------------------------------- /src/dos/video.asm: -------------------------------------------------------------------------------- 1 | ; Termtris - a tetris game for ANSI/VT100 terminals 2 | ; Copyright (C) 2019-2023 John Tsiombikas 3 | ; 4 | ; This program is free software: you can redistribute it and/or modify 5 | ; it under the terms of the GNU General Public License as published by 6 | ; the Free Software Foundation, either version 3 of the License, or 7 | ; (at your option) any later version. 8 | ; 9 | ; This program is distributed in the hope that it will be useful, 10 | ; but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | ; GNU General Public License for more details. 13 | ; 14 | ; You should have received a copy of the GNU General Public License 15 | ; along with this program. If not, see . 16 | 17 | bits 16 18 | cpu 8086 19 | 20 | segment _TEXT class=CODE 21 | 22 | extern _monochrome 23 | extern _use_gfxchar 24 | 25 | %include "video.inc" 26 | 27 | global detect_video_ 28 | detect_video_: 29 | push bx 30 | ; try the VGA detect call first 31 | mov ax, 1a00h 32 | int 10h 33 | cmp al, 1ah 34 | jnz .skip_vgainfo ; function not available 35 | cmp bl, 0ffh 36 | jz .skip_vgainfo ; unknown display type 37 | cmp bl, 1 38 | jnz .nomda 39 | mov word [vidtype], VIDTYPE_MDA 40 | mov word [_monochrome], 1 41 | jmp .done 42 | .nomda: cmp bl, 4 43 | jae .nocga 44 | mov word [vidtype], VIDTYPE_CGA 45 | mov word [_monochrome], 0 46 | jmp .done 47 | .nocga: ; for ega/vga first determine monochrome/color display 48 | mov ax, bx 49 | and ax, 1 50 | mov [_monochrome], ax 51 | cmp bl, 7 52 | jae .noega 53 | mov word [vidtype], VIDTYPE_EGA 54 | jmp .done 55 | .noega: mov word [vidtype], VIDTYPE_VGA 56 | jmp .done 57 | 58 | .skip_vgainfo: 59 | ; try get ega info 60 | mov ah, 12h 61 | mov bx, 0ff10h 62 | int 10h 63 | cmp bh, 0ffh 64 | jz .skip_egainfo 65 | mov word [vidtype], VIDTYPE_EGA 66 | test bh, bh 67 | jnz .ega_mono 68 | mov word [_monochrome], 0 69 | jmp .done 70 | .ega_mono: 71 | mov word [_monochrome], 1 72 | jmp .done 73 | 74 | .skip_egainfo: 75 | ; try int 11h (get equipment list) 76 | int 11h 77 | and ax, 30h 78 | cmp ax, 30h 79 | jz .mda 80 | mov word [vidtype], VIDTYPE_CGA 81 | mov word [_monochrome], 0 82 | jmp .done 83 | .mda: mov word [vidtype], VIDTYPE_MDA 84 | mov word [_monochrome], 1 85 | 86 | .done: cmp word [vidtype], VIDTYPE_EGA 87 | jb .end 88 | mov word [_use_gfxchar], 1 89 | .end: pop bx 90 | ret 91 | 92 | align 2 93 | global vidtype 94 | vidtype dw 0 95 | 96 | ; vi:ft=nasm ts=8 sts=8 sw=8: 97 | -------------------------------------------------------------------------------- /src/pieces.h: -------------------------------------------------------------------------------- 1 | /* 2 | Termtris - a tetris game for ANSI/VT100 terminals 3 | Copyright (C) 2019-2022 John Tsiombikas 4 | 5 | This program is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | This program is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with this program. If not, see . 17 | */ 18 | #ifndef PIECES_H_ 19 | #define PIECES_H_ 20 | 21 | #define BLK(x, y) ((x) | ((y) << 4)) 22 | #define BLKX(c) ((unsigned char)(c) & 0xf) 23 | #define BLKY(c) ((unsigned char)(c) >> 4) 24 | 25 | #define NUM_PIECES 7 26 | 27 | static unsigned char pieces[NUM_PIECES][4][4] = { 28 | /* L block */ 29 | { 30 | {BLK(0, 1), BLK(0, 2), BLK(1, 1), BLK(2, 1)}, 31 | {BLK(0, 0), BLK(1, 0), BLK(1, 1), BLK(1, 2)}, 32 | {BLK(0, 1), BLK(1, 1), BLK(2, 1), BLK(2, 0)}, 33 | {BLK(1, 0), BLK(1, 1), BLK(1, 2), BLK(2, 2)} 34 | }, 35 | /* J block */ 36 | { 37 | {BLK(0, 1), BLK(1, 1), BLK(2, 1), BLK(2, 2)}, 38 | {BLK(1, 0), BLK(1, 1), BLK(1, 2), BLK(0, 2)}, 39 | {BLK(0, 0), BLK(0, 1), BLK(1, 1), BLK(2, 1)}, 40 | {BLK(1, 0), BLK(2, 0), BLK(1, 1), BLK(1, 2)} 41 | }, 42 | /* I block */ 43 | { 44 | {BLK(0, 2), BLK(1, 2), BLK(2, 2), BLK(3, 2)}, 45 | {BLK(1, 0), BLK(1, 1), BLK(1, 2), BLK(1, 3)}, 46 | {BLK(0, 2), BLK(1, 2), BLK(2, 2), BLK(3, 2)}, 47 | {BLK(1, 0), BLK(1, 1), BLK(1, 2), BLK(1, 3)} 48 | }, 49 | /* O block */ 50 | { 51 | {BLK(1, 1), BLK(2, 1), BLK(1, 2), BLK(2, 2)}, 52 | {BLK(1, 1), BLK(2, 1), BLK(1, 2), BLK(2, 2)}, 53 | {BLK(1, 1), BLK(2, 1), BLK(1, 2), BLK(2, 2)}, 54 | {BLK(1, 1), BLK(2, 1), BLK(1, 2), BLK(2, 2)} 55 | }, 56 | /* Z block */ 57 | { 58 | {BLK(0, 1), BLK(1, 1), BLK(1, 2), BLK(2, 2)}, 59 | {BLK(0, 1), BLK(1, 1), BLK(1, 0), BLK(0, 2)}, 60 | {BLK(0, 1), BLK(1, 1), BLK(1, 2), BLK(2, 2)}, 61 | {BLK(0, 1), BLK(1, 1), BLK(1, 0), BLK(0, 2)} 62 | }, 63 | /* S block */ 64 | { 65 | {BLK(1, 1), BLK(2, 1), BLK(0, 2), BLK(1, 2)}, 66 | {BLK(0, 0), BLK(0, 1), BLK(1, 1), BLK(1, 2)}, 67 | {BLK(1, 1), BLK(2, 1), BLK(0, 2), BLK(1, 2)}, 68 | {BLK(0, 0), BLK(0, 1), BLK(1, 1), BLK(1, 2)} 69 | }, 70 | /* T block */ 71 | { 72 | {BLK(0, 1), BLK(1, 1), BLK(2, 1), BLK(1, 2)}, 73 | {BLK(1, 0), BLK(1, 1), BLK(1, 2), BLK(0, 1)}, 74 | {BLK(0, 1), BLK(1, 1), BLK(2, 1), BLK(1, 0)}, 75 | {BLK(1, 0), BLK(1, 1), BLK(1, 2), BLK(2, 1)} 76 | } 77 | }; 78 | 79 | static int piece_spawnpos[NUM_PIECES][2] = { 80 | {-1, -2}, {-1, -2}, {-2, -2}, {-1, -2}, {-1, -2}, {-1, -2}, {-1, -2} 81 | }; 82 | 83 | 84 | #endif /* PIECES_H_ */ 85 | -------------------------------------------------------------------------------- /src/vt52.c: -------------------------------------------------------------------------------- 1 | /* 2 | Termtris - a tetris game for ANSI/VT100 terminals 3 | Copyright (C) 2019-2023 John Tsiombikas 4 | 5 | This program is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | This program is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with this program. If not, see . 17 | */ 18 | #include 19 | #include 20 | #include "term.h" 21 | 22 | void vt52_reset(void); 23 | void vt52_clearscr(void); 24 | void vt52_setcursor(int row, int col); 25 | void vt52_cursor(int show); 26 | void vt52_setcolor(int fg, int bg); 27 | void vt52_ibmchar(unsigned char c, unsigned char attr); 28 | 29 | 30 | static int gmode; 31 | 32 | 33 | void vt52_init(void) 34 | { 35 | term_type = TERM_VT52; 36 | term_reset = vt52_reset; 37 | term_clearscr = vt52_clearscr; 38 | term_setcursor = vt52_setcursor; 39 | term_cursor = vt52_cursor; 40 | term_setcolor = vt52_setcolor; 41 | term_ibmchar = vt52_ibmchar; 42 | } 43 | 44 | void vt52_reset(void) 45 | { 46 | /* make sure we leave the terminal in ASCII mode */ 47 | fputs("\033G", stdout); 48 | fflush(stdout); 49 | } 50 | 51 | void vt52_clearscr(void) 52 | { 53 | fputs("\033H\033J", stdout); /* home + erase to end of screen */ 54 | } 55 | 56 | void vt52_setcursor(int row, int col) 57 | { 58 | if((row | col) == 0) { 59 | fputs("\033H", stdout); 60 | } else { 61 | printf("\033Y%c%c", row + 32, col + 32); 62 | } 63 | } 64 | 65 | void vt52_cursor(int show) 66 | { 67 | } 68 | 69 | void vt52_setcolor(int fg, int bg) 70 | { 71 | } 72 | 73 | void vt52_ibmchar(unsigned char c, unsigned char attr) 74 | { 75 | char cmd[32]; 76 | char *ptr = cmd; 77 | 78 | if(c == G_CHECKER || c == G_HLINE) { 79 | if(!gmode) { 80 | gmode = 1; 81 | memcpy(ptr, "\033F", 2); 82 | ptr += 2; 83 | } 84 | } else { 85 | if(gmode) { 86 | gmode = 0; 87 | memcpy(ptr, "\033G", 2); 88 | ptr += 2; 89 | } 90 | } 91 | 92 | switch(c) { 93 | case G_CHECKER: 94 | c = 'a'; /* combined with switch to graphics mode above */ 95 | break; 96 | case G_HLINE: 97 | c = 'p'; 98 | break; 99 | case G_LR_CORNER: 100 | case G_UR_CORNER: 101 | case G_UL_CORNER: 102 | case G_LL_CORNER: 103 | case G_L_TEE: 104 | case G_R_TEE: 105 | case G_B_TEE: 106 | case G_T_TEE: 107 | c = '+'; 108 | break; 109 | case G_CROSS: 110 | c = 'X'; 111 | break; 112 | case G_VLINE: 113 | c = '|'; 114 | break; 115 | case G_CDOT: 116 | c = '.'; 117 | break; 118 | default: 119 | break; 120 | } 121 | 122 | *ptr++ = c; 123 | *ptr = 0; 124 | fputs(cmd, stdout); 125 | } 126 | -------------------------------------------------------------------------------- /tools/fontconv.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | /* max rows must be a multiple of 6 */ 8 | #define MAX_ROWS 24 9 | #define MAX_COLS 10 10 | 11 | int proc_font(const char *fname); 12 | void glyph_to_sixel(int glyph[MAX_ROWS][MAX_COLS], int width, int height, int cnum); 13 | void glyph_to_asm(int glyph[MAX_ROWS][MAX_COLS], int width, int height, int cnum); 14 | char *clean_line(char *s); 15 | 16 | int main(int argc, char **argv) 17 | { 18 | int i; 19 | 20 | for(i=1; i= 3) { 69 | cnum = tmp; 70 | } else { 71 | cnum++; 72 | } 73 | fontsz = (width << 8) | height; 74 | if(!FONT_VALID(fontsz)) { 75 | fprintf(stderr, "unknown font size: %dx%d\n", width, height); 76 | goto end; 77 | } 78 | if(FONT_IS_VT(fontsz) && (cnum <= 0x20 || cnum >= 0xff)) { 79 | fprintf(stderr, "invalid char num: %d\n", cnum); 80 | goto end; 81 | } 82 | cury = 0; 83 | state = 1; 84 | break; 85 | 86 | case 1: 87 | for(i=0; i= height) { 98 | if(FONT_IS_VT(fontsz)) { 99 | glyph_to_sixel(glyph, width, height, cnum); 100 | } else { 101 | glyph_to_asm(glyph, width, height, cnum); 102 | } 103 | state = 0; 104 | } 105 | break; 106 | 107 | default: 108 | break; 109 | } 110 | } 111 | 112 | res = 0; 113 | end: 114 | fclose(fp); 115 | return res; 116 | } 117 | 118 | void glyph_to_sixel(int glyph[MAX_ROWS][MAX_COLS], int width, int height, int cnum) 119 | { 120 | int i, j, fontsz, cury = 0; 121 | unsigned int sixel; 122 | 123 | fontsz = (width << 8) | height; 124 | switch(fontsz) { 125 | case FONT_8X10_VT2: 126 | printf("\\033P1;%d;1;4{ @", cnum - 32); 127 | break; 128 | 129 | case FONT_15X12_VT3: 130 | printf("\\033P1;%d;1;15;0;2;12{ @", cnum - 32); 131 | break; 132 | 133 | case FONT_10X16_VT4: 134 | printf("\\033P1;%d;1;10;0;2;16{ @", cnum - 32); 135 | break; 136 | 137 | default: 138 | break; 139 | } 140 | 141 | for(;;) { 142 | for(i=0; i> 1) | (glyph[cury + j][i] << 5); 146 | } 147 | sixel += 0x3f; /* offset into valid range of symbols */ 148 | putchar((char)sixel); 149 | } 150 | cury += 6; 151 | if(cury >= height) break; 152 | putchar('/'); 153 | } 154 | 155 | printf("\\033\\\\\n"); 156 | } 157 | 158 | void glyph_to_asm(int glyph[MAX_ROWS][MAX_COLS], int width, int height, int cnum) 159 | { 160 | int i, j; 161 | unsigned int bits; 162 | 163 | printf("glyph_%02xh:\n", (unsigned int)cnum); 164 | 165 | for(i=0; i= s && isspace(*endp)) { 188 | *endp = 0; 189 | } 190 | return *s ? s : 0; 191 | } 192 | -------------------------------------------------------------------------------- /src/dos/pcbios.asm: -------------------------------------------------------------------------------- 1 | ; Termtris - a tetris game for ANSI/VT100 terminals 2 | ; Copyright (C) 2019-2023 John Tsiombikas 3 | ; 4 | ; This program is free software: you can redistribute it and/or modify 5 | ; it under the terms of the GNU General Public License as published by 6 | ; the Free Software Foundation, either version 3 of the License, or 7 | ; (at your option) any later version. 8 | ; 9 | ; This program is distributed in the hope that it will be useful, 10 | ; but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | ; GNU General Public License for more details. 13 | ; 14 | ; You should have received a copy of the GNU General Public License 15 | ; along with this program. If not, see . 16 | 17 | bits 16 18 | cpu 8086 19 | 20 | segment _TEXT class=CODE 21 | 22 | %include "video.inc" 23 | 24 | extern _monochrome 25 | extern _use_gfxchar 26 | extern vidtype 27 | 28 | extern _term_type 29 | extern _term_reset 30 | extern _term_clearscr 31 | extern _term_setcursor 32 | extern _term_cursor 33 | extern _term_setcolor 34 | extern _term_ibmchar 35 | 36 | global pcbios_init_ 37 | pcbios_init_: 38 | mov word [_term_type], 0xb105 39 | mov word [_term_type + 2], 0 40 | mov word [_term_reset], pcbios_reset_ 41 | mov word [_term_clearscr], pcbios_clearscr_ 42 | mov word [_term_setcursor], pcbios_setcursor_ 43 | mov word [_term_cursor], pcbios_cursor_ 44 | mov word [_term_setcolor], pcbios_setcolor_ 45 | mov word [_term_ibmchar], pcbios_ibmchar_ 46 | 47 | mov ax, [_use_gfxchar] 48 | test ax, ax 49 | jz .end 50 | 51 | push bx 52 | push cx 53 | push dx 54 | push bp 55 | push es 56 | 57 | mov ax, cs 58 | mov es, ax 59 | cmp word [vidtype], VIDTYPE_VGA 60 | jz .vga 61 | mov bp, glyphs_ega 62 | mov bx, 0e00h ; 14 bytes per char, load block 0 63 | jmp .skipvga 64 | .vga: mov bp, glyphs_vga 65 | mov bx, 1000h ; 16 bytes per char, load block 0 66 | .skipvga: 67 | mov cx, 3 ; load 3 chars: [ \ ] 68 | mov dx, 0dbh ; load at graphics range to dup last column (set bit 7) 69 | mov ax, 1110h ; user alpha load 70 | int 10h 71 | 72 | pop es 73 | pop bp 74 | pop dx 75 | pop cx 76 | pop bx 77 | .end: ret 78 | 79 | global pcbios_reset_ 80 | pcbios_reset_: 81 | mov ax, 0f00h 82 | int 10h 83 | xor ah, ah 84 | int 10h 85 | ret 86 | 87 | global pcbios_clearscr_ 88 | pcbios_clearscr_: 89 | call pcbios_reset_ 90 | jmp pcbios_init_ 91 | 92 | global pcbios_cursor_ 93 | pcbios_cursor_: 94 | push bx 95 | push cx 96 | 97 | push ax 98 | mov ax, 300h 99 | xor bx, bx 100 | int 10h 101 | and ch, 0dfh 102 | pop ax 103 | 104 | test ax, ax 105 | jnz .skiphide 106 | or ch, 20h 107 | .skiphide: 108 | mov ax, 100h 109 | int 10h 110 | pop cx 111 | pop bx 112 | ret 113 | 114 | 115 | global pcbios_setcursor_ 116 | pcbios_setcursor_: 117 | push bx 118 | mov dh, al ; row (col is already in dl) 119 | xor bx, bx 120 | mov ax, 200h 121 | int 10h 122 | pop bx 123 | ret 124 | 125 | 126 | align 2 127 | attr dw 0700h 128 | 129 | global pcbios_setcolor_ 130 | pcbios_setcolor_: 131 | shl ax, 1 132 | shl ax, 1 133 | shl ax, 1 134 | shl ax, 1 135 | or ax, dx 136 | mov [attr], al 137 | ret 138 | 139 | advcursor: 140 | mov ax, 300h 141 | xor bx, bx 142 | int 10h 143 | inc dx 144 | mov ax, 200h 145 | int 10h 146 | ret 147 | 148 | 149 | global pcbios_ibmchar_ 150 | pcbios_ibmchar_: 151 | push bx 152 | push cx 153 | ; if it's [ or ], set bit 7 to use the graphic blocks 154 | cmp word [_use_gfxchar], 0 155 | jz .noremap 156 | cmp al, 5bh 157 | jz .remap 158 | cmp al, 5dh 159 | jnz .noremap 160 | .remap: or al, 80h 161 | .noremap: 162 | mov ah, 9 163 | mov bx, 7 164 | cmp word [_monochrome], 0 165 | jnz .skipattr 166 | rol dl, 1 167 | rol dl, 1 168 | rol dl, 1 169 | rol dl, 1 170 | mov bl, dl 171 | .skipattr: 172 | mov cx, 1 173 | int 10h 174 | call advcursor 175 | pop cx 176 | pop bx 177 | ret 178 | 179 | glyphs_vga: 180 | db 0xff 181 | db 0xff 182 | db 0xf5 183 | db 0xda 184 | db 0xd6 185 | db 0xeb 186 | db 0xda 187 | db 0xed 188 | db 0xeb 189 | db 0xf5 190 | db 0xd6 191 | db 0xeb 192 | db 0xed 193 | db 0xd5 194 | db 0xe0 195 | db 0x08 196 | 197 | times 16 db 0 198 | 199 | db 0xff 200 | db 0xfe 201 | db 0x6a 202 | db 0xb4 203 | db 0xd6 204 | db 0x5a 205 | db 0xea 206 | db 0x54 207 | db 0xae 208 | db 0xd4 209 | db 0xba 210 | db 0x56 211 | db 0xea 212 | db 0x5a 213 | db 0x00 214 | db 0x84 215 | 216 | glyphs_ega: 217 | db 0xff 218 | db 0xff 219 | db 0xf5 220 | db 0xd6 221 | db 0xeb 222 | db 0xda 223 | db 0xed 224 | db 0xeb 225 | db 0xf5 226 | db 0xd6 227 | db 0xed 228 | db 0xd5 229 | db 0xe0 230 | db 0x08 231 | 232 | times 14 db 0 233 | 234 | db 0xff 235 | db 0xfe 236 | db 0x6a 237 | db 0xd6 238 | db 0x5a 239 | db 0xea 240 | db 0x54 241 | db 0xae 242 | db 0xd4 243 | db 0xba 244 | db 0xea 245 | db 0x5a 246 | db 0x00 247 | db 0x84 248 | 249 | ; vi:ft=nasm ts=8 sts=8 sw=8: 250 | -------------------------------------------------------------------------------- /src/dos/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | Termtris - a tetris game for ANSI/VT100 terminals 3 | Copyright (C) 2019-2023 John Tsiombikas 4 | 5 | This program is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | This program is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with this program. If not, see . 17 | */ 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include "game.h" 24 | #include "scoredb.h" 25 | 26 | 27 | int init(void); 28 | void cleanup(void); 29 | int parse_args(int argc, char **argv); 30 | void print_usage(const char *argv0); 31 | long get_msec(void); 32 | 33 | long timer_ticks; 34 | const char *progpath; 35 | 36 | /* defined in video.asm */ 37 | void detect_video(void); 38 | 39 | /* defined in timer.asm */ 40 | void init_timer(void); 41 | void cleanup_timer(void); 42 | 43 | 44 | int main(int argc, char **argv) 45 | { 46 | int i, res, maxfd; 47 | long msec, next; 48 | int key; 49 | 50 | detect_video(); 51 | 52 | if(parse_args(argc, argv) == -1) { 53 | return 1; 54 | } 55 | 56 | if(init() == -1) { 57 | return 1; 58 | } 59 | 60 | for(;;) { 61 | while(kbhit()) { 62 | key = getch(); 63 | switch(key) { 64 | case 0x4b: 65 | game_input('a'); 66 | break; 67 | case 0x4d: 68 | game_input('d'); 69 | break; 70 | case 0x48: 71 | game_input('w'); 72 | break; 73 | case 0x50: 74 | game_input('s'); 75 | break; 76 | default: 77 | game_input(key); 78 | } 79 | if(quit) goto end; 80 | } 81 | 82 | msec = get_msec(); 83 | next = update(msec); 84 | } 85 | 86 | end: 87 | cleanup(); 88 | return 0; 89 | } 90 | 91 | void wait_display(void) 92 | { 93 | } 94 | 95 | int init(void) 96 | { 97 | term_width = 80; 98 | term_height = 25; 99 | 100 | init_timer(); 101 | 102 | if(init_game() == -1) { 103 | return -1; 104 | } 105 | return 0; 106 | } 107 | 108 | void cleanup(void) 109 | { 110 | cleanup_game(); 111 | cleanup_timer(); 112 | } 113 | 114 | int parse_args(int argc, char **argv) 115 | { 116 | int i; 117 | 118 | progpath = argv[0]; 119 | 120 | for(i=1; i// also moves the block left, right, or down\n"); 187 | printf(" , , or rotates the block\n"); 188 | printf(" , , or <0> drops the block immediately\n"); 189 | printf("

pauses and unpauses the game\n"); 190 | printf(" , or starts a new game\n"); 191 | printf(" or hitting escape twice, quits immediately\n"); 192 | printf(" shows or hides the help panel which lists these keybindings.\n"); 193 | printf(" shows or hides the high score table.\n"); 194 | } 195 | 196 | long get_msec(void) 197 | { 198 | return timer_ticks * 1000 / 18; 199 | } 200 | -------------------------------------------------------------------------------- /doc/softfonts.md: -------------------------------------------------------------------------------- 1 | Softfonts notes 2 | =============== 3 | 4 | DEC terminals from VT220 and up support custom character set downloads into the 5 | DRCS buffer (Dynamically Redefinable Character Set), with the DECDLD command. 6 | There are three different downloadable character formats for each generation of 7 | DEC terminals, VT2x0, VT3x0, and VT4x0/VT5x0 (considering only 80x24 modes, 8 | otherwise there are more variations still). 9 | 10 | EGA and VGA PC graphics cards support custom characters to be uploaded into 11 | video RAM, either directly, or with a video BIOS call (int 10h/ax=1110h). 12 | 13 | 14 | DEC downloadable character definition 15 | ------------------------------------- 16 | The following are the maximum character matrix sizes for each generation of DEC 17 | terminals in 80x24 mode: 18 | 19 | - VT2x0: 8x10 20 | - VT3x0: 15x12 with the displayed character being stretched 3x vertically. 21 | - VT4x0/VT5x0: 10x16 22 | 23 | Character bitmaps are converted to sixels, and loaded with the DECDLD command. 24 | The DECDLD on a VT200-series starts with `ESC P`, followed by a number of 25 | parameters: 26 | 27 | font no;first char;erase mode;matrix size/width;columns;text/full { 28 | 29 | followed by the desired character set "name" (which is usually " @", followed by 30 | the sixels for a series of characters, and closed by the terminator `ESC \`. 31 | 32 | VT300 and above adds a few more parameters before the curly brace: 33 | 34 | matrix height;charset size 35 | 36 | - *Font number* is either 0 or 1, but is completely ignored either way. 37 | - *First char* can be a number from 1 to 94, corresponding to characters 38 | '!' (21h) to '~' (7eh) for charset size of 94, or 0 to 95, corresponding to 39 | "space" (20h) up to "delete" (7fh) for charset size of 96 (not supported on 40 | VT200-series). 41 | - *erase mode* is 0 to erase all characters of the specified 42 | font/size/rendition while downloading, 1 to erase only 43 | the characters which re being replaced, and 2 to erase all characters. 44 | - *matrix size* on the VT200-series is 0 for default (7x10), 2 for 5x10, 3 for 45 | 6x10 and 4 for 7x10. On VT300-series and up, it can be the same, or define 46 | the matrix width (like 15 for the full width of the VT3x0 fonts in 80x24 47 | mode). 48 | - *columns* is 0 (default) or 1 if this is the 80-column version of the font, 49 | or 2 if it's intended for the 132-column mode. Beyond the VT200-series, 50 | there are more values to also specify the rows, while the previous ones are 51 | considered to specify 24-row modes. 11 is for 80x36, 12 for 132x36, 21 for 52 | 80x48, and 22 for 132x48. 53 | - *text/full* define wether the downloaded characters are meant for text (0 or 54 | 1), and the terminal needs to leave empty space around them and center them 55 | accordingly, or for graphics (2), in which case the whole matrix is available, 56 | and nearby characters are adjacent with no gap. The VT200-series in 80x24 57 | mode does not support "full" to be specified here. 58 | - *matrix height* can be any value from 1 to 12 on the VT300-series or 16 on 59 | the VT400 and above. 60 | - *charset size* is 0 for 94-character fonts (21h to 7eh), or 1 for 61 | 96-character fonts (20h to 7fh). 62 | 63 | ### Sixel data 64 | 65 | Bitmaps are encoded as sixels. The bitmap is split into horizontal bands each 6 66 | scanlines tall. Each column of each band is encoded into a sixel, a printable 67 | character corresponding to the bit pattern of that column, and those sixels are 68 | transmitted one after the other. After the whole band of 6-pixel columns is 69 | transmitted, a `/` is used as a separator, and the next 6-tall band is encoded 70 | and transmitted in the same way, until the whole height of the bitmap is 71 | exhausted. 72 | 73 | The pixel-column sixel encoding is done by first padding any missing high order 74 | bits with 0 if there are fewer than 6 scanlines in it (this happens in the last 75 | band, if the bitmap height is not a multiple of 6). Pixels at the bottom of the 76 | column correspond to the high order bits of the bit pattern, and the top of the 77 | column to low order bits. The value of the bit pattern is adjusted into the 78 | printable range by adding 63 to it, so `000000` corresponds to `?`, and `111111` 79 | corresponds to `~`. 80 | 81 | Here's a table for easy conversion between bit patterns (octal) and characters: 82 | 83 | 00 ? 20 O 40 _ 60 o 84 | 01 @ 21 P 41 ` 61 p 85 | 02 A 22 Q 42 a 62 q 86 | 03 B 23 R 43 b 63 r 87 | 04 C 24 S 44 c 64 s 88 | 05 D 25 T 45 d 65 t 89 | 06 E 26 U 46 e 66 u 90 | 07 F 27 V 47 f 67 v 91 | 10 G 30 W 50 g 70 w 92 | 11 H 31 X 51 h 71 x 93 | 12 I 32 Y 52 i 72 y 94 | 13 J 33 Z 53 j 73 z 95 | 14 K 34 [ 54 k 74 { 96 | 15 L 35 \ 55 l 75 97 | 16 M 36 ] 56 m 76 } 98 | 17 N 37 ^ 57 n 77 ~ 99 | 100 | -------------------------------------------------------------------------------- /src/dos/scoredb.c: -------------------------------------------------------------------------------- 1 | /* 2 | Termtris - a tetris game for ANSI/VT100 terminals 3 | Copyright (C) 2019-2023 John Tsiombikas 4 | 5 | This program is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | This program is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with this program. If not, see . 17 | */ 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include "scoredb.h" 23 | #include "term.h" 24 | 25 | void ansi_recall(void); 26 | void ansi_reset(void); 27 | void ansi_clearscr(void); 28 | void ansi_setcursor(int row, int col); 29 | void ansi_cursor(int show); 30 | void ansi_setcolor(int fg, int bg); 31 | void ansi_ibmchar(unsigned char c, unsigned char attr); 32 | 33 | 34 | #define SCORES_OFFS (((long)scores - 256) & 0xffff) 35 | 36 | static int name_dialog(char *buf); 37 | 38 | extern const char *progpath; 39 | 40 | #define NUM_SCORES 10 41 | static struct score_entry scores[NUM_SCORES] = {{"\0dummy"}}; 42 | 43 | struct score_entry *read_scores(FILE *fp, int max_scores) 44 | { 45 | int i; 46 | 47 | if(!fp) { 48 | if(!(fp = fopen(progpath, "r+b"))) { 49 | return 0; 50 | } 51 | } 52 | fseek(fp, SCORES_OFFS, SEEK_SET); 53 | fread(scores, 1, sizeof scores, fp); 54 | fclose(fp); 55 | 56 | if(!*scores[0].user) { 57 | return 0; 58 | } 59 | 60 | for(i=0; iscore > scores[i].score) { 79 | idx = i; 80 | break; 81 | } 82 | } 83 | 84 | if(idx == -1) return 0; 85 | rest = NUM_SCORES - idx - 1; 86 | 87 | if(idx > 0) { 88 | memcpy(newsc, scores, idx * sizeof *newsc); 89 | } 90 | if(rest > 0) { 91 | memcpy(newsc + idx + 1, scores + idx, rest * sizeof *newsc); 92 | } 93 | 94 | /* get name */ 95 | name_dialog(sc->user); 96 | if(!*sc->user) { 97 | strcpy(sc->user, "dosuser"); 98 | } 99 | newsc[idx] = *sc; 100 | 101 | /* save the new scores table */ 102 | if(!(fp = fopen(progpath, "r+b"))) { 103 | return -1; 104 | } 105 | fseek(fp, SCORES_OFFS, SEEK_SET); 106 | fwrite(newsc, 1, sizeof newsc, fp); 107 | fclose(fp); 108 | 109 | /* update the next pointers */ 110 | for(i=1; i<10; i++) { 111 | if(*newsc[i].user) { 112 | newsc[i-1].next = newsc + i; 113 | } else { 114 | newsc[i-1].next = 0; 115 | } 116 | } 117 | newsc[9].next = 0; 118 | 119 | return 0; 120 | } 121 | 122 | int print_scores(int num) 123 | { 124 | int i; 125 | struct score_entry *sc = scores; 126 | /*unsigned long offs = ((long)scores - 256) & 0xffff; 127 | 128 | printf("calculated offset %lx (from: %p)\n", offs, (void*)scores); 129 | printf("actual address: %p\n", (void*)scores);*/ 130 | 131 | for(i=0; iuser) break; 133 | 134 | printf("%2d. %s - %ld pts (%ld lines)\n", i + 1, sc->user, sc->score, 135 | sc->lines); 136 | sc++; 137 | } 138 | return 0; 139 | } 140 | 141 | #define DLG_W (MAX_NAME + 2) 142 | #define DLG_X ((80 - DLG_W) / 2) 143 | #define ATTR 7 144 | #define VALID(x) (isalnum(x) || (x) == ' ' || (x) == '_' || (x) == '-') 145 | 146 | static int name_dialog(char *buf) 147 | { 148 | int i, j, key, cur; 149 | char *s; 150 | 151 | term_setcursor(8, DLG_X); 152 | term_ibmchar(G_UL_CORNER, ATTR); 153 | for(i=1; i 0) { 198 | buf[--cur] = 0; 199 | } 200 | break; 201 | 202 | default: 203 | if(VALID(key) && cur < MAX_NAME - 1) { 204 | buf[cur++] = key; 205 | buf[cur] = 0; 206 | } 207 | } 208 | 209 | s = buf; 210 | term_setcursor(9, DLG_X + 1); 211 | for(i=0; i 4 | 5 | This program is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | This program is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with this program. If not, see . 17 | */ 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include "game.h" 25 | #include "scoredb.h" 26 | 27 | int init(void); 28 | void cleanup(void); 29 | int parse_args(int argc, char **argv); 30 | void print_usage(const char *argv0); 31 | long get_msec(void); 32 | void sighandler(int s); 33 | 34 | static const char *termfile; 35 | static long start_time; 36 | 37 | extern int no_autogfx; /* defined in ansi.c */ 38 | extern char *username; /* defined in scoredb.c */ 39 | 40 | 41 | int main(int argc, char **argv) 42 | { 43 | long msec, next; 44 | static unsigned char buf[128]; 45 | 46 | if(parse_args(argc, argv) == -1) { 47 | return 1; 48 | } 49 | 50 | if(init() == -1) { 51 | return 1; 52 | } 53 | 54 | start_time = GetTickCount(); 55 | 56 | for(;;) { 57 | /* 58 | if(res > 0) { 59 | if(FD_ISSET(0, &rdset)) { 60 | int rd = read(0, buf, sizeof buf); 61 | for(i=0; i: terminal device (default: /dev/tty)\n"); 193 | printf(" -b: use bell for sound cues (default: off)\n"); 194 | printf(" -m: monochrome output (default: off)\n"); 195 | printf(" -g: use custom block graphics (default: auto)\n"); 196 | printf(" -T: don't use custom block graphics, inhibit auto-detection\n"); 197 | printf(" -a: use only ASCII characters\n"); 198 | printf(" -u : override username for high scores\n"); 199 | printf(" -r: reverse (counter-clockwise) rotation\n"); 200 | printf(" -s: print top 10 high-scores and exit\n"); 201 | printf(" -h: print usage information and exit\n"); 202 | printf("Controls:\n"); 203 | printf(" left/right/down arrow key moves the block left, right, or down\n"); 204 | printf(" // also moves the block left, right, or down\n"); 205 | printf(" , , or rotates the block\n"); 206 | printf(" , , or <0> drops the block immediately\n"); 207 | printf("

pauses and unpauses the game\n"); 208 | printf(" , or starts a new game\n"); 209 | printf(" or hitting escape twice, quits immediately\n"); 210 | printf(" shows or hides the help panel which lists these keybindings.\n"); 211 | printf(" shows or hides the high score table.\n"); 212 | } 213 | 214 | long get_msec(void) 215 | { 216 | return GetTickCount() - start_time; 217 | } 218 | -------------------------------------------------------------------------------- /src/win/scoredb.c: -------------------------------------------------------------------------------- 1 | /* 2 | Termtris - a tetris game for ANSI/VT100 terminals 3 | Copyright (C) 2019-2023 John Tsiombikas 4 | 5 | This program is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | This program is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with this program. If not, see . 17 | */ 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include "scoredb.h" 25 | 26 | #ifdef SCOREDIR 27 | #define SCOREDB_PATH SCOREDIR "/scores" 28 | #else 29 | #define SCOREDB_PATH "scores" 30 | #endif 31 | 32 | static void write_score(FILE *fp, struct score_entry *s); 33 | static int parse_score(char *buf, struct score_entry *ent); 34 | static void free_list(struct score_entry *s); 35 | 36 | char *username; 37 | 38 | 39 | struct score_entry *read_scores(FILE *fp, int max_scores) 40 | { 41 | char buf[128]; 42 | struct score_entry *node, *head = 0, *tail = 0; 43 | int close_file = 0; 44 | 45 | /* 46 | if(!fp) { 47 | if(!(fp = fopen(SCOREDB_PATH, "rb"))) { 48 | return 0; 49 | } 50 | close_file = 1; 51 | 52 | flk.l_type = F_RDLCK; 53 | flk.l_start = flk.l_len = 0; 54 | flk.l_whence = SEEK_SET; 55 | while(fcntl(fileno(fp), F_SETLKW, &flk) == -1); 56 | } 57 | */ 58 | if(max_scores <= 0) max_scores = INT_MAX; 59 | 60 | while(max_scores-- > 0 && fgets(buf, sizeof buf, fp)) { 61 | struct score_entry ent; 62 | if(parse_score(buf, &ent) == -1) { 63 | continue; 64 | } 65 | 66 | if(!(node = malloc(sizeof *node)) || !(node->user = malloc(strlen(ent.user) + 1))) { 67 | perror("failed to allocate scorelist"); 68 | free(node); 69 | free_list(head); 70 | return 0; 71 | } 72 | strcpy(node->user, ent.user); 73 | node->score = ent.score; 74 | node->lines = ent.lines; 75 | node->level = ent.level; 76 | node->next = 0; 77 | 78 | if(!head) { 79 | head = tail = node; 80 | } else { 81 | tail->next = node; 82 | tail = node; 83 | } 84 | } 85 | 86 | if(close_file) { 87 | /* 88 | flk.l_type = F_UNLCK; 89 | flk.l_start = flk.l_len = 0; 90 | flk.l_whence = SEEK_SET; 91 | fcntl(fileno(fp), F_SETLK, &flk); 92 | */ 93 | fclose(fp); 94 | } 95 | 96 | return head; 97 | } 98 | 99 | int save_score(struct score_entry *sc) 100 | { 101 | int count; 102 | FILE *fp; 103 | struct score_entry *slist, *sptr; 104 | struct score_entry newscore; 105 | 106 | struct passwd *pw; 107 | 108 | if(!username) { 109 | /* 110 | if(!(pw = getpwuid(getuid()))) { 111 | perror("save_score: failed to retrieve user information"); 112 | return -1; 113 | } 114 | username = pw->pw_name; 115 | */ 116 | username = "user"; 117 | } 118 | newscore.user = username; 119 | newscore.score = sc->score; 120 | newscore.lines = sc->lines; 121 | newscore.level = sc->level; 122 | 123 | if(!(fp = fopen(SCOREDB_PATH, "r+"))) { 124 | fclose(fp); 125 | fprintf(stderr, "failed to save scores to %s: %s\n", SCOREDB_PATH, strerror(errno)); 126 | return -1; 127 | } 128 | 129 | /* lock the file */ 130 | /* 131 | flk.l_type = F_WRLCK; 132 | flk.l_start = flk.l_len = 0; 133 | flk.l_whence = SEEK_SET; 134 | while(fcntl(fd, F_SETLKW, &flk) == -1); 135 | */ 136 | 137 | slist = read_scores(fp, 0); 138 | 139 | rewind(fp); 140 | 141 | count = 0; 142 | sptr = slist; 143 | while(sptr && sptr->score >= sc->score && count++ < 100) { 144 | write_score(fp, sptr); 145 | sptr = sptr->next; 146 | } 147 | if(count++ < 100) { 148 | write_score(fp, &newscore); 149 | sc->user = newscore.user; 150 | } 151 | while(sptr && count++ < 100) { 152 | write_score(fp, sptr); 153 | sptr = sptr->next; 154 | } 155 | fflush(fp); 156 | 157 | /* unlock the file */ 158 | /* 159 | flk.l_type = F_UNLCK; 160 | flk.l_start = flk.l_len = 0; 161 | flk.l_whence = SEEK_SET; 162 | fcntl(fd, F_SETLK, &flk); 163 | */ 164 | 165 | free_list(slist); 166 | fclose(fp); 167 | return 0; 168 | } 169 | 170 | static void write_score(FILE *fp, struct score_entry *s) 171 | { 172 | fprintf(fp, "%s %ld/%ld/%ld\n", s->user, s->score, s->lines, s->level); 173 | } 174 | 175 | static char *skip_space(char *s) 176 | { 177 | if(!s) return 0; 178 | while(*s && isspace(*s)) s++; 179 | return *s ? s : 0; 180 | } 181 | 182 | static int parse_score(char *buf, struct score_entry *ent) 183 | { 184 | char *userptr, *scoreptr; 185 | long scores[3]; 186 | 187 | if(!(userptr = skip_space(buf))) return -1; 188 | scoreptr = userptr; 189 | while(*scoreptr && !isspace(*scoreptr)) scoreptr++; 190 | if(!*scoreptr) return -1; 191 | 192 | *scoreptr = 0; 193 | if(!(scoreptr = skip_space(scoreptr + 1))) return -1; 194 | 195 | if(sscanf(scoreptr, "%ld/%ld/%ld", scores, scores + 1, scores + 2) != 3) { 196 | return -1; 197 | } 198 | 199 | ent->user = userptr; 200 | ent->score = scores[0]; 201 | ent->lines = scores[1]; 202 | ent->level = scores[2]; 203 | return 0; 204 | } 205 | 206 | static void free_list(struct score_entry *s) 207 | { 208 | while(s) { 209 | struct score_entry *tmp = s; 210 | s = s->next; 211 | free(tmp->user); 212 | free(tmp); 213 | } 214 | } 215 | 216 | int print_scores(int num) 217 | { 218 | int idx; 219 | struct score_entry *sc; 220 | 221 | if(!(sc = read_scores(0, num))) { 222 | fprintf(stderr, "no high-scores found\n"); 223 | return -1; 224 | } 225 | 226 | idx = 0; 227 | while(sc) { 228 | printf("%2d. %s - %ld pts (%ld lines)\n", ++idx, sc->user, sc->score, sc->lines); 229 | sc = sc->next; 230 | } 231 | 232 | return 0; 233 | } 234 | -------------------------------------------------------------------------------- /src/unix/scoredb.c: -------------------------------------------------------------------------------- 1 | /* 2 | Termtris - a tetris game for ANSI/VT100 terminals 3 | Copyright (C) 2019-2023 John Tsiombikas 4 | 5 | This program is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | This program is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with this program. If not, see . 17 | */ 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include "scoredb.h" 28 | 29 | #ifdef SCOREDIR 30 | #define SCOREDB_PATH SCOREDIR "/scores" 31 | #else 32 | #define SCOREDB_PATH "scores" 33 | #endif 34 | 35 | static void write_score(FILE *fp, struct score_entry *s); 36 | static int parse_score(char *buf, struct score_entry *ent); 37 | static void free_list(struct score_entry *s); 38 | 39 | char *username; 40 | 41 | 42 | struct score_entry *read_scores(FILE *fp, int max_scores) 43 | { 44 | char buf[128]; 45 | struct score_entry *node, *head = 0, *tail = 0; 46 | struct flock flk; 47 | int close_file = 0; 48 | 49 | if(!fp) { 50 | if(!(fp = fopen(SCOREDB_PATH, "rb"))) { 51 | return 0; 52 | } 53 | close_file = 1; 54 | 55 | flk.l_type = F_RDLCK; 56 | flk.l_start = flk.l_len = 0; 57 | flk.l_whence = SEEK_SET; 58 | while(fcntl(fileno(fp), F_SETLKW, &flk) == -1); 59 | } 60 | if(max_scores <= 0) max_scores = INT_MAX; 61 | 62 | while(max_scores-- > 0 && fgets(buf, sizeof buf, fp)) { 63 | struct score_entry ent; 64 | if(parse_score(buf, &ent) == -1) { 65 | continue; 66 | } 67 | 68 | if(!(node = malloc(sizeof *node)) || !(node->user = malloc(strlen(ent.user) + 1))) { 69 | perror("failed to allocate scorelist"); 70 | free(node); 71 | free_list(head); 72 | return 0; 73 | } 74 | strcpy(node->user, ent.user); 75 | node->score = ent.score; 76 | node->lines = ent.lines; 77 | node->level = ent.level; 78 | node->next = 0; 79 | 80 | if(!head) { 81 | head = tail = node; 82 | } else { 83 | tail->next = node; 84 | tail = node; 85 | } 86 | } 87 | 88 | if(close_file) { 89 | flk.l_type = F_UNLCK; 90 | flk.l_start = flk.l_len = 0; 91 | flk.l_whence = SEEK_SET; 92 | fcntl(fileno(fp), F_SETLK, &flk); 93 | fclose(fp); 94 | } 95 | 96 | return head; 97 | } 98 | 99 | int save_score(struct score_entry *sc) 100 | { 101 | int fd, count; 102 | FILE *fp; 103 | struct score_entry *slist, *sptr; 104 | struct score_entry newscore; 105 | 106 | struct passwd *pw; 107 | struct flock flk; 108 | 109 | if(!username) { 110 | if(!(pw = getpwuid(getuid()))) { 111 | perror("save_score: failed to retrieve user information"); 112 | return -1; 113 | } 114 | username = pw->pw_name; 115 | } 116 | newscore.user = username; 117 | newscore.score = sc->score; 118 | newscore.lines = sc->lines; 119 | newscore.level = sc->level; 120 | 121 | if((fd = open(SCOREDB_PATH, O_RDWR | O_CREAT, 0666)) == -1 || !(fp = fdopen(fd, "r+"))) { 122 | close(fd); 123 | fprintf(stderr, "failed to save scores to %s: %s\n", SCOREDB_PATH, strerror(errno)); 124 | return -1; 125 | } 126 | 127 | /* lock the file */ 128 | flk.l_type = F_WRLCK; 129 | flk.l_start = flk.l_len = 0; 130 | flk.l_whence = SEEK_SET; 131 | while(fcntl(fd, F_SETLKW, &flk) == -1); 132 | 133 | slist = read_scores(fp, 0); 134 | 135 | rewind(fp); 136 | 137 | count = 0; 138 | sptr = slist; 139 | while(sptr && sptr->score >= sc->score && count++ < 100) { 140 | write_score(fp, sptr); 141 | sptr = sptr->next; 142 | } 143 | if(count++ < 100) { 144 | write_score(fp, &newscore); 145 | sc->user = newscore.user; 146 | } 147 | while(sptr && count++ < 100) { 148 | write_score(fp, sptr); 149 | sptr = sptr->next; 150 | } 151 | fflush(fp); 152 | 153 | /* unlock the file */ 154 | flk.l_type = F_UNLCK; 155 | flk.l_start = flk.l_len = 0; 156 | flk.l_whence = SEEK_SET; 157 | fcntl(fd, F_SETLK, &flk); 158 | 159 | free_list(slist); 160 | fclose(fp); 161 | return 0; 162 | } 163 | 164 | static void write_score(FILE *fp, struct score_entry *s) 165 | { 166 | fprintf(fp, "%s %ld/%ld/%ld\n", s->user, s->score, s->lines, s->level); 167 | } 168 | 169 | static char *skip_space(char *s) 170 | { 171 | if(!s) return 0; 172 | while(*s && isspace(*s)) s++; 173 | return *s ? s : 0; 174 | } 175 | 176 | static int parse_score(char *buf, struct score_entry *ent) 177 | { 178 | char *userptr, *scoreptr; 179 | long scores[3]; 180 | 181 | if(!(userptr = skip_space(buf))) return -1; 182 | scoreptr = userptr; 183 | while(*scoreptr && !isspace(*scoreptr)) scoreptr++; 184 | if(!*scoreptr) return -1; 185 | 186 | *scoreptr = 0; 187 | if(!(scoreptr = skip_space(scoreptr + 1))) return -1; 188 | 189 | if(sscanf(scoreptr, "%ld/%ld/%ld", scores, scores + 1, scores + 2) != 3) { 190 | return -1; 191 | } 192 | 193 | ent->user = userptr; 194 | ent->score = scores[0]; 195 | ent->lines = scores[1]; 196 | ent->level = scores[2]; 197 | return 0; 198 | } 199 | 200 | static void free_list(struct score_entry *s) 201 | { 202 | while(s) { 203 | struct score_entry *tmp = s; 204 | s = s->next; 205 | free(tmp->user); 206 | free(tmp); 207 | } 208 | } 209 | 210 | int print_scores(int num) 211 | { 212 | int idx; 213 | struct score_entry *sc; 214 | 215 | if(!(sc = read_scores(0, num))) { 216 | fprintf(stderr, "no high-scores found\n"); 217 | return -1; 218 | } 219 | 220 | idx = 0; 221 | while(sc) { 222 | printf("%2d. %s - %ld pts (%ld lines)\n", ++idx, sc->user, sc->score, sc->lines); 223 | sc = sc->next; 224 | } 225 | 226 | return 0; 227 | } 228 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Termtris - tetris game for terminals 2 | ==================================== 3 | 4 | ![shots](http://nuclear.mutantstargoat.com/sw/termtris/img/termtris-banner.png) 5 | 6 | About 7 | ----- 8 | 9 | Termtris is a text-based tetris game, initially written for UNIX systems and 10 | ANSI/VT100-compatible terminals, but later extended to support more terminal 11 | types, operating systems, and text-mode video hardware. 12 | 13 | I always liked the GameBoy tetris best. So I made termtris as close as possible 14 | to that version gameplay-wise. 15 | 16 | Termtris can run in color or monochrome mode, and can load custom character sets 17 | for graphical blocks, if there is support for that. 18 | 19 | There are two main flavors of termtris which can be compiled from the same 20 | source tree: 21 | 22 | 1. The original **UNIX version** which runs on terminals, and can also be 23 | compiled for windows using a POSIX emulation layer such as *msys2* or 24 | *cygwin*. It has been tested on multiple UNIX systems (GNU/Linux, FreeBSD, IRIX, 25 | Solaris, MacOSX), and multiple terminals (VT52, VT100, VT220, VT420, 26 | MS 6102), and terminal emulators (xterm, linux console, kermit, iTerm2). 27 | 28 | 2. The **DOS version** which uses the PC BIOS to access display and keyboard, 29 | but can also optionally work with the same terminals supported by the UNIX 30 | version. 31 | 32 | ### Links 33 | - website: http://nuclear.mutantstargoat.com/sw/termtris 34 | - source repository: https://github.com/jtsiomb/termtris 35 | 36 | 37 | Controls 38 | -------- 39 | - `A` or *left arrow* moves the block left. 40 | - `D` or *right arrow* moves the block right. 41 | - `S` or *down arrow* drops the block faster. 42 | - `W`, *up arrow*, or *space* rotates the block. 43 | - *enter*, *tab*, or `0` drops the block immediately. 44 | - `P` pauses and unpauses the game. 45 | - *backspace*, or *delete* starts a new game. 46 | - `Q` or hitting escape twice, quits immediately. 47 | - `H` shows or hides the help panel which lists these keybindings. 48 | - `R` shows or hides the high score table. 49 | 50 | Additionally on GNU/Linux systems any joystick can be used to control the game. 51 | The first available joystick is used automatically, or you can specify a 52 | joystick device with the `-j` commandline option. If you'd rather disable 53 | joystick control, use the option `-j /dev/null`. 54 | 55 | When using a joystick, even axes are mapped to left/right, Odd axes are mapped 56 | to up/down, the first four buttons rotate, and the rest of the buttons pause 57 | and unpause. 58 | 59 | There is no way to remap the controls without changing the source code at this 60 | time. 61 | 62 | 63 | Custom character set 64 | -------------------- 65 | For terminals with support for custom loadable character sets, you can instruct 66 | termtris to use graphical blocks instead of the bracket characters with the `-g` 67 | command-line argument. If `TERM` is set to a known supported terminal, graphical 68 | blocks are enabled automatically, use the `-T` option to disable and inhibit 69 | auto-detection. 70 | 71 | Different terminals have different capabilities when it comes to soft character 72 | sets. Termtris will try to detect the terminal type and choose the correct soft 73 | font to load, but if it fails or you want to force a different terminal type, 74 | make sure to define the `TERM` environent variable. If `TERM` is undefined, or 75 | an unknown or unsupported terminal type, termtris will try to interrogate the 76 | terminal itself about its capabilities. 77 | 78 | The DOS version of termtris will automatically enable graphical blocks when it 79 | detects an EGA or VGA/SVGA graphics card. Use `-g` or `-T` to enable or disable 80 | it explicitly. 81 | 82 | 83 | License 84 | ------- 85 | Copyright (C) 2019-2023 John Tsiombikas 86 | 87 | This program is free software. Feel free to use, modify, and/or redistribute it 88 | under the terms of the GNU General Public License version 3, or at your option, 89 | any later version published by the Free Software Foundation. See COPYING for 90 | details. 91 | 92 | 93 | Download 94 | -------- 95 | ### Source code 96 | Current release (1.9): http://nuclear.mutantstargoat.com/sw/termtris/termtris-1.9.tar.gz 97 | 98 | Releases are also mirrored on github: https://github.com/jtsiomb/termtris/releases 99 | 100 | Alternatively you may clone the latest version of the source code from the git 101 | repository: 102 | 103 | git clone https://github.com/jtsiomb/termtris 104 | 105 | ### Pre-compiled 106 | Building from source is highly recommended. If however you want to try a 107 | pre-compiled binary, you might be able to find something for your system in: 108 | http://nuclear.mutantstargoat.com/sw/termtris/bin 109 | Not all releases are compiled for all platforms. Any binaries available here are 110 | provided purely for convenience. Do not open bug reports about a pre-compiled 111 | binary not running, or misbehaving on your system; compile it yourself first. 112 | 113 | Another source of binaries, is the github automated builds. Head over to the 114 | "actions" page (https://github.com/jtsiomb/termtris/actions), choose the latest 115 | successful build for your platform, and download the binary from the "artifacts" 116 | section at the bottom. 117 | 118 | Pre-compiled binaries might also be available in your package management system of 119 | choice, but please do not submit any bug reports without first trying to build 120 | termtris yourself from the latest git source code. 121 | - Debian/Ubuntu/Mint: `apt-get install termtris` 122 | 123 | The latest pre-compiled DOS binary (currently 1.9) is always available at: 124 | http://nuclear.mutantstargoat.com/sw/termtris/termtris.com 125 | 126 | Build (UNIX) 127 | ------------ 128 | There are no external dependencies. Simply type `make` to build, and `make 129 | install` if you wish to install termtris. 130 | 131 | Default installation prefix is `/usr/local`. Change the first line of the 132 | `Makefile` if you'd rather install somewhere else, or simply invoke the install 133 | rule like so: 134 | 135 | make PREFIX=/some/other/prefix install 136 | 137 | If you don't have GNU make and GCC installed, or you'd rather use the default 138 | make utility and C compiler on your UNIX system, you can use `Makefile.sgi`: 139 | 140 | make -f Makefile.sgi 141 | 142 | (originally included for building on SGI IRIX with MIPSPro and the native make). 143 | 144 | 145 | Build (DOS) 146 | ----------- 147 | To build the DOS version of termtris you'll need some version of the Watcom or 148 | OpenWatcom C compiler, and the NASM assembler: 149 | 150 | make -f Makefile.wat 151 | -------------------------------------------------------------------------------- /src/ansi.c: -------------------------------------------------------------------------------- 1 | /* 2 | Termtris - a tetris game for ANSI/VT100 terminals 3 | Copyright (C) 2019-2023 John Tsiombikas 4 | 5 | This program is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | This program is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with this program. If not, see . 17 | */ 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include "game.h" 23 | #include "term.h" 24 | 25 | 26 | void ansi_recall(void); 27 | void ansi_reset(void); 28 | void ansi_clearscr(void); 29 | void ansi_setcursor(int row, int col); 30 | void ansi_cursor(int show); 31 | void ansi_setcolor(int fg, int bg); 32 | void ansi_ibmchar(unsigned char c, unsigned char attr); 33 | 34 | int no_autogfx; 35 | 36 | enum { CS_ASCII, CS_GRAPH, CS_CUSTOM }; 37 | 38 | #define GMAP_FIRST 0xb0 39 | #define GMAP_LAST 0xda 40 | 41 | unsigned char gmap[] = { 42 | 0x61, 0x61, 0x61, /* checker */ 43 | 0x78, /* vline */ 44 | 0x75, 0x75, 0x75, /* right T */ 45 | 0x6b, 0x6b, /* upper-right corner */ 46 | 0x75, /* right T */ 47 | 0x78, /* vline */ 48 | 0x6b, /* upper-right corner */ 49 | 0x6a, 0x6a, 0x6a, /* lower-right corner */ 50 | 0x6b, /* upper-right corner */ 51 | 0x6d, /* lower-left corner */ 52 | 0x76, /* bottom T */ 53 | 0x77, /* top T */ 54 | 0x74, /* left T */ 55 | 0x71, /* hline */ 56 | 0x6e, /* cross */ 57 | 0x74, 0x74, /* left T */ 58 | 0x6d, /* lower-left corner */ 59 | 0x6c, /* upper-left corner */ 60 | 0x76, /* bottom T */ 61 | 0x77, /* top T */ 62 | 0x74, /* left T */ 63 | 0x71, /* hline */ 64 | 0x6e, /* cross */ 65 | 0x76, 0x76, /* bottom T */ 66 | 0x77, 0x77, /* top T */ 67 | 0x6d, 0x6d, /* lower-left corner */ 68 | 0x6c, 0x6c, /* upper-left corner */ 69 | 0x6e, 0x6e, /* cross */ 70 | 0x6a, /* lower-right corner */ 71 | 0x6c /* upper-left corner */ 72 | }; 73 | 74 | static unsigned char cmap[] = {0, 4, 2, 6, 1, 5, 3, 7}; 75 | 76 | static unsigned char cur_attr = 0xff; 77 | static int cur_cs = CS_ASCII; 78 | 79 | static const char *sixels8x10[] = { 80 | "~~jvXf\\j/NFADAF@E", 81 | "t~RlZd^@/BEBDF?F?" 82 | }; 83 | static const char *sixels10x16[] = { 84 | "~~f^jVzf^j/~~mXfYtmjY/FFDAHBGBAH", 85 | "VZvNzfZvR@/d\\jTyTmtGA/AHB@IBGB?A" 86 | }; 87 | static const char *sixels15x12[] = { 88 | "~~~~nRNpB^jFxB~/^^^NGN@AN@?N@EK", 89 | "Jf|Jb^bLZzV@@??" "/B@MJ?KF?LFL????" 90 | }; 91 | static char custom_char[] = {"[]"}; 92 | #define NUM_CUSTOM 2 93 | 94 | 95 | void ansi_init(void) 96 | { 97 | int i, val, vtclass = -1; 98 | char buf[64]; 99 | 100 | /* detect the terminal type 101 | * if there is a TERM env var with "vtxxx" where xxx >= 200, enable 102 | * graphical blocks and set vtclass accordingly. 103 | */ 104 | if(termenv && termenv[0] == 'v' && termenv[1] == 't') { 105 | if((val = atoi(termenv + 2)) >= 200 && val < 600 && !no_autogfx) { 106 | use_gfxchar = 1; 107 | } 108 | vtclass = 60 + val / 100; 109 | } 110 | 111 | /* unknown or unset TERM, try asking for the device attributes string */ 112 | if(vtclass == -1) { 113 | char *ptr; 114 | int have_softchar = 0; 115 | 116 | printf("\033[c\n"); 117 | fflush(stdout); 118 | if(fgets(buf, sizeof buf, stdin) && (memcmp(buf, "\033[?", 3) == 0 || 119 | memcmp(buf, "\233?", 2) == 0)) { 120 | ptr = buf + (buf[0] == '\033' ? 3 : 2); 121 | 122 | fprintf(stderr, "term id: %s\n", ptr); 123 | 124 | for(;;) { 125 | switch((val = atoi(ptr))) { 126 | case 7: 127 | have_softchar = 1; 128 | break; 129 | 130 | default: 131 | if(val >= 62 && val < 70) { 132 | /* assume it's a VT class */ 133 | vtclass = val; 134 | } 135 | } 136 | while(*ptr && *ptr != 'c' && *ptr != ';') ptr++; 137 | if(*ptr != ';') break; 138 | ptr++; 139 | } 140 | 141 | if(vtclass != -1) { 142 | /* found a vt class, treat the rest as valid */ 143 | fprintf(stderr, "detected VT class %d [DRCS:%d]\n", 144 | vtclass, have_softchar); 145 | 146 | if(!no_autogfx && have_softchar) { 147 | use_gfxchar = 1; 148 | } 149 | } 150 | } 151 | } 152 | 153 | if(use_gfxchar) { 154 | if(vtclass == -1) { 155 | vtclass = 64; /* default to VT420 */ 156 | fprintf(stderr, "Custom graphics charset enabled, but failed to detect terminal type. Assuming VT420 or higher.\n"); 157 | } 158 | 159 | /* load custom character set */ 160 | fprintf(stderr, "Loading custom character set (VT%dx0) ... \n", vtclass % 10); 161 | printf("Loading custom character set (VT%dx0) ... ", vtclass % 10); 162 | fflush(stdout); 163 | for(i=0; i= GMAP_FIRST && c <= GMAP_LAST) { 246 | if(cur_cs != CS_GRAPH) { 247 | memcpy(ptr, "\033(0", 3); 248 | ptr += 3; 249 | cur_cs = CS_GRAPH; 250 | } 251 | 252 | c = gmap[c - GMAP_FIRST]; 253 | } else if(use_gfxchar && (c == '[' || c == ']')) { 254 | if(cur_cs != CS_CUSTOM) { 255 | memcpy(ptr, "\033( @", 4); 256 | ptr += 4; 257 | cur_cs = CS_CUSTOM; 258 | } 259 | } else { 260 | if(cur_cs != CS_ASCII) { 261 | memcpy(ptr, "\033(B", 3); 262 | ptr += 3; 263 | cur_cs = CS_ASCII; 264 | } 265 | } 266 | 267 | if(monochrome) { 268 | attr &= 0x80; 269 | } 270 | 271 | if(attr != cur_attr) { 272 | int bold = attr & 0x80 ? 1 : 0; 273 | 274 | if(monochrome) { 275 | ptr += sprintf(ptr, "\033[%dm", bold); 276 | } else { 277 | unsigned char bg = cmap[attr & 7]; 278 | unsigned char fg = cmap[(attr >> 4) & 7]; 279 | 280 | ptr += sprintf(ptr, "\033[%d;%d;%dm", bold, fg + 30, bg + 40); 281 | } 282 | cur_attr = attr; 283 | } 284 | 285 | *ptr++ = c; 286 | *ptr = 0; 287 | 288 | fputs(cmd, stdout); 289 | } 290 | -------------------------------------------------------------------------------- /src/unix/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | Termtris - a tetris game for ANSI/VT100 terminals 3 | Copyright (C) 2019-2023 John Tsiombikas 4 | 5 | This program is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | This program is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with this program. If not, see . 17 | */ 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include "game.h" 31 | #include "scoredb.h" 32 | 33 | #ifdef __linux__ 34 | #include 35 | #include 36 | #define USE_JOYSTICK 37 | #endif 38 | 39 | int init(void); 40 | void cleanup(void); 41 | int parse_args(int argc, char **argv); 42 | void print_usage(const char *argv0); 43 | long get_msec(void); 44 | void sighandler(int s); 45 | 46 | static const char *termfile = "/dev/tty"; 47 | static struct termios saved_term; 48 | static struct timeval tv0; 49 | 50 | 51 | #ifdef USE_JOYSTICK 52 | enum { 53 | BN_LEFT = 0x1000000, 54 | BN_RIGHT = 0x2000000, 55 | BN_UP = 0x4000000, 56 | BN_DOWN = 0x8000000 57 | }; 58 | static unsigned int jstate; 59 | static int autorepeat; 60 | 61 | static const char *jsdevfile; 62 | static int jsdev = -1; 63 | 64 | static void read_joystick(void); 65 | static void update_joystick(void); 66 | #endif 67 | 68 | extern int no_autogfx; /* defined in ansi.c */ 69 | extern char *username; /* defined in scoredb.c */ 70 | 71 | 72 | int main(int argc, char **argv) 73 | { 74 | int i, res, maxfd; 75 | long msec, next; 76 | struct timeval tv; 77 | static unsigned char buf[128]; 78 | 79 | if(parse_args(argc, argv) == -1) { 80 | return 1; 81 | } 82 | 83 | if(init() == -1) { 84 | return 1; 85 | } 86 | tcdrain(1); 87 | 88 | gettimeofday(&tv0, 0); 89 | 90 | tv.tv_sec = tick_interval / 1000; 91 | tv.tv_usec = (tick_interval % 1000) * 1000; 92 | 93 | for(;;) { 94 | fd_set rdset; 95 | FD_ZERO(&rdset); 96 | FD_SET(0, &rdset); 97 | maxfd = 0; 98 | 99 | #ifdef USE_JOYSTICK 100 | if(jsdev != -1) { 101 | FD_SET(jsdev, &rdset); 102 | maxfd = jsdev + 1; 103 | } 104 | 105 | if(autorepeat) { 106 | tv.tv_sec = autorepeat <= 2 ? 0 : 0; 107 | tv.tv_usec = autorepeat <= 2 ? 500000 : 50000; 108 | } 109 | #endif 110 | 111 | while((res = select(maxfd + 1, &rdset, 0, 0, &tv)) == -1 && errno == EINTR); 112 | 113 | if(res > 0) { 114 | if(FD_ISSET(0, &rdset)) { 115 | int rd = read(0, buf, sizeof buf); 116 | for(i=0; i 0) { 199 | term_width = winsz.ws_col; 200 | term_height = winsz.ws_row; 201 | } 202 | 203 | #ifdef USE_JOYSTICK 204 | if(jsdev != -1) { 205 | char name[256] = {0}; 206 | if(ioctl(jsdev, JSIOCGNAME(sizeof name), name) != -1) { 207 | fprintf(stderr, "Using joystick %s: %s\n", jsdevfile, name); 208 | } 209 | } 210 | #endif 211 | 212 | signal(SIGWINCH, sighandler); 213 | 214 | if(init_game() == -1) { 215 | return -1; 216 | } 217 | 218 | return 0; 219 | } 220 | 221 | void cleanup(void) 222 | { 223 | cleanup_game(); 224 | tcsetattr(0, TCSAFLUSH, &saved_term); 225 | } 226 | 227 | int parse_args(int argc, char **argv) 228 | { 229 | int i; 230 | 231 | for(i=1; i: terminal device (default: /dev/tty)\n"); 319 | printf(" -b: use bell for sound cues (default: off)\n"); 320 | printf(" -m: monochrome output (default: off)\n"); 321 | #ifdef USE_JOYSTICK 322 | printf(" -j : use joystick device for input\n"); 323 | #endif 324 | printf(" -g: use custom block graphics (default: auto)\n"); 325 | printf(" -T: don't use custom block graphics, inhibit auto-detection\n"); 326 | printf(" -a: use only ASCII characters\n"); 327 | printf(" -d: use dark playfield in color mode (default: white)\n"); 328 | printf(" -u : override username for high scores\n"); 329 | printf(" -r: reverse (counter-clockwise) rotation\n"); 330 | printf(" -s: print top 10 high-scores and exit\n"); 331 | printf(" -h: print usage information and exit\n"); 332 | printf("Controls:\n"); 333 | printf(" left/right/down arrow key moves the block left, right, or down\n"); 334 | printf(" // also moves the block left, right, or down\n"); 335 | printf(" , , or rotates the block\n"); 336 | printf(" , , or <0> drops the block immediately\n"); 337 | printf("

pauses and unpauses the game\n"); 338 | printf(" , or starts a new game\n"); 339 | printf(" or hitting escape twice, quits immediately\n"); 340 | printf(" shows or hides the help panel which lists these keybindings.\n"); 341 | printf(" shows or hides the high score table.\n"); 342 | #ifdef USE_JOYSTICK 343 | printf(" This build also accepts joystick input (-j /dev/null to disable).\n"); 344 | #endif 345 | } 346 | 347 | long get_msec(void) 348 | { 349 | struct timeval tv; 350 | 351 | gettimeofday(&tv, 0); 352 | 353 | return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000; 354 | } 355 | 356 | void sighandler(int s) 357 | { 358 | struct winsize winsz; 359 | 360 | signal(s, sighandler); 361 | 362 | switch(s) { 363 | case SIGWINCH: 364 | ioctl(1, TIOCGWINSZ, &winsz); 365 | term_width = winsz.ws_col; 366 | term_height = winsz.ws_row; 367 | game_input('`'); /* redraw */ 368 | break; 369 | 370 | default: 371 | break; 372 | } 373 | } 374 | 375 | #ifdef USE_JOYSTICK 376 | static void read_joystick(void) 377 | { 378 | struct js_event ev; 379 | unsigned int dir; 380 | 381 | while(read(jsdev, &ev, sizeof ev) > 0) { 382 | if(ev.type & JS_EVENT_AXIS) { 383 | int axis = ev.number & 1; 384 | int val = abs(ev.value) < 32587 ? 0 : ev.value; 385 | 386 | if(axis == 0) { 387 | if(val) { 388 | dir = val > 0 ? BN_RIGHT : BN_LEFT; 389 | } else { 390 | dir = BN_LEFT | BN_RIGHT; 391 | } 392 | } else { 393 | if(val) { 394 | dir = val > 0 ? BN_DOWN : BN_UP; 395 | if(dir == BN_UP) { 396 | game_input('\n'); 397 | } 398 | } else { 399 | dir = BN_DOWN | BN_UP; 400 | } 401 | } 402 | 403 | if(val) { 404 | jstate |= dir; 405 | } else { 406 | jstate &= ~dir; 407 | } 408 | } 409 | if(ev.type & JS_EVENT_BUTTON) { 410 | if(ev.value) { 411 | if(ev.number >= 4) { 412 | game_input('p'); 413 | } else { 414 | game_input('w'); 415 | } 416 | } 417 | } 418 | } 419 | 420 | autorepeat = jstate ? 1 : 0; 421 | } 422 | 423 | static void update_joystick(void) 424 | { 425 | if(jstate) autorepeat++; 426 | 427 | if(jstate & BN_LEFT) { 428 | game_input('a'); 429 | } 430 | if(jstate & BN_RIGHT) { 431 | game_input('d'); 432 | } 433 | if(jstate & BN_DOWN) { 434 | game_input('s'); 435 | } 436 | if(jstate & 0x3) { 437 | game_input('w'); 438 | } 439 | } 440 | #endif 441 | -------------------------------------------------------------------------------- /src/game.c: -------------------------------------------------------------------------------- 1 | /* 2 | Termtris - a tetris game for ANSI/VT100 terminals 3 | Copyright (C) 2019-2023 John Tsiombikas 4 | 5 | This program is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | This program is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with this program. If not, see . 17 | */ 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include "game.h" 24 | #include "pieces.h" 25 | #include "term.h" 26 | #include "scoredb.h" 27 | 28 | 29 | int quit; 30 | long tick_interval; 31 | int use_bell; 32 | int monochrome; 33 | int use_gfxchar; 34 | int onlyascii; 35 | int blackpf; 36 | int rotstep = 1; 37 | int term_width, term_height; 38 | 39 | 40 | enum { ERASE_PIECE, DRAW_PIECE }; 41 | 42 | /* dimensions of the whole screen */ 43 | #define SCR_ROWS 20 44 | #define SCR_COLS 20 45 | 46 | /* dimensions of the playfield */ 47 | #define PF_ROWS 18 48 | #define PF_COLS 10 49 | /* offset of the playfield from the left side of the screen */ 50 | #define PF_XOFFS 2 51 | #define PF_YOFFS 0 52 | 53 | #define CHAR(c, fg, bg) \ 54 | ((uint16_t)(c) | ((uint16_t)(fg) << 12) | ((uint16_t)(bg) << 8)) 55 | 56 | int scr[SCR_COLS * SCR_ROWS]; 57 | 58 | static void update_cur_piece(void); 59 | static void addscore(int nlines); 60 | static void print_numbers(void); 61 | static void print_help(void); 62 | static void print_slist(void); 63 | static void full_redraw(void); 64 | static int spawn(void); 65 | static int collision(int piece, const int *pos); 66 | static void stick(int piece, const int *pos); 67 | static void erase_completed(void); 68 | static void draw_piece(int piece, const int *pos, int rot, int mode); 69 | static void clear(void); 70 | static void drawbg(void); 71 | static void drawpf(int start_row); 72 | static void draw_line(int row, int blink); 73 | static void wrtile(int tileid); 74 | 75 | 76 | static int pos[2], next_pos[2]; 77 | static int cur_piece, next_piece, prev_piece; 78 | static int cur_rot, prev_rot; 79 | static int complines[4]; 80 | static int num_complines; 81 | static int gameover; 82 | static int pause; 83 | static int just_spawned; 84 | static int show_help, show_highscores; 85 | 86 | static struct score_entry *scores; 87 | static struct score_entry cur_score; 88 | 89 | static int term_xoffs = 20, term_yoffs = 0; /* TODO detect terminal size to set offsets */ 90 | 91 | static const int preview_pos[] = {13, 13}; 92 | 93 | enum { 94 | TILE_BLACK, 95 | TILE_PF, 96 | TILE_PFSEP, 97 | TILE_GAMEOVER, 98 | TILE_LPIECE, 99 | TILE_JPIECE, 100 | TILE_IPIECE, 101 | TILE_OPIECE, 102 | TILE_ZPIECE, 103 | TILE_SPIECE, 104 | TILE_TPIECE, 105 | TILE_FRM_TL, 106 | TILE_FRM_TR, 107 | TILE_FRM_BL, 108 | TILE_FRM_BR, 109 | TILE_FRM_LTEE, 110 | TILE_FRM_RTEE, 111 | TILE_FRM_HLINE, 112 | TILE_FRM_LVLINE, 113 | TILE_FRM_RVLINE 114 | }; 115 | #define FIRST_PIECE_TILE TILE_LPIECE 116 | 117 | static uint16_t tiles[][2] = { 118 | { CHAR(' ', BLACK, BLACK), CHAR(' ', BLACK, BLACK) }, /* black tile */ 119 | { CHAR(' ', WHITE, WHITE), CHAR(' ', WHITE, WHITE) }, /* playfield background */ 120 | { CHAR(G_CHECKER, WHITE, BLACK), CHAR(G_CHECKER, WHITE, BLACK) }, /* well separator */ 121 | { CHAR(G_CROSS, RED, BLACK), CHAR(G_CROSS, RED, BLACK) }, /* gameover fill */ 122 | { CHAR('[', WHITE, YELLOW), CHAR(']', WHITE, YELLOW) }, /* L */ 123 | { CHAR('[', WHITE, GREEN), CHAR(']', WHITE, GREEN) }, /* J */ 124 | { CHAR('[', WHITE, CYAN), CHAR(']', WHITE, CYAN) }, /* I */ 125 | { CHAR('[', WHITE, BLUE), CHAR(']', WHITE, BLUE) }, /* O */ 126 | { CHAR('[', WHITE, RED), CHAR(']', WHITE, RED) }, /* Z */ 127 | { CHAR('[', WHITE, MAGENTA), CHAR(']', WHITE, MAGENTA) }, /* S */ 128 | { CHAR('[', WHITE, BLACK), CHAR(']', WHITE, BLACK) }, /* T */ 129 | /* frame tiles */ 130 | { CHAR(G_UL_CORNER, WHITE, BLACK), CHAR(G_HLINE, WHITE, BLACK) }, /* top-left corner */ 131 | { CHAR(G_HLINE, WHITE, BLACK), CHAR(G_UR_CORNER, WHITE, BLACK) }, /* top-right corner */ 132 | { CHAR(G_LL_CORNER, WHITE, BLACK), CHAR(G_HLINE, WHITE, BLACK) }, /* bottom-left corner */ 133 | { CHAR(G_HLINE, WHITE, BLACK), CHAR(G_LR_CORNER, WHITE, BLACK) }, /* bottom-right corner */ 134 | { CHAR(G_L_TEE, WHITE, BLACK), CHAR(G_HLINE, WHITE, BLACK) }, /* left-T */ 135 | { CHAR(G_HLINE, WHITE, BLACK), CHAR(G_R_TEE, WHITE, BLACK) }, /* right-T */ 136 | { CHAR(G_HLINE, WHITE, BLACK), CHAR(G_HLINE, WHITE, BLACK) }, /* horizontal line */ 137 | { CHAR(G_VLINE, WHITE, BLACK), CHAR(' ', WHITE, BLACK) }, /* left vertical line */ 138 | { CHAR(' ', WHITE, BLACK), CHAR(G_VLINE, WHITE, BLACK) } /* right vertical line */ 139 | }; 140 | 141 | static const char *bgdata[SCR_ROWS] = { 142 | " #..........#{-----}", 143 | " #..........#(.....)", 144 | " #..........#[-----]", 145 | " #..........#.......", 146 | " #..........# ", 147 | " #..........#{-----}", 148 | " #..........#(.....)", 149 | " #..........#(.....)", 150 | " #..........#>-----<", 151 | " #..........#(.....)", 152 | " #..........#(.....)", 153 | " #..........#[-----]", 154 | " #..........# {----}", 155 | " #..........# (....)", 156 | " #..........# (....)", 157 | " #..........# (....)", 158 | " #..........# (....)", 159 | " #..........# [----]", 160 | " ############ ", 161 | " " 162 | }; 163 | 164 | #define NUM_LEVELS 21 165 | static const long level_speed[NUM_LEVELS] = { 166 | 887, 820, 753, 686, 619, 552, 469, 368, 285, 184, 167 | 167, 151, 134, 117, 107, 98, 88, 79, 69, 60, 50 168 | }; 169 | 170 | static const char *helpstr[] = { 171 | " Toggle (H)elp", 172 | "", 173 | "left,right,down/asd:", 174 | " move piece", 175 | " up/w/space: rotate", 176 | "enter/tab/0: drop", 177 | " backsp/del: restart", 178 | " P: pause", 179 | " Q: quit", 180 | "", 181 | " High scores:", 182 | " termtris -s" 183 | }; 184 | 185 | int init_game(void) 186 | { 187 | int i, j; 188 | int *row = scr; 189 | 190 | scores = read_scores(0, 10); 191 | memset(&cur_score, 0, sizeof cur_score); 192 | 193 | srand(time(0)); 194 | 195 | pause = 0; 196 | gameover = 0; 197 | num_complines = 0; 198 | tick_interval = level_speed[0]; 199 | cur_piece = -1; 200 | prev_piece = 0; 201 | next_piece = rand() % NUM_PIECES; 202 | 203 | term_init(); 204 | term_setcolor(WHITE, BLACK); 205 | term_clearscr(); 206 | term_cursor(0); 207 | 208 | if(blackpf) { 209 | tiles[TILE_PF][0] = CHAR(' ', BLACK, BLACK); 210 | tiles[TILE_PF][1] = CHAR(' ', BLACK, BLACK); 211 | tiles[TILE_TPIECE][0] = CHAR('[', BLACK, WHITE); 212 | tiles[TILE_TPIECE][1] = CHAR(']', BLACK, WHITE); 213 | } 214 | 215 | /* fill the screen buffer, and draw */ 216 | for(i=0; i': 239 | tile = TILE_FRM_LTEE; 240 | break; 241 | case '<': 242 | tile = TILE_FRM_RTEE; 243 | break; 244 | case '-': 245 | tile = TILE_FRM_HLINE; 246 | break; 247 | case '(': 248 | tile = TILE_FRM_LVLINE; 249 | break; 250 | case ')': 251 | tile = TILE_FRM_RVLINE; 252 | break; 253 | case ' ': 254 | default: 255 | tile = TILE_BLACK; 256 | } 257 | row[j] = tile; 258 | } 259 | row += SCR_COLS; 260 | } 261 | 262 | if(onlyascii) { 263 | for(i=0; i= 0) { 344 | ptr = scr + (row + PF_YOFFS) * SCR_COLS + PF_XOFFS; 345 | for(i=0; i> 8; 366 | 367 | if(blink > 6) { 368 | erase_completed(); 369 | wait_display(); 370 | num_complines = 0; 371 | return 0; 372 | } 373 | 374 | for(i=0; i= tick_interval) { 384 | if(cur_piece >= 0) { 385 | just_spawned = 0; 386 | next_pos[0] = pos[0] + 1; 387 | if(collision(cur_piece, next_pos)) { 388 | next_pos[0] = pos[0]; 389 | stick(cur_piece, next_pos); 390 | return 0; 391 | } 392 | } else { 393 | /* respawn */ 394 | if(spawn() == -1) { 395 | gameover = 1; 396 | return 0; 397 | } 398 | } 399 | 400 | dt -= tick_interval; 401 | prev_tick = msec; 402 | } 403 | 404 | update_cur_piece(); 405 | return tick_interval - dt; 406 | } 407 | 408 | #define MAX_TILE_OPS 32 409 | struct tileop { 410 | int op; 411 | int x, y; 412 | }; 413 | 414 | static int diff_piece(int pp, const int *ppos, int prot, int np, const int *npos, int nrot, struct tileop *ops) 415 | { 416 | int i, j, num, inval = 0; 417 | unsigned char *p = pieces[pp][prot]; 418 | struct tileop tmpop, *op = ops; 419 | 420 | for(i=0; i<4; i++) { 421 | op->op = ERASE_PIECE; 422 | op->x = ppos[1] + BLKX(*p); 423 | op->y = ppos[0] + BLKY(*p); 424 | if(op->y >= 0) { 425 | op++; 426 | } 427 | p++; 428 | } 429 | p = pieces[np][nrot]; 430 | for(i=0; i<4; i++) { 431 | op->op = DRAW_PIECE; 432 | op->x = npos[1] + BLKX(*p); 433 | op->y = npos[0] + BLKY(*p); 434 | if(op->y >= 0) { 435 | op++; 436 | } 437 | p++; 438 | } 439 | 440 | /* find double-changes to the same tile */ 441 | num = op - ops; 442 | for(i=0; iop == -1) { 476 | op++; 477 | continue; 478 | } 479 | 480 | /* if the new op is not under the cursor, handle cursor movement */ 481 | if(op->x != x || op->y != y) { 482 | term_setcursor(term_yoffs + op->y + PF_YOFFS, term_xoffs + 483 | (op->x + PF_XOFFS) * 2); 484 | x = op->x; 485 | y = op->y; 486 | } 487 | tile = op->op == ERASE_PIECE ? TILE_PF : FIRST_PIECE_TILE + piece; 488 | wrtile(tile); 489 | x++; 490 | op++; 491 | } 492 | } 493 | 494 | static void update_cur_piece(void) 495 | { 496 | int numops; 497 | struct tileop ops[MAX_TILE_OPS]; 498 | 499 | if(cur_piece < 0) return; 500 | 501 | if((numops = diff_piece(cur_piece, pos, prev_rot, cur_piece, next_pos, cur_rot, ops)) > 0) { 502 | draw_tileops(cur_piece, ops, numops); 503 | 504 | /* for terminals which can't hide the cursor, move it out of the way */ 505 | term_setcursor(0, 0); 506 | fflush(stdout); 507 | 508 | memcpy(pos, next_pos, sizeof pos); 509 | prev_rot = cur_rot; 510 | } 511 | } 512 | 513 | 514 | static void addscore(int nlines) 515 | { 516 | static const int stab[] = {40, 100, 300, 1200}; /* bonus per line completed */ 517 | 518 | cur_score.score += stab[nlines - 1] * (cur_score.level + 1); 519 | cur_score.lines += nlines; 520 | 521 | cur_score.level = cur_score.lines / 10; 522 | if(cur_score.level > NUM_LEVELS - 1) { 523 | cur_score.level = NUM_LEVELS - 1; 524 | } 525 | 526 | tick_interval = level_speed[cur_score.level]; 527 | 528 | print_numbers(); 529 | 530 | if(show_highscores) { 531 | print_slist(); 532 | } 533 | } 534 | 535 | static void print_numbers(void) 536 | { 537 | char buf[16]; 538 | 539 | term_setcolor(BLACK, WHITE); 540 | 541 | term_setcursor(term_yoffs + 3, term_xoffs + 14 * 2); 542 | sprintf(buf, "%10ld", cur_score.score); 543 | term_putstr(buf, 7); 544 | 545 | term_setcursor(term_yoffs + 7, term_xoffs + 17 * 2); 546 | sprintf(buf, "%2ld", cur_score.level); 547 | term_putstr(buf, 7); 548 | 549 | term_setcursor(term_yoffs + 10, term_xoffs + 14 * 2); 550 | sprintf(buf, "%8ld", cur_score.lines); 551 | term_putstr(buf, 7); 552 | } 553 | 554 | static void print_help(void) 555 | { 556 | int i; 557 | 558 | for(i=0; i 63) len = 63; 574 | 575 | if(len <= 0) { 576 | buf[0] = 0; 577 | return buf; 578 | } 579 | 580 | if(strlen(s) <= len) { 581 | strcpy(buf, s); 582 | return buf; 583 | } 584 | 585 | if(len < 3) { 586 | memset(buf, ' ', len); 587 | buf[len] = 0; 588 | return buf; 589 | } 590 | 591 | for(i=0; i 127) maxlen = 127; 609 | 610 | term_setcursor(1, x); 611 | if(maxlen < 15) { 612 | term_putstr("Sco(r)es", 0x70); 613 | } else { 614 | term_putstr("Toggle Sco(r)es", 0x70); 615 | } 616 | 617 | sc = scores; 618 | for(i=0; i<10; i++) { 619 | term_setcursor(i + 3, x); 620 | if(!show_highscores) { 621 | fillblank: memset(fmtbuf, ' ', maxlen); 622 | fmtbuf[maxlen] = 0; 623 | term_putstr(fmtbuf, 0x70); 624 | if(sc) sc = sc->next; 625 | continue; 626 | } 627 | 628 | color = 0x70; 629 | if(!printed_cur) { 630 | if(!sc || cur_score.score > sc->score) { 631 | cur_score.next = sc; 632 | sc = &cur_score; 633 | printed_cur = 1; 634 | color |= 0x80; 635 | } 636 | } else { 637 | if(!sc) goto fillblank; 638 | } 639 | 640 | user = (sc->user && *sc->user) ? sc->user : "***"; 641 | 642 | sclen = sprintf(fmtbuf, "%ld", sc->score); 643 | if(sclen > maxlen - 5) goto fillblank; 644 | namelen = maxlen - sclen - 5; 645 | 646 | if(strlen(user) <= namelen) { 647 | sprintf(fmt, "%%2d. %%-%ds %%ld", namelen); 648 | len = sprintf(fmtbuf, fmt, i + 1, user, sc->score); 649 | } else { 650 | len = sprintf(fmtbuf, "%2d. %s %ld", i + 1, clampstr(user, namelen), sc->score); 651 | } 652 | if(len < maxlen) { 653 | memset(fmtbuf + len, ' ', maxlen - len); 654 | } 655 | fmtbuf[maxlen] = 0; 656 | sc = sc->next; 657 | 658 | term_putstr(fmtbuf, color); 659 | } 660 | } 661 | 662 | #define C0 0x9b 663 | #define SS3 0x8f 664 | 665 | static void runesc(int csi, char *buf) 666 | { 667 | if(csi != C0) return; 668 | 669 | if(buf[1] == 0) { 670 | switch(buf[0]) { 671 | case 'A': 672 | game_input('w'); /* up */ 673 | break; 674 | case 'B': 675 | game_input('s'); /* down */ 676 | break; 677 | case 'C': 678 | game_input('d'); /* right */ 679 | break; 680 | case 'D': 681 | game_input('a'); /* left */ 682 | break; 683 | default: 684 | break; 685 | } 686 | return; 687 | } 688 | 689 | if(strcmp(buf, "28~") == 0) { 690 | /* help key */ 691 | game_input('h'); 692 | } 693 | } 694 | 695 | void game_input(int c) 696 | { 697 | static int esc, csi; 698 | static int esctop; 699 | static char escbuf[64]; 700 | 701 | if(esc) { 702 | esc = 0; 703 | if(c == 27) { 704 | quit = 1; 705 | return; 706 | } 707 | 708 | switch(c) { 709 | case '[': 710 | csi = C0; 711 | return; 712 | case 'O': 713 | csi = SS3; 714 | return; 715 | default: 716 | break; 717 | } 718 | } 719 | 720 | if(csi) { 721 | if(c < 0x20 || c >= 0x80) { 722 | csi = 0; 723 | esctop = 0; 724 | } 725 | 726 | escbuf[esctop++] = c; 727 | 728 | if(c >= 0x40) { 729 | int prevcsi = csi; 730 | escbuf[esctop] = 0; 731 | csi = 0; 732 | esctop = 0; 733 | runesc(prevcsi, escbuf); 734 | } 735 | return; 736 | } 737 | 738 | switch(c) { 739 | case 27: 740 | esc = 1; 741 | break; 742 | 743 | case C0: 744 | esc = 1; 745 | csi = C0; 746 | break; 747 | 748 | case 'q': 749 | quit = 1; 750 | break; 751 | 752 | case 'a': 753 | if(!pause) { 754 | next_pos[1] = pos[1] - 1; 755 | if(collision(cur_piece, next_pos)) { 756 | next_pos[1] = pos[1]; 757 | } 758 | } 759 | break; 760 | 761 | case 'd': 762 | if(!pause) { 763 | next_pos[1] = pos[1] + 1; 764 | if(collision(cur_piece, next_pos)) { 765 | next_pos[1] = pos[1]; 766 | } 767 | } 768 | break; 769 | 770 | case 'w': 771 | case ' ': 772 | if(!pause) { 773 | prev_rot = cur_rot; 774 | cur_rot = (cur_rot + rotstep) & 3; 775 | if(collision(cur_piece, next_pos)) { 776 | cur_rot = prev_rot; 777 | } 778 | } 779 | break; 780 | 781 | case 's': 782 | /* ignore drops until the first update after a spawn */ 783 | if(cur_piece >= 0 && !just_spawned && !pause && !gameover) { 784 | next_pos[0] = pos[0] + 1; 785 | if(collision(cur_piece, next_pos)) { 786 | next_pos[0] = pos[0]; 787 | update_cur_piece(); 788 | stick(cur_piece, next_pos); /* stick immediately */ 789 | } 790 | } 791 | break; 792 | 793 | case '\t': 794 | case '\n': 795 | case '\r': 796 | case '0': 797 | if(!pause && !gameover && cur_piece >= 0) { 798 | next_pos[0] = pos[0] + 1; 799 | while(!collision(cur_piece, next_pos)) { 800 | next_pos[0]++; 801 | } 802 | next_pos[0]--; 803 | update_cur_piece(); 804 | stick(cur_piece, next_pos); /* stick immediately */ 805 | } 806 | break; 807 | 808 | case 'p': 809 | if(gameover) { 810 | init_game(); 811 | } else { 812 | pause ^= 1; 813 | } 814 | break; 815 | 816 | case '\b': 817 | case 127: 818 | init_game(); 819 | break; 820 | 821 | case 'h': 822 | show_help ^= 1; 823 | print_help(); 824 | fflush(stdout); 825 | break; 826 | 827 | case 'r': 828 | show_highscores ^= 1; 829 | print_slist(); 830 | fflush(stdout); 831 | break; 832 | 833 | case '`': 834 | full_redraw(); 835 | break; 836 | 837 | default: 838 | /*fprintf(stderr, "unhandled input: %x\n", c);*/ 839 | break; 840 | } 841 | } 842 | 843 | static void full_redraw(void) 844 | { 845 | clear(); 846 | print_help(); 847 | drawbg(); 848 | print_slist(); 849 | print_numbers(); 850 | drawpf(0); 851 | if(!gameover) { 852 | draw_piece(next_piece, preview_pos, 0, DRAW_PIECE); 853 | draw_piece(cur_piece, next_pos, cur_rot, DRAW_PIECE); 854 | term_setcursor(0, 0); 855 | fflush(stdout); 856 | } 857 | wait_display(); 858 | } 859 | 860 | static int spawn(void) 861 | { 862 | int r, tries = 2; 863 | int numops; 864 | struct tileop ops[MAX_TILE_OPS]; 865 | 866 | do { 867 | r = rand() % NUM_PIECES; 868 | } while(tries-- > 0 && (r | prev_piece | next_piece) == prev_piece); 869 | 870 | if((numops = diff_piece(next_piece, preview_pos, 0, r, preview_pos, 0, ops)) > 0) { 871 | draw_tileops(r, ops, numops); 872 | term_setcursor(0, 0); 873 | fflush(stdout); 874 | } 875 | 876 | cur_piece = next_piece; 877 | next_piece = r; 878 | 879 | prev_rot = cur_rot = 0; 880 | pos[0] = piece_spawnpos[cur_piece][0]; 881 | next_pos[0] = pos[0] + 1; 882 | pos[1] = next_pos[1] = PF_COLS / 2 + piece_spawnpos[cur_piece][1]; 883 | 884 | if(collision(cur_piece, next_pos)) { 885 | return -1; 886 | } 887 | 888 | just_spawned = 1; 889 | return 0; 890 | } 891 | 892 | static int collision(int piece, const int *pos) 893 | { 894 | int i; 895 | unsigned char *p = pieces[piece][cur_rot]; 896 | 897 | for(i=0; i<4; i++) { 898 | int x = PF_XOFFS + pos[1] + BLKX(*p); 899 | int y = PF_YOFFS + pos[0] + BLKY(*p); 900 | p++; 901 | 902 | if(y < 0) continue; 903 | 904 | if(scr[y * SCR_COLS + x] != TILE_PF) return 1; 905 | } 906 | 907 | return 0; 908 | } 909 | 910 | static void stick(int piece, const int *pos) 911 | { 912 | int i, j, nblank; 913 | int *pfline; 914 | unsigned char *p = pieces[piece][cur_rot]; 915 | 916 | num_complines = 0; 917 | prev_piece = cur_piece; /* used by the spawn routine */ 918 | cur_piece = -1; 919 | 920 | for(i=0; i<4; i++) { 921 | int x = pos[1] + BLKX(*p); 922 | int y = pos[0] + BLKY(*p); 923 | p++; 924 | 925 | pfline = scr + (y + PF_YOFFS) * SCR_COLS + PF_XOFFS; 926 | pfline[x] = piece + FIRST_PIECE_TILE; 927 | 928 | nblank = 0; 929 | for(j=0; j complines[i]) { 960 | int tmp = complines[j]; 961 | complines[j] = complines[i]; 962 | complines[i] = tmp; 963 | } 964 | } 965 | } 966 | 967 | toprow = srow = drow = PF_ROWS - 1; 968 | dptr = pfstart + drow * SCR_COLS; 969 | 970 | for(i=0; i= sizeof tiles / sizeof *tiles) { 1094 | return; 1095 | } 1096 | 1097 | for(i=0; i<2; i++) { 1098 | uint16_t c = tiles[tileid][i]; 1099 | unsigned char cc = c & 0xff; 1100 | unsigned char ca = c >> 8; 1101 | 1102 | if(use_gfxchar && (cc == '[' || cc == ']')) { 1103 | ca <<= 4; /* for gfx blocks bg->fg and bg=0 */ 1104 | #if defined(MSDOS) || defined(__COM__) 1105 | if(term_type == TERM_PCBIOS) { 1106 | if(!ca) ca = 0x80; /* special case for T which has black bg */ 1107 | } else 1108 | #endif 1109 | if(!ca) ca = 0x70; /* special case for T which has black bg */ 1110 | } 1111 | 1112 | term_ibmchar(cc, ca); 1113 | } 1114 | } 1115 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------