Author: jtn
Date: Tue Dec 22 10:51:23 2015
New Revision: 31147

URL: http://svn.gna.org/viewcvs/freeciv?rev=31147&view=rev
Log:
Tell client whether player color is set and whether it can be chosen in
pregame, to allow UI for color choosing in future.

See gna patch #6703.

Modified:
    trunk/client/colors_common.c
    trunk/client/colors_common.h
    trunk/client/packhand.c
    trunk/client/tilespec.c
    trunk/common/packets.def
    trunk/common/player.c
    trunk/common/player.h
    trunk/fc_version
    trunk/server/plrhand.c
    trunk/server/plrhand.h
    trunk/server/stdinhand.c

Modified: trunk/client/colors_common.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/client/colors_common.c?rev=31147&r1=31146&r2=31147&view=diff
==============================================================================
--- trunk/client/colors_common.c        (original)
+++ trunk/client/colors_common.c        Tue Dec 22 10:51:23 2015
@@ -106,7 +106,21 @@
 }
 
 /**********************************************************************
+  Return whether the player has a color assigned yet.
+  Should only be FALSE in pregame.
+***********************************************************************/
+bool player_has_color(const struct tileset *t,
+                      const struct player *pplayer)
+{
+  fc_assert_ret_val(pplayer != NULL, NULL);
+
+  return pplayer->rgb != NULL;
+}
+
+/**********************************************************************
   Return the color of the player.
+  In pregame, callers should check player_has_color() before calling
+  this.
 ***********************************************************************/
 struct color *get_player_color(const struct tileset *t,
                                const struct player *pplayer)

Modified: trunk/client/colors_common.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/client/colors_common.h?rev=31147&r1=31146&r2=31147&view=diff
==============================================================================
--- trunk/client/colors_common.h        (original)
+++ trunk/client/colors_common.h        Tue Dec 22 10:51:23 2015
@@ -117,6 +117,8 @@
 #include "specenum_gen.h"
 
 struct color *get_color(const struct tileset *t, enum color_std stdcolor);
