algorithm.c
author viric@llimona
Sun, 07 May 2006 01:33:40 +0200
changeset 13 c2b272938a1f
parent 12 a2c334a71a6b
child 16 fe078fb2e8b4
permissions -rw-r--r--
actual_map is initialized at first, so almost never a SIGSEGV can happen. Even though it should be initialized at init_so, and protecting against SIGSEGV there.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
     1
#include <assert.h>
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
     2
#include <string.h>
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
     3
#include <stdio.h> /**********************************PROVA */
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
     4
#include "general.h"
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
     5
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
     6
/* Variables related to the showing of information by os */
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
     7
extern float percent_to_show;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
     8
extern int depth_to_show;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
     9
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    10
extern int max_depth;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    11
extern int min_depth_period;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    12
extern int max_depth_period;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    13
extern struct Map * actual_map;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    14
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
    15
/* Prototipes */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
    16
static Bool are_there_fixed_boxes(struct Map *m,
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
    17
	struct BoxMove movements[MAX_MOVES], int *num_movements);
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
    18
static void force_move_box(struct Map *m, const struct BoxMove move);
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
    19
static Bool search_did_found(struct Map maps[], int depth,
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
    20
	struct BoxMove movements[],
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
    21
	int num_movements, float percent, float total_percent);
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
    22
