1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
|
/* $Id: robot_proceso.c,v 1.18 2002/04/19 04:12:40 riq Exp $ */
/****************************************************************************
Funcion basada en la del cliente.
Comportamiento del Robot a nivel BNP
****************************************************************************/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <unistd.h>
#include <signal.h>
#include <netdb.h>
#include <stdio.h>
#include <syslog.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <config.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include "robot_cliente.h"
#include "robot_random_board.h"
#include "robot_ai.h"
#include "bnwrite.h"
#include "protocol.h"
#include "parser.h"
#include "net.h"
#include "common.h"
CLIENTE cliente;
GtkWidget *window;
GtkWidget *box;
GtkWidget *imagen;
GtkWidget *label;
GtkWidget *status_label;
gchar* pix;
GtkWidget *pbar;
/* This describes all the arguments we understand. */
static char *server_name=NULL;
static const GOptionEntry options[] =
{
{ "server", 's', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, &server_name,
N_("Server name. Default is localhost"),"SERVER"},
{ "port", 'p', G_OPTION_FLAG_NONE, G_OPTION_ARG_INT, &cliente.port,
N_("Port number. Default is 1995"),"PORT"},
#ifdef WITH_GGZ
{ "ggz", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &cliente.with_ggz,
N_("Enable GGZ mode"),NULL },
#endif /* WITH_GGZ */
{ "nogui", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &cliente.with_nogui,
N_("Disable the GUI"),NULL },
{ NULL }
};
/* nombre y skill del robot */
struct {
char *nombre;
robotai_t ai;
} robot_nombres[] = {
{ "homer", ROBOT_AI_AVERAGE },
{ "bart", ROBOT_AI_AVERAGE },
{ "marge", ROBOT_AI_AVERAGE },
{ "lisa", ROBOT_AI_HI },
{ "albondiga" ,ROBOT_AI_AVERAGE },
{ "clorofila", ROBOT_AI_AVERAGE },
{ "maceta", ROBOT_AI_AVERAGE },
{ "cebolla", ROBOT_AI_AVERAGE },
{ "edu", ROBOT_AI_AVERAGE },
{ "gonza", ROBOT_AI_AVERAGE },
{ "pitufo", ROBOT_AI_AVERAGE },
{ "cacho", ROBOT_AI_AVERAGE },
{ "president", ROBOT_AI_AVERAGE },
{ "chuchi", ROBOT_AI_AVERAGE },
{ "sandro", ROBOT_AI_AVERAGE },
{ "beto", ROBOT_AI_AVERAGE },
{ "raul", ROBOT_AI_AVERAGE },
{ "medusa", ROBOT_AI_AVERAGE },
{ "juan", ROBOT_AI_AVERAGE },
{ "pipito", ROBOT_AI_AVERAGE },
{ "chichi", ROBOT_AI_AVERAGE },
{ "pichu", ROBOT_AI_AVERAGE },
{ "cachi", ROBOT_AI_AVERAGE },
{ "roque", ROBOT_AI_AVERAGE },
{ "charly", ROBOT_AI_AVERAGE },
{ "chupete", ROBOT_AI_AVERAGE },
{ "tula", ROBOT_AI_AVERAGE },
{ "loko", ROBOT_AI_AVERAGE },
{ "cachito", ROBOT_AI_AVERAGE },
{ "sandro", ROBOT_AI_AVERAGE },
{ "vince", ROBOT_AI_AVERAGE },
{ "corcho", ROBOT_AI_AVERAGE },
{ "bombi", ROBOT_AI_AVERAGE },
{ "edna", ROBOT_AI_AVERAGE },
{ "viole", ROBOT_AI_AVERAGE },
{ "papi", ROBOT_AI_AVERAGE },
{ "mami", ROBOT_AI_AVERAGE },
{ "arjona", ROBOT_AI_AVERAGE },
{ "piojo", ROBOT_AI_AVERAGE },
{ "valerio", ROBOT_AI_AVERAGE },
{ "sofi", ROBOT_AI_AVERAGE },
{ "miko", ROBOT_AI_AVERAGE },
{ "onda", ROBOT_AI_AVERAGE },
{ "llave", ROBOT_AI_AVERAGE }
};
static int rnombres = sizeof(robot_nombres) / sizeof(robot_nombres[0] );
/*****************************************************************************
funiones
auxiliares
******************************************************************************/
/* convierte un char[10][10] a un char[100] */
static void iwtable( char *dest)
{
int i,x,y;
x=0;
y=0;
for(i=0;i<100;i++) {
dest[i]=cliente.mitabla[x][y];
x++;
if(x>=10) {
x=0;
y++;
}
}
}
/* convierte un char[100] a un char[10][10] */
static void poner_en_board( int d, char *orig)
{
int i,x,y;
x=0;
y=0;
for(i=0;i<100;i++) {
cliente.boards[d][x][y]=orig[i];
x++;
if(x>=10) {
x=0;
y++;
}
}
}
/* Esta funcion tiene algo que ver con el AI */
static void que_usrfrom()
{
int i;
for(i=cliente.numjug+1;i<MAXPLAYER;i++) {
if( cliente.play[i]>=BOARD && cliente.play[i]<=TURN) {
cliente.usrfrom=i;
return;
}
}
for(i=0;i<=cliente.numjug;i++) {
if( cliente.play[i]>=BOARD && cliente.play[i]<=TURN ) {
cliente.usrfrom=i;
return;
}
}
printf("gbnrobot: robot %d: que_usrfrom: Error, aca no tendria que llegar\n",cliente.numjug);
}
static void dime_quien_soy( void )
{
int tmp;
tmp = ((cliente.random<<1 ) ^ (cliente.random>>1)) % rnombres;
if(tmp < 0)
tmp=-tmp;
strncpy( cliente.mi_nombre, robot_nombres[tmp].nombre, sizeof(cliente.mi_nombre) );
cliente.ai = robot_nombres[tmp].ai;
}
static BATNAV_STATUS send_new_board()
{
char buf[101];
generar();
iwtable(buf);
buf[10*10]=0;
if( bnwrite(cliente.sock,BN_SEND"=%s",buf) < 0)
return BATNAV_STATUS_ERROR;
if(!cliente.with_nogui) {
gtk_progress_bar_set_text (GTK_PROGRESS_BAR (pbar),
"[20/20] 100%");
gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (pbar), 1.0);
}
return BATNAV_STATUS_SUCCESS;
}
/*****************************************************************************
funciones
robot_*
******************************************************************************/
static void robot_fire(char *str)
{
gfloat new_val;
int i,j,x,y,z;
PARSER p;
DELIM igualador={ '=','=','=' };
DELIM separador={ '/',',','/' };
p.igualador = &igualador;
p.separador = &separador;
strncpy(p.sig,str,sizeof(p.sig));
j=0;
do{
i=parser_init( &p );
if(p.status && j==0) /* X */
x=atoi(p.token);
else if(p.status && j==1) /* Y */
y=atoi(p.token);
else if(p.status && j==2) /* Tocado, hundio, etc */
z=atoi(p.token);
j++;
} while(i);
if(j!=3) {
printf("gbnrobot: Error parsing in '"BN_FIRE"'");
return;
}
if(z==HUNDIDO) {
if(!cliente.with_nogui) {
gchar *s;
new_val = gtk_progress_bar_get_fraction
(GTK_PROGRESS_BAR (pbar)) - 0.05;
s = g_strdup_printf ("[%d/20] %d%%",
(int) (new_val * 20),
(int) (new_val * 100));
gtk_progress_bar_set_text (GTK_PROGRESS_BAR (pbar), s);
gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (pbar),
new_val);
g_free (s);
}
}
}
static void robot_numjug( char *str)
{
char buf[101];
int i;
i=atoi(str);
cliente.numjug=i;
generar();
dime_quien_soy();
iwtable(buf);
buf[10*10]=0;
bnwrite(cliente.sock,BN_NAME"=%s;"BN_CLI_VER"="ROBOTVER";"BN_SER_VER";"BN_PROTOCOL";"BN_SEND"=%s",cliente.mi_nombre,buf);
if(!cliente.with_nogui) {
sprintf(buf,"-> %s[%i] <-",cliente.mi_nombre,cliente.numjug);
gtk_label_set_text(GTK_LABEL(label), buf);
}
}
static void robot_ignore(char *_unused)
{
}
static void robot_killmyself(char *_unused)
{
bnwrite(cliente.sock,BN_EXIT);
close( cliente.sock );
if( !cliente.with_nogui )
gtk_main_quit();
exit(1);
}
static void robot_board_not_ok(char *_unused)
{
printf("gbnrobot: Robot: You sent an invalid board. Report this bug!\n");
robot_killmyself(NULL);
}
static void robot_board_ok( char *_unused )
{
if(!cliente.with_nogui) {
gtk_label_set_text(GTK_LABEL(status_label), _("Ready"));
gtk_progress_bar_set_text (GTK_PROGRESS_BAR (pbar),
"[20/20] 100%");
gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (pbar), 1.0);
}
}
static void robot_start( char *str)
{
int i,k,l,m;
PARSER p;
DELIM igualador={ '=','=','=' };
DELIM separador={ '/',',','/' };
p.igualador = &igualador;
p.separador = &separador;
strncpy(p.sig,str,sizeof(p.sig));
m=0;k=0;l=0;
do{
i=parser_init( &p );
if(p.status && m==0) /* number of player */
l=atoi(p.token);
else if(p.status && m==1) { /* estado */
k=atoi(p.token);
cliente.play[l]=k;
}
m++;
m=m%3;
} while(i);
que_usrfrom();
/* printf("Robot %d: Starting...usrfrom=%i\n",cliente.numjug,cliente.usrfrom); */
}
static void robot_discon( char *str )
{
int x;
x=atoi(str);
if( cliente.numjug==x ) {
/* printf("Robot %i: Me estan desconectando\n",x); */
robot_killmyself(NULL);
}
if(x>=0 || x < MAXPLAYER)
cliente.play[x]=DISCON;
que_usrfrom();
return;
}
static void robot_turn( char *str )
{
if( cliente.play[cliente.numjug]==PERDIO)
return;
if(atoi(str)==cliente.numjug) {
bnwrite(cliente.sock,BN_READ"=%i",cliente.usrfrom);
cliente.play[cliente.numjug]=TURN;
if(!cliente.with_nogui)
gtk_label_set_text(GTK_LABEL(status_label), _("My turn"));
} else {
cliente.play[cliente.numjug]=PLAY;
if(!cliente.with_nogui)
gtk_label_set_text(GTK_LABEL(status_label), _("Waiting turn..."));
}
}
static void robot_read( char *str )
{
int i,m,l,x,y;
char buf[10*10];
PARSER p;
DELIM igualador={ '=','=','=' };
DELIM separador={ '/',',','/' };
if(cliente.play[cliente.numjug]!=TURN) {
/* printf("Robot %d: No es mi turno \n",cliente.numjug); */
return;
}
p.igualador = &igualador;
p.separador = &separador;
strncpy(p.sig,str,sizeof(p.sig));
m=0;
do{
i=parser_init( &p );
if(p.status && m==0) /* numero de jugador */
l=atoi(p.token);
else if(p.status && m==1) /* board del jugador */
strncpy(buf,p.token,10*10);
m++;
} while(i);
if(l==cliente.usrfrom ) {
if(cliente.usrfrom == cliente.numjug ) {
/* printf("Robot %i : usrfrom = numjug",cliente.numjug); */
}
poner_en_board(l,buf);
robot_ai(&x,&y);
usleep(75000);
bnwrite(cliente.sock,BN_FIRE"=%i,%i",x,y);
} else
bnwrite(cliente.sock,BN_READ"=%i",cliente.usrfrom);
}
static void robot_lost( char *str)
{
int x;
x=atoi(str);
if(x>=0 || x<MAXPLAYER)
cliente.play[x]=PERDIO;
if(x==cliente.numjug && !cliente.with_nogui)
gtk_label_set_text(GTK_LABEL(status_label), _("Game Over"));
else
que_usrfrom();
return;
}
static void robot_ggz(char *_unused)
{
bnwrite(cliente.sock,BN_NUMJUG);
}
static void robot_win( char *str)
{
int a;
/* alguien gano, asi que todo bien */
send_new_board();
return;
}
static void robot_game_over( char *_unused )
{
if(!cliente.with_nogui)
gtk_label_set_text(GTK_LABEL(status_label), _("Game Over"));
send_new_board();
}
/**********************************************************************
lookup token, etc
**********************************************************************/
static int robot_lookup_funcion( PARSER *p )
{
int i;
struct {
char *label;
void (*func) (char *);
} tokens[] = {
{ BN_BOARD_NOT_OK, robot_board_not_ok },
{ BN_BOARD_OK, robot_board_ok },
{ BN_DISCON, robot_discon },
{ BN_GAME_OVER, robot_game_over },
{ BN_LOST, robot_lost },
{ BN_MESSAGE, NULL },
{ BN_NAME, NULL },
{ BN_NUMJUG, robot_numjug },
{ BN_PROTOCOL, NULL },
{ BN_QUMM, NULL },
{ BN_READ, robot_read },
{ BN_READY_TO_PLAY, NULL },
{ BN_REM, robot_ignore },
{ BN_SCORES, NULL },
{ BN_SER_FULL, robot_killmyself },
{ BN_SER_VER, NULL },
{ BN_SOL, robot_killmyself },
{ BN_START, robot_start },
{ BN_STATUS, NULL },
{ BN_TEST, NULL },
{ BN_TURN, robot_turn },
{ BN_WAIT, robot_killmyself },
{ BN_WIN, robot_win },
{ BN_FIRE, robot_fire },
{ BN_GGZ, robot_ggz },
};
int ntokens = sizeof (tokens) / sizeof (tokens[0]);
for (i = 0; i < ntokens; i++) {
if (strcmp( p->token, tokens[i].label )==0 ){
if (tokens[i].func)
(tokens[i].func)( p->value );
else {
/* printf("Robot %d: Function '%s' is not implemented yet!\n",cliente.numjug,tokens[i].label); */
}
return TRUE;
}
}
return FALSE;
}
static gboolean robot_proceso( GIOChannel *src, GIOCondition cond, gpointer data )
{
gint sock;
int i,j;
PARSER p;
DELIM igualador={ '=', ':', '=' };
DELIM separador={ ';', ';', ';' };
char str[PROT_MAX_LEN];
p.igualador = &igualador;
p.separador = &separador;
str[0]=0;
if( !src )
sock=GPOINTER_TO_INT( data );
else
sock=g_io_channel_unix_get_fd( src );
/* printf("ROBOT: Entrando a robot_proceso %i\n",sock); */
j=net_readline( sock, str,PROT_MAX_LEN );
if( j<1 ) {
robot_killmyself(NULL);
return FALSE;
}
strncpy(p.sig,str,sizeof(p.sig));
do{
i=parser_init( &p );
if(p.status) {
if( !robot_lookup_funcion( &p ) ) {
// printf("gbnrobot: Token '%s' no encontrado\n",p.token);
}
}
} while(i);
/* printf("Saliendo de robot_proceso %i\n",sock); */
return TRUE;
}
static int robot_init( void )
{
GIOChannel *channel;
#ifdef WITH_GGZ
if( cliente.with_ggz ) {
cliente.with_nogui = 1;
cliente.sock = 3;
} else
#endif /* WITH_GGZ */
cliente.sock = net_connect_tcp( cliente.server, cliente.port);
if(cliente.sock<0) {
printf(_("gbnrobot Error: Is the server running?\n"));
return -1;
}
if(!cliente.with_nogui) {
channel = g_io_channel_unix_new( cliente.sock );
cliente.tag = g_io_add_watch( channel, G_IO_IN, robot_proceso,
NULL );
g_io_channel_unref( channel );
}
return 0;
}
/**
* @fn char * load_pixmap_file( char *name )
* Carga un pixmap. Busca en $prefix, localdir & $datadir
* @param name Pixmap a buscar
* @return NULL si no encontro o path del pixmap. Hay que g_free ese string
*/
char * load_pixmap_file( char *name )
{
char *filename = NULL;
filename = g_strconcat( PIXMAPDIR,name,NULL );
return filename;
}
static void on_exit_activate(GtkWidget *widget, gpointer data)
{
robot_killmyself(NULL);
}
static void init_X( )
{
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window),
_("Gnome Batalla Naval robot") );
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
gtk_widget_realize (window);
g_signal_connect ( G_OBJECT( window), "destroy",
G_CALLBACK( on_exit_activate ),
G_OBJECT(window) );
g_signal_connect ( G_OBJECT( window), "delete_event",
G_CALLBACK( on_exit_activate ),
G_OBJECT(window) );
box = gtk_box_new( GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add(GTK_CONTAINER(window), box);
gtk_container_set_border_width ( GTK_CONTAINER(box), 0);
pix = load_pixmap_file("gbnrobot.png");
if( pix ) {
imagen = gtk_image_new_from_file( pix );
gtk_widget_show( imagen );
g_free( pix );
gtk_box_pack_start( GTK_BOX(box),imagen,TRUE,TRUE,0);
}
pbar = gtk_progress_bar_new ();
gtk_progress_bar_set_show_text (GTK_PROGRESS_BAR (pbar), TRUE );
gtk_box_pack_start (GTK_BOX (box), pbar, FALSE, FALSE, 5);
label = gtk_label_new (_("."));
gtk_box_pack_start (GTK_BOX (box), label, FALSE, TRUE, 5);
status_label = gtk_label_new (_("Connecting..."));
gtk_box_pack_start (GTK_BOX (box), status_label, FALSE, TRUE, 5);
/* Principal */
gtk_widget_show_all( window);
}
void nogui_main()
{
int fd_max;
fd_set active_fd,read_fd;
int r;
FD_ZERO(&active_fd);
FD_SET(cliente.sock,&active_fd);
fd_max = cliente.sock;
while(1) {
read_fd = active_fd;
r = select(fd_max+1,&read_fd,NULL,NULL,NULL);
if(r < 0 && errno == EINTR )
continue;
if(r < 0) {
fprintf(stderr,"abnormal error\n");
return;
}
if( FD_ISSET(cliente.sock,&read_fd))
robot_proceso(NULL,0,GINT_TO_POINTER(cliente.sock));
}
}
/****************************************************************************
* MAIN * MAIN * MAIN
****************************************************************************/
int main (int argc, char *argv[])
{
int i;
GOptionContext *ctx=NULL;
GError *error=NULL;
dont_run_as_root();
bindtextdomain( PACKAGE, GNOMELOCALEDIR );
textdomain( PACKAGE );
cliente.port = 1995;
strncpy(cliente.server,"127.0.0.1",sizeof(cliente.server)-1);
cliente.with_nogui=0;
ctx=g_option_context_new(_("- Batalla Naval Robot"));
g_option_context_add_main_entries(ctx, options, PACKAGE);
g_option_context_add_group(ctx, gtk_get_option_group (TRUE));
if( !cliente.with_nogui ) {
if( !g_option_context_parse(ctx, &argc, &argv, &error) ) {
fprintf(stderr, _("Option parsing failed: %s\n"),
error->message);
g_error_free(error);
exit(EXIT_FAILURE);
}
gtk_init(&argc, &argv);
init_X();
}
if( server_name ) {
strncpy(cliente.server,server_name,sizeof(cliente.server)-1);
cliente.server[sizeof(cliente.server)-1]=0;
}
if( robot_init() < 0 ) exit(-1);
bnwrite( cliente.sock,BN_NUMJUG);
if(cliente.with_nogui)
nogui_main();
else
gtk_main();
return 0;
}
|