Complete.Org: Mailing Lists: Archives: freeciv-dev: December 2001:
[Freeciv-Dev] PATCH: tile markers
Home

[Freeciv-Dev] PATCH: tile markers

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] PATCH: tile markers
From: Teemu Kurppa <tkurppa@xxxxxxxxxx>
Date: Sat, 15 Dec 2001 11:53:25 +0200

The following patch is an initial attempt to introduce tile markers to
Freeciv. It allows player to place markers on tiles. These can be used
to plan city placement etc. I found them very helpful in early phase of
the game and especially with citymindist 4 or larger.

TeamCiv has similar functionality and I took the idea from there.
However, this implementation is rather lightweight compared to Teamciv's
implementation. Instead of a fancy red dot, just a little cross is drawn
on a marked tile. No modifications to tilesets are needed. 

I implemented this only to GTK-client for non-isometric tiles. 
Tiles are marked and unmarked with '*' key, similar to TeamCiv. 

A Couple of notes on the implementation:
- Tile marker locations are stored in list. For that, I introduced a new
type "coords", which is just a coordinate pair (x,y). 
  
- tile markers are drawn after and outside of tile drawing iteration. It
is better to iterate over marked tiles than to check on every tile, if
that tile is marked

- I felt a little bit uncomfortable with client code organization. It
was hard to figure out, where to add e.g. coords list defintions.
Anyway, I just put everything to somewhere and included appropriate
headers. Someone with better vision of client code's structure, can
suggest better places, if that matters. 

- Tile markers are not saved anywhere, so they are lost, if you get
temporarily disconnected. This a bit inconvenient, but I'm not sure is
it the worth of trouble to bring marker information to server.

- I didn't yet implement the view tile markers -toggle, which probably
should be there. 

Comments ?

--
Teemu Kurppa
diff -Nur -X freeciv-cvs/diff_ignore freeciv-cvs/client/civclient.c 
freeciv/client/civclient.c
--- freeciv-cvs/client/civclient.c      Fri Oct 26 10:13:55 2001
+++ freeciv/client/civclient.c  Sat Dec 15 10:52:34 2001
@@ -50,6 +50,7 @@
 #include "gui_main_g.h"
 #include "helpdata.h"          /* boot_help_texts() */
 #include "mapctrl_g.h"
+#include "mapview_common.h"
 #include "mapview_g.h"
 #include "menu_g.h"
 #include "messagewin_g.h"
@@ -176,6 +177,7 @@
   init_our_capability();
   game_init();
   attribute_init();
+  coords_list_init(&marked_tiles);
 
   /* This seed is not saved anywhere; randoms in the client should
      have cosmetic effects only (eg city name suggestions).  --dwp */
diff -Nur -X freeciv-cvs/diff_ignore freeciv-cvs/client/gui-gtk/gui_main.c 
freeciv/client/gui-gtk/gui_main.c
--- freeciv-cvs/client/gui-gtk/gui_main.c       Tue Oct  9 21:56:59 2001
+++ freeciv/client/gui-gtk/gui_main.c   Sat Dec 15 10:52:39 2001
@@ -253,7 +253,7 @@
     case GDK_KP_Enter:         key_end_turn();                 break;
 
     case GDK_Escape:           key_cancel_action();            break;
-
+      
     case GDK_t:                        key_city_workers(widget,event); break;
     default:                                                   return FALSE;
     }
@@ -301,6 +301,8 @@
     case GDK_Escape:           key_cancel_action();            break;
 
     case GDK_t:                        key_city_workers(widget,event); break;
+      
+    case GDK_asterisk:          key_mark_tile();                break;
     default:                                                   return FALSE;
     }
   }
diff -Nur -X freeciv-cvs/diff_ignore freeciv-cvs/client/gui-gtk/mapctrl.c 
freeciv/client/gui-gtk/mapctrl.c
--- freeciv-cvs/client/gui-gtk/mapctrl.c        Thu Dec 13 22:05:35 2001
+++ freeciv/client/gui-gtk/mapctrl.c    Sat Dec 15 11:19:35 2001
@@ -23,7 +23,9 @@
 
 #include "fcintl.h"
 #include "game.h"
+#include "log.h"
 #include "map.h"
+#include "mem.h" 
 #include "player.h"
 #include "support.h"
 #include "unit.h"
@@ -467,6 +469,36 @@
   return TRUE;
 }
 
