1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
/*
* Copyright (c) 1997 - 2001 Hj. Malthaner
*
* This file is part of the Simutrans project under the artistic license.
* (see license.txt)
*/
/*
* Basic class of all visible things
*
* Hj. Maltahner
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "simdebug.h"
#include "simimg.h"
#include "simcolor.h"
#include "simgraph.h"
#include "simwin.h"
#include "player/simplay.h"
#include "simdings.h"
#include "simworld.h"
#include "dings/baum.h"
#include "vehicle/simvehikel.h"
#include "dataobj/translator.h"
#include "dataobj/loadsave.h"
#include "boden/grund.h"
#include "gui/thing_info.h"
#include "utils/cbuffer_t.h"
#include "utils/simstring.h"
/**
* Pointer to the world of this thing. Static to conserve space.
* Change to instance variable once more than one world is available.
* @author Hj. Malthaner
*/
karte_t * ding_t::welt = NULL;
bool ding_t::show_owner = false;
void ding_t::init(karte_t *wl)
{
welt = wl;
pos = koord3d::invalid;
xoff = 0;
yoff = 0;
besitzer_n = PLAYER_UNOWNED;
flags = keine_flags;
set_flag(dirty);
}
ding_t::ding_t(karte_t *wl)
{
init(wl);
}
ding_t::ding_t(karte_t *wl, loadsave_t *file)
{
init(wl);
rdwr(file);
}
ding_t::ding_t(karte_t *wl, koord3d pos)
{
init(wl);
this->pos = pos;
}
// removes an object and tries to delete it also from the corresponding dinglist
ding_t::~ding_t()
{
destroy_win((long)this);
if(flags¬_on_map || !welt->ist_in_kartengrenzen(pos.get_2d())) {
return;
}
// find object on the map and remove it
grund_t *gr = welt->lookup(pos);
if(!gr || !gr->obj_remove(this)) {
// not found? => try harder at all map locations
dbg->warning("ding_t::~ding_t()","couldn't remove %p from %d,%d,%d",this, pos.x , pos.y, pos.z);
// first: try different height ...
gr = welt->access(pos.get_2d())->get_boden_von_obj(this);
if(gr && gr->obj_remove(this)) {
dbg->warning("ding_t::~ding_t()",
"removed %p from %d,%d,%d, but it should have been on %d,%d,%d",
this,
gr->get_pos().x, gr->get_pos().y, gr->get_pos().z,
pos.x, pos.y, pos.z);
return;
}
// then search entire map
koord k;
for(k.y=0; k.y<welt->get_groesse_y(); k.y++) {
for(k.x=0; k.x<welt->get_groesse_x(); k.x++) {
grund_t *gr = welt->access(k)->get_boden_von_obj(this);
if (gr && gr->obj_remove(this)) {
dbg->warning("ding_t::~ding_t()",
"removed %p from %d,%d,%d, but it should have been on %d,%d,%d",
this,
gr->get_pos().x, gr->get_pos().y, gr->get_pos().z,
pos.x, pos.y, pos.z);
return;
}
}
}
}
}
/**
* sets owner of object
*/
void ding_t::set_besitzer(spieler_t *sp)
{
int i = welt->sp2num(sp);
assert(i>=0);
besitzer_n = (uint8)i;
}
spieler_t *ding_t::get_besitzer() const
{
return welt->get_spieler(besitzer_n);
}
/* the only general info we can give is the name
* we want to format it nicely,
* with two linebreaks at the end => thus the little extra effort
*/
void ding_t::info(cbuffer_t & buf) const
{
char translation[256];
char const* const owner =
besitzer_n == 1 ? translator::translate("Eigenbesitz\n") :
besitzer_n == PLAYER_UNOWNED ? translator::translate("Kein Besitzer\n") :
get_besitzer()->get_name();
tstrncpy(translation, owner, lengthof(translation));
// remove trailing linebreaks etc.
rtrim(translation);
buf.append( translation );
// only append linebreaks if not empty
if( buf.len()>0 ) {
buf.append( "\n\n" );
}
}
void ding_t::zeige_info()
{
create_win( new ding_infowin_t(this), w_info, (long)this);
}
// returns NULL, if removal is allowed
const char *ding_t::ist_entfernbar(const spieler_t *sp)
{
if(besitzer_n==PLAYER_UNOWNED || welt->get_spieler(besitzer_n) == sp || welt->get_spieler(1) == sp) {
return NULL;
}
else {
return "Der Besitzer erlaubt das Entfernen nicht";
}
}
void ding_t::rdwr(loadsave_t *file)
{
xml_tag_t d( file, "ding_t" );
if( file->get_version()<101000) {
pos.rdwr( file );
}
sint8 byte = (sint8)(((sint16)16*(sint16)xoff)/OBJECT_OFFSET_STEPS);
file->rdwr_byte(byte);
xoff = (sint8)(((sint16)byte*OBJECT_OFFSET_STEPS)/16);
byte = (sint8)(((sint16)16*(sint16)yoff)/OBJECT_OFFSET_STEPS);
file->rdwr_byte(byte);
yoff = (sint8)(((sint16)byte*OBJECT_OFFSET_STEPS)/16);
byte = besitzer_n;
file->rdwr_byte(byte);
besitzer_n = byte;
}
/**
* draw the object
* the dirty-flag is resetted from dingliste_t::display_dinge_fg
*/
void ding_t::display(int xpos, int ypos) const
{
image_id bild = get_bild();
image_id const outline_bild = get_outline_bild();
if( bild!=IMG_LEER || outline_bild!=IMG_LEER ) {
const int raster_width = get_current_tile_raster_width();
const bool is_dirty = get_flag(ding_t::dirty);
if (vehikel_basis_t const* const v = ding_cast<vehikel_basis_t>(this)) {
// vehicles need finer steps to appear smoother
v->get_screen_offset( xpos, ypos, raster_width );
}
xpos += tile_raster_scale_x(get_xoff(), raster_width);
ypos += tile_raster_scale_y(get_yoff(), raster_width);
const int start_ypos = ypos;
for( int j=0; bild!=IMG_LEER; ) {
if(besitzer_n!=PLAYER_UNOWNED) {
if( ding_t::show_owner ) {
display_blend(bild, xpos, ypos, besitzer_n, (welt->get_spieler(besitzer_n)->get_player_color1()+2) | OUTLINE_FLAG | TRANSPARENT75_FLAG, 0, is_dirty);
}
else {
display_color(bild, xpos, ypos, besitzer_n, true, is_dirty);
}
}
else {
display_normal(bild, xpos, ypos, 0, true, is_dirty);
}
// this ding has another image on top (e.g. skyscraper)
ypos -= raster_width;
bild = get_bild(++j);
}
if( outline_bild!=IMG_LEER ) {
// transparency?
const PLAYER_COLOR_VAL transparent = get_outline_colour();
if(TRANSPARENT_FLAGS&transparent) {
// only transparent outline
display_blend(get_outline_bild(), xpos, start_ypos, besitzer_n, transparent, 0, is_dirty);
}
else if( ding_t::get_flag( highlight ) ) {
// highlight this tile
display_blend(get_bild(), xpos, start_ypos, besitzer_n, COL_RED | OUTLINE_FLAG | TRANSPARENT75_FLAG, 0, is_dirty);
}
}
else if( ding_t::get_flag( highlight ) ) {
// highlight this tile
display_blend(get_bild(), xpos, start_ypos, besitzer_n, COL_RED | OUTLINE_FLAG | TRANSPARENT75_FLAG, 0, is_dirty);
}
}
}
// called during map rotation
void ding_t::rotate90()
{
// most basic: rotate coordinate
pos.rotate90( welt->get_groesse_y()-1 );
if(xoff!=0) {
sint8 new_dx = -2*yoff;
yoff = xoff/2;
xoff = new_dx;
}
}
void ding_t::display_after(int xpos, int ypos, bool) const
{
image_id bild = get_after_bild();
if(bild != IMG_LEER) {
const int raster_width = get_current_tile_raster_width();
const bool is_dirty = get_flag(ding_t::dirty);
xpos += tile_raster_scale_x(get_xoff(), raster_width);
ypos += tile_raster_scale_y(get_yoff(), raster_width);
if(besitzer_n!=PLAYER_UNOWNED) {
if( ding_t::show_owner ) {
display_blend(bild, xpos, ypos, besitzer_n, (welt->get_spieler(besitzer_n)->get_player_color1()+2) | OUTLINE_FLAG | TRANSPARENT75_FLAG, 0, is_dirty);
}
else if( ding_t::get_flag( highlight ) ) {
// highlight this tile
display_blend( bild, xpos, ypos, besitzer_n, COL_RED | OUTLINE_FLAG | TRANSPARENT75_FLAG, 0, is_dirty);
}
else {
display_color(bild, xpos, ypos, besitzer_n, true, is_dirty);
}
}
else if( ding_t::get_flag( highlight ) ) {
// highlight this tile
display_blend( bild, xpos, ypos, besitzer_n, COL_RED | OUTLINE_FLAG | TRANSPARENT75_FLAG, 0, is_dirty);
}
else {
display_normal(bild, xpos, ypos, 0, true, is_dirty );
}
}
}
/*
* when a vehicle moves or a cloud moves, it needs to mark the old spot as dirty (to copy to screen)
* sometimes they have an extra offset, this is the yoff parameter
* @author prissi
*/
void ding_t::mark_image_dirty(image_id bild,sint16 yoff) const
{
if(bild!=IMG_LEER) {
int xpos=0, ypos=0;
if( is_moving() ) {
vehikel_basis_t const* const v = ding_cast<vehikel_basis_t>(this);
// vehicles need finer steps to appear smoother
v->get_screen_offset( xpos, ypos, get_tile_raster_width() );
}
// better not try to twist your brain to follow the retransformation ...
const sint16 rasterweite=get_tile_raster_width();
const koord diff = get_pos().get_2d()-welt->get_world_position()-welt->get_ansicht_ij_offset();
const sint16 x = (diff.x-diff.y)*(rasterweite/2) + tile_raster_scale_x(get_xoff(), rasterweite) + xpos;
const sint16 y = (diff.x+diff.y)*(rasterweite/4) + tile_raster_scale_y( yoff+get_yoff()-get_pos().z*TILE_HEIGHT_STEP/Z_TILE_STEP, rasterweite) + ((display_get_width()/rasterweite)&1)*(rasterweite/4) + ypos;
// mark the region after the image as dirty
display_mark_img_dirty( bild, x+welt->get_x_off(), y+welt->get_y_off() );
}
}
|