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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
|
#include "config.h"
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <png.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <gdk/gdkkeysyms.h>
#include <gnome.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "client-types.h"
#include "gnome-cf.h"
#include "client.h"
#include "gnomeproto.h"
/* Start of map handling code.
* For the most part, this actually is not window system specific,
* but certainly how the client wants to store this may vary.
*/
struct Map the_map;
uint8 map_did_scroll=0;
/*
* Added for fog of war. Current size of the map structure in memory.
* We assume a rectangular map so this is the length of one side.
* command.c needs to know about this so not static
* FIX ME: Don't assume rectangle
*/
int map_size= 0;
PlayerPosition pl_pos;
/* gnome does not currently support this - just turn it off to keep the
* code more common
*/
static uint8 sdlimage=0;
/*
* Takes three args, first is a return value that is a pointer
* we should put map info into. Next two are map dimensions.
* This function supports non rectangular maps but the client
* pretty much doesn't. The caller is responsible for freeing
* the data. I have the caller pass in a map struct instead of
* returning a pointer because I didn't want to change all the
* the_map.cells to the_map->cells...
* The returned map memory is zero'ed.
*/
void allocate_map( struct Map* new_map, int ax, int ay)
{
int i= 0;
if( new_map == NULL)
return;
if( ax < 1 || ay < 1) {
new_map->cells= NULL;
return;
}
new_map->cells= (struct MapCell**)calloc( sizeof( struct MapCell*) * ay
+ sizeof( struct MapCell) *
map_size * map_size, 1);
if( new_map->cells == NULL)
return;
/* Skip past the first row of pointers to rows and assign the start of
* the actual map data
*/
new_map->cells[0]= (struct MapCell*)((char*)new_map->cells +
(sizeof( struct MapCell*) * ay));
/* Finish assigning the beginning of each row relative to the first row
* assigned above
*/
for( i= 0; i < ay; i++)
{
new_map->cells[i]= new_map->cells[0] + ( i * ax);
}
new_map->x= ax;
new_map->y= ay;
return;
}
/*
* Clears out all the cells in the current view (which is
* the whole map if not using fog_of_war, and request
* a map update from the server
*/
void reset_map()
{
if( fog_of_war == TRUE)
{
int x= 0;
int y= 0;
pl_pos.x= the_map.x/2;
pl_pos.y= the_map.y/2;
memset( the_map.cells[0], 0,
sizeof( struct MapCell) * the_map.x * the_map.y);
for( x= pl_pos.x; x < (pl_pos.x + mapx); x++)
{
for( y= pl_pos.y; y < (pl_pos.y + mapy); y++)
{
the_map.cells[x][y].need_update= 1;
}
}
}
else
{
int x= 0;
int y= 0;
memset( the_map.cells[0], 0,
sizeof( struct MapCell) * the_map.x * the_map.y);
for( x= 0; x < mapx; x++)
{
for( y= 0; y < mapy; y++)
{
the_map.cells[x][y].need_update= 1;
}
}
}
cs_print_string(csocket.fd, "mapredraw");
return;
}
void display_map_clearcell(long x,long y)
{
if( fog_of_war == TRUE)
{
/* we don't want to clear out the values yet. We will do that
* next time we try to write some data to this tile. For now
* we just mark that it has been cleared. Also mark it for
* update so we can draw the proper fog cell over it
*/
x+= pl_pos.x;
y+= pl_pos.y;
the_map.cells[x][y].cleared= 1;
the_map.cells[x][y].need_update= 1;
}
else
{
int i;
the_map.cells[x][y].count = 0;
the_map.cells[x][y].darkness = 0;
the_map.cells[x][y].need_update = 1;
the_map.cells[x][y].have_darkness = 0;
the_map.cells[x][y].cleared= 0;
for (i=0; i<MAXFACES; i++)
the_map.cells[x][y].faces[i] = -1; /* empty/blank face */
}
return;
}
void print_darkness()
{
int x= 0;
int y= 0;
for( y= 0; y < mapy; y++)
{
for( x= 0; x < mapx; x++)
{
if( the_map.cells[x][y].count== 0)
fprintf( stderr, "[ - ]");
else
fprintf( stderr, "[%3d]", the_map.cells[x][y].darkness);
}
fprintf( stderr, "\n");
}
}
void print_map()
{
int x= 0;
int y= 0;
int z= 0;
int local_mapx;
int local_mapy;
if( fog_of_war == TRUE)
{
local_mapx= pl_pos.x + mapx;
local_mapy= pl_pos.y + mapy;
printf( " Current X pos: %d -- Current Y pos: %d\n",
pl_pos.x, pl_pos.y);
}
else
{
local_mapx= mapx;
local_mapy= mapy;
}
fprintf( stderr, "-----------------------\n");
for( y= (fog_of_war == TRUE ? pl_pos.y : 0); y < local_mapy; y++)
{
for( z= 0; z < MAXFACES; z++)
{
for( x= (fog_of_war == TRUE ? pl_pos.x : 0); x < local_mapx; x++)
{
if( the_map.cells[x][y].count == 0)
fprintf( stderr, "[ -- ]");
else
fprintf( stderr, "[%4d]", the_map.cells[x][y].faces[z]);
}
fprintf( stderr, "\n");
}
fprintf( stderr, "\n");
}
fprintf( stderr, "-----------------------\n");
return;
}
void set_map_darkness(int x, int y, uint8 darkness)
{
if( fog_of_war == TRUE)
{
x+= pl_pos.x;
y+= pl_pos.y;
}
the_map.cells[x][y].have_darkness = 1;
if (darkness != (255 - the_map.cells[x][y].darkness )) {
the_map.cells[x][y].darkness = 255 - darkness;
the_map.cells[x][y].need_update = 1;
/* pretty ugly - since the light code with pngximage uses
* neighboring spaces to adjust the darkness, we now need to
* let the neighbors know they should update their darkness
* now.
*/
if (sdlimage) {
if (x-1>0) the_map.cells[x-1][y].need_update = 1;
if (y-1>0) the_map.cells[x][y-1].need_update = 1;
if (x+1<mapx) the_map.cells[x+1][y].need_update = 1;
if (y+1<mapy) the_map.cells[x][y+1].need_update = 1;
}
}
}
/* sets the face at layer to some value. We just can't
* restact arbitrarily, as the server now sends faces only
* for layers that change, and not the entire space.
*/
void set_map_face(int x, int y, int layer, int face)
{
if( fog_of_war == TRUE)
{
x+= pl_pos.x;
y+= pl_pos.y;
}
if( (fog_of_war == TRUE) && (the_map.cells[x][y].cleared == 1) )
{
/* This cell has been cleared previously but now we are
* writing new data to do. So we have to clear it for real now
*/
int i= 0;
the_map.cells[x][y].count= 0;
the_map.cells[x][y].darkness= 0;
the_map.cells[x][y].need_update= 1;
the_map.cells[x][y].have_darkness= 0;
the_map.cells[x][y].cleared= 0;
for (i=0; i<MAXFACES; i++)
the_map.cells[x][y].faces[i]= -1; /* empty/blank face */
}
the_map.cells[x][y].faces[layer] = face;
if ((layer+1) > the_map.cells[x][y].count)
the_map.cells[x][y].count = layer+1;
the_map.cells[x][y].need_update = 1;
the_map.cells[x][y].have_darkness = 1;
}
void display_map_addbelow(long x,long y,long face)
{
if( fog_of_war == TRUE)
{
x+= pl_pos.x;
y+= pl_pos.y;
}
if( (fog_of_war == TRUE) && (the_map.cells[x][y].cleared == 1) )
{
/* This cell has been cleared previously but now we are
* writing new data to do. So we have to clear it for real now
*/
int i= 0;
the_map.cells[x][y].count= 0;
the_map.cells[x][y].darkness= 0;
the_map.cells[x][y].need_update= 1;
the_map.cells[x][y].have_darkness= 0;
the_map.cells[x][y].cleared= 0;
for (i=0; i<MAXFACES; i++)
the_map.cells[x][y].faces[i]= -1; /* empty/blank face */
}
the_map.cells[x][y].faces[the_map.cells[x][y].count] = face&0xFFFF;
the_map.cells[x][y].count ++;
the_map.cells[x][y].need_update = 1;
}
/*
* Returns true if virtual view is about to butt up against
* the side of the virtual map on the next scroll
* Only used for fog of war code
*/
static int need_recenter_map( int dx, int dy)
{
if( pl_pos.x + dx + mapx >= the_map.x ||
pl_pos.y + dx + mapy >= the_map.y ||
pl_pos.x + dx <= 0 ||
pl_pos.y + dy <= 0 )
{
return TRUE;
}
return FALSE;
}
/*
* Only used in fog of war code.
* Will recenter the virtual coordinates of the player view
* to the center of the map and try to keep as much current
* state in memory as possible
* If view is already close to center it won't move it
*/
static void recenter_virtual_map_view( struct Map *map)
{
static struct Map tmpmap;
struct MapCell **tmpcells;
int y_shift= 0;
int x_shift= 0;
int x= 0, y= 0;
if( map == NULL)
return;
if( tmpmap.cells == NULL)
{
allocate_map( &tmpmap, map->x, map->y);
}
/*
* If mapsize changed, allocate a new map
*/
if( tmpmap.x != map->x || tmpmap.y != map->y)
{
if( tmpmap.cells)
free( tmpmap.cells);
allocate_map( &tmpmap, map->x, map->y);
}
/*
* If we are less then 1/4 away from either edge of the virtual map
* or the next move would push us up against the edge (for small
* virtual maps with large views this could happen before our 0,0 view
* coordinate is within 1/4 of the edge) we shift to the center.
*/
if( pl_pos.x <= (map->x/4) || pl_pos.x >= (map->x*3/4) ||
pl_pos.x + mapx + 1 >= map->x )
{
x_shift= map->x/2 - pl_pos.x;
}
if( pl_pos.y <= (map->y/4) || pl_pos.y >= (map->y*3/4) ||
pl_pos.y + mapy + 1 >= map->y )
{
y_shift= map->y/2 - pl_pos.y;
}
if( x_shift == 0 && y_shift == 0)
return;
for( x= 0; x < map->x; x++)
{
if( x + x_shift >= map->x || x + x_shift < 0)
continue;
for( y= 0; y < map->y; y++)
{
if( y + y_shift >= map->y || y + y_shift < 0)
continue;
memcpy( (char*)&tmpmap.cells[x + x_shift][y + y_shift],
(char*)&map->cells[x][y],
sizeof( struct MapCell) );
}
}
pl_pos.x+= x_shift;
pl_pos.y+= y_shift;
/*
* Swap cell arrays then zero out the old cells to avoid another big memcopy
*/
tmpcells= map->cells;
map->cells= tmpmap.cells;
tmpmap.cells= tmpcells;
memset( (char*)&tmpmap.cells[0][0], 0,
sizeof( struct MapCell) * tmpmap.x * tmpmap.y);
return;
}
void display_mapscroll(int dx,int dy)
{
int x,y;
static struct Map newmap;
int local_mapx= 0, local_mapy= 0;
if( fog_of_war == TRUE)
{
/* We don't need to memcopy any of this stuff around cause
* we are keeping it in memory. We do need to update our
* virtual position though
*/
if( need_recenter_map( dx, dy) == TRUE)
{
recenter_virtual_map_view( &the_map);
}
pl_pos.x+= dx;
pl_pos.y+= dy;
local_mapx= pl_pos.x + mapx;
local_mapy= pl_pos.y + mapy;
/*
* For cells about to enter the view, mark them as
* needing an update. Cells that are already in
* view don't need to be updated since we just memcpy
* the image data around. This is needed for proper
* drawing of blank or black tiles coming into view
*/
for( x= pl_pos.x; x < pl_pos.x + mapx; x++) {
for( y= pl_pos.y; y < pl_pos.y + mapy; y++) {
if( (x + dx) < pl_pos.x || (x + dx) >= (mapx + pl_pos.x) ||
(y + dy) < pl_pos.y || (y + dy) >= (mapy + pl_pos.y) )
{
if( x < 0 || y < 0 || x >= the_map.x ||
y >= the_map.y)
{
continue;
}
the_map.cells[x][y].need_update= 1;
the_map.cells[x][y].cleared= 1;
}
} /* for y */
} /* for x */
}
else
{
local_mapx= mapx;
local_mapy= mapy;
}
if( newmap.cells == NULL)
allocate_map( &newmap, map_size, map_size);
/* Check to see if map_size changed since we allocated newmap */
if( newmap.x != map_size)
{
if( newmap.cells)
free( newmap.cells);
allocate_map( &newmap, map_size, map_size);
}
if( fog_of_war == FALSE) {
for(x=0;x<mapx;x++) {
for(y=0;y<mapy;y++) {
/* In case its own of range, set the count to zero */
if (x+dx < 0 || x+dx >= mapx ||y+dy < 0 || y+dy >= mapy) {
memset((char*)&newmap.cells[x][y], 0, sizeof(struct MapCell));
/*
* Changed my smacfiggen 6/20/2001 -- When new cells come onto
* the map and we aren't using the new map command we want to
* mark these as updated or else blank tiles get blitted with
* old info.
*
*/
if ( !map1cmd)
newmap.cells[x][y].need_update=1;
} else {
memcpy((char*)&(newmap.cells[x][y]), (char*)&(the_map.cells[x+dx][y+dy]),
sizeof(struct MapCell));
/* if using pngximage, we will instead set the map_did_scroll
* to 1 - we don't want to regen the backing image
*/
if( !sdlimage) {
newmap.cells[x][y].need_update=1;
}
}
}
}
memcpy((char*)the_map.cells[0],(char*)newmap.cells[0],
sizeof(struct MapCell)*newmap.x*newmap.y );
}
#if 0
if (sdlimage)
sdl_mapscroll(dx,dy);
#endif
/* fprintf(stderr,"scroll command: %d %d\n", dx, dy);*/
}
/*
* Clears all map data - this is only called when we have lost our connection
* to a server - this way bogus data won't be around when we connect
* to the new server
*/
void reset_map_data()
{
if( fog_of_war == TRUE)
{
int x= 0;
int y= 0;
pl_pos.x= the_map.x/2;
pl_pos.y= the_map.y/2;
memset( the_map.cells[0], 0,
sizeof( struct MapCell) * the_map.x * the_map.y);
for( x= pl_pos.x; x < (pl_pos.x + mapx); x++)
{
for( y= pl_pos.y; y < (pl_pos.y + mapy); y++)
{
the_map.cells[x][y].need_update= 1;
}
}
}
else
{
int x= 0;
int y= 0;
memset( the_map.cells[0], 0,
sizeof( struct MapCell) * the_map.x * the_map.y);
for( x= 0; x < mapx; x++)
{
for( y= 0; y < mapy; y++)
{
the_map.cells[x][y].need_update= 1;
}
}
}
}
/* gtk -> gnome mapping */
#define mapgc gc
/* This draws the map using GdkPixmaps/GdkBitmaps. It is based off of sdl_gen_map.
* We draw on a GdkPixmap drawable (mapwindow), which we then copy to the
* visible area on the screen. Doing this reduces flickering that otherwise happen
* if we draw directly to the window.
* The performance of this function seems to be very good - presumably because
* most of this data is stored in the x-server in a format native to it. Also,
* per pixel manipulations are not done - either the entire pixel is copied,
* or it is not, no blending is done. This means that true alpha blending
* is not supported, but nothing uses that now anyways.
*/
/*#define TIME_MAP_REDRAW*/
void gtk_draw_map()
{
int x,y,onlayer,layer, dst_x, dst_y;
#ifdef TIME_MAP_REDRAW
struct timeval tv1, tv2, tv3;
long elapsed1, elapsed2;
gettimeofday(&tv1, NULL);
#endif
gdk_draw_rectangle (drawable->window, drawable->style->black_gc,
TRUE, 0,0, map_image_size*mapx, map_image_size * mapy);
for (onlayer=MAXFACES-1; onlayer>=0; onlayer--) {
if (map1cmd) layer = (MAXFACES -1) - onlayer;
else layer = onlayer;
/* Fog not currently supported, so don't need to worry about that complexity */
for( x= mapx-1; x>= 0; x--) {
for(y = mapy-1; y >= 0; y--) {
/* there must be a real face for this layer, and we must have data for that face. */
if ((the_map.cells[x][y].faces[layer]>0) &&
pixmaps[the_map.cells[x][y].faces[layer]].map_image) {
/* Figure out how much data is being copied, and adjust the origin accordingly.
* This probably needs additional checking in case the image extends beyond the
* map boundries.
*/
dst_x = (x+1) * map_image_size - pixmaps[the_map.cells[x][y].faces[layer]].map_width;
dst_y = (y+1) * map_image_size - pixmaps[the_map.cells[x][y].faces[layer]].map_height;
gdk_gc_set_clip_mask (mapgc, pixmaps[the_map.cells[x][y].faces[layer]].map_mask);
gdk_gc_set_clip_origin(mapgc, dst_x, dst_y);
gdk_draw_pixmap(drawable->window, mapgc,
pixmaps[the_map.cells[x][y].faces[layer]].map_image,
0, 0, dst_x, dst_y,
pixmaps[the_map.cells[x][y].faces[layer]].map_width,
pixmaps[the_map.cells[x][y].faces[layer]].map_height);
}
/* On last past, do our special processing, like applying darkness */
if (onlayer==0) {
the_map.cells[x][y].need_update=0;
if (the_map.cells[x][y].darkness > 192) { /* Full dark */
gdk_draw_rectangle (drawable->window, drawable->style->black_gc,
TRUE,map_image_size*x, map_image_size *y,
map_image_size, map_image_size);
} else if (the_map.cells[x][y].darkness> 128) {
gdk_gc_set_clip_mask(mapgc, dark1);
gdk_gc_set_clip_origin(mapgc, x * map_image_size, y*map_image_size);
gdk_draw_pixmap(drawable->window, mapgc, dark, 0, 0,
x * map_image_size, y*map_image_size, map_image_size, map_image_size);
} else if (the_map.cells[x][y].darkness> 64) {
gdk_gc_set_clip_mask(mapgc, dark2);
gdk_gc_set_clip_origin(mapgc, x * map_image_size, y*map_image_size);
gdk_draw_pixmap(drawable->window, mapgc, dark, 0, 0,
x * map_image_size, y*map_image_size, map_image_size, map_image_size);
} else if (the_map.cells[x][y].darkness> 1) {
gdk_gc_set_clip_mask(mapgc, dark3);
gdk_gc_set_clip_origin(mapgc, x * map_image_size, y*map_image_size);
gdk_draw_pixmap(drawable->window, mapgc, dark, 0, 0,
x * map_image_size, y*map_image_size, map_image_size, map_image_size);
}
}
} /* for y */
} /* for x */
} /* for layers */
#ifdef TIME_MAP_REDRAW
gettimeofday(&tv2, NULL);
#endif
gdk_draw_pixmap(drawable->window, drawable->style->black_gc, drawable->window,
0, 0, 0, 0, mapx * map_image_size, mapy * map_image_size);
#ifdef TIME_MAP_REDRAW
gettimeofday(&tv3, NULL);
elapsed1 = (tv2.tv_sec - tv1.tv_sec)*1000000 + (tv2.tv_usec - tv1.tv_usec);
elapsed2 = (tv3.tv_sec - tv2.tv_sec)*1000000 + (tv3.tv_usec - tv2.tv_usec);
/* I care about performance for 'long' updates, so put the check in to make
* these a little more noticable */
if ((elapsed1 + elapsed2)>10000)
fprintf(stderr,"gtk_draw_map: gen took %7ld, flip took %7ld, total = %7ld\n",
elapsed1, elapsed2, elapsed1 + elapsed2);
#endif
}
|