Complete.Org: Mailing Lists: Archives: freeciv-dev: April 2000:
[Freeciv-Dev] timeout patch
Home

[Freeciv-Dev] timeout patch

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] timeout patch
From: Robert Rendell <rob@xxxxxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 18 Apr 2000 15:39:37 +1000 (EST)

Hi - I've hacked the version of freeciv a bunch of us have been using for
our own nefarious purposes, and then thought it might be of more general
interest.

The basic intent was to allow us to run a game with predictable updates
(after a more rapid initial stage), so we could have, say, 2 turns run a
day, and people could connect and do their turns and disconnect again...
True, civ isn't as well suited to this style of play as PBEM and other such
games which typically have weekly updates, but we thought it might be worth
a go (having burned three afternoons while at work playing turn-turn-turn
:)

Anyway, so, I added an option to the server, fixedlength, which means that
the only way the turn advances is through a timeout - it ignores players
hitting the "Turn Done" button.  I also increased the maximum timeout you
can set, which in turn required some changes to the timeout display in the
client, so it comes up with, say, 40m rather than 2400 (seconds).

Then we noticed that a connecting client doesn't start the timeout clock,
because it doesn't know when the turn started.  So, I changed the way
timeouts work, removing the static time_at_turn_end, and putting a field in
the game structure, turn_start, which is sent to the clients.  This also
meant that changing the timeout would affect the current turn, which I also
wanted.

Oh - something else.  Even if you ignore this patch, I also fixed what is
probably a bug, and you'll probably want to fix it in the official version.
If you first set a timeout mid-game, the current turn immediately times
out, since time_at_turn_end has already passed, and now game.timeout != 0.

Anyway, feel free to tell me to go away, and I'll just accumulate my diffs
to apply to the official version on each release :)

                                                        Have fun,
                                                         Rob R.
                                                          \((/
                                                          ~oo~
                                                          /))\
--------------------------------------------------------------------------------
diff -rc freeciv-1.10.0/client/gui-gtk/mapview.c 
freeciv-mod/client/gui-gtk/mapview.c
*** freeciv-1.10.0/client/gui-gtk/mapview.c     Tue Feb 15 13:32:37 2000
--- freeciv-mod/client/gui-gtk/mapview.c        Mon Apr 17 06:25:08 2000
***************
*** 276,282 ****
  {
    char buffer[512];
  
!   my_snprintf(buffer, sizeof(buffer), "%d", seconds_to_turndone);
    gtk_set_label(timeout_label, buffer);
  }
  
--- 276,289 ----
  {
    char buffer[512];
  
!   if (seconds_to_turndone <= 999)
!     my_snprintf(buffer, sizeof(buffer), "%d", seconds_to_turndone);
!   else if (seconds_to_turndone <= 5940)       /* 99 minutes */
!     my_snprintf(buffer, sizeof(buffer), "%dm", seconds_to_turndone/60);
!   else if (seconds_to_turndone < 35640) /* 9.9 hours */
!     my_snprintf(buffer, sizeof(buffer), "%.1fh", seconds_to_turndone/3600.0);
!   else
!     my_snprintf(buffer, sizeof(buffer), "%dh", seconds_to_turndone/3600);
    gtk_set_label(timeout_label, buffer);
  }
  
diff -rc freeciv-1.10.0/client/gui-xaw/mapview.c 
freeciv-mod/client/gui-xaw/mapview.c
*** freeciv-1.10.0/client/gui-xaw/mapview.c     Sun Jan 23 03:13:13 2000
--- freeciv-mod/client/gui-xaw/mapview.c        Mon Apr 17 06:25:09 2000
***************
*** 261,267 ****
  {
    char buffer[512];
  
!   my_snprintf(buffer, sizeof(buffer), "%d", seconds_to_turndone);
    xaw_set_label(timeout_label, buffer);
  }
  
