예제 #1
0
파일: tile.c 프로젝트: valisc/freeciv
/****************************************************************************
  Set the given terrain at the specified tile.
****************************************************************************/
void tile_set_terrain(struct tile *ptile, struct terrain *pterrain)
{
  /* The terrain change is valid if one of the following is TRUE:
   * - pterrain is NULL (= unknown terrain)
   * - ptile is a virtual tile
   * - pterrain does not has the flag TER_NO_CITIES
   * - there is no city on ptile.
   * This should be read as: The terrain change is INVALID if a terrain with
   * the flag TER_NO_CITIES is given for a real tile with a city (i.e. all
   * check evaluate to TRUE). */
  fc_assert_msg(NULL == pterrain
                || tile_virtual_check(ptile)
                || !terrain_has_flag(pterrain, TER_NO_CITIES)
                || NULL == tile_city(ptile),
                "At (%d, %d), the terrain \"%s\" (nb %d) doesn't "
                "support cities, whereas \"%s\" (nb %d) is built there.",
                TILE_XY(ptile), terrain_rule_name(pterrain),
                terrain_number(pterrain), city_name(tile_city(ptile)),
                tile_city(ptile)->id);

  ptile->terrain = pterrain;
  if (NULL != pterrain
      && NULL != ptile->resource
      && terrain_has_resource(pterrain, ptile->resource)) {
    ptile->resource_valid = TRUE;
  } else {
    ptile->resource_valid = FALSE;
  }
}
예제 #2
0
/*****************************************************************************
  Find best tile the paratrooper should jump to.
*****************************************************************************/
static struct tile *find_best_tile_to_paradrop_to(struct ai_type *ait,
                                                  struct unit *punit)
{
  int best = 0;
  int val;
  struct tile* best_tile = NULL;
  int range = unit_type(punit)->paratroopers_range;
  struct city* acity;
  struct player* pplayer = unit_owner(punit);

  /* First, we search for undefended cities in danger */
  square_iterate(unit_tile(punit), range, ptile) {
    if (!map_is_known(ptile, pplayer)) {
      continue;
    }
  
    acity = tile_city(ptile);
    if (acity && city_owner(acity) == unit_owner(punit)
        && unit_list_size(ptile->units) == 0) {
      val = city_size_get(acity) * def_ai_city_data(acity, ait)->urgency;
      if (val > best) {
	best = val;
	best_tile = ptile;
      }
    }
  } square_iterate_end;
  
  if (best_tile != NULL) {
    acity = tile_city(best_tile);
    UNIT_LOG(LOGLEVEL_PARATROOPER, punit, 
             "Choose to jump in order to protect allied city %s (%d %d). "
	     "Benefit: %d",
	     city_name(acity), TILE_XY(best_tile), best);
    return best_tile;
  }

  /* Second, we search for undefended enemy cities */
  square_iterate(unit_tile(punit), range, ptile) {
    acity = tile_city(ptile);
    if (acity && pplayers_at_war(unit_owner(punit), city_owner(acity)) &&
        (unit_list_size(ptile->units) == 0)) {
      if (!map_is_known_and_seen(ptile, pplayer, V_MAIN)
          && ai_handicap(pplayer, H_FOG)) {
        continue;
      }
      /* Prefer big cities on other continents */
      val = city_size_get(acity)
            + (tile_continent(unit_tile(punit)) != tile_continent(ptile));
      if (val > best) {
        best = val;
	best_tile = ptile;
      }
    }
  } square_iterate_end;
예제 #3
0
/***********************************************************************
  Iterates through reachable cities and appraises them as a possible 
  base for air operations by (air)unit punit.  Returns NULL if not
  found.  The path is stored in the path argument if not NULL.
**********************************************************************/
static struct tile *dai_find_strategic_airbase(struct ai_type *ait,
                                               const struct unit *punit,
                                               struct pf_path **path)
{
  struct player *pplayer = unit_owner(punit);
  struct pf_parameter parameter;
  struct pf_map *pfm;
  struct tile *best_tile = NULL;
  struct city *pcity;
  struct unit *pvirtual = NULL;
  int best_worth = 0, target_worth;

  pft_fill_unit_parameter(&parameter, punit);
  pfm = pf_map_new(&parameter);
  pf_map_move_costs_iterate(pfm, ptile, move_cost, FALSE) {
    if (move_cost >= punit->moves_left) {
      break; /* Too far! */
    }

    if (!is_airunit_refuel_point(ptile, pplayer,
                                 unit_type(punit), FALSE)) {
      continue; /* Cannot refuel here. */
    }

    if ((pcity = tile_city(ptile))
        && def_ai_city_data(pcity, ait)->grave_danger != 0) {
      best_tile = ptile;
      break; /* Fly there immediately!! */
    }

    if (!pvirtual) {
      pvirtual =
        unit_virtual_create(pplayer,
                            player_city_by_number(pplayer, punit->homecity),
                            unit_type(punit), punit->veteran);
    }

    unit_tile_set(pvirtual, ptile);
    target_worth = find_something_to_bomb(ait, pvirtual, NULL, NULL);
    if (target_worth > best_worth) {
      /* It's either a first find or it's better than the previous. */
      best_worth = target_worth;
      best_tile = ptile;
      /* We can still look for something better. */
    }
  } pf_map_move_costs_iterate_end;

  if (pvirtual) {
    unit_virtual_destroy(pvirtual);
  }

  if (path) {
    /* Stores the path. */
    *path = best_tile ? pf_map_path(pfm, best_tile) : NULL;
  }
  pf_map_destroy(pfm);

  return best_tile;
}
예제 #4
0
/****************************************************************************
  Is tile native to base?
****************************************************************************/
bool is_native_tile_to_base(const struct base_type *pbase,
                            const struct tile *ptile)
{
  /* FIXME: Bases cannot be built in a city currently?  It should be a
   * a negative requirement in the ruleset. */
  return (!tile_city(ptile)
          && are_reqs_active(NULL, NULL, NULL, ptile,
                             NULL, NULL, NULL, &pbase->reqs, RPT_POSSIBLE));
}
예제 #5
0
/****************************************************************************
  Return color for overview map tile.
****************************************************************************/
static struct color *overview_tile_color(struct tile *ptile)
{
  if (overview.layers[OLAYER_CITIES]) {
    struct city *pcity = tile_city(ptile);

    if (pcity) {
      if (NULL == client.conn.playing
          || city_owner(pcity) == client.conn.playing) {
	return get_color(tileset, COLOR_OVERVIEW_MY_CITY);
      } else if (pplayers_allied(city_owner(pcity), client.conn.playing)) {
	/* Includes teams. */
	return get_color(tileset, COLOR_OVERVIEW_ALLIED_CITY);
      } else {
	return get_color(tileset, COLOR_OVERVIEW_ENEMY_CITY);
      }
    }
  }
  if (overview.layers[OLAYER_UNITS]) {
    struct unit *punit = find_visible_unit(ptile);

    if (punit) {
      if (NULL == client.conn.playing
          || unit_owner(punit) == client.conn.playing) {
	return get_color(tileset, COLOR_OVERVIEW_MY_UNIT);
      } else if (pplayers_allied(unit_owner(punit), client.conn.playing)) {
	/* Includes teams. */
	return get_color(tileset, COLOR_OVERVIEW_ALLIED_UNIT);
      } else {
	return get_color(tileset, COLOR_OVERVIEW_ENEMY_UNIT);
      }
    }
  }
  if (overview.layers[OLAYER_BORDERS]) {
    struct player *owner = tile_owner(ptile);

    if (owner) {
      if (overview.layers[OLAYER_BORDERS_ON_OCEAN]) {
        return get_player_color(tileset, owner);
      } else if (!is_ocean_tile(ptile)) {
        return get_player_color(tileset, owner);
      }
    }
  }
  if (overview.layers[OLAYER_RELIEF] && tile_terrain(ptile) != T_UNKNOWN) {
    return get_terrain_color(tileset, tile_terrain(ptile));
  }
  if (overview.layers[OLAYER_BACKGROUND] && tile_terrain(ptile) != T_UNKNOWN) {
    if (is_ocean_tile(ptile)) {
      return get_color(tileset, COLOR_OVERVIEW_OCEAN);
    } else {
      return get_color(tileset, COLOR_OVERVIEW_LAND);
    }
  }

