/************************************************************\ * * * RISK - for X11 * * * * Lasse \O verlier * * Hans Kristian Nordengen * * * \************************************************************/ #include "xrisk.h" #include "xriskex.h" #include "risklibex.h" #include "randomex.h" #include "init_riskex.h" #include "init_playex.h" #include "functionsex.h" #include "cursors2.h" #include "actions_menu_but.h" #include "cardex.h" #include "actions_menu_butex.h" #include "control_statesex.h" #include "xrisklibex.h" #include "parseoptex.h" #include "errorhandlersex.h" struct Player *players[MAX_PLAYERS+1]; /* List of all players */ struct Card *cards[NUM_CARDS+1]; /* List of cards not out */ struct Continent continents[NUM_CONTINENTS]; /* List of continents*/ struct Mission *missions[MAX_MISSIONS+1]; /* List of missions */ struct Country countries[NUM_COUNTRIES]; /* List of countries */ struct Continent free_choise; /* Continent of free choice */ struct Country Joker; /* The joker */ XColor colors[MAX_PLAYERS]; /* Basis color-table */ char *sentencemapping[MAX_MAPS]; /* List of mappings */ char *map; /* Byte-representation of map */ char *unitp[3]; /* Byte-representation of units */ int num_players; /* Players still alive */ struct State *states[NUM_STATES]; /* Availible states */ struct Control_Data control_data; /* Status of the game */ int who_where[NUM_STATES];/* Which player, what state*/ struct Game_Options game_options; /* Options in this game */ /* Check if player has to change cards */ int check_cards(player) playerp player; { int state; state = player->state->state; if ( (player->num_cards < 5) || (state <= WAIT_FOR_ALL_1) || (state == WAIT_FOR_TURN) || (state == START_TURN) || (state == P_A_WHERE) || (state == P_A_STRENG) || (state == P_A_NUMBER) || (state == P_A_PLACE) || (state == ATTACK_LAND) || (state == A_STRENG) || (state == A_NUMBER) || (state == END_GAME) || (state == CLOSE_GAME)) return(0); player->pstat->armies_left = 0; new_state(player,P_A_WHERE); player->card_win->allow_ch = 2; exchange_card(player); return(0); } /* Parse an action */ int do_action(player, action) playerp player; actionp action; { int i; if (!action->func) return(0); if ((!(action)) || (! (i=(action->func)(player)) )) return(0); new_state(player,action->next_state[i-1]); return(1); } /* Initialize a new player-state */ int new_state(player, next_state) playerp player; int next_state; { xconp xinfo; xinfo = player->xcon; if (!do_action(player,&(player->state->all_actions->exit_func))) { (who_where[player->state->state]) ^= (1<indexs); player->state = states[next_state]; (who_where[next_state]) ^= (1<indexs); if (xinfo) { if (all_cursors[next_state].map != NUMB_CURSORS) XDefineCursor (xinfo->disp,xinfo->win, xinfo->cursors[all_cursors[next_state].map]); if (all_cursors[next_state].cards != NUMB_CURSORS) XDefineCursor (xinfo->disp,player->card_win->win, xinfo->cursors[all_cursors[next_state].cards]); if (all_cursors[next_state].info != NUMB_CURSORS) XDefineCursor (xinfo->disp,player->info_win->win, xinfo->cursors[all_cursors[next_state].info]); if (all_cursors[next_state].scroll != NUMB_CURSORS) XDefineCursor (xinfo->disp,xinfo->scrollwin, xinfo->cursors[all_cursors[next_state].scroll]); } do_action(player,&(player->state->all_actions->init_func)); } return(1); } /* Parsing when a player press a button */ int parse_buttons(player, actions) playerp player; button_actionsp actions; { XEvent *eventp; eventp = GetEventp(player); if (eventp->type == KeyPress) do_action (player,&(player->state->all_actions->action_kB)); else if (eventp->type == ButtonPress) { if (eventp->xbutton.button == Button1) do_action(player,&(actions->action_mB1)); else if (eventp->xbutton.button == Button2) do_action(player,&(actions->action_mB2)); else if (eventp->xbutton.button == Button3) do_action(player,&(actions->action_mB3)); } return(0); } /* Always return 1 */ int chk_allways() { return(1); } /* Stand here and parse events */ void xrisk_mainloop() { xconp xinfo; statep state; playerp player; int t,t1; int eventType; Window eventWindow; while(!(control_data.control_action->c_func)()) Do_each_player(t) if ((!do_action(players[t],&(players[t]->state->all_actions->func))) && (players[t]->xcon) && (RegEvent(players[t]))) { player = players[t]; xinfo = player->xcon; state = player->state; eventType = GetEventp(player)->type; eventWindow = GetEventp(player)->xany.window; switch (eventType) { case KeyPress: case ButtonPress: if (eventWindow == player->pstat->spec_win) do_action (player,&(state->all_actions->spec_func)); else if (eventWindow == xinfo->win) parse_buttons (player,&(state->all_actions->map_but_actions)); else if (eventWindow == player->xcon->scrollwin) parse_buttons (player,&(state->actions_other_win[SCROLL_WIN].button_actions)); else if (eventWindow == player->info_win->win) parse_buttons (player,&(state->actions_other_win[INFO_WIN].button_actions)); else if (eventWindow == player->card_win->win) parse_buttons (player,&(state->actions_other_win[CARD_WIN].button_actions)); else if (eventWindow == xinfo->menu_butt[MENU_WRITE]) get_menu_string(player); else for(t1=0;t1menu_butt[t1]) parse_buttons(player,&(actions_menu_but[t1].button_actions)); check_cards(player); check_mission(player); break; case Expose: if (eventWindow == xinfo->win) expose_mapwin(player); else if (eventWindow == player->xcon->scrollwin) { if (!GetEventp(player)->xexpose.count) write_msg_all((char *)NULL); } else if (eventWindow == player->info_win->upper) expose_infowin(player); else if (eventWindow == player->info_win->lower) expose_infowin(player); else for(t1=0;t1card_win->buttons[t1]) { expose_card_button(player, t1); break; } else if (eventWindow == player->card_win->cards[t1]) { expose_card_win(player, t1); break; } for(t1=0;t1menu_butt[t1]) { expose_action_menu(player, t1); break; } break; default: fprintf(stderr,"Window %d Type %d\n",eventWindow, eventType); } } /* The only way to handle errorsituations is to restart a new xrisk_mainloop for every error that occures */ exit(0); } /* Our main-function */ void main(argc,argv) int argc; char **argv; { int numopt; numopt = parseopt(argc,argv); num_players = argc-numopt; if ((num_players<2)||(num_players>MAX_PLAYERS)) { printf("Mail bugs to xrisk@pvv.unit.no\n"); printf("Usage: xrisk [-m] [-d] [-llanguage] "); printf("machine1 machine2 [machine3 ..]\n"); printf("-m to disable mission\n"); printf("-l to use another language ( norwegian, english, german)\n"); printf("-d two players\n"); printf("Min : 2 machines \n"); printf("Max : 8 machines\n"); risk_exit("Your number %d client(s)\n",num_players,LAST_ARG); } map = get_picture(PICTURFILE,XSIZE,YSIZE); init_risk(); init_play(&argv[numopt]); init_errorhandlers(); xrisk_mainloop(); }