--- 261,274 ----
  {
    char buffer[512];
  
!   if (seconds_to_turndone <= 999)
!     my_snprintf(buffer, sizeof(buffer), "%d", seconds_to_turndone);
!   else if (seconds_to_turndone <= 5940)       /* 99 minutes */
!     my_snprintf(buffer, sizeof(buffer), "%dm", seconds_to_turndone/60);
!   else if (seconds_to_turndone < 35640) /* 9.9 hours */
!     my_snprintf(buffer, sizeof(buffer), "%.1fh", seconds_to_turndone/3600.0);
!   else
!     my_snprintf(buffer, sizeof(buffer), "%dh", seconds_to_turndone/3600);
    xaw_set_label(timeout_label, buffer);
  }
  
diff -rc freeciv-1.10.0/client/packhand.c freeciv-mod/client/packhand.c
*** freeciv-1.10.0/client/packhand.c    Wed Jan 19 16:33:13 2000
--- freeciv-mod/client/packhand.c       Tue Apr 18 04:49:23 2000
***************
*** 18,23 ****
--- 18,27 ----
  #include <stdlib.h>
  #include <string.h>
  #include <assert.h>
+ #include <time.h>
+ #ifdef HAVE_SYS_TIME_H
+ #include <sys/time.h>
+ #endif
  
  #include "capability.h"
  #include "capstr.h"
***************
*** 637,643 ****
    game.techlevel=pinfo->techlevel;
    game.skill_level=pinfo->skill_level;
    game.timeout=pinfo->timeout;
! 
    game.end_year=pinfo->end_year;
    game.year=pinfo->year;
    game.min_players=pinfo->min_players;
--- 641,649 ----
    game.techlevel=pinfo->techlevel;
    game.skill_level=pinfo->skill_level;
    game.timeout=pinfo->timeout;
!   game.turn_start=pinfo->turn_start;
!   if (game.timeout)
!     seconds_to_turndone = game.turn_start + game.timeout - time(NULL);
    game.end_year=pinfo->end_year;
    game.year=pinfo->year;
    game.min_players=pinfo->min_players;
diff -rc freeciv-1.10.0/common/game.h freeciv-mod/common/game.h
*** freeciv-1.10.0/common/game.h        Tue Feb 15 14:40:33 2000
--- freeciv-mod/common/game.h   Tue Apr 18 04:52:30 2000
***************
*** 13,18 ****
--- 13,23 ----
  #ifndef FC__GAME_H
  #define FC__GAME_H
  
+ #include <time.h>     /* time_t */
+ #ifdef HAVE_SYS_TIME_H
+ #include <sys/time.h>
+ #endif
+ 
  #include "shared.h"
  #include "player.h"
  
***************
*** 46,51 ****
--- 51,57 ----
    int tech;
    int skill_level;
    int timeout;
+   time_t turn_start;
    int end_year;
    int year;
    int techlevel;
***************
*** 84,89 ****
--- 90,96 ----
    int add_to_size_limit;
    int spacerace;
    int turnblock;
+   int fixedlength;
    
    int num_unit_types;
    int num_tech_types;  /* including A_NONE */
***************
*** 255,261 ****
  
  #define GAME_DEFAULT_TIMEOUT          0
  #define GAME_MIN_TIMEOUT              0
! #define GAME_MAX_TIMEOUT            999
  
  #define GAME_DEFAULT_BARBARIANRATE   2
  #define GAME_MIN_BARBARIANRATE       0
--- 262,268 ----
  
  #define GAME_DEFAULT_TIMEOUT          0
  #define GAME_MIN_TIMEOUT              0
! #define GAME_MAX_TIMEOUT             86400
  
  #define GAME_DEFAULT_BARBARIANRATE   2
  #define GAME_MIN_BARBARIANRATE       0
diff -rc freeciv-1.10.0/common/packets.c freeciv-mod/common/packets.c
*** freeciv-1.10.0/common/packets.c     Sat Feb 19 05:15:47 2000
--- freeciv-mod/common/packets.c        Tue Apr 18 01:57:46 2000
***************
*** 1383,1388 ****
--- 1383,1389 ----
  
    cptr=put_uint32(cptr, pinfo->skill_level);
    cptr=put_uint16(cptr, pinfo->timeout);
