Exemple #1
0
/****************************************************************************
  Create a sprite with the given height, width and color.
****************************************************************************/
struct sprite *create_sprite(int width, int height, struct color *pcolor)
{
  struct sprite *plrcolor;

  fc_assert_ret_val(width > 0, NULL);
  fc_assert_ret_val(height > 0, NULL);
  fc_assert_ret_val(pcolor != NULL, NULL);

  {
    /* FIXME: I do not know why it works but the code below allows the creation
     *        of the needed player color sprites. */
    fc_assert_ret_val(tileset != NULL, NULL);
    struct sprite *psprite_dummy = get_basic_fog_sprite(tileset);
    Pixmap mypixmap, mymask;
    GC plane_gc;

    mypixmap = XCreatePixmap(display, root_window, width, height,
                             display_depth);
    mymask = XCreatePixmap(display, root_window, width, height, 1);
    plane_gc = XCreateGC(display, mymask, 0, NULL);
    XCopyArea(display, psprite_dummy->mask, mymask, plane_gc,
              0, 0, width, height, 0, 0);
    XFreeGC(display, plane_gc);
    plrcolor = ctor_sprite_mask(mypixmap, mymask, width, height);
  }

  XSetForeground(display, fill_bg_gc, pcolor->color.pixel);
  XFillRectangle(display, plrcolor->pixmap, fill_bg_gc, 0, 0, width, height);