  return get_color(tileset, COLOR_OVERVIEW_UNKNOWN);
}
예제 #6
0
/**************************************************************************
  Log unit messages, they will appear like this
    2: Polish Archers[139] (5,35)->(0,0){0,0} stays to defend city
  where [] is unit id, ()->() are coordinates present and goto, and
  {,} contains bodyguard and ferryboat ids.
**************************************************************************/
void UNIT_LOG(int level, const struct unit *punit, const char *msg, ...)
{
  char buffer[500];
  char buffer2[500];
  va_list ap;
  int minlevel = MIN(LOGLEVEL_UNIT, level);
  int gx, gy;
  bool messwin = FALSE; /* output to message window */

  if (punit->debug) {
    minlevel = LOG_TEST;
  } else {
    /* Are we a virtual unit evaluated in a debug city?. */
    if (punit->id == 0) {
      struct city *pcity = tile_city(punit->tile);

      if (pcity && pcity->debug) {
        minlevel = LOG_TEST;
        messwin = TRUE;
      }
    }
    if (minlevel > fc_log_level) {
      return;
    }
  }

  if (punit->goto_tile) {
    gx = punit->goto_tile->x;
    gy = punit->goto_tile->y;
  } else {
    gx = gy = -1;
  }
  
  my_snprintf(buffer, sizeof(buffer),
	      "%s %s[%d] %s (%d,%d)->(%d,%d){%d,%d} ",
              nation_rule_name(nation_of_unit(punit)),
              unit_rule_name(punit),
              punit->id,
	      get_activity_text(punit->activity),
	      TILE_XY(punit->tile),
	      gx, gy,
              punit->ai.bodyguard, punit->ai.ferryboat);

  va_start(ap, msg);
  my_vsnprintf(buffer2, sizeof(buffer2), msg, ap);
  va_end(ap);

  cat_snprintf(buffer, sizeof(buffer), "%s", buffer2);
  if (punit->debug || messwin) {
    notify_conn(NULL, NULL, E_AI_DEBUG, ftc_log, "%s", buffer);
  }
  freelog(minlevel, "%s", buffer);
}
예제 #7
0
/**************************************************************************
  Update city dialogs when the given unit's status changes.  This
  typically means updating both the unit's home city (if any) and the
  city in which it is present (if any).
**************************************************************************/
void refresh_unit_city_dialogs(struct unit *punit)
{
	freelog(LOG_VERBOSE, "Port Me %s [@%s:%d]", __func__, __FILE__, __LINE__);
#if 0
  /* Demo code */
  struct city *pcity_sup, *pcity_pre;
  struct city_dialog *pdialog;

  pcity_sup = game_find_city_by_number(punit->homecity);
  pcity_pre = tile_city(punit->tile);

  if (pcity_sup && (pdialog = get_city_dialog(pcity_sup))) {
    city_dialog_update_supported_units(pdialog);
  }

  if (pcity_pre && (pdialog = get_city_dialog(pcity_pre))) {
    city_dialog_update_present_units(pdialog);
  }
#endif
}
예제 #8
0
/**********************************************************************
  Very preliminary estimate for our intent to attack the tile (x, y).
  Used by bombers only.
**********************************************************************/
static bool dai_should_we_air_attack_tile(struct ai_type *ait,
                                          struct unit *punit, struct tile *ptile)
{
  struct city *acity = tile_city(ptile);

  /* For a virtual unit (punit->id == 0), all targets are good */
  /* TODO: There is a danger of producing too many units that will not 
   * attack anything.  Production should not happen if there is an idle 
   * unit of the same type nearby */
  if (acity && punit->id != 0
      && def_ai_city_data(acity, ait)->invasion.occupy == 0
      && !unit_can_take_over(punit)) {
    /* No units capable of occupying are invading */
    log_debug("Don't want to attack %s, although we could",
              city_name(acity));
    return FALSE;
  }

  return TRUE;
}
예제 #9
0
파일: infracache.c 프로젝트: valisc/freeciv
/**************************************************************************
  Calculate the benefit of transforming the given tile.

  The return value is the goodness of the tile after the transform.  This
  should be compared to the goodness of the tile currently (see
  city_tile_value(); note that this depends on the AI's weighting
  values).
**************************************************************************/
static int adv_calc_transform(const struct city *pcity,
                             const struct tile *ptile)
{
  int goodness;
  struct tile *vtile;
  struct terrain *old_terrain, *new_terrain;

  fc_assert_ret_val(ptile != NULL, -1)

  old_terrain = tile_terrain(ptile);
  new_terrain = old_terrain->transform_result;

  if (old_terrain == new_terrain || new_terrain == T_NONE) {
    return -1;
  }

  if (is_ocean(old_terrain) && !is_ocean(new_terrain)
      && !can_reclaim_ocean(ptile)) {
    /* Can't change ocean into land here. */
    return -1;
  }
  if (is_ocean(new_terrain) && !is_ocean(old_terrain)
      && !can_channel_land(ptile)) {
    /* Can't change land into ocean here. */
    return -1;
  }

  if (tile_city(ptile) && terrain_has_flag(new_terrain, TER_NO_CITIES)) {
    return -1;
  }

  vtile = tile_virtual_new(ptile);
  tile_change_terrain(vtile, new_terrain);
  goodness = city_tile_value(pcity, vtile, 0, 0);
  tile_virtual_destroy(vtile);

  return goodness;
}
예제 #10
0
PyObject* get_unit_properties(struct unit* s) {
    struct terrain* t = tile_terrain(s->tile);
    return Py_BuildValue("llOs", (long)s, (long)s->tile, py_mapper_city(tile_city(s->tile)),
                terrain_rule_name(t));
}
예제 #11
0
/************************************************************************
  Trying to manage bombers and stuff.
  If we are in the open {
    if moving intelligently on a valid GOTO, {
      carry on doing it.
    } else {
      go refuel
    }
  } else {
    try to attack something
  } 
  TODO: distant target selection, support for fuel > 2
***********************************************************************/
void dai_manage_airunit(struct ai_type *ait, struct player *pplayer,
                        struct unit *punit)
{
  struct tile *dst_tile = unit_tile(punit);
  /* Loop prevention */
  int moves = punit->moves_left;
  int id = punit->id;
  struct pf_parameter parameter;
  struct pf_map *pfm;
  struct pf_path *path;

  CHECK_UNIT(punit);