static void fill_deps(struct Map *m);
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
    23
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    24
#if 0 /*** THIS IS AN ERROR!!!  The box_will_be_blocked function doesn't work!*/
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    25
 Situation:
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    26
    
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    27
  ->@$ #
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    28
      $
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    29
*/
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    30
int box_will_be_blocked(const struct Map * m, const struct Position mov,
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    31
	const struct Position box_pos)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    32
{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    33
	struct Position next_pos2, tmp, tmp2[2];
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    34
	int i;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    35
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    36
	next_pos2.x = box_pos.x + mov.x;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    37
	next_pos2.y = box_pos.y + mov.y;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    38
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    39
	tmp.x = next_pos2.x + mov.x;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    40
	tmp.y = next_pos2.y + mov.y;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    41
	tmp2[0].x = next_pos2.x + mov.y;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    42
	tmp2[0].y = next_pos2.y + mov.x;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    43
	tmp2[1].x = next_pos2.x - mov.y;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    44
	tmp2[1].y = next_pos2.y - mov.x;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    45
	for (i=0; i < 2; i++)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    46
	{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    47
		if (m->man_moves[tmp.y][tmp.x] == WALL &&
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    48
			m->man_moves[tmp2[i].y][tmp2[i].x] == BOX)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    49
		{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    50
			return TRUE;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    51
		}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    52
		else if (m->man_moves[tmp.y][tmp.x] == BOX &&
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    53
			m->man_moves[tmp2[i].y][tmp2[i].x] == WALL)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    54
		{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    55
			return TRUE;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    56
		}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    57
		else if (m->man_moves[tmp.y][tmp.x] == BOX &&
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    58
			m->man_moves[tmp2[i].y][tmp2[i].x] == BOX)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    59
		{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    60
			return TRUE;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    61
		}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    62
	}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    63
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    64
	return FALSE;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    65
}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    66
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    67
int is_corner_area(const struct Map * m, const struct Position p,
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    68
	const struct Position box, const struct Position new_box)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    69
{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    70
	int NumMoves, NewMoves;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    71
	struct Position pos[MAX_MOVES];
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    72
	struct Position new_pos[MAX_MOVES];
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    73
	char corners[MAX_Y][MAX_X];
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    74
	int i,j;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    75
	struct Position next_pos;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    76
	char *next_cell;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    77
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    78
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    79
	/* Blank the garden */
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    80
	for (j = 0; j<m->SizeY; j++)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    81
		for (i=0; i<m->SizeX; i++)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    82
			corners[j][i] = m->Cells[j][i];
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    83
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    84
	/* Let's put the boxes */
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    85
	for (i = 0; i<m->NumBoxes; i++)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    86
		corners[m->Box[i].y][m->Box[i].x] = BOX;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    87
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    88
	/* Let's put our box - it can be simply added */
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    89
	corners[new_box.y][new_box.x] = BOX;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    90
	/* Let's remove the original box. */
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    91
	corners[box.y][box.x] = BLANK;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    92
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    93
	NewMoves = 1;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    94
	new_pos[0] = p;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    95
	while (NewMoves > 0)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    96
	{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    97
		/* The before named "New Moves" become the moves we have
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    98
		   to analyze */
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
    99
		NumMoves = NewMoves;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   100
		for (i=0; i<NewMoves; i++)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   101
		{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   102
			pos[i] = new_pos[i];
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   103
		}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   104
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   105
		/* Search new positions for each position */
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   106
		NewMoves = 0;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   107
		for (i=0; i<NumMoves; i++)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   108
		{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   109
			/* For each direction */
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   110
			for (j=0; j<4; j++)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   111
			{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   112
				next_pos.x = pos[i].x + move_vectors[j].x;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   113
				next_pos.y = pos[i].y + move_vectors[j].y;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   114
				next_cell = &corners[next_pos.y][next_pos.x];
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   115
				if(*next_cell == BLANK ||
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   116
					*next_cell == PLATFORM)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   117
				{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   118
					return FALSE;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   119
				}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   120
				else if(*next_cell == CORNER)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   121
				{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   122
					new_pos[NewMoves] = next_pos;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   123
					*next_cell = MANCANMOVE;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   124
					NewMoves++;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   125
				}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   126
			}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   127
		}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   128
	}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   129
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   130
	return TRUE;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   131
}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   132
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   133
int does_box_close_corners(const struct Map * m, const struct Position mov,
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   134
	const struct Position box_pos)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   135
{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   136
	struct Position p, p2;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   137
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   138
	p.x = box_pos.x + mov.x;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   139
	p.y = box_pos.y + mov.y;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   140
	
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   141
	/* Let's plan the checks */
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   142
	/* The point will be marked with a MANCANMOVE */
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   143
	p2.x = p.x + mov.x;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   144
	p2.y = p.y + mov.y;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   145
	if (m->Cells[p2.y][p2.x] == CORNER)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   146
	{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   147
		if (is_corner_area(m, p2, box_pos, p))
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   148
			return TRUE;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   149
	}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   150
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   151
	p2.x = p.x + mov.y;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   152
	p2.y = p.y + mov.x;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   153
	if (m->Cells[p2.y][p2.x] == CORNER)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   154
	{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   155
		if (is_corner_area(m, p2, box_pos, p))
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   156
			return TRUE;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   157
	}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   158
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   159
	p2.x = p.x - mov.y;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   160
	p2.y = p.y - mov.x;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   161
	if (m->Cells[p2.y][p2.x] == CORNER)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   162
	{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   163
		if (is_corner_area(m, p2, box_pos, p))
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   164
			return TRUE;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   165
	}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   166
	return FALSE;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   167
}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   168
#endif
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   169
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   170
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   171
static void force_move_box(struct Map *m, const struct BoxMove move)
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   172
{
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   173
	struct Position newpos;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   174
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   175
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   176
	/* Add coords */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   177
	newpos.x = m->Box[move.box].x + move.dir.x;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   178
	newpos.y = m->Box[move.box].y + move.dir.y;
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   179
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   180
	/* Be certain the move can be done */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   181
	assert(m->Cells[newpos.y][newpos.x] != BOX);
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   182
	assert(m->Cells[newpos.y][newpos.x] != WALL);
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   183
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   184
	/* Control if we moved the box to a platform */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   185
	if(m->Cells[newpos.y][newpos.x] == PLATFORM)
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   186
	{
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   187
		m->NumBoxesInPlatform++;
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   188
	}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   189
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   190
	/* Control if we moved the box from a platform */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   191
	if (m->Cells[m->Box[move.box].y][m->Box[move.box].x] == PLATFORM)
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   192
	{
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   193
		m->NumBoxesInPlatform--;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   194
	}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   195
	m->Man = m->Box[move.box];
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   196
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   197
	m->Box[move.box] = newpos;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   198
}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   199
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   200
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   201
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   202
static Bool search_did_found(struct Map maps[], int depth,
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   203
	struct BoxMove movements[],
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   204
	int num_movements, float percent, float total_percent)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   205
{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   206
	struct BoxMove new_movements[MAX_MOVES];
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   207
	int num_new_movements;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   208
	struct Map *m; 
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   209
	int i;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   210
	float next_percent;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   211
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   212
	next_percent = percent / num_movements;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   213
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   214
	m = &maps[depth+1];
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   215
	if (depth > max_depth)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   216
		max_depth = depth;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   217
	if (depth > max_depth_period)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   218
		max_depth_period = depth;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   219
	if (depth < min_depth_period)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   220
		min_depth_period = depth;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   221
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   222
	for (i=0; i < num_movements; i++)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   223
	{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   224
		CopyMap(m, &maps[depth]);
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   225
		force_move_box(m, movements[i]);
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   226
		/* Let's see if we finished */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   227
		if (m->NumPlatforms == m->NumBoxesInPlatform)
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   228
		{
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   229
			PrintMove(movements[i]);
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   230
			actual_map = &maps[depth+1];
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   231
			show_percent_and_map();
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   232
			return TRUE;
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   233
		}
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   234
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   235
		if (is_new_map(maps, depth+1))
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   236
		{
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   237
			actual_map = &maps[depth+1];
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   238
#ifdef DEBUG		/* to be out */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   239
			show_percent_and_map();
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   240
#endif
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   241
			fill_deps(m);
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   242
			if (are_there_fixed_boxes(m,
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   243
				new_movements, &num_new_movements))
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   244
			{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   245
				/* That means that the map is illegal */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   246
				/* Maybe we could update the percent here... */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   247
				return FALSE;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   248
			}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   249
			/* the assert should be IN the function before,
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   250
			   before OVERfilling new_movements */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   251
			assert(num_new_movements < MAX_MOVES);
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   252
		}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   253
		else
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   254
			num_new_movements = 0;
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   255
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   256
		if (num_new_movements == 0)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   257
		{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   258
			percent_to_show = total_percent + next_percent*i;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   259
			depth_to_show = depth;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   260
		}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   261
		else
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   262
		{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   263
			if (depth+1 < MAX_STEPS)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   264
			{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   265
				if(search_did_found(maps, depth+1, new_movements,
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   266
					num_new_movements, next_percent,
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   267
					total_percent + next_percent*i) == TRUE)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   268
				{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   269
					PrintMove(movements[i]);
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   270
					actual_map = &maps[depth+1];
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   271
					show_percent_and_map();
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   272
					return TRUE;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   273
				}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   274
			}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   275
		}
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   276
	}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   277
	return FALSE;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   278
}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   279
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   280
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   281
/* It only fills the m->man_moves structure */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   282
static void paint_mancanmove(struct Map *m)
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   283
{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   284
	struct Position pos[MAX_MOVES];
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   285
	struct Position new_pos[MAX_MOVES];
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   286
	int NumMoves, NewMoves;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   287
	int j, i;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   288
	struct Position next_pos;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   289
	char *next_cell;
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   290
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   291
	/* Let's  the map with only walls in man_moves - other, blanks */
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   292
	for (j = 0; j<m->SizeY; j++)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   293
		for (i=0; i<m->SizeX; i++)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   294
		{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   295
			if (m->Cells[j][i] == WALL)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   296
				m->man_moves[j][i] = WALL;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   297
			else
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   298
				m->man_moves[j][i] = BLANK;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   299
		}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   300
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   301
	/* Let's put the boxes */
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   302
	for (i = 0; i<m->NumBoxes; i++)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   303
	{
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   304
		m->man_moves[m->Box[i].y][m->Box[i].x] = WALL;
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   305
	}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   306
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   307
	NewMoves = 1;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   308
	new_pos[0].x = m->Man.x;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   309
	new_pos[0].y = m->Man.y;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   310
	m->man_moves[m->Man.y][m->Man.x] = MANCANMOVE;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   311
	while (NewMoves > 0)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   312
	{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   313
		/* The before named "New Moves" become the moves we have
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   314
		   to analyze */
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   315
		NumMoves = NewMoves;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   316
		for (i=0; i<NewMoves; i++)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   317
		{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   318
			pos[i] = new_pos[i];
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   319
		}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   320
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   321
		/* Search new positions for each position */
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   322
		NewMoves = 0;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   323
		for (i=0; i<NumMoves; i++)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   324
		{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   325
			/* For each direction */
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   326
			for (j=0; j<4; j++)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   327
			{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   328
				next_pos.x = pos[i].x + move_vectors[j].x;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   329
				next_pos.y = pos[i].y + move_vectors[j].y;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   330
				next_cell = &m->man_moves[next_pos.y][next_pos.x];
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   331
				if(*next_cell == BLANK)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   332
				{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   333
					new_pos[NewMoves] = next_pos;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   334
					*next_cell = MANCANMOVE;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   335
					NewMoves++;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   336
				}
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   337
			}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   338
		}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   339
	}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   340
}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   341
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   342
/* Returns TRUE if there are fixed boxes that should be moved, FALSE otherwise*/
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   343
/* For 'm' it modifies only m->boxes[][] */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   344
static Bool are_there_fixed_boxes(struct Map *m,
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   345
	struct BoxMove movements[MAX_MOVES], int *num_movements)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   346
{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   347
	int i,j;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   348
	enum e_direction d;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   349
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   350
	/* Box dependencies. The first depends on seconds. */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   351
	struct
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   352
	{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   353
		Bool box[MAX_BOXES][MAX_BOXES];
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   354
		int ndeps[MAX_BOXES];
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   355
	} dep;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   356
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   357
	/* Free boxes' arrays */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   358
	struct
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   359
	{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   360
		Bool box_is_free[MAX_BOXES]; /* This is only used in 'free' */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   361
		int box[MAX_BOXES];
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   362
		int n;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   363
	} free,			/* List of all boxes free-to-move */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   364
		new_free,	/* List of boxes for the next loop (code) */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   365
		tfree;	/* List of boxes of the loop (code) */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   366
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   367
	struct Position tpos;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   368
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   369
	Bool is_not_set_free;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   370
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   371
	/* Initialize to 0 the dependency structures */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   372
	memset((void *) &dep, 0, sizeof(dep));
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   373
	for(i=0; i<m->NumBoxes; i++)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   374
	{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   375
		new_free.box[i] = FALSE;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   376
	}
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   377
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   378
	/* Let's fill the structure */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   379
	for(i=0; i<m->NumBoxes; i++)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   380
	{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   381
		/* See if the box is blocked. That could be
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   382
		   done in fill_deps */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   383
		if (m->box_deps[i].dep_dir[0] == BLOCKED &&
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   384
			m->box_deps[i].dep_dir[1] == BLOCKED &&
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   385
			m->box_deps[i].dep_dir[2] == BLOCKED &&
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   386
			m->box_deps[i].dep_dir[3] == BLOCKED )
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   387
		{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   388
			if (m->Cells[m->Box[i].y][m->Box[i].x] !=
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   389
				PLATFORM)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   390
				return TRUE;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   391
			/* This is not used actually 
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   392
			else
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   393
				m->boxes[m->Box[i].y][m->Box[i].x] =
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   394
					WALL;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   395
			*/
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   396
		}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   397
		/* Let's take note of the dependencies */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   398
		is_not_set_free = TRUE;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   399
		for(d=0; d<4; d++)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   400
		{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   401
			if(m->box_deps[i].dep_dir[d] > 0)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   402
			{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   403
				dep.box[i][m->box_deps[i].dep_dir[d]] =
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   404
					TRUE;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   405
				dep.ndeps[i]++;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   406
			} else
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   407
			if(m->box_deps[i].dep_dir[d] == FREE)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   408
			{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   409
				if (is_not_set_free)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   410
				{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   411
					new_free.box[new_free.n++] = i;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   412
					is_not_set_free = FALSE;
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   413
				}
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   414
			}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   415
		}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   416
	}
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   417
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   418
	/* Let's analyze if the free boxes can really be moved, and
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   419
	   set them as dependant, if they're dependant.
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   420
	   If some can be really moved, we should know _where to_. */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   421
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   422
	/* Paint MANCANMOVE at each cell the man can reach */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   423
	paint_mancanmove(m);
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   424
	
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   425
	/* For each free movable box, let's see if the man can reach them
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   426
	   fine. If we can, add it to the possible movements. */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   427
	*num_movements=0;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   428
	for(i=0; i<new_free.n; i++)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   429
	{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   430
		/* For each direction, try if the box can be moved */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   431
		for(d=0; d<4; d++)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   432
		{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   433
			if(m->box_deps[i].dep_dir[d] == FREE)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   434
			{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   435
				add_position3(tpos, m->Box[i], move_vectors[OPPOSITE_DIR(d)])
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   436
				if (m->man_moves[tpos.y][tpos.x] == MANCANMOVE)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   437
				{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   438
					movements[*num_movements].box = i;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   439
					movements[*num_movements].dir =
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   440
						move_vectors[d];
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   441
					(*num_movements)++;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   442
				}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   443
				/* ELSE - we should catch those cases,
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   444
				where free boxes cannot be moved. Will they
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   445
				ever be movable? 
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   446
				THIS HAS TO BE IMPROVED */
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   447
			}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   448
		}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   449
	}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   450
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   451
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   452
	/* ---------------- The next possible movements have been calculated --
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   453
	We can still find that this map is illegal. We need to find
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   454
	further more if there are boxes unmovable (due to dependencies) */
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   455
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   456
	/* Let's take out all dependencies from 'free' boxes */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   457
	/* Prepare the 'free' list */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   458
	for(i=0; i<m->NumBoxes;i++)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   459
		free.box_is_free[i] = FALSE;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   460
	free.n = new_free.n;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   461
	for(i=0; i<new_free.n; i++)
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   462
	{
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   463
		free.box[i] = new_free.box[i];
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   464
		free.box_is_free[i] = TRUE;
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   465
	}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   466
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   467
	/* KACXO-loop  */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   468
	while (new_free.n > 0)
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   469
	{
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   470
		/* Copy new_free into free */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   471
		tfree.n = new_free.n;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   472
		for(i=0; i<new_free.n; i++)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   473
			tfree.box[i] = new_free.box[i];
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   474
		new_free.n = 0;
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   475
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   476
		for(i=0; i<tfree.n; i++)
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   477
		{
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   478
			for(j=0; j<m->NumBoxes; j++)
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   479
			{
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   480
				/* The box j no more depends on
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   481
				   the box tfree[i] */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   482
				if(dep.box[j][tfree.box[i]])
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   483
				{
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   484
					dep.box[j][tfree.box[i]] = FALSE;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   485
					dep.ndeps[j]--;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   486
					assert(dep.ndeps[j] >= 0);
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   487
					if (dep.ndeps[j] == 0 &&
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   488
						!free.box_is_free[i])
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   489
					{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   490
						new_free.box[new_free.n++]=j;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   491
						free.box[free.n++]=j;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   492
					}
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   493
				}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   494
			}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   495
		}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   496
	}
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   497
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   498
	/* Now are left only boxes which depend on others-non-free */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   499
	for(i=0; i<m->NumBoxes; i++)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   500
	{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   501
		for(j=0; j<m->NumBoxes; j++)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   502
		{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   503
			/* We only do one-level search !!! */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   504
			/* If the box only depends on another,
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   505
			   which also only depends on this... */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   506
			if (dep.box[i][j] && dep.ndeps[i] == 1
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   507
				&& dep.box[j][i] && dep.ndeps[j] == 1)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   508
			{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   509
				if (m->Cells[m->Box[i].y][m->Box[i].x]!=
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   510
					PLATFORM)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   511
				{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   512
					return TRUE;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   513
				}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   514
				/* We don't set any wall here, they're not
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   515
				   important */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   516
			}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   517
		}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   518
	}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   519
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   520
	return FALSE;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   521
}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   522
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   523
static void init_cell_boxes(struct Map *m)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   524
{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   525
	int i,j;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   526
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   527
	/* Let's  the map with only walls in man_moves - other, blanks */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   528
	for (j = 0; j<m->SizeY; j++)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   529
		for (i=0; i<m->SizeX; i++)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   530
			m->cells_boxes[j][i] = -1;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   531
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   532
	for(i=0; i<m->NumBoxes; i++)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   533
	{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   534
		m->cells_boxes[m->Box[i].y][m->Box[i].x] = i;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   535
	}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   536
}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   537
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   538
/* It fills only m->box_deps */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   539
static void fill_deps(struct Map *m)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   540
{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   541
	int i;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   542
	enum e_direction dir;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   543
	struct Position n; /* Position next to the box */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   544
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   545
	/* TO BE OPTIMIZED - get into force_move_box */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   546
	init_cell_boxes(m);
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   547
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   548
	for(i=0; i<m->NumBoxes; i++)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   549
	{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   550
		/* Initialize the move freedom */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   551
		for(dir=0;dir<4; dir++)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   552
		{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   553
			m->box_deps[i].dep_dir[dir] = FREE;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   554
		}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   555
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   556
		/* Limit the freedom */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   557
		for(dir=0;dir<4; dir++)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   558
		{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   559
			add_position3(n,m->Box[i],move_vectors[dir]);
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   560
			if(m->Cells[n.y][n.x] == WALL)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   561
			{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   562
				/* IMPROVEMENT: The fixed-box detection
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   563
				could be done here */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   564
				m->box_deps[i].dep_dir[dir] = BLOCKED;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   565
				m->box_deps[i].dep_dir[OPPOSITE_DIR(dir)] =
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   566
					BLOCKED;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   567
				continue;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   568
			}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   569
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   570
			/* if there is Wall limit, we don't want to set box
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   571
			   dependency */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   572
			if(m->box_deps[i].dep_dir[dir] == WALL)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   573
				continue;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   574
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   575
			/* Put the box dependency in the list */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   576
			if(m->cells_boxes[n.y][n.x] >= 0)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   577
			{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   578
				m->box_deps[i].dep_dir[dir] =
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   579
					m->cells_boxes[n.y][n.x];
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   580
				/* If the other site is not limited by another
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   581
				   box, we set it up too. */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   582
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   583
				/* *** MAYBE IT'S BETTER NOT TO SET IT */
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   584
				if(m->box_deps[i].dep_dir[dir] == FREE)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   585
				{
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   586
				m->box_deps[i].dep_dir[OPPOSITE_DIR(dir)] =
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   587
					m->cells_boxes[n.y][n.x];
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   588
				}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   589
				continue;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   590
			}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   591
			if(m->Cells[n.y][n.x] == CORNER)
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   592
				m->box_deps[i].dep_dir[dir] = BLOCKED;
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   593
				
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   594
		}
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   595
	}
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   596
}
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   597
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   598
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   599
int solve_map(const struct Map origin)
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   600
{
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   601
	struct Map maps[MAX_STEPS+1];
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   602
	struct BoxMove new_movements[MAX_MOVES];
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   603
	int num_new_movements;
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   604
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   605
	CopyMap(&maps[0], &origin);
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   606
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   607
	fill_deps(&maps[0]);
13
c2b272938a1f actual_map is initialized at first, so almost never a SIGSEGV can happen.
viric@llimona
parents: 12
diff changeset
   608
	/* Initializing actual_map here AT LEAST gives protection against
c2b272938a1f actual_map is initialized at first, so almost never a SIGSEGV can happen.
viric@llimona
parents: 12
diff changeset
   609
	SIGSEGV */
c2b272938a1f actual_map is initialized at first, so almost never a SIGSEGV can happen.
viric@llimona
parents: 12
diff changeset
   610
	actual_map = &maps[0];
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   611
	assert(!are_there_fixed_boxes(&maps[0], new_movements,
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   612
		&num_new_movements));
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   613
	search_did_found(maps, 0, new_movements, num_new_movements, 100, 0);
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   614
12
a2c334a71a6b A minimum working (hope so!) version of the algorithm is working, although
viric@llimona
parents: 10
diff changeset
   615
	return 0;
6
bfbca2c0fc70 More file separation.
viric@llimona
parents:
diff changeset
   616
}