  return plrcolor;
}
Exemple #2
0
/*****************************************************************************
  Evaluate a Lua function call or loaded script on the stack.
  Return nonzero if an error occured.

  If available pass the source code string as code, else NULL.

  Will pop function and arguments (1 + narg values) from the stack.
  Will push nret return values to the stack.

  On error, print an error message with traceback. Nothing is pushed to
  the stack.
*****************************************************************************/
int luascript_call(struct fc_lua *fcl, int narg, int nret, const char *code)
{
  int status;
  int base;          /* Index of function to call */
  int traceback = 0; /* Index of traceback function  */

  fc_assert_ret_val(fcl, -1);
  fc_assert_ret_val(fcl->state, -1);

  base = lua_gettop(fcl->state) - narg;

  /* Find the traceback function, if available */
  luascript_traceback_func_push(fcl->state);
  if (lua_isfunction(fcl->state, -1)) {
    lua_insert(fcl->state, base);  /* insert traceback before function */
    traceback = base;
  } else {
    lua_pop(fcl->state, 1);   /* pop non-function traceback */
  }

  luascript_hook_start(fcl->state);
  status = lua_pcall(fcl->state, narg, nret, traceback);
  luascript_hook_end(fcl->state);

  if (status) {
    luascript_report(fcl, status, code);
  }

  if (traceback) {
    lua_remove(fcl->state, traceback);
  }

  return status;
}
Exemple #3
0
/**************************************************************************
  Accessor for requirements.
**************************************************************************/
struct advance *advance_requires(const struct advance *padvance,
				 enum tech_req require)
{
  fc_assert_ret_val(require >= 0 && require < AR_SIZE, NULL);
  fc_assert_ret_val(NULL != padvance, NULL);
  return padvance->require[require];
}
Exemple #4
0
/****************************************************************************
  Convert a hex string into a rgb color
****************************************************************************/
bool rgbcolor_from_hex(struct rgbcolor **prgbcolor, const char *hex)
{
  int rgb, r, g, b;
  char hex2[16];

  fc_assert_ret_val(*prgbcolor == NULL, FALSE);
  fc_assert_ret_val(hex != NULL, FALSE);

  if (hex[0] == '#') {
    hex++;
  }

  if (strlen(hex) != 6) {
    return FALSE;
  }

  fc_snprintf(hex2, sizeof(hex2), "0x%s", hex);
  if (!sscanf(hex2, "%x", &rgb)) {
    return FALSE;
  }

  r = rgb / 256 / 256;
  g = (rgb - r * 256 * 256) / 256;
  b = rgb % 256;

  *prgbcolor = rgbcolor_new(r, g, b);

  return TRUE;
}
Exemple #5
0
/****************************************************************************
  Lookup an RGB color definition (<colorpath>.red, <colorpath>.green and
  <colorpath>.blue). Returns TRUE on success and FALSE on error.
****************************************************************************/
bool rgbcolor_load(struct section_file *file, struct rgbcolor **prgbcolor,
                   char *path, ...)
{
  int r, g, b;
  char colorpath[256];
  va_list args;

  fc_assert_ret_val(file != NULL, FALSE);
  fc_assert_ret_val(*prgbcolor == NULL, FALSE);

  va_start(args, path);
  fc_vsnprintf(colorpath, sizeof(colorpath), path, args);
  va_end(args);

  if (!secfile_lookup_int(file, &r, "%s.r", colorpath)
      || !secfile_lookup_int(file, &g, "%s.g", colorpath)
      || !secfile_lookup_int(file, &b, "%s.b", colorpath)) {
    /* One color value (red, green or blue) is missing. */
    return FALSE;
  }

  rgbcolor_check(colorpath, r, g, b);
  *prgbcolor = rgbcolor_new(r, g, b);

  return TRUE;
}
Exemple #6
0
/**************************************************************************
  Convenience function to add a column to a GtkTreeView. Returns the added
  column, or NULL if an error occurred.
**************************************************************************/
GtkTreeViewColumn *add_treeview_column(GtkWidget *view, const char *title,
                                       GType gtype, int model_index)
{
  GtkTreeViewColumn *col;
  GtkCellRenderer *rend;
  const char *attr;

  fc_assert_ret_val(view != NULL, NULL);
  fc_assert_ret_val(GTK_IS_TREE_VIEW(view), NULL);
  fc_assert_ret_val(title != NULL, NULL);

  if (gtype == G_TYPE_BOOLEAN) {
    rend = gtk_cell_renderer_toggle_new();
    attr = "active";
  } else if (gtype == GDK_TYPE_PIXBUF) {
    rend = gtk_cell_renderer_pixbuf_new();
    attr = "pixbuf";
  } else {
    rend = gtk_cell_renderer_text_new();
    attr = "text";
  }

  col = gtk_tree_view_column_new_with_attributes(title, rend, attr,
                                                 model_index, NULL);
  gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
  return col;
}
Exemple #7
0
/***************************************************************************
  Test if any bit is set.
***************************************************************************/
bool dbv_isset_any(const struct dbv *pdbv)
{
  fc_assert_ret_val(pdbv != NULL, FALSE);
  fc_assert_ret_val(pdbv->vec != NULL, FALSE);

  return bv_check_mask(pdbv->vec, pdbv->vec, _BV_BYTES(pdbv->bits),
                       _BV_BYTES(pdbv->bits));
}
Exemple #8
0
/***************************************************************************
  Check if the bit 'bit' is set.
***************************************************************************/
bool dbv_isset(const struct dbv *pdbv, int bit)
{
  fc_assert_ret_val(pdbv != NULL, FALSE);
  fc_assert_ret_val(pdbv->vec != NULL, FALSE);
  fc_assert_ret_val(bit < pdbv->bits, FALSE);

  return ((pdbv->vec[_BV_BYTE_INDEX(bit)] & _BV_BITMASK(bit)) != 0);
}
Exemple #9
0
/***************************************************************************
  Check if the two dynamic bitvectors are equal.
***************************************************************************/
bool dbv_are_equal(const struct dbv *pdbv1, const struct dbv *pdbv2)
{
  fc_assert_ret_val(pdbv1 != NULL, FALSE);
  fc_assert_ret_val(pdbv1->vec != NULL, FALSE);
  fc_assert_ret_val(pdbv2 != NULL, FALSE);
  fc_assert_ret_val(pdbv2->vec != NULL, FALSE);

  return bv_are_equal(pdbv1->vec, pdbv2->vec, _BV_BYTES(pdbv1->bits),
                      _BV_BYTES(pdbv2->bits));
}
Exemple #10
0
/**************************************************************************
  Accessor for requirements.
**************************************************************************/
Tech_type_id advance_required(const Tech_type_id tech,
			      enum tech_req require)
{
  fc_assert_ret_val(require >= 0 && require < AR_SIZE, -1);
  fc_assert_ret_val(tech >= A_NONE || tech < A_LAST, -1);
  if (A_NEVER == advances[tech].require[require]) {
    /* out of range */
    return A_LAST;
  }
  return advance_number(advances[tech].require[require]);
}
Exemple #11
0
/*****************************************************************************
  Return if the function 'funcname' is define in the lua state 'fcl->state'.
*****************************************************************************/
bool luascript_check_function(struct fc_lua *fcl, const char *funcname)
{
  bool defined;

  fc_assert_ret_val(fcl, FALSE);
  fc_assert_ret_val(fcl->state, FALSE);

  lua_getglobal(fcl->state, funcname);
  defined = lua_isfunction(fcl->state, -1);
  lua_pop(fcl->state, 1);

  return defined;
}
Exemple #12
0
/*****************************************************************************
  lua_dostring replacement with error message showing on errors.
*****************************************************************************/
int luascript_do_string(struct fc_lua *fcl, const char *str, const char *name)
{
  int status;

  fc_assert_ret_val(fcl, -1);
  fc_assert_ret_val(fcl->state, -1);

  status = luaL_loadbuffer(fcl->state, str, strlen(str), name);
  if (status) {
    luascript_report(fcl, status, str);
  } else {
    status = luascript_call(fcl, 0, 0, str);
  }
  return status;
}
Exemple #13
0
/*****************************************************************************
  Parse and execute the script at filename.
*****************************************************************************/
int luascript_do_file(struct fc_lua *fcl, const char *filename)
{
  int status;

  fc_assert_ret_val(fcl, -1);
  fc_assert_ret_val(fcl->state, -1);

  status = luaL_loadfile(fcl->state, filename);
  if (status) {
    luascript_report(fcl, status, NULL);
  } else {
    status = luascript_call(fcl, 0, 0, NULL);
  }
  return status;
}
Exemple #14
0
/*************************************************************************
  Get trade route settings related to type.
*************************************************************************/
struct trade_route_settings *
trade_route_settings_by_type(enum trade_route_type type)
{
  fc_assert_ret_val(type >= TRT_NATIONAL && type < TRT_LAST, NULL);

  return &trtss[type];
}
Exemple #15
0
/**************************************************************************
  Returns state of the tech for current pplayer.
  This can be: TECH_KNOWN, TECH_UNKNOWN, or TECH_PREREQS_KNOWN
  Should be called with existing techs or A_FUTURE

  If pplayer is NULL this checks whether any player knows the tech (used
  by the client).
**************************************************************************/
enum tech_state player_invention_state(const struct player *pplayer,
				       Tech_type_id tech)
{
  fc_assert_ret_val(tech == A_FUTURE
                    || (tech >= 0 && tech < game.control.num_tech_types),
                    -1);

  if (!pplayer) {
    if (tech != A_FUTURE && game.info.global_advances[tech]) {
      return TECH_KNOWN;
    } else {
      return TECH_UNKNOWN;
    }
  } else {
    struct player_research *research = player_research_get(pplayer);

    /* Research can be null in client when looking for tech_leakage
     * from player not yet received. */
    if (research) {
      return research->inventions[tech].state;
    } else {
      return TECH_UNKNOWN;
    }
  }
}
Exemple #16
0
/*****************************************************************************
  Get the freeciv lua struct from a lua state.
*****************************************************************************/
struct fc_lua *luascript_get_fcl(lua_State *L)
{
  struct fc_lua *fcl;