  if (!is_unit_being_refueled(punit)) {
    /* We are out in the open, what shall we do? */
    if (punit->activity == ACTIVITY_GOTO
        /* We are on a GOTO.  Check if it will get us anywhere */
        && NULL != punit->goto_tile
        && !same_pos(unit_tile(punit), punit->goto_tile)
        && is_airunit_refuel_point(punit->goto_tile, 
                                   pplayer, unit_type(punit), FALSE)) {
      pfm = pf_map_new(&parameter);
      path = pf_map_path(pfm, punit->goto_tile);
      if (path) {
        bool alive = adv_follow_path(punit, path, punit->goto_tile);

        pf_path_destroy(path);
        pf_map_destroy(pfm);
        if (alive && punit->moves_left > 0) {
          /* Maybe do something else. */
          dai_manage_airunit(ait, pplayer, punit);
        }
        return;
      }
      pf_map_destroy(pfm);
    } else if ((dst_tile = find_nearest_airbase(punit, &path))) {
      /* Go refuelling */
      if (!adv_follow_path(punit, path, dst_tile)) {
        pf_path_destroy(path);
        return; /* The unit died. */
      }
      pf_path_destroy(path);
    } else {
      if (punit->fuel == 1) {
	UNIT_LOG(LOG_DEBUG, punit, "Oops, fallin outta the sky");
      }
      def_ai_unit_data(punit, ait)->done = TRUE; /* Won't help trying again */
      return;
    }

  } else if (punit->fuel == unit_type(punit)->fuel) {
    /* We only leave a refuel point when we are on full fuel */

    if (find_something_to_bomb(ait, punit, &path, &dst_tile) > 0) {
      /* Found target, coordinates are in punit's goto_dest.
       * TODO: separate attacking into a function, check for the best 
       * tile to attack from */
      fc_assert_ret(path != NULL && dst_tile != NULL);
      if (!adv_follow_path(punit, path, dst_tile)) {
        pf_path_destroy(path);
        return; /* The unit died. */
      }
      pf_path_destroy(path);

      /* goto would be aborted: "Aborting GOTO for AI attack procedures"
       * now actually need to attack */
      /* We could use ai_military_findvictim here, but I don't trust it... */
      unit_activity_handling(punit, ACTIVITY_IDLE);
      if (is_tiles_adjacent(unit_tile(punit), dst_tile)) {
        (void) unit_move_handling(punit, dst_tile, TRUE, FALSE);
      }
    } else if ((dst_tile = dai_find_strategic_airbase(ait, punit, &path))) {
      log_debug("%s will fly to (%i, %i) (%s) to fight there",
                unit_rule_name(punit), TILE_XY(dst_tile),
                tile_city(dst_tile) ? city_name(tile_city(dst_tile)) : "");
      def_ai_unit_data(punit, ait)->done = TRUE; /* Wait for next turn */
      if (!adv_follow_path(punit, path, dst_tile)) {
        pf_path_destroy(path);
        return; /* The unit died. */
      }
      pf_path_destroy(path);
    } else {
      log_debug("%s cannot find anything to kill and is staying put", 
                unit_rule_name(punit));
      def_ai_unit_data(punit, ait)->done = TRUE;
      unit_activity_handling(punit, ACTIVITY_IDLE);
    }
  }