+   cptr=put_uint32(cptr, pinfo->turn_start);
    cptr=put_uint32(cptr, pinfo->end_year);
    cptr=put_uint32(cptr, pinfo->year);
    cptr=put_uint8(cptr, pinfo->min_players);
***************
*** 1428,1433 ****
--- 1429,1435 ----
    iget_uint8(&iter, &pinfo->techlevel);
    iget_uint32(&iter, &pinfo->skill_level);
    iget_uint16(&iter, &pinfo->timeout);
+   iget_uint32(&iter, &pinfo->turn_start);
    iget_uint32(&iter, &pinfo->end_year);
    iget_uint32(&iter, &pinfo->year);
    iget_uint8(&iter, &pinfo->min_players);
diff -rc freeciv-1.10.0/common/packets.h freeciv-mod/common/packets.h
*** freeciv-1.10.0/common/packets.h     Wed Jan 19 16:33:21 2000
--- freeciv-mod/common/packets.h        Tue Apr 18 01:35:00 2000
***************
*** 694,699 ****
--- 694,700 ----
    int techlevel;
    int skill_level;
    int timeout;
+   time_t turn_start;
    int end_year;
    int year;
    int min_players, max_players, nplayers;
diff -rc freeciv-1.10.0/server/civserver.c freeciv-mod/server/civserver.c
*** freeciv-1.10.0/server/civserver.c   Wed Feb  2 17:06:19 2000
--- freeciv-mod/server/civserver.c      Tue Apr 18 04:02:17 2000
***************
*** 775,780 ****
--- 775,781 ----
    do_apollo_program();
    make_history_report();
    freelog(LOG_DEBUG, "Turn ended.");
+   game.turn_start = time(NULL);
    return 1;
  }
  
***************
*** 828,833 ****
--- 829,835 ----
  
    server_state=SELECT_RACES_STATE; /* loaded ??? */
    force_end_of_sniff=1;
+   game.turn_start = time(NULL);
  }
  
  