  fc_assert_ret_val(L, NULL);

  /* Get the freeciv lua struct from the lua state. */
  lua_pushstring(L, LUASCRIPT_GLOBAL_VAR_NAME);
  lua_gettable(L, LUA_REGISTRYINDEX);
  fcl = lua_touserdata(L, -1);

  /* This is an error! */
  fc_assert_ret_val(fcl != NULL, NULL);

  return fcl;
}
Exemple #17
0
/****************************************************************************
  Convert a rgb color to a hex string (like 0xff0000 for red [255,  0,  0]).
****************************************************************************/
bool rgbcolor_to_hex(const struct rgbcolor *prgbcolor, char *hex,
                     size_t hex_len)
{
  fc_assert_ret_val(prgbcolor != NULL, FALSE);
  /* Needs a length greater than 7 ('#' + 6 hex digites and '\0'). */
  fc_assert_ret_val(hex_len > 7, FALSE);

  fc_assert_ret_val(0 <= prgbcolor->r && prgbcolor->r <= 255, FALSE);
  fc_assert_ret_val(0 <= prgbcolor->g && prgbcolor->g <= 255, FALSE);
  fc_assert_ret_val(0 <= prgbcolor->b && prgbcolor->b <= 255, FALSE);

  fc_snprintf(hex, hex_len, "#%06x",
              (prgbcolor->r * 256 + prgbcolor->g) * 256 + prgbcolor->b);

  return TRUE;
}
Exemple #18
0
/**************************************************************************
  Calculates the value of removing fallout at the given tile.

  The return value is the goodness of the tile after the cleanup.  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_fallout(const struct city *pcity,
                            const struct tile *ptile, int best)
{
  int goodness;
  struct tile *vtile;
  bool polluted = FALSE;

  fc_assert_ret_val(ptile != NULL, -1);

  vtile = tile_virtual_new(ptile);

  extra_type_by_cause_iterate(EC_FALLOUT, pextra) {
    if (tile_has_extra(ptile, pextra)) {
      tile_remove_extra(vtile, pextra);
      polluted = TRUE;
    }
  } extra_type_by_cause_iterate_end;

  if (!polluted) {
    goodness = -1;
  } else {
    goodness = city_tile_value(pcity, vtile, 0, 0);

    /* FIXME: need a better way to guarantee fallout is cleaned up. */
    if (!city_owner(pcity)->ai_controlled) {
      goodness = (goodness + best + 50) * 2;
    }
  }

  tile_virtual_destroy(vtile);

  return goodness;
}
Exemple #19
0
/****************************************************************************
  Create a sprite with the given height, width and color.
****************************************************************************/
struct sprite *create_sprite(int width, int height, struct color *pcolor)
{
  GdkPixbuf *mypixbuf = NULL;
  GdkColor *color = NULL;

