-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.rs
475 lines (421 loc) · 11.8 KB
/
main.rs
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#![deny(overflowing_literals, unsafe_code)]
#![feature(conservative_impl_trait)]
#[macro_use]
extern crate bitflags;
extern crate rand;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
#[cfg(feature = "cli")]
extern crate clap;
#[cfg(feature = "replay")]
extern crate chrono;
#[macro_use]
#[cfg(feature = "opengl")]
extern crate glium;
#[cfg(feature = "piston")]
extern crate piston_window;
#[cfg(any(feature = "piston", feature = "opengl"))]
extern crate image;
#[cfg(feature = "libtcod")]
pub extern crate tcod;
#[cfg(feature = "terminal")]
extern crate rustbox;
#[cfg(feature = "remote")]
extern crate zmq;
use state::State;
use std::path::Path;
mod ai;
mod animation;
mod blocker;
mod color;
mod engine;
mod formula;
mod game;
mod generators;
mod graphics;
mod item;
mod keys;
mod level;
mod monster;
mod pathfinding;
mod player;
mod point;
mod ranged_int;
mod rect;
mod render;
mod state;
mod stats;
mod timer;
mod util;
mod world;
#[cfg(feature = "libtcod")]
fn run_libtcod(
display_size: point::Point,
default_background: color::Color,
window_title: &str,
font_path: &Path,
state: State,
) {
println!("Using the libtcod backend.");
let mut engine =
engine::tcod::Engine::new(display_size, default_background, window_title, &font_path);
engine.main_loop(state, update);
}
#[cfg(not(feature = "libtcod"))]
fn run_libtcod(
_display_size: point::Point,
_default_background: color::Color,
_window_title: &str,
_font_path: &Path,
_state: State,
) {
println!("The \"libtcod\" feature was not compiled in.");
}
#[cfg(feature = "piston")]
fn run_piston(
display_size: point::Point,
default_background: color::Color,
window_title: &str,
font_path: &Path,
state: State,
update: engine::UpdateFn<State>,
) {
println!("Using the piston backend.");
engine::piston::main_loop(
display_size,
default_background,
window_title,
&font_path,
state,
update,
);
}
#[cfg(not(feature = "piston"))]
fn run_piston(
_display_size: point::Point,
_default_background: color::Color,
_window_title: &str,
_font_path: &Path,
_state: State,
_update: engine::UpdateFn,
) {
println!("The \"piston\" feature was not compiled in.");
}
#[cfg(feature = "terminal")]
fn run_terminal() {
println!("Using the rustbox backend.\n "
"TODO: this is not implemented yet.");
}
#[cfg(not(feature = "terminal"))]
fn run_terminal() {
println!("The \"terminal\" feature was not compiled in.");
}
#[cfg(feature = "opengl")]
fn run_opengl(
display_size: point::Point,
default_background: color::Color,
window_title: &str,
state: State,
update: engine::UpdateFn,
) {
println!("Using the default backend: opengl");
engine::glium::main_loop(
display_size,
default_background,
window_title,
state,
update,
);
}
#[cfg(not(feature = "opengl"))]
fn run_opengl(
_display_size: point::Point,
_default_background: color::Color,
_window_title: &str,
_state: State,
_update: engine::UpdateFn,
) {
println!("The \"opengl\" feature was not compiled in.");
}
#[cfg(feature = "remote")]
fn run_remote(
display_size: point::Point,
default_background: color::Color,
window_title: &str,
state: State,
update: engine::UpdateFn<State>,
) {
engine::remote::main_loop(
display_size,
default_background,
window_title,
state,
update,
);
}
#[cfg(not(feature = "remote"))]
fn run_remote(
_display_size: point::Point,
_default_background: color::Color,
_window_title: &str,
_state: State,
_update: engine::UpdateFn,
) {
println!("The \"remote\" feature was not compiled in.");
}
#[cfg(feature = "cli")]
fn process_cli_and_run_game(
display_size: point::Point,
world_size: point::Point,
map_size: i32,
panel_width: i32,
default_background: color::Color,
title: &str,
update: engine::UpdateFn,
) {
use clap::{App, Arg, ArgGroup};
let matches = App::new(title)
.author("Tomas Sedovic <tomas@sedovic.cz>")
.about("Roguelike game about addiction")
.arg(
Arg::with_name("replay")
.value_name("FILE")
.help(
"Replay this file instead of starting and playing a new \
game",
)
.takes_value(true),
)
.arg(
Arg::with_name("replay-full-speed")
.help(
"Don't slow the replay down (useful for getting accurate \
measurements)",
)
.long("replay-full-speed"),
)
.arg(
Arg::with_name("replay-file")
.help("Path where to store the replay log.")
.long("replay-file")
.value_name("FILE")
.takes_value(true),
)
.arg(
Arg::with_name("exit-after")
.help("Exit after the game or replay has finished")
.long("exit-after"),
)
.arg(
Arg::with_name("invincible")
.help("Makes the player character invincible. They do not die.")
.long("invincible"),
)
.arg(Arg::with_name("libtcod").long("libtcod").help(
"Use the libtcod rendering backend",
))
.arg(Arg::with_name("piston").long("piston").help(
"Use the Piston rendering backend",
))
.arg(Arg::with_name("opengl").long("opengl").help(
"Use the Glium (OpenGL) rendering backend",
))
.arg(Arg::with_name("terminal").long("terminal").help(
"Use the Rustbox (terminal-only) rendering backend",
))
.arg(Arg::with_name("remote").long("remote").help(
"Don't create a game window. The input and output is \
controled via ZeroMQ.",
))
.group(ArgGroup::with_name("graphics").args(
&[
"libtcod",
"piston",
"opengl",
"terminal",
"remote",
],
))
.get_matches();
let state = if let Some(replay) = matches.value_of("replay") {
if matches.is_present("replay-file") {
panic!(
"The `replay-file` option can only be used during regular \
game, not replay."
);
}
let replay_path = Path::new(replay);
State::replay_game(
world_size,
map_size,
panel_width,
display_size,
&replay_path,
matches.is_present("invincible"),
matches.is_present("replay-full-speed"),
matches.is_present("exit-after"),
)
} else {
if matches.is_present("replay-full-speed") {
panic!(
"The `full-replay-speed` option can only be used if the \
replay log is passed."
);
}
let replay_file = match matches.value_of("replay-file") {
Some(file) => Some(file.into()),
None => state::generate_replay_path(),
};
State::new_game(
world_size,
map_size,
panel_width,
display_size,
matches.is_present("exit-after"),
replay_file,
matches.is_present("invincible"),
)
};
if matches.is_present("libtcod") {
run_libtcod(
display_size,
default_background,
title,
&Path::new(""),
state,
);
} else if matches.is_present("piston") {
run_piston(
display_size,
default_background,
title,
&Path::new(""),
state,
update,
);
} else if matches.is_present("terminal") {
run_terminal();
} else if matches.is_present("remote") {
run_remote(display_size, default_background, title, state, update);
} else {
run_opengl(display_size, default_background, title, state, update);
}
}
#[cfg(not(feature = "cli"))]
fn process_cli_and_run_game(
_display_size: point::Point,
_world_size: point::Point,
_map_size: i32,
_panel_width: i32,
_default_background: color::Color,
_title: &str,
_update: engine::UpdateFn,
) {
// TODO: run the game here
}
#[no_mangle]
pub fn initialise() -> *mut State {
let mut state = {
// NOTE: at our current font, the height of 43 is the maximum
// value for 1336x768 monitors.
let map_size = 43;
let panel_width = 20;
let display_size: point::Point = (map_size + panel_width, map_size).into();
// NOTE: 2 ^ 30
let world_size: point::Point = (1_073_741_824, 1_073_741_824).into();
let _title = "Dose Response";
Box::new(State::new_game(
world_size,
map_size,
panel_width,
display_size,
false, // exit-after
None, // replay file
false, // invincible
))
};
Box::into_raw(state)
}
extern {
fn draw(nums: *const u8, len: usize);
}
#[no_mangle]
pub fn update(state_ptr: *mut State) {
#[allow(unsafe_code)]
let mut state: Box<State> = unsafe { Box::from_raw(state_ptr) };
let dt = std::time::Duration::new(0, 0);
let display_size = point::Point::new(0, 0);
let fps = 60;
let keys: Vec<keys::Key> = vec![];
let mouse: engine::Mouse = Default::default();
let mut settings = engine::Settings{ fullscreen: false };
let mut drawcalls: Vec<engine::Draw> = vec![];
let result = game::update(
&mut state,
dt,
display_size,
fps,
&keys,
mouse,
&mut settings,
&mut drawcalls,
);
// Each "drawcall" will be 6 u8 values: x, y, char, r, g, b
let mut js_drawcalls = Vec::with_capacity(drawcalls.len() * 6);
for dc in &drawcalls {
match dc {
&engine::Draw::Char(point, glyph, color) => {
assert!(point.x >= 0 && point.x < 255);
assert!(point.y >= 0 && point.y < 255);
assert!(glyph.is_ascii());
js_drawcalls.push(point.x as u8);
js_drawcalls.push(point.y as u8);
js_drawcalls.push(glyph as u8);
js_drawcalls.push(color.r);
js_drawcalls.push(color.g);
js_drawcalls.push(color.b);
}
_ => {} // TODO
}
}
#[allow(unsafe_code)]
unsafe {
draw(js_drawcalls.as_ptr(), js_drawcalls.len());
}
std::mem::forget(state);
}
#[no_mangle]
pub extern "C" fn key_pressed(
state_ptr: *mut State,
external_code: i32,
ctrl: bool, alt: bool, shift: bool, location: i32
)
{
use std::ffi::CStr;
#[allow(unsafe_code)]
let mut state: Box<State> = unsafe { Box::from_raw(state_ptr) };
let code = match external_code {
0 => Some(keys::KeyCode::Up),
1 => Some(keys::KeyCode::Down),
2 => Some(keys::KeyCode::Left),
3 => Some(keys::KeyCode::Right),
_ => None,
};
if let Some(code) = code {
state.keys.push(keys::Key { code, alt, ctrl, shift});
}
std::mem::forget(state);
}
fn main() {
// NOTE: at our current font, the height of 43 is the maximum
// value for 1336x768 monitors.
let map_size = 43;
let panel_width = 20;
let display_size = (map_size + panel_width, map_size).into();
// NOTE: 2 ^ 30
let world_size = (1_073_741_824, 1_073_741_824).into();
let title = "Dose Response";
process_cli_and_run_game(display_size, world_size, map_size, panel_width,
color::background, title, game::update);
}