***************
*** 1103,1108 ****
--- 1105,1113 ----
  {
    int i;
  
+   /* fixedlength is only applicable if we have a timeout set */
+   if (game.fixedlength && game.timeout)
+     return 0;
    for(i=0; i<game.nplayers; i++) {
      if (game.turnblock) {
        if (!game.players[i].ai.control && game.players[i].is_alive &&
diff -rc freeciv-1.10.0/server/gamehand.c freeciv-mod/server/gamehand.c
*** freeciv-1.10.0/server/gamehand.c    Tue Feb 15 14:40:34 2000
--- freeciv-mod/server/gamehand.c       Tue Apr 18 01:35:21 2000
***************
*** 188,193 ****
--- 188,194 ----
    ginfo.techlevel=game.techlevel;
    ginfo.skill_level=game.skill_level;
    ginfo.timeout=game.timeout;
+   ginfo.turn_start=game.turn_start;
    ginfo.end_year=game.end_year;
    ginfo.year=game.year;
    ginfo.min_players=game.min_players;
diff -rc freeciv-1.10.0/server/sernet.c freeciv-mod/server/sernet.c
*** freeciv-1.10.0/server/sernet.c      Sun Jan  2 11:32:16 2000
--- freeciv-mod/server/sernet.c Tue Apr 18 03:50:03 2000
***************
*** 121,127 ****
    int max_desc;
    fd_set readfs;
    struct timeval tv;
-   static time_t time_at_turn_end;
    static int year;
  #ifdef SOCKET_ZERO_ISNT_STDIN
    char buf[BUF_SIZE+1];
--- 121,126 ----
***************
*** 129,137 ****
  #endif
    
    if(year!=game.year) {
-     time_at_turn_end = time(NULL) + game.timeout;
      if (server_state == RUN_GAME_STATE) year=game.year;
    }
    
    while(1) {
      con_prompt_on();          /* accepting new input */
--- 128,137 ----
  #endif
    
    if(year!=game.year) {
      if (server_state == RUN_GAME_STATE) year=game.year;
    }
+   if (!game.timeout)
+     game.turn_start = time(NULL);
    
    while(1) {
      con_prompt_on();          /* accepting new input */
***************
*** 142,148 ****
        return 2;
      }
      
!     tv.tv_sec=1; tv.tv_usec=0;
      
      MY_FD_ZERO(&readfs);
      FD_SET(0, &readfs);       
--- 142,149 ----
        return 2;
      }
      
!     tv.tv_sec=1;
!     tv.tv_usec=0;
      
      MY_FD_ZERO(&readfs);
      FD_SET(0, &readfs);       
***************
*** 160,166 ****
      if(select(max_desc+1, &readfs, NULL, NULL, &tv)==0) { /* timeout */
        send_server_info_to_metaserver(0,0);
        if((game.timeout) 
!       && (time(NULL)>time_at_turn_end)
        && (server_state == RUN_GAME_STATE)){
        con_prompt_off();
        return 0;
--- 161,167 ----
      if(select(max_desc+1, &readfs, NULL, NULL, &tv)==0) { /* timeout */
        send_server_info_to_metaserver(0,0);
        if((game.timeout) 
!       && (time(NULL)>game.turn_start + game.timeout)
        && (server_state == RUN_GAME_STATE)){
        con_prompt_off();
        return 0;
***************
*** 170,175 ****
--- 171,178 ----
  #endif
        continue;
      }
+     if (!game.timeout)
+       game.turn_start = time(NULL);
    
      if(FD_ISSET(sock, &readfs)) {          /* new players connects */
        freelog(LOG_VERBOSE, "got new connection");
***************
*** 225,232 ****
    con_prompt_off();
    
    if((game.timeout) 
!     && (time(NULL)>time_at_turn_end)
!     && (game.timeout)) return 0;
    return 1;
  }
    
--- 228,234 ----
    con_prompt_off();
    
    if((game.timeout) 
!     && (time(NULL)>game.turn_start + game.timeout)) return 0;
    return 1;
  }
    
diff -rc freeciv-1.10.0/server/stdinhand.c freeciv-mod/server/stdinhand.c
*** freeciv-1.10.0/server/stdinhand.c   Tue Feb 15 14:40:35 2000
--- freeciv-mod/server/stdinhand.c      Tue Apr 18 03:52:53 2000
***************
*** 539,545 ****
      N_("Turn-blocking game play mode"),
      N_("If this is set to 1 the game turn is not advanced until all players "
         "have finished their turn, including disconnected players.") },
!   
    { "demography", NULL,
      SSET_META, SSET_TO_CLIENT,
      0, 0, 0,
--- 539,552 ----
      N_("Turn-blocking game play mode"),
      N_("If this is set to 1 the game turn is not advanced until all players "
         "have finished their turn, including disconnected players.") },
! 
!   { "fixedlength", &game.fixedlength,
!     SSET_META, SSET_TO_CLIENT,
!     0, 1, 0,
!     N_("Fixed-length turns play mode"),
!     N_("If this is set to 1 the game turn will not advance until the timeout "
!        "has expired, irrespective of players clicking on \"Turn Done\".") },
! 
    { "demography", NULL,
      SSET_META, SSET_TO_CLIENT,
      0, 0, 0,
***************
*** 2106,2111 ****
--- 2113,2120 ----
                      settings[cmd].name, val);
        /* canonify map generator settings( all of which are int ) */
        adjust_terrain_param();
+       /* send any modified game parameters to the clients */
+       send_game_info(0);
        }
      } else {
        cmd_reply(CMD_SET, caller, C_SYNTAX,



[Prev in Thread] Current Thread [Next in Thread]