  fc_assert_ret_val(width > 0, NULL);
  fc_assert_ret_val(height > 0, NULL);
  fc_assert_ret_val(pcolor != NULL, NULL);

  color = &pcolor->color;
  mypixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, width, height);
  gdk_pixbuf_fill(mypixbuf, ((guint32)(color->red & 0xff00) << 16)
                            | ((color->green & 0xff00) << 8)
                            | (color->blue & 0xff00) | 0xff);

  return ctor_sprite(mypixbuf);
}
Exemple #20
0
/*****************************************************************************
  Return TRUE iff the lua console is open.
*****************************************************************************/
bool luaconsole_dialog_is_open(void)
{
  struct luaconsole_data *pdialog = luaconsole_dialog_get();

  fc_assert_ret_val(pdialog, FALSE);

  return (NULL != pdialog->shell);
}
Exemple #21
0
/****************************************************************************
  Test whether two rgbcolor structures represent the exact same color value.
  (Does not attempt to determine whether they are visually distinguishable.)
****************************************************************************/
bool rgbcolors_are_equal(const struct rgbcolor *c1, const struct rgbcolor *c2)
{
  fc_assert_ret_val(c1 != NULL && c2 != NULL, FALSE);

  /* No check of cached 'color' member -- if values are equal, it should be
   * equivalent */
  return (c1->r == c2->r && c1->g == c2->g && c1->b == c2->b);
}
Exemple #22
0
/**************************************************************************
  Return the road index.

  Currently same as road_number(), paired with road_count()
  indicates use as an array index.
**************************************************************************/
Road_type_id road_index(const struct road_type *proad)
{
  fc_assert_ret_val(NULL != proad, -1);

  /* FIXME: */
  /* return proad - roads; */
  return road_number(proad);
}
Exemple #23
0
/****************************************************************************
  Create a sprite with the given height, width and color.
****************************************************************************/
struct sprite *create_sprite(int width, int height, struct color *pcolor)
{
  SDL_Surface *mypixbuf = NULL;
  SDL_Surface *pmask = NULL;

  fc_assert_ret_val(width > 0, NULL);
  fc_assert_ret_val(height > 0, NULL);
  fc_assert_ret_val(pcolor != NULL, NULL);

  mypixbuf = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
                                  0x00ff0000, 0x0000ff00, 0x000000ff,
                                  0xff000000);
  pmask = SDL_DisplayFormatAlpha(mypixbuf);
  SDL_FillRect(mypixbuf, NULL, map_rgba(pmask->format, *pcolor->color));

  return ctor_sprite(mypixbuf);
}
Exemple #24
0
/**************************************************************************
  Loads the sprite. If the sprite is already loaded a reference
  counter is increased. Can return NULL if the sprite couldn't be
  loaded.
**************************************************************************/
static struct sprite *load_sprite(struct theme *t, const char *tag_name)
{
  /* Lookup information about where the sprite is found. */
  struct small_sprite *ss;

  log_debug("load_sprite(tag='%s')", tag_name);
  if (!small_sprite_hash_lookup(t->sprite_hash, tag_name, &ss)) {
    return NULL;
  }

  fc_assert_ret_val(ss->ref_count >= 0, NULL);

  if (!ss->sprite) {
    /* If the sprite hasn't been loaded already, then load it. */
    fc_assert_ret_val(ss->ref_count == 0, NULL);
    if (ss->file) {
      ss->sprite = load_gfx_file(ss->file);
      if (!ss->sprite) {
        log_fatal("Couldn't load gfx file \"%s\" for sprite '%s'.",
                  ss->file, tag_name);
        exit(EXIT_FAILURE);
      }
    } else {
      int sf_w, sf_h;

      ensure_big_sprite(ss->sf);
      get_sprite_dimensions(ss->sf->big_sprite, &sf_w, &sf_h);
      if (ss->x < 0 || ss->x + ss->width > sf_w
          || ss->y < 0 || ss->y + ss->height > sf_h) {
        log_error("Sprite '%s' in file \"%s\" isn't within the image!",
                  tag_name, ss->sf->file_name);
        return NULL;
      }
      ss->sprite =
	crop_sprite(ss->sf->big_sprite, ss->x, ss->y, ss->width, ss->height,
		    NULL, -1, -1);
    }
  }

