summaryrefslogtreecommitdiff
path: root/digital/io-hub/src/robospierre/element.h
blob: 41985e951f55772312566e4a95ec6537ccfb0046 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#ifndef element_h
#define element_h
/* element.h */
/* robospierre - Eurobot 2011 AI. {{{
 *
 * Copyright (C) 2011 Nicolas Schodet, Jérôme Jutteau
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "defs.h"

/** None. */
#define ELEMENT_NONE 1
/** A pawn, not a head. */
#define ELEMENT_PAWN 2
/** Queen pawn, used for statistic update of table data. */
#define ELEMENT_QUEEN 4
/** King pawn, used for statistic update of table data. */
#define ELEMENT_KING 8
/** Any head, queen or king. */
#define ELEMENT_HEAD (ELEMENT_QUEEN |ELEMENT_KING)
/** Any element, pawn, queen, or king. */
#define ELEMENT_ANY (ELEMENT_HEAD |ELEMENT_PAWN)
/** Tower types. */
#define ELEMENT_TOWER_1_QUEEN 16
#define ELEMENT_TOWER_2_QUEEN 32
#define ELEMENT_TOWER_1_KING 64
#define ELEMENT_TOWER_2_KING 128

#define ELEMENT_TOWER (ELEMENT_TOWER_1_QUEEN | \
		       ELEMENT_TOWER_2_QUEEN | \
		       ELEMENT_TOWER_1_KING | \
		       ELEMENT_TOWER_2_KING)

/** Return non zero if element is a head, not a pawn. */
#define ELEMENT_IS_HEAD(e) ((e) && !((e) & ELEMENT_PAWN))

/** Return non zero if element may be a head. */
#define ELEMENT_CAN_BE_HEAD(e) ((e) & ELEMENT_HEAD)

#define ELEMENT_PAWN_SCORE 10
#define ELEMENT_ANY_SCORE 15
#define ELEMENT_QUEEN_SCORE 20 * 30
#define ELEMENT_KING_SCORE 30 * 30

/** Emplacement attributes bits. */
#define ELEMENT_BONUS 1
/**
 * Two meanings:
 * - When it is on an intersection or in the green zone, this mean that it is positioned on the
 *   left size of the table.
 * - When it is located on an color, this corresponds to the red color.
 */
#define ELEMENT_LEFT 2
/**
 * Two meanings:
 * - When it is on an intersection or in the green zone, this mean that it is positioned on the
 *   left right of the table.
 * - When it is located on an color, this corresponds to the blue color.
 */
#define ELEMENT_RIGHT 4
#define ELEMENT_SAFE 8
#define ELEMENT_GREEN 16
#define ELEMENT_INTERSEC 64
/** Center of a square or central element. */
#define ELEMENT_CENTER 128

struct element_t
{
    /** May have several types if unknown (pawn, king, queen). */
    uint8_t type;
    /** Element position. */
    vect_t pos;
    /** Emplacement attributes. */
    uint8_t attr;
    /** Bonus coefficient (or Mallus if negative) for load scores. */
    int8_t bonus_load;
    /** Bonus coefficient (or Mallus if negative) for unload scores. */
    int8_t bonus_unload;
    /** Failure expiration date, can not take this element until chrono is
     * lower than this date in seconds. */
    int8_t failure_until_s;

};
typedef struct element_t element_t;

/* Elements range, see element table content. */
#define ELEMENT_INTERSEC_START 0
#define ELEMENT_INTERSEC_END 19
#define ELEMENT_CENTRAL_PAWN 20
#define ELEMENT_GREEN_START 21
#define ELEMENT_GREEN_END 30
#define ELEMENT_UNLOAD_START 31
#define ELEMENT_UNLOAD_END 66

#define ELEMENT_BONUS_COEFF 100

/** Elements on table. */
extern struct element_t element_table[];

/** Initialize elements. */
void
element_init (void);

/** Gives the score of an element considering it as an unload zone. */
int32_t
element_unload_score (position_t robot_pos, uint8_t element_id);

/** Gives best unload. */
uint8_t
element_unload_best (position_t robot_pos);

/** Gives score of an element. */
int32_t
element_score (position_t robot_pos, uint8_t element_id);

/** Return a probability of an element to be here.
  * This depends of the remaining time.
  * Returns a probability from 0 (element may really not be here)
  * to maximal value (element may be here).
  */
uint32_t
element_proba (uint8_t element_id);

/** Return the best element to pick. */
uint8_t
element_best (position_t robot_pos);

/** Function to call when we see an element is not here. */
void
element_not_here (uint8_t element_id);

/** Call this function when an element is taken. */
void
element_taken (uint8_t element_id, uint8_t element_type);

/** Call this function when the robot put down an element. */
void
element_down (uint8_t element_id, uint8_t element_type);

/** Call this function when taking an element failled. */
void
element_failure (uint8_t element_id);

/** Gives the nearest element from a position. */
uint8_t
element_give_position (position_t pos);

/** Give the opposed element. */
uint8_t
element_opposed (uint8_t element_id);

/** Return 1 if the element is in our side, 0 otherwise. */
int
element_our_side (uint8_t element_id);

inline void
element_intersec_symetric (uint8_t element_id, uint8_t element_type);

uint8_t
element_nearest_element_id (position_t robot_pos);

/**
   Give the position where the robot need to be placed to get an element.
 */
vect_t
element_get_pos (uint8_t element_id);

extern inline element_t
element_get (uint8_t element_id)
{
    return element_table[element_id];
}

/** Return whether an element is blocking robot. */
uint8_t
element_blocking (uint8_t element_id, uint8_t blocking);

/** Return whether an element is blocking a line segment.
 * - a: line segment first point.
 * - b: line segment second point.
 * - ab: line segment length.
 * - escape: trying to escape, be a little bit more permissive.
 * - returns: 1 if the path should not be used. */
uint8_t
element_blocking_path (vect_t a, vect_t b, int16_t ab, uint8_t escape);

/** Ask to adjust score for the opposed green zone. */
void
element_i_like_green ();

/** Do not go to our green zone any more. */
void
element_no_more_green (void);

#endif /* element_h */