1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#![allow(unused)]
use ffi::{self, ToAPI, ToFFI};
use std::os::raw::{c_float, c_double, c_int, c_void};
use state::*;
pub const TAILLE_BANQUISE: c_int = 25;
pub const NB_CASES: c_int = TAILLE_BANQUISE * TAILLE_BANQUISE;
pub const NB_TOURS: c_int = 100;
pub const NB_POINTS_ACTION: c_int = 8;
pub const COUT_DEPLACEMENT: c_int = 1;
pub const COUT_GLISSADE: c_int = 3;
pub const COUT_POUSSER: c_int = 5;
pub const NB_AGENTS: usize = 4;
pub const NB_TOURS_CAPTURE: c_int = 3;
pub const PLAYERS: &[c_int] = &[1, 2];
pub const AGENTS: &[c_int] = &[0, 1, 2, 3];
#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub enum CaseType {
Libre,
Mur,
Erreur,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub enum Direction {
Nord,
Est,
Sud,
Ouest,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub enum Erreur {
Ok,
PaInsuffisants,
PositionInvalide,
ObstacleMur,
ObstacleAgent,
DeplacementHorsLimites,
DirectionInvalide,
IdAgentInvalide,
RienAPousser,
DrapeauInvalide,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub enum ActionType {
ActionDeplacer,
ActionGlisser,
ActionPousser,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub enum DebugDrapeau {
AucunDrapeau,
DrapeauBleu,
DrapeauVert,
DrapeauRouge,
}
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub struct Position(pub c_int, pub c_int);
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub struct AlienInfo {
pub pos: Position,
pub points_capture: c_int,
pub tour_invasion: c_int,
pub duree_invasion: c_int,
pub capture_en_cours: c_int,
}
#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub struct ActionHist {
pub atype: ActionType,
pub id_agent: c_int,
pub dir: Direction,
}
pub fn deplacer(id_agent: c_int, dir: Direction) -> Erreur
{
unsafe { ffi::deplacer(id_agent.to_ffi(), dir.to_ffi()).to_api() }
}
pub fn glisser(id_agent: c_int, dir: Direction) -> Erreur
{
unsafe { ffi::glisser(id_agent.to_ffi(), dir.to_ffi()).to_api() }
}
pub fn pousser(id_agent: c_int, dir: Direction) -> Erreur
{
unsafe { ffi::pousser(id_agent.to_ffi(), dir.to_ffi()).to_api() }
}
pub fn debug_afficher_drapeau(pos: Position, drapeau: DebugDrapeau) -> Erreur
{
unsafe { ffi::debug_afficher_drapeau(pos.to_ffi(), drapeau.to_ffi()).to_api() }
}
pub fn points_action_agent(id_agent: c_int) -> c_int
{
unsafe { ffi::points_action_agent(id_agent.to_ffi()).to_api() }
}
pub fn chemin(pos1: Position, pos2: Position) -> Vec<Direction>
{
unsafe { ffi::chemin(pos1.to_ffi(), pos2.to_ffi()).to_api() }
}
pub fn type_case(pos: Position) -> CaseType
{
unsafe { ffi::type_case(pos.to_ffi()).to_api() }
}
pub fn agent_sur_case(pos: Position) -> c_int
{
unsafe { ffi::agent_sur_case(pos.to_ffi()).to_api() }
}
pub fn alien_sur_case(pos: Position) -> bool
{
unsafe { ffi::alien_sur_case(pos.to_ffi()).to_api() }
}
pub fn position_agent(id_joueur: c_int, id_agent: c_int) -> Position
{
unsafe { ffi::position_agent(id_joueur.to_ffi(), id_agent.to_ffi()).to_api() }
}
pub fn info_alien(pos: Position) -> Option<AlienInfo>
{
let ret = unsafe { ffi::info_alien(pos.to_ffi()).to_api() };
if ret.points_capture == -1 { None } else { Some(ret) }
}
pub fn liste_aliens() -> Vec<AlienInfo>
{
unsafe { ffi::liste_aliens().to_api() }
}
pub fn historique() -> Vec<ActionHist>
{
unsafe { ffi::historique().to_api() }
}
pub fn score(id_joueur: c_int) -> c_int
{
unsafe { ffi::score(id_joueur.to_ffi()).to_api() }
}
static mut MOI: i32 = 0;
pub fn moi() -> c_int
{
unsafe {
if MOI == 0 {
MOI = ffi::moi()
}
MOI
}
}
static mut TOI: i32 = 0;
pub fn toi() -> c_int
{
unsafe {
if TOI == 0 {
TOI = ffi::adversaire()
}
TOI
}
}
pub fn annuler() -> bool
{
unsafe { ffi::annuler().to_api() }
}
pub fn tour_actuel() -> c_int
{
unsafe { ffi::tour_actuel().to_api() }
}
pub fn afficher_case_type(v: CaseType)
{
unsafe { ffi::afficher_case_type(v.to_ffi()).to_api() }
}
pub fn afficher_direction(v: Direction)
{
unsafe { ffi::afficher_direction(v.to_ffi()).to_api() }
}
pub fn afficher_erreur(v: Erreur)
{
unsafe { ffi::afficher_erreur(v.to_ffi()).to_api() }
}
pub fn afficher_action_type(v: ActionType)
{
unsafe { ffi::afficher_action_type(v.to_ffi()).to_api() }
}
pub fn afficher_debug_drapeau(v: DebugDrapeau)
{
unsafe { ffi::afficher_debug_drapeau(v.to_ffi()).to_api() }
}
pub fn afficher_position(v: Position)
{
unsafe { ffi::afficher_position(v.to_ffi()).to_api() }
}
pub fn afficher_alien_info(v: AlienInfo)
{
unsafe { ffi::afficher_alien_info(v.to_ffi()).to_api() }
}
pub fn afficher_action_hist(v: ActionHist)
{
unsafe { ffi::afficher_action_hist(v.to_ffi()).to_api() }
}
#[no_mangle]
pub unsafe extern "C" fn partie_init()
{
STATE = Some(super::partie_init())
}
#[no_mangle]
pub unsafe extern "C" fn jouer_tour()
{
super::jouer_tour(&mut STATE.as_mut().unwrap()).to_ffi()
}
#[no_mangle]
pub unsafe extern "C" fn partie_fin()
{
super::partie_fin(&mut STATE.as_mut().unwrap()).to_ffi()
}