From cbb6311af9b0300f6879903b906bc3a90838d722 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Fri, 17 Feb 2023 20:55:56 +0200
Subject: [PATCH 15/15] Client: Fix float/integer handling trouble with the
 timeout

- Add 0.1 second marginal to the float passed to ceil(), not
  to the integer that it returns
- Treat get_seconds_to_new_turn() return value as an int
  which it is

Reported anonymously

See osdn #44902

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 client/client_main.c | 12 ++++++------
 client/text.c        |  7 ++++---
 client/text.h        |  3 +--
 3 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/client/client_main.c b/client/client_main.c
index ea5cac8b77..a6e49549db 100644
--- a/client/client_main.c
+++ b/client/client_main.c
@@ -1120,7 +1120,7 @@ void set_seconds_to_turndone(double seconds)
 
     /* Maybe we should do an update_timeout_label here, but it doesn't
      * seem to be necessary. */
-    seconds_shown_to_turndone = ceil(seconds) + 0.1;
+    seconds_shown_to_turndone = ceil(seconds + 0.1);
   }
 }
 
@@ -1137,7 +1137,7 @@ bool is_waiting_turn_change(void)
 **************************************************************************/
 void start_turn_change_wait(void)
 {
-  seconds_shown_to_new_turn = ceil(game.tinfo.last_turn_change_time) + 0.1;
+  seconds_shown_to_new_turn = ceil(game.tinfo.last_turn_change_time + 0.1);
   between_turns = timer_renew(between_turns, TIMER_USER, TIMER_ACTIVE);
   timer_start(between_turns);
 
@@ -1168,7 +1168,7 @@ int get_seconds_to_turndone(void)
 }
 
 /**************************************************************************
-  Return the number of seconds until turn-done.  Don't call this unless
+  Return the number of seconds until turn-done. Don't call this unless
   current_turn_timeout() != 0.
 **************************************************************************/
 int get_seconds_to_new_turn(void)
@@ -1212,9 +1212,9 @@ double real_timer_callback(void)
 
   /* It is possible to have current_turn_timeout() > 0 but !turndone_timer,
    * in the first moments after the timeout is set. */
-  if (current_turn_timeout() > 0 && turndone_timer) {
+  if (current_turn_timeout() > 0 && turndone_timer != NULL) {
     double seconds = seconds_to_turndone - timer_read_seconds(turndone_timer);
-    int iseconds = ceil(seconds) + 0.1; /* Turn should end right on 0. */
+    int iseconds = ceil(seconds + 0.1); /* Turn should end right on 0. */
 
     if (iseconds < seconds_shown_to_turndone) {
       seconds_shown_to_turndone = iseconds;
@@ -1226,7 +1226,7 @@ double real_timer_callback(void)
   }
   if (waiting_turn_change) {
     double seconds = game.tinfo.last_turn_change_time - timer_read_seconds(between_turns);
-    int iseconds = ceil(seconds) + 0.1; /* Turn should end right on 0. */
+    int iseconds = ceil(seconds + 0.1); /* Turn should end right on 0. */
 
     if (iseconds < game.tinfo.last_turn_change_time) {
       seconds_shown_to_new_turn = iseconds;
diff --git a/client/text.c b/client/text.c
index 583c3dde01..9966ce5037 100644
--- a/client/text.c
+++ b/client/text.c
@@ -52,6 +52,7 @@
 
 
 static int get_bulbs_per_turn(int *pours, bool *pteam, int *ptheirs);
+static const char *format_duration(int duration);
 
 /****************************************************************************
   Return a (static) string with a tile's food/prod/trade
@@ -1583,9 +1584,9 @@ const char *get_timeout_label_text(void)
   astr_clear(&str);
 
   if (is_waiting_turn_change() && game.tinfo.last_turn_change_time >= 1.5) {
-    double wt = get_seconds_to_new_turn();
+    int wt = get_seconds_to_new_turn();
 
-    if (wt < 0.01) {
+    if (wt <= 0) {
       astr_add(&str, "%s", Q_("?timeout:wait"));
     } else {
       astr_add(&str, "%s: %s", Q_("?timeout:eta"), format_duration(wt));
@@ -1609,7 +1610,7 @@ const char *get_timeout_label_text(void)
 
   Not re-entrant
 ****************************************************************************/
-const char *format_duration(int duration)
+static const char *format_duration(int duration)
 {
   static struct astring str = ASTRING_INIT;
 
diff --git a/client/text.h b/client/text.h
index 9cab7046c6..ab4fae1953 100644
--- a/client/text.h
+++ b/client/text.h
@@ -1,4 +1,4 @@
-/**********************************************************************
+/***********************************************************************
  Freeciv - Copyright (C) 2004 - The Freeciv Project
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -49,7 +49,6 @@ bool get_units_disband_info(char *buf, size_t bufsz,
 			    struct unit_list *punits);
 const char *get_spaceship_descr(struct player_spaceship *pship);
 const char *get_timeout_label_text(void);
-const char *format_duration(int duration);
 const char *get_ping_time_text(const struct player *pplayer);
 const char *get_score_text(const struct player *pplayer);
 const char *get_report_title(const char *report_name);
-- 
2.39.1