+/**************************************************************************
+  Puts marker on tile.
+**************************************************************************/
+gint key_mark_tile() {
+  int x,y;
+  struct coords *pcoords;
+  
+  if(get_client_state()!=CLIENT_GAME_RUNNING_STATE)
+    return TRUE;
+  
+  /* Only these two lines are gui-specific.
+     Other code can be moved to common client code. TeeK */
+  gdk_window_get_pointer(map_canvas->window, &x, &y, NULL);
+  get_map_xy(x, y, &x, &y);
+  
+  if( (pcoords = coords_list_find(&marked_tiles, x, y)) ) {
+    coords_list_unlink(&marked_tiles, pcoords);
+    free(pcoords);
+    freelog(LOG_DEBUG, "Removed marker from tile (%d, %d)", x, y);
+  } else {
+    pcoords = fc_malloc(sizeof(struct coords));
+    pcoords->x = x;
+    pcoords->y = y;    
+    coords_list_insert(&marked_tiles, pcoords);
+    freelog(LOG_DEBUG, "Put marker on tile (%d, %d)", x, y);
+  }
+
+  update_map_canvas(x,y,1,1,1);
+  return TRUE;
+}
 
 /**************************************************************************
 ...
diff -Nur -X freeciv-cvs/diff_ignore freeciv-cvs/client/gui-gtk/mapctrl.h 
freeciv/client/gui-gtk/mapctrl.h
--- freeciv-cvs/client/gui-gtk/mapctrl.h        Mon Jan 29 20:55:28 2001
+++ freeciv/client/gui-gtk/mapctrl.h    Sat Dec 15 10:52:53 2001
@@ -20,6 +20,7 @@
 struct unit;
 
 gint key_city_workers(GtkWidget *w, GdkEventKey *ev);
+gint key_mark_tile();
 gint adjust_workers(GtkWidget *widget, GdkEventButton *ev);
 
 gint butt_down_mapcanvas(GtkWidget *w, GdkEventButton *ev);
diff -Nur -X freeciv-cvs/diff_ignore freeciv-cvs/client/gui-gtk/mapview.c 
freeciv/client/gui-gtk/mapview.c
--- freeciv-cvs/client/gui-gtk/mapview.c        Thu Dec 13 22:05:35 2001
+++ freeciv/client/gui-gtk/mapview.c    Sat Dec 15 11:24:39 2001
@@ -1138,6 +1138,27 @@
   }
 }
 
+void draw_mark_on_tile(GdkDrawable *pm, int x, int y, 
+                      int canvas_x, int canvas_y) {  
+  int dx = NORMAL_TILE_WIDTH / 4;
+  int dy = NORMAL_TILE_HEIGHT / 4;
+  dx = dx > 1 ? dx : 1;
+  dy = dy > 1 ? dy : 1;
+  
+  gdk_gc_set_foreground(civ_gc, colors_standard[3]);
+  gdk_draw_line(pm, civ_gc, 
+               canvas_x + dx, 
+               canvas_y + dy, 
+               canvas_x + NORMAL_TILE_WIDTH - dx, 
+               canvas_y + NORMAL_TILE_HEIGHT - dy);
+  gdk_draw_line(pm, civ_gc, 
+               canvas_x + dx, 
+               canvas_y + NORMAL_TILE_HEIGHT - dy, 
+               canvas_x + NORMAL_TILE_WIDTH - dx,
+               canvas_y + dy);
+}
+
+
 /**************************************************************************
 Refresh and draw to sceen all the tiles in a rectangde width,height (as
 seen in overhead ciew) with the top corner at x,y.
@@ -1268,6 +1289,15 @@
        }
       }
     }
+    
+    coords_list_iterate(marked_tiles, pcoords) {
+      if( tile_visible_mapcanvas(pcoords->x, pcoords->y) ) {
+       get_canvas_xy(pcoords->x, pcoords->y, &canvas_x, &canvas_y);
+       draw_mark_on_tile(map_canvas_store, pcoords->x, pcoords->y, 
+                         canvas_x, canvas_y);
+      }
+    }
+    coords_list_iterate_end;
 
     if (write_to_screen) {
       get_canvas_xy(x, y, &canvas_x, &canvas_y);
diff -Nur -X freeciv-cvs/diff_ignore freeciv-cvs/client/mapview_common.c 
freeciv/client/mapview_common.c
--- freeciv-cvs/client/mapview_common.c Thu Dec 13 22:05:32 2001
+++ freeciv/client/mapview_common.c     Sat Dec 15 11:22:53 2001
@@ -23,6 +23,28 @@
 #include "mapview_common.h"
 #include "tilespec.h"
 
+/* List for coordinate pairs (x,y). */ 
+#define SPECLIST_TAG coords
+#define SPECLIST_TYPE struct coords
+#include "speclist_c.h"
+
+struct coords *coords_list_find(struct coords_list *This, int x, int y) {
+  struct genlist_iterator myiter;
+  struct coords *pcoords;
+  
+  genlist_iterator_init(&myiter, &This->list, 0);
+  for(; ITERATOR_PTR(myiter); ITERATOR_NEXT(myiter)) {
+    pcoords = (struct coords*)ITERATOR_PTR(myiter);
+    if( pcoords->x == x && pcoords->y == y) 
+      return ITERATOR_PTR(myiter);
+  }
+  return 0;
+}
+
+/* List to store marked tiles. */
+struct coords_list marked_tiles;
+
+
 /**************************************************************************
  Refreshes a single tile on the map canvas.
 **************************************************************************/
@@ -260,3 +282,5 @@
     *map_view_topleft_map_y = new_map_view_y0;
   }
 }
+
+
diff -Nur -X freeciv-cvs/diff_ignore freeciv-cvs/client/mapview_common.h 
freeciv/client/mapview_common.h
--- freeciv-cvs/client/mapview_common.h Wed Nov 28 22:31:09 2001
+++ freeciv/client/mapview_common.h     Sat Dec 15 10:58:17 2001
@@ -60,4 +60,22 @@
                                int map_view_map_width,
                                int map_view_map_height);
 
+/* Coordinate pairs (x,y) and typed list for them. */
+
+struct coords {
+  int x,y;
+};
+
+#define SPECLIST_TAG coords
+#define SPECLIST_TYPE struct coords
+#include "speclist.h"
+
+#define coords_list_iterate(coordslist, pcoords) \
+    TYPED_LIST_ITERATE(struct coords, coordslist, pcoords)
+#define coords_list_iterate_end  LIST_ITERATE_END
+
+struct coords *coords_list_find(struct coords_list *This, int x, int y);
+
+extern struct coords_list marked_tiles;
+
 #endif /* FC__MAPVIEW_COMMON_H */

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