  if ((punit = game_unit_by_number(id)) != NULL && punit->moves_left > 0
      && punit->moves_left != moves) {
    /* We have moved this turn, might have ended up stuck out in the fields
     * so, as a safety measure, let's manage again */
    dai_manage_airunit(ait, pplayer, punit);
  }

}
예제 #12
0
/**************************************************************************
  Make a chat link at the current position or make the current selection
  clickable.
**************************************************************************/
void inputline_make_chat_link(struct tile *ptile, bool unit)
{
  char buf[MAX_LEN_MSG];
  GtkWidget *entry = toolkit.entry;
  GtkEditable *editable = GTK_EDITABLE(entry);
  gint start_pos, end_pos;
  gchar *chars;
  struct unit *punit;

  /* Get the target. */
  if (unit) {
    punit = find_visible_unit(ptile);
    if (!punit) {
      output_window_append(ftc_client, _("No visible unit on this tile."));
      return;
    }
  } else {
    punit = NULL;
  }

  if (gtk_editable_get_selection_bounds(editable, &start_pos, &end_pos)) {
    /* There is a selection, make it clickable. */
    gpointer target;
    enum text_link_type type;

    chars = gtk_editable_get_chars(editable, start_pos, end_pos);
    if (punit) {
      type = TLT_UNIT;
      target = punit;
    } else if (tile_city(ptile)) {
      type = TLT_CITY;
      target = tile_city(ptile);
    } else {
      type = TLT_TILE;
      target = ptile;
    }

    if (0 != featured_text_apply_tag(chars, buf, sizeof(buf), TTT_LINK,
                                     0, FT_OFFSET_UNSET, type, target)) {
      /* Replace the selection. */
      gtk_editable_delete_text(editable, start_pos, end_pos);
      end_pos = start_pos;
      gtk_editable_insert_text(editable, buf, -1, &end_pos);
      gtk_widget_grab_focus(entry);
      gtk_editable_select_region(editable, start_pos, end_pos);
    }
  } else {
    /* Just insert the link at the current position. */
    start_pos = gtk_editable_get_position(editable);
    end_pos = start_pos;
    chars = gtk_editable_get_chars(editable, MAX(start_pos - 1, 0),
                                   start_pos + 1);
    if (punit) {
      sz_strlcpy(buf, unit_link(punit));
    } else if (tile_city(ptile)) {
      sz_strlcpy(buf, city_link(tile_city(ptile)));
    } else {
      sz_strlcpy(buf, tile_link(ptile));
    }

    if (start_pos > 0 && strlen(chars) > 0 && chars[0] != ' ') {
      /* Maybe insert an extra space. */
      gtk_editable_insert_text(editable, " ", 1, &end_pos);
    }
    gtk_editable_insert_text(editable, buf, -1, &end_pos);
    if (chars[start_pos > 0 ? 1 : 0] != '\0'
        && chars[start_pos > 0 ? 1 : 0] != ' ') {
      /* Maybe insert an extra space. */
      gtk_editable_insert_text(editable, " ", 1, &end_pos);
    }
    gtk_widget_grab_focus(entry);
    gtk_editable_set_position(editable, end_pos);
  }

  g_free(chars);
}
예제 #13
0
/****************************************************************
 Popup new diplomat dialog.
*****************************************************************/
void popup_diplomat_dialog(struct unit *punit, struct tile *dest_tile)
{
    struct city *pcity;
    struct unit *ptunit;
    GtkWidget *shl;
    struct astring title = ASTRING_INIT, text = ASTRING_INIT;

    diplomat_target_id[ATK_CITY] = -1;
    diplomat_target_id[ATK_UNIT] = -1;

    diplomat_id = punit->id;

    astr_set(&title,
             /* TRANS: %s is a unit name, e.g., Spy */
             _("Choose Your %s's Strategy"), unit_name_translation(punit));
    astr_set(&text,
             /* TRANS: %s is a unit name, e.g., Diplomat, Spy */
             _("Your %s is waiting for your command."),
             unit_name_translation(punit));

    shl = choice_dialog_start(GTK_WINDOW(toplevel), astr_str(&title),
                              astr_str(&text));

    if ((pcity = tile_city(dest_tile))) {
        /* Spy/Diplomat acting against a city */

        diplomat_target_id[ATK_CITY] = pcity->id;

        action_entry(shl,
                     action_enabled_unit_on_city_local(
                         ACTION_ESTABLISH_EMBASSY, punit, pcity),
                     _("Establish _Embassy"),
                     (GCallback)diplomat_embassy_callback);

        action_entry(shl,
                     action_enabled_unit_on_city_local(
                         ACTION_SPY_INVESTIGATE_CITY, punit, pcity),
                     _("_Investigate City"),
                     (GCallback)diplomat_investigate_callback);

        action_entry(shl,
                     action_enabled_unit_on_city_local(ACTION_SPY_POISON,
                             punit, pcity),
                     _("_Poison City"),
                     (GCallback)spy_poison_callback);

        action_entry(shl,
                     action_enabled_unit_on_city_local(
                         ACTION_SPY_SABOTAGE_CITY, punit, pcity),
                     _("_Sabotage City"),
                     (GCallback)diplomat_sabotage_callback);

        action_entry(shl,
                     action_enabled_unit_on_city_local(
                         ACTION_SPY_TARGETED_SABOTAGE_CITY, punit, pcity),
                     _("Industrial _Sabotage"),
                     (GCallback)spy_request_sabotage_list);

        action_entry(shl,
                     action_enabled_unit_on_city_local(
                         ACTION_SPY_STEAL_TECH, punit, pcity),
                     _("Steal _Technology"),
                     (GCallback)diplomat_steal_callback);

        action_entry(shl,
                     action_enabled_unit_on_city_local(
                         ACTION_SPY_TARGETED_STEAL_TECH, punit, pcity),
                     _("Industrial espionage"),
                     (GCallback)spy_steal_popup);

        action_entry(shl,
                     action_enabled_unit_on_city_local(ACTION_SPY_INCITE_CITY,
                             punit, pcity),
                     _("Incite a _Revolt"),
                     (GCallback)diplomat_incite_callback);
    }

    if ((ptunit = unit_list_get(dest_tile->units, 0))) {
        /* Spy/Diplomat acting against a unit */

        diplomat_target_id[ATK_UNIT] = ptunit->id;

        action_entry(shl,
                     action_enabled_unit_on_unit_local(
                         ACTION_SPY_BRIBE_UNIT, punit, ptunit),
                     _("_Bribe Enemy Unit"),
                     (GCallback)diplomat_bribe_callback);

        action_entry(shl,
                     action_enabled_unit_on_unit_local(
                         ACTION_SPY_SABOTAGE_UNIT, punit, ptunit),
                     _("_Sabotage Enemy Unit"),
                     (GCallback)spy_sabotage_unit_callback);
    }

    if (diplomat_can_do_action(punit, DIPLOMAT_MOVE, dest_tile)) {
        if (pcity) {
            choice_dialog_add(shl, _("_Keep moving"),
                              (GCallback)diplomat_keep_moving_city_callback,
                              NULL, FALSE);
        } else {
            choice_dialog_add(shl, _("_Keep moving"),
                              (GCallback)diplomat_keep_moving_unit_callback,
                              NULL, FALSE);
        }
    }

    choice_dialog_add(shl, GTK_STOCK_CANCEL,
                      (GCallback)diplomat_cancel_callback, NULL, FALSE);

    choice_dialog_end(shl);

    diplomat_dialog = shl;

    choice_dialog_set_hide(shl, TRUE);
    g_signal_connect(shl, "destroy",
                     G_CALLBACK(diplomat_destroy_callback), NULL);
    g_signal_connect(shl, "delete_event",
                     G_CALLBACK(diplomat_cancel_callback), NULL);
    astr_free(&title);
    astr_free(&text);
}
예제 #14
0
// TODO: Support irrigation modes
JNIEXPORT jbyteArray JNICALL Java_net_hackcasual_freeciv_NativeHarness_getAvailableCommandsForActiveUnit
  (JNIEnv * env, jobject obj) {
	struct unit * pUnit;
	char availCommands[COM_UNIT_LAST];
	int commandCount = 0;

	pUnit = head_of_units_in_focus();

	if (pUnit) {

	    struct tile *pTile = pUnit->tile;
	    struct city *pCity = tile_city(pTile);
	    struct terrain *pTerrain = tile_terrain(pTile);
	    struct base_type *pbase;
	    struct unit_list *punits = get_units_in_focus();


		if (can_unit_build_city(pUnit)) {
			availCommands[commandCount++] = COM_BUILD_CITY;
		}

		if (can_unit_do_activity(pUnit, ACTIVITY_GOTO)) {
			availCommands[commandCount++] = COM_GO_TO;
			availCommands[commandCount++] = COM_GO_TO_CITY;
		}

		if (can_unit_do_activity(pUnit, ACTIVITY_SENTRY)) {
			availCommands[commandCount++] = COM_SENTRY;
		}

		if (can_unit_do_activity(pUnit, ACTIVITY_FORTIFYING)) {
			availCommands[commandCount++] = COM_FORTIFY;
		}

		if (can_unit_do_activity(pUnit, ACTIVITY_EXPLORE)) {
			availCommands[commandCount++] = COM_EXPLORE;
		}

		availCommands[commandCount++] = COM_DISBAND;

		if (can_unit_do_activity(pUnit, ACTIVITY_ROAD) || can_unit_do_activity(pUnit, ACTIVITY_RAILROAD)) {
			availCommands[commandCount++] = COM_ROAD;
		}

		if (can_unit_do_activity(pUnit, ACTIVITY_IRRIGATE)) {
			availCommands[commandCount++] = COM_IRRIGATION;
		}

		if (can_unit_do_autosettlers(pUnit)) {
			availCommands[commandCount++] = COM_AUTO_WORKER;
		}

		if (can_unit_do_connect(pUnit, ACTIVITY_IRRIGATE)) {
			availCommands[commandCount++] = COM_CONNECT_IRRIGATION;
		}

		if (can_unit_do_connect(pUnit, ACTIVITY_ROAD)) {
			availCommands[commandCount++] = COM_CONNECT_ROAD;
		}

		if (can_unit_do_connect(pUnit, ACTIVITY_RAILROAD)) {
			availCommands[commandCount++] = COM_CONNECT_RAILROAD;
		}

		availCommands[commandCount++] = COM_WAIT;

		if (unit_can_help_build_wonder_here(pUnit)) {
			availCommands[commandCount++] = COM_BUILD_WONDER;
		}

		if (unit_can_est_trade_route_here(pUnit)) {
			availCommands[commandCount++] = COM_TRADE_ROUTE;
		}

		if (can_unit_do_activity(pUnit, ACTIVITY_MINE)) {
			availCommands[commandCount++] = COM_MINE;
		}

		if (can_unit_do_activity(pUnit, ACTIVITY_TRANSFORM)) {
			availCommands[commandCount++] = COM_TRANSFORM;
		}

		pbase = get_base_by_gui_type(BASE_GUI_FORTRESS, pUnit, pUnit->tile);

		if (!pCity && pbase) {
			availCommands[commandCount++] = COM_FORTRESS;
		}

		pbase = get_base_by_gui_type(BASE_GUI_AIRBASE, pUnit, pUnit->tile);

		if (!pCity && pbase) {
			availCommands[commandCount++] = COM_AIRBASE;
		}

		if (can_unit_do_activity(pUnit, ACTIVITY_POLLUTION)) {
			availCommands[commandCount++] = COM_POLLUTION;
		}

		if (can_unit_do_activity(pUnit, ACTIVITY_FALLOUT)) {
			availCommands[commandCount++] = COM_FALLOUT;
		}

		if (can_unit_paradrop(pUnit)) {
			availCommands[commandCount++] = COM_PARADROP;
		}

		if (can_unit_do_activity(pUnit, ACTIVITY_PILLAGE)) {
			availCommands[commandCount++] = COM_FALLOUT;
		}

	    if (pCity && can_unit_change_homecity(pUnit)
			&& pCity->id != pUnit->homecity) {
	    	  availCommands[commandCount++] = COM_HOMECITY;
	    }

	    if (pUnit->occupy && get_transporter_occupancy(pUnit) > 0) {
	    	availCommands[commandCount++] = COM_UNLOAD_TRANSPORT;
	    }

	    if (units_can_load(punits)) {
	    	availCommands[commandCount++] = COM_LOAD;
	    }

	    if (units_can_unload(punits)) {
	    	availCommands[commandCount++] = COM_UNLOAD;
	    }

	    if (pCity && pCity->airlift) {
	    	availCommands[commandCount++] = COM_AIRLIFT;
	    }

	    if (pCity && can_upgrade_unittype(client.conn.playing, unit_type(pUnit))) {
	    	availCommands[commandCount++] = COM_UPGRADE;
	    }

	    if (diplomat_can_do_action(pUnit, DIPLOMAT_ANY_ACTION, pUnit->tile)) {
			availCommands[commandCount++] = COM_DIPLOMAT;
		}

	    if (unit_has_type_flag(pUnit, F_NUCLEAR)) {
	    	availCommands[commandCount++] = COM_NUKE;
	    }

	}



	jbyteArray commands;

	commands = (*env)->NewByteArray(env, commandCount);

	(*env)->SetByteArrayRegion(env, commands, 0,
			commandCount, (jbyte *)availCommands);

	return commands;
}
예제 #15
0
/**************************************************************************
  Popup a dialog giving a diplomatic unit some options when moving into
  the target tile.
**************************************************************************/
void popup_diplomat_dialog(struct unit *pUnit, struct tile *ptile)
{
  struct widget *pWindow = NULL, *pBuf = NULL;
  SDL_String16 *pStr;
  struct city *pCity;
  struct unit *pTunit;
  bool spy;
  SDL_Rect area;
  
  if (pDiplomat_Dlg) {
    return;
  }
  
  is_unit_move_blocked = TRUE;
  pCity = tile_city(ptile);
  spy = unit_has_type_flag(pUnit, F_SPY);
  
  pDiplomat_Dlg = fc_calloc(1, sizeof(struct diplomat_dialog));
  pDiplomat_Dlg->diplomat_id = pUnit->id;
  pDiplomat_Dlg->pdialog = fc_calloc(1, sizeof(struct ADVANCED_DLG));
  
  /* window */
  if (pCity)
  {
    if(spy) {
      pStr = create_str16_from_char(_("Choose Your Spy's Strategy") , adj_font(12));
    }
    else
    {
      pStr = create_str16_from_char(_("Choose Your Diplomat's Strategy"), adj_font(12));
    }
  }
  else
  {
    pStr = create_str16_from_char(_("Subvert Enemy Unit"), adj_font(12));
  }
  
  pStr->style |= TTF_STYLE_BOLD;
  
  pWindow = create_window_skeleton(NULL, pStr, 0);
    
  pWindow->action = diplomat_dlg_window_callback;
  set_wstate(pWindow, FC_WS_NORMAL);
  
  add_to_gui_list(ID_CARAVAN_DLG_WINDOW, pWindow);
  pDiplomat_Dlg->pdialog->pEndWidgetList = pWindow;
    
  area = pWindow->area;
  area.w = MAX(area.w, adj_size(8));
  area.h = MAX(area.h, adj_size(2));
  
  /* ---------- */
  if((pCity))
  {
    /* Spy/Diplomat acting against a city */

    pDiplomat_Dlg->diplomat_target_id = pCity->id;    
    
    /* -------------- */
    if (diplomat_can_do_action(pUnit, DIPLOMAT_EMBASSY, ptile))
    {
	
      create_active_iconlabel(pBuf, pWindow->dst, pStr,
	    _("Establish Embassy"), diplomat_embassy_callback);
      
      pBuf->data.city = pCity;
      set_wstate(pBuf, FC_WS_NORMAL);
  
      add_to_gui_list(MAX_ID - pUnit->id, pBuf);
    
      area.w = MAX(area.w, pBuf->size.w);
      area.h += pBuf->size.h;
    }
  
    /* ---------- */
    if (diplomat_can_do_action(pUnit, DIPLOMAT_INVESTIGATE, ptile)) {
    
      create_active_iconlabel(pBuf, pWindow->dst, pStr,
			      _("Investigate City"),
			      diplomat_investigate_callback);
      pBuf->data.city = pCity;
      set_wstate(pBuf, FC_WS_NORMAL);
  
      add_to_gui_list(MAX_ID - pUnit->id, pBuf);
    
      area.w = MAX(area.w, pBuf->size.w);
      area.h += pBuf->size.h;
    }
  
    /* ---------- */
    if (spy && diplomat_can_do_action(pUnit, SPY_POISON, ptile)) {
    
      create_active_iconlabel(pBuf, pWindow->dst, pStr,
	    _("Poison City"), spy_poison_callback);
      
      pBuf->data.city = pCity;
      set_wstate(pBuf, FC_WS_NORMAL);
  
      add_to_gui_list(MAX_ID - pUnit->id, pBuf);
    
      area.w = MAX(area.w, pBuf->size.w);
      area.h += pBuf->size.h;
    }    
    /* ---------- */
    if (diplomat_can_do_action(pUnit, DIPLOMAT_SABOTAGE, ptile)) {
    
      create_active_iconlabel(pBuf, pWindow->dst, pStr,
	    _("Sabotage City"), 
      		spy ? spy_sabotage_request : diplomat_sabotage_callback);
      
      pBuf->data.city = pCity;
      set_wstate(pBuf, FC_WS_NORMAL);
  
      add_to_gui_list(MAX_ID - pUnit->id, pBuf);
    
      area.w = MAX(area.w, pBuf->size.w);
      area.h += pBuf->size.h;
    }
  
    /* ---------- */
    if (diplomat_can_do_action(pUnit, DIPLOMAT_STEAL, ptile)) {
    
      create_active_iconlabel(pBuf, pWindow->dst, pStr,
	    _("Steal Technology"),
      			spy ? spy_steal_popup : diplomat_steal_callback);
      pBuf->data.city = pCity;
      set_wstate(pBuf , FC_WS_NORMAL);
  
      add_to_gui_list(MAX_ID - pUnit->id , pBuf);
    
      area.w = MAX(area.w , pBuf->size.w);
      area.h += pBuf->size.h;
    }
      
    /* ---------- */
    if (diplomat_can_do_action(pUnit, DIPLOMAT_INCITE, ptile)) {
    
      create_active_iconlabel(pBuf, pWindow->dst, pStr,
	    _("Incite a Revolt"), diplomat_incite_callback);
      pBuf->data.city = pCity;
      set_wstate(pBuf , FC_WS_NORMAL);
  
      add_to_gui_list(MAX_ID - pUnit->id , pBuf);
    
      area.w = MAX(area.w , pBuf->size.w);
      area.h += pBuf->size.h;
    }
      
    /* ---------- */
    if (diplomat_can_do_action(pUnit, DIPLOMAT_MOVE, ptile)) {
    
      create_active_iconlabel(pBuf, pWindow->dst, pStr,
	    _("Keep moving"), diplomat_keep_moving_callback);
      
      pBuf->data.city = pCity;
      set_wstate(pBuf , FC_WS_NORMAL);
  
      add_to_gui_list(MAX_ID - pUnit->id , pBuf);
    
      area.w = MAX(area.w, pBuf->size.w);
      area.h += pBuf->size.h;
    }
  }
  else
  {
    if((pTunit=unit_list_get(ptile->units, 0))){
       /* Spy/Diplomat acting against a unit */ 
      
      pDiplomat_Dlg->diplomat_target_id = pTunit->id;
      
      /* ---------- */
      if (diplomat_can_do_action(pUnit, DIPLOMAT_BRIBE, ptile)) {
    
        create_active_iconlabel(pBuf, pWindow->dst, pStr,
	    _("Bribe Enemy Unit"), diplomat_bribe_callback);
	
        pBuf->data.unit = pTunit;
        set_wstate(pBuf , FC_WS_NORMAL);
  
        add_to_gui_list(MAX_ID - pUnit->id , pBuf);
    
        area.w = MAX(area.w , pBuf->size.w);
        area.h += pBuf->size.h;
      }
      
      /* ---------- */
      if (diplomat_can_do_action(pUnit, SPY_SABOTAGE_UNIT, ptile)) {
    
        create_active_iconlabel(pBuf, pWindow->dst, pStr,
	    _("Sabotage Enemy Unit"), spy_sabotage_unit_callback);
	
        pBuf->data.unit = pTunit;
        set_wstate(pBuf , FC_WS_NORMAL);
  
        add_to_gui_list(MAX_ID - pUnit->id , pBuf);
    
        area.w = MAX(area.w , pBuf->size.w);
        area.h += pBuf->size.h;
      }
    }
  }
  /* ---------- */
  
  create_active_iconlabel(pBuf, pWindow->dst, pStr,
	    _("Cancel"), diplomat_close_callback);
    
  set_wstate(pBuf , FC_WS_NORMAL);
  pBuf->key = SDLK_ESCAPE;
  
  add_to_gui_list(ID_LABEL , pBuf);
    
  area.w = MAX(area.w, pBuf->size.w);
  area.h += pBuf->size.h;
  /* ---------- */
  pDiplomat_Dlg->pdialog->pBeginWidgetList = pBuf;
  
  /* setup window size and start position */

  resize_window(pWindow, NULL, NULL, 
                (pWindow->size.w - pWindow->area.w) + area.w,
                (pWindow->size.h - pWindow->area.h) + area.h);
  
  area = pWindow->area;
  
  auto_center_on_focus_unit();
  put_window_near_map_tile(pWindow, pWindow->size.w, pWindow->size.h,
                           pUnit->tile);
 
  /* setup widget size and start position */
    
  pBuf = pWindow->prev;
  setup_vertical_widgets_position(1,
	area.x,
  	area.y + 1, area.w, 0,
	pDiplomat_Dlg->pdialog->pBeginWidgetList, pBuf);
  
  /* --------------------- */
  /* redraw */
  redraw_group(pDiplomat_Dlg->pdialog->pBeginWidgetList, pWindow, 0);

  widget_flush(pWindow);
  
}
예제 #16
0
파일: infracache.c 프로젝트: valisc/freeciv
/**************************************************************************
  Calculate the benefit of irrigating the given tile.

  The return value is the goodness of the tile after the irrigation.  This
  should be compared to the goodness of the tile currently (see
  city_tile_value(); note that this depends on the AI's weighting
  values).
**************************************************************************/
static int adv_calc_irrigate(const struct city *pcity,
                             const struct tile *ptile)
{
  int goodness;
  struct terrain *old_terrain, *new_terrain;

  fc_assert_ret_val(ptile != NULL, -1)

  old_terrain = tile_terrain(ptile);
  new_terrain = old_terrain->irrigation_result;

  if (new_terrain != old_terrain && new_terrain != T_NONE) {
    struct tile *vtile;

    if (tile_city(ptile) && terrain_has_flag(new_terrain, TER_NO_CITIES)) {
      /* Not a valid activity. */
      return -1;
    }
    /* Irrigation would change the terrain type, clearing conflicting
     * extras in the process.  Calculate the benefit of doing so. */
    vtile = tile_virtual_new(ptile);

    tile_change_terrain(vtile, new_terrain);
    goodness = city_tile_value(pcity, vtile, 0, 0);
    tile_virtual_destroy(vtile);

    return goodness;
  } else if (old_terrain == new_terrain) {
    struct extra_type *pextra = next_extra_for_tile(ptile, EC_IRRIGATION,
                                                    city_owner(pcity), NULL);

    if (pextra != NULL) {
      struct tile *vtile = tile_virtual_new(ptile);

      /* Try to add extra, and to remove conflicting ones. */
      if (tile_extra_apply(vtile, pextra)) {
        struct extra_type *pextra2 = next_extra_for_tile(vtile, EC_IRRIGATION,
                                                         city_owner(pcity), NULL);

        goodness = city_tile_value(pcity, vtile, 0, 0);

        if (pextra2 != NULL) {
          struct tile *vtile2 = tile_virtual_new(vtile);

          /* If the player can further irrigate to make farmland, consider the
           * potentially greater benefit.  Note the hack: autosettler ordinarily
           * discounts benefits by the time it takes to make them; farmland takes
           * twice as long, so make it look half as good. */
          if (tile_extra_apply(vtile2, pextra2)) {
            int second_goodness = city_tile_value(pcity, vtile2, 0, 0);
            int oldv = city_tile_value(pcity, ptile, 0, 0);

            second_goodness = oldv + (second_goodness - oldv) / 2;

            if (second_goodness > goodness) {
              goodness = second_goodness;
            }
          }
          tile_virtual_destroy(vtile2);
        }
        tile_virtual_destroy(vtile);

        return goodness;
      }
    }

    /* Cannot build irrigation extra */
    return -1;
  } else {
    return -1;
  }
}
예제 #17
0
/****************************************************************
  Popups the diplomat dialog
*****************************************************************/
void popup_diplomat_dialog(struct unit *punit, struct tile *dest_tile)
{
  struct city *pcity;
  struct unit *ptunit;
  struct astring text = ASTRING_INIT;

  diplomat_id=punit->id;

  if((pcity=tile_city(dest_tile))){
    /* Spy/Diplomat acting against a city */

    diplomat_target_id=pcity->id;
    astr_set(&text,
             _("Your %s has arrived at %s.\nWhat is your command?"),
             unit_name_translation(punit),
             city_name(pcity));

    if (!unit_has_type_flag(punit, UTYF_SPY)) {
      diplomat_dialog =
        popup_message_dialog(toplevel, "diplomatdialog", astr_str(&text),
			       diplomat_embassy_callback, 0, 1,
			       diplomat_investigate_callback, 0, 1,
			       spy_poison_callback, 0, 1,
			       diplomat_sabotage_callback, 0, 1,
			       diplomat_steal_callback, 0, 1,
			       diplomat_incite_callback, 0, 1,
			       diplomat_keep_moving_callback_city, 0, 1,
			       diplomat_cancel_callback, 0, 0,
			       NULL);
    } else {
      diplomat_dialog =
        popup_message_dialog(toplevel, "spydialog", astr_str(&text),
			       diplomat_embassy_callback, 0,  1,
			       diplomat_investigate_callback, 0, 1,
			       spy_poison_callback, 0, 1,
			       spy_request_sabotage_list, 0, 1,
			       spy_steal_popup, 0, 1,
			       diplomat_incite_callback, 0, 1,
			       diplomat_keep_moving_callback_city, 0, 1,
			       diplomat_cancel_callback, 0, 0,
			       NULL);
    }
      
    if(!diplomat_can_do_action(punit, DIPLOMAT_EMBASSY, dest_tile)) {
      XtSetSensitive(XtNameToWidget(diplomat_dialog, "*button0"), FALSE);
    }
    if(!diplomat_can_do_action(punit, DIPLOMAT_INVESTIGATE, dest_tile)) {
      XtSetSensitive(XtNameToWidget(diplomat_dialog, "*button1"), FALSE);
    }
    if(!diplomat_can_do_action(punit, SPY_POISON, dest_tile)) {
      XtSetSensitive(XtNameToWidget(diplomat_dialog, "*button2"), FALSE);
    }
    if(!diplomat_can_do_action(punit, DIPLOMAT_SABOTAGE, dest_tile)) {
      XtSetSensitive(XtNameToWidget(diplomat_dialog, "*button3"), FALSE);
    }
    if(!diplomat_can_do_action(punit, DIPLOMAT_STEAL, dest_tile)) {
      XtSetSensitive(XtNameToWidget(diplomat_dialog, "*button4"), FALSE);
    }
    if(!diplomat_can_do_action(punit, DIPLOMAT_INCITE, dest_tile)) {
      XtSetSensitive(XtNameToWidget(diplomat_dialog, "*button5"), FALSE);
    }
    if(!diplomat_can_do_action(punit, DIPLOMAT_MOVE, dest_tile)) {
      XtSetSensitive(XtNameToWidget(diplomat_dialog, "*button6"), FALSE);
    }
  } else { 
    if ((ptunit = unit_list_get(dest_tile->units, 0))) {
      /* Spy/Diplomat acting against a unit */
      
      Widget shl;

      astr_set(&text,
               _("Your %s is waiting for your command."),
               unit_name_translation(punit));
      
      diplomat_target_id=ptunit->id;

      shl=popup_message_dialog(toplevel, "spybribedialog",
			       astr_str(&text),
			       diplomat_bribe_callback, 0, 0,
			       spy_sabotage_unit_callback, 0, 0,
			       diplomat_keep_moving_callback_unit, 0, 1,
			       diplomat_cancel_callback, 0, 0,
			       NULL);
	
      if(!diplomat_can_do_action(punit, DIPLOMAT_BRIBE, dest_tile))
	XtSetSensitive(XtNameToWidget(shl, "*button0"), FALSE);
      if(!diplomat_can_do_action(punit, SPY_SABOTAGE_UNIT, dest_tile))
	XtSetSensitive(XtNameToWidget(shl, "*button1"), FALSE);
      if(!diplomat_can_do_action(punit, DIPLOMAT_MOVE, dest_tile))
        XtSetSensitive(XtNameToWidget(shl, "*button2"), FALSE);
    }
  }
  astr_free(&text);
}
예제 #18
0
파일: mapctrl.c 프로젝트: jheusala/freeciv
/**************************************************************************
 Handle all mouse button press on canvas.
 Future feature: User-configurable mouse clicks.
**************************************************************************/
gboolean butt_down_mapcanvas(GtkWidget *w, GdkEventButton *ev, gpointer data)
{
  struct city *pcity = NULL;
  struct tile *ptile = NULL;

  if (editor_is_active()) {
    return handle_edit_mouse_button_press(ev);
  }

  if (!can_client_change_view()) {
    return TRUE;
  }

  gtk_widget_grab_focus(map_canvas);
  ptile = canvas_pos_to_tile(ev->x, ev->y);
  pcity = ptile ? tile_city(ptile) : NULL;

  switch (ev->button) {

  case 1: /* LEFT mouse button */

    /* <SHIFT> + <CONTROL> + LMB : Adjust workers. */
    if ((ev->state & GDK_SHIFT_MASK) && (ev->state & GDK_CONTROL_MASK)) {
      adjust_workers_button_pressed(ev->x, ev->y);
    }
    /* <CONTROL> + LMB : Quickselect a sea unit. */
    else if (ev->state & GDK_CONTROL_MASK) {
      action_button_pressed(ev->x, ev->y, SELECT_SEA);
    }
    /* <SHIFT> + LMB: Append focus unit. */
    else if (ptile && (ev->state & GDK_SHIFT_MASK)) {
      action_button_pressed(ev->x, ev->y, SELECT_APPEND);
    }
    /* <ALT> + LMB: popit (same as middle-click) */
    /* FIXME: we need a general mechanism for letting freeciv work with
     * 1- or 2-button mice. */
    else if (ptile && (ev->state & GDK_MOD1_MASK)) {
      popit(ev, ptile);
    }
    /* LMB in Area Selection mode. */
    else if(tiles_hilited_cities) {
      if (ptile) {
        toggle_tile_hilite(ptile);
      }
    }
    /* Plain LMB click. */
    else {
      action_button_pressed(ev->x, ev->y, SELECT_POPUP);
    }
    break;

  case 2: /* MIDDLE mouse button */

    /* <CONTROL> + MMB: Wake up sentries. */
    if (ev->state & GDK_CONTROL_MASK) {
      wakeup_button_pressed(ev->x, ev->y);
    }
    /* Plain Middle click. */
    else if (ptile) {
      popit(ev, ptile);
    }
    break;

  case 3: /* RIGHT mouse button */

    /* <CONTROL> + <ALT> + RMB : insert city or tile chat link. */
    /* <CONTROL> + <ALT> + <SHIFT> + RMB : insert unit chat link. */
    if (ptile && (ev->state & GDK_MOD1_MASK)
        && (ev->state & GDK_CONTROL_MASK)) {
      inputline_make_chat_link(ptile, (ev->state & GDK_SHIFT_MASK) != 0);
    }
    /* <SHIFT> + <ALT> + RMB : Show/hide workers. */
    else if ((ev->state & GDK_SHIFT_MASK) && (ev->state & GDK_MOD1_MASK)) {
      key_city_overlay(ev->x, ev->y);
    }
    /* <SHIFT + CONTROL> + RMB: Paste Production. */
    else if ((ev->state & GDK_SHIFT_MASK) && (ev->state & GDK_CONTROL_MASK)
             && pcity != NULL) {
      clipboard_paste_production(pcity);
      cancel_tile_hiliting();
    }
    /* <SHIFT> + RMB on city/unit: Copy Production. */
    /* If nothing to copy, fall through to rectangle selection. */
    else if (ev->state & GDK_SHIFT_MASK
             && clipboard_copy_production(ptile)) {
      /* Already done the copy */
    }
    /* <CONTROL> + RMB : Quickselect a land unit. */
    else if (ev->state & GDK_CONTROL_MASK) {
      action_button_pressed(ev->x, ev->y, SELECT_LAND);
    }
    /* Plain RMB click. Area selection. */
    else {
      /*  A foolproof user will depress button on canvas,
       *  release it on another widget, and return to canvas
       *  to find rectangle still active.
       */
      if (rectangle_active) {
        release_right_button(ev->x, ev->y,
                             (ev->state & GDK_SHIFT_MASK) != 0);
        return TRUE;
      }
      if (hover_state == HOVER_NONE) {
        anchor_selection_rectangle(ev->x, ev->y);
        rbutton_down = TRUE; /* causes rectangle updates */
      }
    }
    break;

  default:
    break;
  }


  return TRUE;
}
예제 #19
0
/****************************************************************
 Popup new diplomat dialog.
*****************************************************************/
void popup_diplomat_dialog(struct unit *punit, struct tile *dest_tile)
{
  struct city *pcity;
  struct unit *ptunit;
  GtkWidget *shl;
  struct astring title = ASTRING_INIT, text = ASTRING_INIT;

  diplomat_id = punit->id;

  if ((pcity = tile_city(dest_tile))) {
    /* Spy/Diplomat acting against a city */

    diplomat_target_id = pcity->id;
    astr_set(&title,
             /* TRANS: %s is a unit name, e.g., Spy */
             _("Choose Your %s's Strategy"), unit_name_translation(punit));
    astr_set(&text, 
             _("Your %s has arrived at %s.\nWhat is your command?"),
             unit_name_translation(punit),
             city_name(pcity));

    if (!unit_has_type_flag(punit, UTYF_SPY)) {
      shl = popup_choice_dialog(GTK_WINDOW(toplevel),
        astr_str(&title), astr_str(&text),
	_("Establish _Embassy"), diplomat_embassy_callback, NULL,
	_("_Investigate City"), diplomat_investigate_callback, NULL,
	_("_Sabotage City"), diplomat_sabotage_callback, NULL,
	_("Steal _Technology"), diplomat_steal_callback, NULL,
	_("Incite a _Revolt"), diplomat_incite_callback, NULL,
	_("_Keep moving"), diplomat_keep_moving_callback, NULL,
	GTK_STOCK_CANCEL, diplomat_cancel_callback, NULL,
	NULL);

      if (!diplomat_can_do_action(punit, DIPLOMAT_EMBASSY, dest_tile))
	choice_dialog_button_set_sensitive(shl, 0, FALSE);
      if (!diplomat_can_do_action(punit, DIPLOMAT_INVESTIGATE, dest_tile))
	choice_dialog_button_set_sensitive(shl, 1, FALSE);
      if (!diplomat_can_do_action(punit, DIPLOMAT_SABOTAGE, dest_tile))
	choice_dialog_button_set_sensitive(shl, 2, FALSE);
      if (!diplomat_can_do_action(punit, DIPLOMAT_STEAL, dest_tile))
	choice_dialog_button_set_sensitive(shl, 3, FALSE);
      if (!diplomat_can_do_action(punit, DIPLOMAT_INCITE, dest_tile))
	choice_dialog_button_set_sensitive(shl, 4, FALSE);
      if (!diplomat_can_do_action(punit, DIPLOMAT_MOVE, dest_tile))
	choice_dialog_button_set_sensitive(shl, 5, FALSE);
    } else {
       shl = popup_choice_dialog(GTK_WINDOW(toplevel),
        astr_str(&title), astr_str(&text),
	_("Establish _Embassy"), diplomat_embassy_callback, NULL,
	_("_Investigate City"), diplomat_investigate_callback, NULL,
	_("_Poison City"), spy_poison_callback, NULL,
	_("Industrial _Sabotage"), spy_request_sabotage_list, NULL,
	_("Steal _Technology"), spy_steal_popup, NULL,
	_("Incite a _Revolt"), diplomat_incite_callback, NULL,
	_("_Keep moving"), diplomat_keep_moving_callback, NULL,
	GTK_STOCK_CANCEL, diplomat_cancel_callback, NULL,
	NULL);

      if (!diplomat_can_do_action(punit, DIPLOMAT_EMBASSY, dest_tile))
	choice_dialog_button_set_sensitive(shl, 0, FALSE);
      if (!diplomat_can_do_action(punit, DIPLOMAT_INVESTIGATE, dest_tile))
	choice_dialog_button_set_sensitive(shl, 1, FALSE);
      if (!diplomat_can_do_action(punit, SPY_POISON, dest_tile))
	choice_dialog_button_set_sensitive(shl, 2, FALSE);
      if (!diplomat_can_do_action(punit, DIPLOMAT_SABOTAGE, dest_tile))
	choice_dialog_button_set_sensitive(shl, 3, FALSE);
      if (!diplomat_can_do_action(punit, DIPLOMAT_STEAL, dest_tile))
	choice_dialog_button_set_sensitive(shl, 4, FALSE);
      if (!diplomat_can_do_action(punit, DIPLOMAT_INCITE, dest_tile))
	choice_dialog_button_set_sensitive(shl, 5, FALSE);
      if (!diplomat_can_do_action(punit, DIPLOMAT_MOVE, dest_tile))
	choice_dialog_button_set_sensitive(shl, 6, FALSE);
     }

    diplomat_dialog = shl;

    choice_dialog_set_hide(shl, TRUE);
    g_signal_connect(shl, "destroy",
		     G_CALLBACK(diplomat_destroy_callback), NULL);
    g_signal_connect(shl, "delete_event",
		     G_CALLBACK(diplomat_cancel_callback), NULL);
  } else { 
    if ((ptunit = unit_list_get(dest_tile->units, 0))){
      /* Spy/Diplomat acting against a unit */ 
       
      diplomat_target_id = ptunit->id;
      astr_set(&text,
               /* TRANS: %s is a unit name, e.g., Diplomat, Spy */
               _("Your %s is waiting for your command."),
               unit_name_translation(punit));
 
      shl = popup_choice_dialog(GTK_WINDOW(toplevel),
	_("Subvert Enemy Unit"), astr_str(&text),
	_("_Bribe Enemy Unit"), diplomat_bribe_callback, NULL,
	_("_Sabotage Enemy Unit"), spy_sabotage_unit_callback, NULL,
	GTK_STOCK_CANCEL, diplomat_cancel_callback, NULL,
	NULL);

      if (!diplomat_can_do_action(punit, DIPLOMAT_BRIBE, dest_tile)) {
	choice_dialog_button_set_sensitive(shl, 0, FALSE);
      }
      if (!diplomat_can_do_action(punit, SPY_SABOTAGE_UNIT, dest_tile)) {
	choice_dialog_button_set_sensitive(shl, 1, FALSE);
      }

      diplomat_dialog = shl;

      choice_dialog_set_hide(shl, TRUE);
      g_signal_connect(shl, "destroy",
		       G_CALLBACK(diplomat_destroy_callback), NULL);
      g_signal_connect(shl, "delete_event",
		       G_CALLBACK(diplomat_cancel_callback), NULL);
    }
  }
  astr_free(&title);
  astr_free(&text);
}
예제 #20
0
/**************************************************************************
  Main handler for key presses
**************************************************************************/
static Uint16 main_key_down_handler(SDL_keysym Key, void *pData)
{
    static struct widget *pWidget;
    if ((pWidget = find_next_widget_for_key(NULL, Key)) != NULL) {
        return widget_pressed_action(pWidget);
    } else {
        if (Key.sym == SDLK_TAB) {
            /* input */
            popup_input_line();
        } else {
            if (map_event_handler(Key)
                    && C_S_RUNNING == client_state()) {
                switch (Key.sym) {
                case SDLK_RETURN:
                case SDLK_KP_ENTER:
                    if (LSHIFT || RSHIFT) {
                        disable_focus_animation();
                        key_end_turn();
                    } else {
                        struct unit *pUnit;
                        struct city *pCity;
                        if (NULL != (pUnit = head_of_units_in_focus()) &&
                                (pCity = tile_city(unit_tile(pUnit))) != NULL &&
                                city_owner(pCity) == client.conn.playing) {
                            popup_city_dialog(pCity);
                        }
                    }
                    return ID_ERROR;

                case SDLK_F2:
                    units_report_dialog_popup(FALSE);
                    return ID_ERROR;

                case SDLK_F4:
                    city_report_dialog_popup(FALSE);
                    return ID_ERROR;

                case SDLK_F7:
                    send_report_request(REPORT_WONDERS_OF_THE_WORLD);
                    return ID_ERROR;

                case SDLK_F8:
                    send_report_request(REPORT_TOP_5_CITIES);
                    return ID_ERROR;

                case SDLK_F9:
                    if (meswin_dialog_is_open()) {
                        meswin_dialog_popdown();
                    } else {
                        meswin_dialog_popup(TRUE);
                    }
                    flush_dirty();
                    return ID_ERROR;

                case SDLK_F11:
                    send_report_request(REPORT_DEMOGRAPHIC);
                    return ID_ERROR;

                case SDLK_F12:
                    popup_spaceship_dialog(client.conn.playing);
                    return ID_ERROR;

                default:
                    return ID_ERROR;
                }
            }
        }
    }

    return ID_ERROR;
}