Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / apps / openchess / src / game.c
blob458c6a4dabf06d167911fc6bff484d6046f82a72
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include "client.h"
25 #include "game.h"
27 game_t game_list;
29 /** Reset game board to default positions */
30 int game_board_reset (game_t *game)
32 //Vez,Jezdec,Strelec,Dama,Kral,Strelec,Jezdec,Vez
33 memcpy (game->board[0], "VJSDKSJV", 8); // radek 1 (bily)
34 memcpy (game->board[1], "PPPPPPPP", 8); // radek 2 (bily)
35 memcpy (game->board[2], "........", 8);
36 memcpy (game->board[3], "........", 8);
37 memcpy (game->board[4], "........", 8);
38 memcpy (game->board[5], "........", 8);
39 memcpy (game->board[6], "pppppppp", 8); // radek 7 (cerny)
40 memcpy (game->board[7], "vjsdksjv", 8); // radek 8 (cerny)
42 return 1;
45 game_t *game_find (char *name)
47 game_t *game;
48 for (game = game_list.next; game != &game_list; game = game->next)
49 if (!strcmp (game->name, name))
50 return game;
52 return 0;
55 game_t *game_findbyclient (client_t *client)
57 game_t *game;
58 for (game = game_list.next; game != &game_list; game = game->next)
59 if (game->white == client ||
60 game->black == client)
61 return game;
63 return 0;
66 /** Create new game's structure */
67 game_t *game_new (client_t *client, char *name, unsigned name_len)
69 game_t *game = game_find (name);
71 if (game)
72 return 0;
74 /* alloc and init context */
75 game = (game_t *) malloc (sizeof (game_t));
77 if (!game)
78 return 0;
80 /* setup game name */
81 game->name = (char *) malloc (sizeof (char) * (name_len + 1));
83 if (!game->name)
84 return 0;
86 memcpy (game->name, name, name_len);
87 game->name[name_len] = '\0';
89 /* set variables to default value */
90 game->play = 0;
91 game->round = 0;
92 game->player = 'w';
94 /* white player is each time game creator */
95 game->white = client;
96 /* black player have to join to game*/
97 game->black = 0;
99 /* reset chess board */
100 game_board_reset (game);
102 /* add into list */
103 game->next = &game_list;
104 game->prev = game_list.prev;
105 game->prev->next = game;
106 game->next->prev = game;
108 return game;
111 /** Client would to join to game */
112 int game_join (client_t *client, game_t *game)
114 /* you can join to game, when it not running */
115 if (game->play)
116 return 0;
118 /* player, what create game have to be connected */
119 if (!game->white)
120 return 0;
122 /* player, what create game, cant join as opponent too */
123 if (game->white == client)
124 return 0;
126 game->play = 1;
127 game->black = client;
129 unsigned white_len = strlen (game->white->nick);
130 unsigned black_len = strlen (game->black->nick);
132 char *str = (char *) malloc (white_len+white_len+3);
134 if (!str)
135 return 0;
137 memcpy (str, "s ", 4);
139 str[2] = 'b';
141 memcpy (str+4, game->white->nick, white_len);
143 client_pmmsg (game->black, str, 4+white_len);
146 str[2] = 'w';
148 memcpy (str+4, game->black->nick, black_len);
150 client_pmmsg (game->white, str, 4+black_len);
152 return 1;
155 /** Delete game's structure */
156 int game_quit (game_t *game)
158 game->next->prev = game->prev;
159 game->prev->next = game->next;
161 free (game->name);
163 free (game);
165 return 1;
168 /** Synchronize game with client's positions */
169 int game_sync (client_t *client, game_t *game)
171 char str[73];
173 /*unsigned i = 0;
174 unsigned y = 0;
176 while (i < 8) {
177 y = 0;
179 while (y < 8) {
180 str[y+i*9] = game->board[i][y];
181 y ++;
184 str[8+i*9] = '\n';
186 i ++;
189 int x;
190 int y;
192 int i = 0;
194 for (x = 7; x >= 0; x --) {
195 for (y = 0; y < 8; y ++) {
196 str[i] = game->board[x][y];
197 i ++;
200 str[i] = '\n';
201 i ++;
204 client_pmmsg (client, str, 72);
206 return 1;
209 int game_pos (client_t *client, game_t *game, unsigned char x_old, unsigned char y_old, unsigned char x, unsigned char y)
211 char f = game->board[y_old][x_old];
213 game->board[y_old][x_old] = '.';
215 game->board[y][x] = f;
217 return 1;
220 int game_getlist (client_t *client)
222 int len = 0;
224 game_t *game;
226 /* first calculate length of all names */
227 for (game = game_list.next; game != &game_list; game = game->next)
228 len += (strlen (game->name) + 3);
230 if (!len)
231 return 0;
233 char *list = (char *) malloc (sizeof (char) * (len + 1));
235 if (!list)
236 return 0;
238 len = 0;
240 unsigned l;
242 /* we can save all game names to list now */
243 for (game = game_list.next; game != &game_list; game = game->next) {
244 l = strlen (game->name);
246 if (game->play)
247 list[len] = '-';
248 else
249 list[len] = '+';
251 memcpy (list+1+len, " ", 1);
252 memcpy (list+2+len, game->name, l);
253 memcpy (list+2+len+l, "\n", 1);
255 len += (l + 3);
258 list[len] = '\0';
260 client_pmmsg (client, list, len);
262 free (list);
264 return 1;
267 /** Client Init function */
268 int init_game ()
270 game_list.next = &game_list;
271 game_list.prev = &game_list;
273 return 1;