+bool player_has_color(const struct tileset *t,
+                      const struct player *pplayer);
 struct color *get_player_color(const struct tileset *t,
                                const struct player *pplayer);
 struct color *get_terrain_color(const struct tileset *t,

Modified: trunk/client/packhand.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/client/packhand.c?rev=31147&r1=31146&r2=31147&view=diff
==============================================================================
--- trunk/client/packhand.c     (original)
+++ trunk/client/packhand.c     Tue Dec 22 10:51:23 2015
@@ -2116,13 +2116,21 @@
   new_player = !player_slot_is_used(pslot);
   pplayer = player_new(pslot);
 
-  if (pplayer->rgb == NULL || (pplayer->rgb->r != pinfo->color_red
-                               || pplayer->rgb->g != pinfo->color_green
-                               || pplayer->rgb->b != pinfo->color_blue)) {
-    struct rgbcolor *prgbcolor = rgbcolor_new(pinfo->color_red,
-                                              pinfo->color_green,
-                                              pinfo->color_blue);
-    fc_assert_ret(prgbcolor != NULL);
+  if ((pplayer->rgb == NULL) != !pinfo->color_valid
+      || (pinfo->color_valid &&
+          (pplayer->rgb->r != pinfo->color_red
+           || pplayer->rgb->g != pinfo->color_green
+           || pplayer->rgb->b != pinfo->color_blue))) {
+    struct rgbcolor *prgbcolor;
+
+    if (pinfo->color_valid) {
+      prgbcolor = rgbcolor_new(pinfo->color_red,
+                               pinfo->color_green,
+                               pinfo->color_blue);
+      fc_assert_ret(prgbcolor != NULL);
+    } else {
+      prgbcolor = NULL;
+    }
 
     player_set_color(pplayer, prgbcolor);
     tileset_player_init(tileset, pplayer);
@@ -2132,6 +2140,7 @@
     /* Queue a map update -- may need to redraw borders, etc. */
     update_map_canvas_visible();
   }
+  pplayer->client.color_changeable = pinfo->color_changeable;
 
   if (new_player) {
     /* Initialise client side player data (tile vision). At the moment

Modified: trunk/client/tilespec.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/client/tilespec.c?rev=31147&r1=31146&r2=31147&view=diff
==============================================================================
--- trunk/client/tilespec.c     (original)
+++ trunk/client/tilespec.c     Tue Dec 22 10:51:23 2015
@@ -6398,9 +6398,9 @@
 void tileset_player_init(struct tileset *t, struct player *pplayer)
 {
   int plrid, i, j;
+  struct sprite *color;
 
   fc_assert_ret(pplayer != NULL);
-  fc_assert_ret(pplayer->rgb != NULL);
 
   plrid = player_index(pplayer);
   fc_assert_ret(plrid >= 0);
@@ -6409,10 +6409,18 @@
   /* Free all data before recreating it. */
   tileset_player_free(t, plrid);
 
-  t->sprites.player[plrid].color
-    = create_plr_sprite(ensure_color(pplayer->rgb));
+  if (player_has_color(t, pplayer)) {
+    t->sprites.player[plrid].color = color
+      = create_plr_sprite(get_player_color(t, pplayer));
+  } else {
+    /* XXX: if player hasn't been assigned a color, perhaps there's no
+     * point proceeding with an arbitrary color; this should only happen
+     * in pregame. Probably blank sprites would be better. */
+    color = t->sprites.background.color;
+  }
+
   t->sprites.player[plrid].background
-    = crop_sprite(t->sprites.player[plrid].color, 0, 0,
+    = crop_sprite(color, 0, 0,
                   t->normal_tile_width, t->normal_tile_height,
                   t->sprites.mask.tile, 0, 0);
 
@@ -6420,9 +6428,8 @@
     for (j = 0; j < 2; j++) {
       struct sprite *s;
 
-      if (t->sprites.player[plrid].color
-          && t->sprites.grid.borders[i][j]) {
-        s = crop_sprite(t->sprites.player[plrid].color, 0, 0,
+      if (color && t->sprites.grid.borders[i][j]) {
+        s = crop_sprite(color, 0, 0,
                         t->normal_tile_width, t->normal_tile_height,
                         t->sprites.grid.borders[i][j], 0, 0);
       } else {

Modified: trunk/common/packets.def
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/packets.def?rev=31147&r1=31146&r2=31147&view=diff
==============================================================================
--- trunk/common/packets.def    (original)
+++ trunk/common/packets.def    Tue Dec 22 10:51:23 2015
@@ -825,6 +825,8 @@
   UINT16 culture;
   SINT16 love[MAX_NUM_PLAYER_SLOTS];
 
+  BOOL color_valid;
+  BOOL color_changeable;
   UINT8 color_red;
   UINT8 color_green;
   UINT8 color_blue;

Modified: trunk/common/player.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/player.c?rev=31147&r1=31146&r2=31147&view=diff
==============================================================================
--- trunk/common/player.c       (original)
+++ trunk/common/player.c       Tue Dec 22 10:51:23 2015
@@ -609,17 +609,19 @@
 
 /****************************************************************************
   Set the player's color.
+  May be NULL in pregame.
 ****************************************************************************/
 void player_set_color(struct player *pplayer,
                       const struct rgbcolor *prgbcolor)
 {
-  fc_assert_ret(prgbcolor != NULL);
-
   if (pplayer->rgb != NULL) {
     rgbcolor_destroy(pplayer->rgb);
-  }
-
-  pplayer->rgb = rgbcolor_copy(prgbcolor);
+    pplayer->rgb = NULL;
+  }
+
+  if (prgbcolor) {
+    pplayer->rgb = rgbcolor_copy(prgbcolor);
+  }
 }
 
 /****************************************************************************

Modified: trunk/common/player.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/player.h?rev=31147&r1=31146&r2=31147&view=diff
==============================================================================
--- trunk/common/player.h       (original)
+++ trunk/common/player.h       Tue Dec 22 10:51:23 2015
@@ -348,6 +348,8 @@
       enum mood_type mood;
 
       int tech_upkeep;
+
+      bool color_changeable;
     } client;
   };
 };

Modified: trunk/fc_version
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/fc_version?rev=31147&r1=31146&r2=31147&view=diff
==============================================================================
--- trunk/fc_version    (original)
+++ trunk/fc_version    Tue Dec 22 10:51:23 2015
@@ -54,7 +54,7 @@
 #   - Avoid adding a new mandatory capability to the development branch for
 #     as long as possible.  We want to maintain network compatibility with
 #     the stable branch for as long as possible.
-NETWORK_CAPSTRING_MANDATORY="+Freeciv.Devel-3.0-2015.Dec.18"
+NETWORK_CAPSTRING_MANDATORY="+Freeciv.Devel-3.0-2015.Dec.20"
 NETWORK_CAPSTRING_OPTIONAL=""
 
 FREECIV_DISTRIBUTOR=""

Modified: trunk/server/plrhand.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/server/plrhand.c?rev=31147&r1=31146&r2=31147&view=diff
==============================================================================
--- trunk/server/plrhand.c      (original)
+++ trunk/server/plrhand.c      Tue Dec 22 10:51:23 2015
@@ -1088,6 +1088,7 @@
   } players_iterate_end;
 
   if (plr->rgb != NULL) {
+    packet->color_valid = TRUE;
     packet->color_red = plr->rgb->r;
     packet->color_green = plr->rgb->g;
     packet->color_blue = plr->rgb->b;
@@ -1096,17 +1097,20 @@
      * '/list colors' etc. */
     const struct rgbcolor *preferred = player_preferred_color(plr);
     if (preferred != NULL) {
+      packet->color_valid = TRUE;
       packet->color_red = preferred->r;
       packet->color_green = preferred->g;
       packet->color_blue = preferred->b;
     } else {
       fc_assert(!game_was_started());
-      /* Can't tell the client 'no color', so use dummy values (black). */
+      packet->color_valid = FALSE;
+      /* Client shouldn't use these dummy values */
       packet->color_red = 0;
       packet->color_green = 0;
       packet->color_blue = 0;
     }
   }
+  packet->color_changeable = player_color_changeable(plr, NULL);
 
   /* Only send score if we have contact */
   if (info_level >= INFO_MEETING) {
@@ -1378,6 +1382,23 @@
 }
 
 /****************************************************************************
+  Return whether a player's color can currently be set with the
+  '/playercolor' command. If not, give a reason why not, if 'reason' is
+  not NULL (need not be freed).
+****************************************************************************/
+bool player_color_changeable(const struct player *pplayer, const char **reason)
+{
+  if (!game_was_started() && game.server.plrcolormode != PLRCOL_PLR_SET) {
+    if (reason) {
+      *reason = _("Can only set player color prior to game start if "
+                  "'plrcolormode' is PLR_SET.");
+    }
+    return FALSE;
+  }
+  return TRUE;
+}
+
+/****************************************************************************
   Permanently assign colors to any players that don't already have them.
   First assign preferred colors, then assign the rest randomly, trying to
   avoid clashes.
@@ -1477,7 +1498,7 @@
   if (prgbcolor != NULL) {
     player_set_color(pplayer, prgbcolor);
   } else {
-    /* Server only: this can be NULL in pregame. */
+    /* This can legitimately be NULL in pregame. */
     fc_assert_ret(!game_was_started());
     rgbcolor_destroy(pplayer->rgb);
     pplayer->rgb = NULL;

Modified: trunk/server/plrhand.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/server/plrhand.h?rev=31147&r1=31146&r2=31147&view=diff
==============================================================================
--- trunk/server/plrhand.h      (original)
+++ trunk/server/plrhand.h      Tue Dec 22 10:51:23 2015
@@ -27,6 +27,7 @@
                                     struct rgbcolor *prgbcolor,
                                     bool allow_ai_type_fallbacking);
 const struct rgbcolor *player_preferred_color(struct player *pplayer);
+bool player_color_changeable(const struct player *pplayer, const char 
**reason);
 void assign_player_colors(void);
 void server_player_set_color(struct player *pplayer,
                              const struct rgbcolor *prgbcolor);

Modified: trunk/server/stdinhand.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/server/stdinhand.c?rev=31147&r1=31146&r2=31147&view=diff
==============================================================================
--- trunk/server/stdinhand.c    (original)
+++ trunk/server/stdinhand.c    Tue Dec 22 10:51:23 2015
@@ -3971,12 +3971,13 @@
     goto cleanup;
   }
 
-  if (!game_was_started() && game.server.plrcolormode != PLRCOL_PLR_SET) {
-    cmd_reply(CMD_PLAYERCOLOR, caller, C_FAIL,
-              _("Can only set player color prior to game start if "
-                "'plrcolormode' is PLR_SET."));
-    ret = FALSE;
-    goto cleanup;
+  {
+    const char *reason;
+    if (!player_color_changeable(pplayer, &reason)) {
+      cmd_reply(CMD_PLAYERCOLOR, caller, C_FAIL, "%s", reason);
+      ret = FALSE;
+      goto cleanup;
+    }
   }
 
   if (0 == fc_strcasecmp(token[1], "reset")) {


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to