src/gen-chunk.c
struct chunk *chunk_write(int y0, int x0, int height, int width, bool monsters,
						 bool objects, bool traps)
{
	int x, y;

	struct chunk *new = cave_new(height, width);

	/* Write the location stuff */
	for (y = 0; y < height; y++) {
		for (x = 0; x < width; x++) {
			/* Terrain */
			new->squares[y][x].feat = cave->squares[y0 + y][x0 + x].feat;
			sqinfo_copy(new->squares[y][x].info,
						cave->squares[y0 + y][x0 + x].info);

			/* Dungeon objects */
			if (objects) {
				struct object *obj = square_object(cave, y0 + y, x0 + x);
				if (obj) {
					new->squares[y][x].obj = obj;
					while (obj) {
						/* Adjust stuff */
						obj->iy = y;
						obj->ix = x;
					}
				}
			}

			/* Monsters and held objects */
			if (monsters) {
				if (cave->squares[y0 + y][x0 + x].mon > 0) {
					struct monster *source_mon = square_monster(cave, y0 + y,
															  x0 + x);
					struct monster *dest_mon = NULL;

					/* Valid monster */
					if (!source_mon->race)
						continue;

					/* Copy over */
					new->squares[y][x].mon = ++new->mon_cnt;
					dest_mon = cave_monster(new, new->mon_cnt);
					memcpy(dest_mon, source_mon, sizeof(*source_mon));

					/* Adjust position */
					dest_mon->fy = y;
					dest_mon->fx = x;

					/* Held objects */
					if (objects && source_mon->held_obj)
						dest_mon->held_obj = source_mon->held_obj;

					delete_monster(y0 + y, x0 + x);
				}
			}

			/* Traps */
			if (traps) {
				/* Copy over */
				struct trap *trap = cave->squares[y][x].trap;
				new->squares[y][x].trap = trap;
				cave->squares[y][x].trap = NULL;

				/* Adjust position */
				trap->fy = y;
				trap->fx = x;
			}
		}
	}

	return new;
}
/**
 * Write a chunk to memory and return a pointer to it.  Optionally write
 * monsters, objects and/or traps, and in those cases delete those things from
 * the source chunk
 * \param y0
 * \param x0 coordinates of the top left corner of the chunk being written
 * \param height
 * \param width dimensions of the chunk being written
 * \param monsters whether monsters get written
 * \param objects whether objects get written
 * \param traps whether traps get written
 * \return the memory location of the chunk
 */
Write a chunk to memory and return a pointer to it.
Write a chunk to memory and return a pointer to it .
struct chunk *chunk_write(int y0, int x0, int height, int width, bool monsters,
						 bool objects, bool traps)
{
	int x, y;
	struct chunk *new = cave_new(height, width);
	for (y = 0; y < height; y++) {
		for (x = 0; x < width; x++) {
			new->squares[y][x].feat = cave->squares[y0 + y][x0 + x].feat;
			sqinfo_copy(new->squares[y][x].info,
						cave->squares[y0 + y][x0 + x].info);
			if (objects) {
				struct object *obj = square_object(cave, y0 + y, x0 + x);
				if (obj) {
					new->squares[y][x].obj = obj;
					while (obj) {
						obj->iy = y;
						obj->ix = x;
					}
				}
			}
			if (monsters) {
				if (cave->squares[y0 + y][x0 + x].mon > 0) {
					struct monster *source_mon = square_monster(cave, y0 + y,
															  x0 + x);
					struct monster *dest_mon = NULL;
					if (!source_mon->race)
						continue;
					new->squares[y][x].mon = ++new->mon_cnt;
					dest_mon = cave_monster(new, new->mon_cnt);
					memcpy(dest_mon, source_mon, sizeof(*source_mon));
					dest_mon->fy = y;
					dest_mon->fx = x;
					if (objects && source_mon->held_obj)
						dest_mon->held_obj = source_mon->held_obj;
					delete_monster(y0 + y, x0 + x);
				}
			}
			if (traps) {
				struct trap *trap = cave->squares[y][x].trap;
				new->squares[y][x].trap = trap;
				cave->squares[y][x].trap = NULL;
				trap->fy = y;
				trap->fx = x;
			}
		}
	}
	return new;
}
struct chunk * chunk_write ( int y0 , int x0 , int height , int width , bool monsters , bool objects , bool traps ) { int x , y ; struct chunk * new = cave_new ( height , width ) ; for ( y = 0 ; y < height ; y ++ ) { for ( x = 0 ; x < width ; x ++ ) { new -> squares [ y ] [ x ] . feat = cave -> squares [ y0 + y ] [ x0 + x ] . feat ; sqinfo_copy ( new -> squares [ y ] [ x ] . info , cave -> squares [ y0 + y ] [ x0 + x ] . info ) ; if ( objects ) { struct object * obj = square_object ( cave , y0 + y , x0 + x ) ; if ( obj ) { new -> squares [ y ] [ x ] . obj = obj ; while ( obj ) { obj -> iy = y ; obj -> ix = x ; } } } if ( monsters ) { if ( cave -> squares [ y0 + y ] [ x0 + x ] . mon > 0 ) { struct monster * source_mon = square_monster ( cave , y0 + y , x0 + x ) ; struct monster * dest_mon = NULL ; if ( ! source_mon -> race ) continue ; new -> squares [ y ] [ x ] . mon = ++ new -> mon_cnt ; dest_mon = cave_monster ( new , new -> mon_cnt ) ; memcpy ( dest_mon , source_mon , sizeof ( * source_mon ) ) ; dest_mon -> fy = y ; dest_mon -> fx = x ; if ( objects && source_mon -> held_obj ) dest_mon -> held_obj = source_mon -> held_obj ; delete_monster ( y0 + y , x0 + x ) ; } } if ( traps ) { struct trap * trap = cave -> squares [ y ] [ x ] . trap ; new -> squares [ y ] [ x ] . trap = trap ; cave -> squares [ y ] [ x ] . trap = NULL ; trap -> fy = y ; trap -> fx = x ; } } } return new ; }
Write a chunk to memory and return a pointer to it.
Write a chunk to memory and return a pointer to it .
/* Write the location stuff */ /* Terrain */ /* Dungeon objects */ /* Adjust stuff */ /* Monsters and held objects */ /* Valid monster */ /* Copy over */ /* Adjust position */ /* Held objects */ /* Traps */ /* Copy over */ /* Adjust position */