  /* Track the reference count so we know when to free the sprite. */
  ss->ref_count++;

  return ss->sprite;
}
Exemple #25
0
/*********************************************************************
  Store the highest-ranking item in dest without removing it
 
  Return values:
     TRUE   dest was set.
     FALSE  The queue is empty.
**********************************************************************/
bool pq_peek(struct pqueue *q, pq_data_t * dest)
{
  fc_assert_ret_val(NULL != q, FALSE);
  if (q->size == 1) {
    return FALSE;
  }

  *dest = q->cells[1];
  return TRUE;
}
Exemple #26
0
/****************************************************************************
  Returns the research structure associated with the player.
****************************************************************************/
struct player_research *player_research_get(const struct player *pplayer)
{
  fc_assert_ret_val(NULL != pplayer, NULL);

  if (game.info.team_pooled_research) {
    return &research_array[team_number(pplayer->team)];
  } else {
    return &research_array[player_number(pplayer)];
  }
}
Exemple #27
0
/*****************************************************************************
  Internal api error function.
  Invoking this will cause Lua to stop executing the current context and
  throw an exception, so to speak.
*****************************************************************************/
int luascript_error_vargs(lua_State *L, const char *format, va_list vargs)
{
  fc_assert_ret_val(L != NULL, -1);

  luaL_where(L, 1);
  lua_pushvfstring(L, format, vargs);
  lua_concat(L, 2);

  return lua_error(L);
}
Exemple #28
0
/**************************************************************************
  For a string which _has_ intl marking (caller should check beforehand)
  returns gettext result of string inside marking.
  Below buf is an internal buffer which is never free'd.
**************************************************************************/
static const char *convert_intl_marking(const char *str)
{
  static struct astring buf = ASTRING_INIT;
  int len = strlen(str);

  fc_assert_ret_val(len >= 5, str);
  astr_set(&buf, "%s", str + 3);
  *((char *) &astr_str(&buf)[len - 5]) = '\0';

  return Q_(astr_str(&buf));
}
Exemple #29
0
/**************************************************************************
  ...
**************************************************************************/
SDL_Surface * create_text_surf_smaller_that_w(SDL_String16 *pString, int w)
{
  fc_assert_ret_val(pString != NULL, NULL);

  SDL_Surface *pText = create_text_surf_from_str16(pString);
  
  if(pText && pText->w > w - 4) {
    /* cut string length to w length by replacing space " " with new line "\n" */
    int ptsize = pString->ptsize;
    Uint16 pNew_Line[2], pSpace[2];
    Uint16 *ptr = pString->text;
    
    convertcopy_to_utf16(pNew_Line, sizeof(pNew_Line), "\n");
    convertcopy_to_utf16(pSpace, sizeof(pSpace), " ");
   
    do {
      if (*ptr) {
	if(*ptr == *pSpace) {
          *ptr = *pNew_Line;/* "\n" */
	  FREESURFACE(pText);
          pText = create_text_surf_from_str16(pString);
	}
	ptr++;
      } else {
	FREESURFACE(pText);
        if (pString->ptsize > 8) {
	  change_ptsize16(pString, pString->ptsize - 1);
	  pText = create_text_surf_from_str16(pString);
	} else {
          fc_assert_ret_val(pText != NULL, NULL);
	}
      }
    } while (pText->w > w - 4);    
  
    if(pString->ptsize != ptsize) {
      change_ptsize16(pString, ptsize);
    }    
  }
    
  return pText;
}
Exemple #30
0
/**************************************************************************
  ...
**************************************************************************/
SDL_String16 * copy_chars_to_string16(SDL_String16 *pString,
						const char *pCharString)
{
  size_t n;

  fc_assert_ret_val(pString != NULL, NULL);
  fc_assert_ret_val(pCharString != NULL, NULL);
  
  n = (strlen(pCharString) + 1) * 2;
  
  if (n > pString->n_alloc) {
    /* allocated more if this is only a small increase on before: */
    size_t n1 = (3 * pString->n_alloc) / 2;
    pString->n_alloc = (n > n1) ? n : n1;
    pString->text = fc_realloc(pString->text, pString->n_alloc);
  }
  
  convertcopy_to_utf16(pString->text, pString->n_alloc, pCharString);
  
  return pString;
}