-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
151 lines (117 loc) · 3.7 KB
/
log.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "environment.h"
#include "common.h"
#include "log.h"
#include "vegetation.h"
FILE *current_log_file;
#define LOG_FILE_SUFFIX ".log"
/* Create a new log file for the current environment. */
int
open_log_file (const char *env_filename)
{
size_t log_filename_len = strlen (RESULT_DIR_NAME)
+ 1
+ strlen (env_filename)
+ strlen (LOG_FILE_SUFFIX)
+ 1;
char *log_filename = malloc (log_filename_len);
sprintf (log_filename, RESULT_DIR_NAME "/%s" LOG_FILE_SUFFIX, env_filename);
current_log_file = fopen (log_filename, "w");
if (current_log_file == NULL)
{
fprintf (stderr, "Unable to create log file %s\n", log_filename);
free (log_filename);
return 0;
}
free (log_filename);
return 1;
}
/* Print an ASCII representation of the current environment map, with the following
representations:
Agent: A## (## is agent's ID)
D (D is agent's direction)
Predator: P## (## is predator species ID)
Vegetation: V## (## is vegetation species ID)
$$$ ($$$ is current bloom value, or 0 if not blooming)
*/
void
log_map (void)
{
unsigned int row, col;
/* Print top border. */
fputs (" ", current_log_file);
for (col = 0; col < env.size_x; col++)
fprintf (current_log_file, " %03d", col);
fputc ('\n', current_log_file);
for (row = 0; row < env.size_y; row++)
{
fprintf (current_log_file, "%03d", row);
for (col = 0; col < env.size_x; col++)
{
location_t *loc = &env.map[row][col];
fputc (' ', current_log_file);
switch (loc->occ_type)
{
case OCC_EMPTY:
fputs (" ", current_log_file);
break;
case OCC_AGENT:
fprintf (current_log_file, "A%02d", loc->agent->id);
break;
case OCC_PRED:
fprintf (current_log_file, "P%02d", loc->pred->pred_info->id);
break;
case OCC_VEG:
fprintf (current_log_file, "V%02d", loc->veg.veg_info->id);
break;
}
}
fprintf (current_log_file, " %03d\n", row);
fputs (" ", current_log_file);
for (col = 0; col < env.size_x; col++)
{
location_t *loc = &env.map[row][col];
fputc (' ', current_log_file);
switch (loc->occ_type)
{
case OCC_EMPTY:
case OCC_PRED:
fputs (" ", current_log_file);
break;
case OCC_VEG:
fprintf (current_log_file, "%3d", veg_bloom_value (&loc->veg));
break;
case OCC_AGENT:
switch (loc->agent->dir)
{
case DIR_UP:
fputs (" ^ ", current_log_file);
break;
case DIR_DOWN:
fputs (" v ", current_log_file);
break;
case DIR_LEFT:
fputs (" < ", current_log_file);
break;
case DIR_RIGHT:
fputs (" > ", current_log_file);
break;
}
}
}
fputc ('\n', current_log_file);
}
/* Print bottom border. */
fputs (" ", current_log_file);
for (col = 0; col < env.size_x; col++)
fprintf (current_log_file, " %03d", col);
fputc ('\n', current_log_file);
fflush (current_log_file);
}
void
close_log_file (void)
{
fclose (current_log_file);
}