forked from SpaceManiac/SpacemanDMM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimap.rs
More file actions
623 lines (548 loc) · 18.7 KB
/
minimap.rs
File metadata and controls
623 lines (548 loc) · 18.7 KB
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
use std::sync::RwLock;
use std::collections::{HashSet, BTreeMap};
use ndarray::Axis;
use dm::objtree::*;
use dm::constants::Constant;
use crate::dmm::{Map, ZLevel, Prefab};
use crate::dmi::{Dir, Image};
use crate::render_passes::RenderPass;
use crate::icon_cache::IconCache;
const TILE_SIZE: u32 = 32;
// ----------------------------------------------------------------------------
// Main minimap code
#[derive(Clone, Copy)]
pub struct Context<'a> {
pub objtree: &'a ObjectTree,
pub map: &'a Map,
pub level: ZLevel<'a>,
pub min: (usize, usize),
pub max: (usize, usize),
pub render_passes: &'a [Box<dyn RenderPass>],
pub errors: &'a RwLock<HashSet<String>>,
pub bump: &'a bumpalo::Bump,
}
pub fn generate(ctx: Context, icon_cache: &IconCache) -> Result<Image, ()> {
let Context {
objtree,
map,
level,
render_passes,
bump,
..
} = ctx;
// transform min/max from bottom-left-based to top-left-based
// probably doesn't belong here
let (len_y, _) = level.grid.dim();
let (min_y, max_y) = (len_y - ctx.max.1 - 1, len_y - ctx.min.1 - 1);
let (len_x, len_y) = (ctx.max.0 - ctx.min.0 + 1, ctx.max.1 - ctx.min.1 + 1);
// create atom arrays from the map dictionary
let mut atoms = BTreeMap::new();
for (key, prefabs) in map.dictionary.iter() {
atoms.insert(key, get_atom_list(objtree, prefabs, render_passes, ctx.errors));
}
// loads atoms from the prefabs on the map and adds overlays and smoothing
let mut sprites = Vec::new();
let mut underlays = Vec::new();
let mut overlays = Vec::new();
for (y, row) in level.grid.axis_iter(Axis(0)).enumerate() {
if y < min_y || y > max_y {
continue;
}
for (x, e) in row.iter().enumerate() {
if x < ctx.min.0 || x > ctx.max.0 {
continue;
}
let loc = (x as u32, y as u32);
'atom: for atom in atoms.get(e).expect("bad key").iter() {
for pass in render_passes.iter() {
// Note that late_filter is NOT called during smoothing lookups.
if !pass.late_filter(&atom, objtree) {
continue 'atom;
}
}
let mut sprite = Sprite::from_vars(objtree, atom);
for pass in render_passes {
pass.adjust_sprite(&atom, &mut sprite, objtree, bump);
}
if sprite.icon.is_empty() {
println!("no icon: {}", atom.type_.path);
continue;
}
let atom = Atom { sprite, .. *atom };
for pass in render_passes {
pass.overlays(&atom, objtree, &mut underlays, &mut overlays, bump);
}
// smoothing time
let mut neighborhood = [&[][..]; 9];
for (i, (dx, dy)) in [
(-1, 1), (0, 1), (1, 1),
(-1, 0), (0, 0), (1, 0),
(-1, -1), (0, -1), (1, -1),
].iter().enumerate() {
let new_x = x as i32 + dx;
let new_y = y as i32 - dy;
let (dim_y, dim_x) = ctx.level.grid.dim();
if new_x < 0 || new_y < 0 || new_x >= dim_x as i32 || new_y >= dim_y as i32 {
continue;
}
neighborhood[i] = &atoms[&ctx.level.grid[(new_y as usize, new_x as usize)]][..];
}
let neighborhood = Neighborhood::new(neighborhood);
let mut normal_appearance = true;
for pass in render_passes {
if !pass.neighborhood_appearance(&atom, objtree, &neighborhood, &mut underlays, bump) {
normal_appearance = false;
}
}
if normal_appearance {
underlays.push(atom.sprite.clone());
}
sprites.extend(underlays.drain(..).map(|o| (loc, o)));
sprites.extend(overlays.drain(..).map(|o| (loc, o)));
}
}
}
drop(underlays);
drop(overlays);
// sorts the atom list and renders them onto the output image
sprites.sort_by_key(|(_, s)| (s.plane, s.layer));
let mut map_image = Image::new_rgba(len_x as u32 * TILE_SIZE, len_y as u32 * TILE_SIZE);
'sprite: for (loc, sprite) in sprites {
for pass in render_passes.iter() {
if !pass.sprite_filter(&sprite) {
continue 'sprite;
}
}
let icon_file = match icon_cache.retrieve_shared(sprite.icon.as_ref()) {
Some(icon_file) => icon_file,
None => continue,
};
if let Some(rect) = icon_file.rect_of(sprite.icon_state, sprite.dir) {
let pixel_x = sprite.ofs_x;
let pixel_y = sprite.ofs_y + icon_file.metadata.height as i32;
let loc = (
((loc.0 - ctx.min.0 as u32) * TILE_SIZE) as i32 + pixel_x,
((loc.1 + 1 - min_y as u32) * TILE_SIZE) as i32 - pixel_y,
);
if let Some((loc, rect)) = clip((map_image.width, map_image.height), loc, rect) {
map_image.composite(&icon_file.image, loc, rect, sprite.color);
}
} else {
let key = format!("bad icon: {:?}, state: {:?}", sprite.icon, sprite.icon_state);
if !ctx.errors.read().unwrap().contains(&key) {
eprintln!("{}", key);
ctx.errors.write().unwrap().insert(key);
}
}
}
Ok(map_image)
}
// OOB handling
fn clip(bounds: (u32, u32), mut loc: (i32, i32), mut rect: (u32, u32, u32, u32)) -> Option<((u32, u32), (u32, u32, u32, u32))> {
if loc.0 < 0 {
rect.0 += (-loc.0) as u32;
match rect.2.checked_sub((-loc.0) as u32) {
Some(s) => rect.2 = s,
None => return None, // out of the viewport
}
loc.0 = 0;
}
while loc.0 + rect.2 as i32 > bounds.0 as i32 {
rect.2 -= 1;
if rect.2 == 0 {
return None;
}
}
if loc.1 < 0 {
rect.1 += (-loc.1) as u32;
match rect.3.checked_sub((-loc.1) as u32) {
Some(s) => rect.3 = s,
None => return None, // out of the viewport
}
loc.1 = 0;
}
while loc.1 + rect.3 as i32 > bounds.1 as i32 {
rect.3 -= 1;
if rect.3 == 0 {
return None;
}
}
Some(((loc.0 as u32, loc.1 as u32), rect))
}
fn get_atom_list<'a>(
objtree: &'a ObjectTree,
prefabs: &'a [Prefab],
render_passes: &[Box<dyn RenderPass>],
errors: &RwLock<HashSet<String>>,
) -> Vec<Atom<'a>> {
let mut result = Vec::new();
'fab: for fab in prefabs {
for pass in render_passes {
if !pass.path_filter(&fab.path) {
continue 'fab;
}
}
// look up the type
let atom = match Atom::from_prefab(objtree, fab) {
Some(x) => x,
None => {
let key = format!("bad path: {}", fab.path);
if !errors.read().unwrap().contains(&key) {
println!("{}", key);
errors.write().unwrap().insert(key);
}
continue;
}
};
for pass in render_passes {
if !pass.early_filter(&atom, objtree) {
continue 'fab;
}
}
// convert structure spanwers to their structures
for pass in render_passes {
if !pass.expand(&atom, objtree, &mut result) {
continue 'fab;
}
}
result.push(atom);
}
result
}
// ----------------------------------------------------------------------------
// Atoms
#[derive(Debug, Clone)]
pub struct Atom<'a> {
type_: &'a Type,
prefab: Option<&'a Vars>,
pub sprite: Sprite<'a>,
}
impl<'a> Atom<'a> {
pub fn from_prefab(objtree: &'a ObjectTree, fab: &'a Prefab) -> Option<Self> {
objtree.find(&fab.path).map(|type_| Atom {
type_: type_.get(),
prefab: Some(&fab.vars),
sprite: Sprite::default(),
})
}
pub fn istype(&self, parent: &str) -> bool {
subpath(&self.type_.path, parent)
}
}
impl<'a> From<&'a Type> for Atom<'a> {
fn from(type_: &'a Type) -> Self {
Atom {
type_: type_,
prefab: None,
sprite: Sprite::default(),
}
}
}
impl<'a> From<TypeRef<'a>> for Atom<'a> {
fn from(type_ref: TypeRef<'a>) -> Self {
Atom::from(type_ref.get())
}
}
pub struct Neighborhood<'objtree, 'atoms> {
// 0 1 2
// 3 4 5
// 6 7 8
inner: [&'atoms [Atom<'objtree>]; 9],
}
impl<'a, 'b> Neighborhood<'a, 'b> {
pub fn new(inner: [&'b [Atom<'a>]; 9]) -> Self {
Neighborhood { inner }
}
pub fn center(&self) -> &'b [Atom<'a>] {
self.inner[4]
}
pub fn offset(&self, dir: Dir) -> &'b [Atom<'a>] {
self.inner[match dir {
Dir::North => 1,
Dir::South => 7,
Dir::East => 5,
Dir::West => 3,
Dir::Northeast => 2,
Dir::Northwest => 0,
Dir::Southeast => 8,
Dir::Southwest => 6,
}]
}
}
// ----------------------------------------------------------------------------
// Vars abstraction
pub trait GetVar<'a> {
fn get_path(&self) -> &str;
fn get_var(&self, key: &str, objtree: &'a ObjectTree) -> &'a Constant {
self.get_var_inner(key, objtree).unwrap_or(Constant::null())
}
fn get_var_notnull(&self, key: &str, objtree: &'a ObjectTree) -> Option<&'a Constant> {
match self.get_var_inner(key, objtree) {
None | Some(&Constant::Null(_)) => None,
Some(other) => Some(other),
}
}
fn get_var_inner(&self, key: &str, objtree: &'a ObjectTree) -> Option<&'a Constant>;
}
impl<'a> GetVar<'a> for Atom<'a> {
fn get_path(&self) -> &str {
&self.type_.path
}
fn get_var_inner(&self, key: &str, objtree: &'a ObjectTree) -> Option<&'a Constant> {
if let Some(ref prefab) = self.prefab {
if let Some(v) = prefab.get(key) {
return Some(v);
}
}
let mut current = Some(self.type_);
while let Some(t) = current.take() {
if let Some(v) = t.vars.get(key) {
return Some(v.value.constant.as_ref().unwrap_or(Constant::null()));
}
current = objtree.parent_of(t);
}
None
}
}
impl<'a> GetVar<'a> for &'a Prefab {
fn get_path(&self) -> &str {
&self.path
}
fn get_var_inner(&self, key: &str, objtree: &'a ObjectTree) -> Option<&'a Constant> {
if let Some(v) = self.vars.get(key) {
return Some(v);
}
let mut current = objtree.find(&self.path);
while let Some(t) = current.take() {
if let Some(v) = t.get().vars.get(key) {
return Some(v.value.constant.as_ref().unwrap_or(Constant::null()));
}
current = t.parent_type();
}
None
}
}
impl<'a> GetVar<'a> for &'a Type {
fn get_path(&self) -> &str {
&self.path
}
fn get_var_inner(&self, key: &str, objtree: &'a ObjectTree) -> Option<&'a Constant> {
let mut current = Some(*self);
while let Some(t) = current.take() {
if let Some(v) = t.vars.get(key) {
return Some(v.value.constant.as_ref().unwrap_or(Constant::null()));
}
current = objtree.parent_of(t);
}
None
}
}
impl<'a> GetVar<'a> for TypeRef<'a> {
fn get_path(&self) -> &str {
&self.path
}
fn get_var_inner(&self, key: &str, _: &'a ObjectTree) -> Option<&'a Constant> {
let mut current = Some(*self);
while let Some(t) = current.take() {
if let Some(v) = t.get().vars.get(key) {
return Some(v.value.constant.as_ref().unwrap_or(Constant::null()));
}
current = t.parent_type();
}
None
}
}
// ----------------------------------------------------------------------------
// Renderer-agnostic sprite structure
/// Information about when a sprite should be shown or hidden.
#[derive(Default, Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct Category {
raw: u32,
}
impl Category {
const AREA: Category = Category { raw: 1 };
const TURF: Category = Category { raw: 2 };
const OBJ: Category = Category { raw: 3 };
const MOB: Category = Category { raw: 4 };
///
pub fn from_path(path: &str) -> Category {
if path.starts_with("/area") {
Category::AREA
} else if path.starts_with("/turf") {
Category::TURF
} else if path.starts_with("/obj") {
Category::OBJ
} else if path.starts_with("/mob") {
Category::MOB
} else {
Category { raw: 0 }
}
}
/// Encode this category for FFI representation.
pub fn matches_basic_layers(self, visible: &[bool]) -> bool {
visible.get(self.raw as usize).copied().unwrap_or(false)
}
}
#[cfg(feature="gfx_core")]
impl gfx_core::shade::BaseTyped for Category {
fn get_base_type() -> gfx_core::shade::BaseType {
u32::get_base_type()
}
}
/// A guaranteed sortable representation of a `layer` float.
#[derive(Default, Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Layer {
whole: i16,
frac: u16,
}
impl From<i16> for Layer {
fn from(whole: i16) -> Layer {
Layer { whole, frac: 0 }
}
}
impl From<i32> for Layer {
fn from(whole: i32) -> Layer {
use std::convert::TryFrom;
Layer { whole: i16::try_from(whole).expect("layer out of range"), frac: 0 }
}
}
impl From<f32> for Layer {
fn from(f: f32) -> Layer {
Layer { whole: f.floor() as i16, frac: ((f.fract() + 1.).fract() * 65536.) as u16 }
}
}
#[cfg(feature="gfx_core")]
impl gfx_core::shade::BaseTyped for Layer {
fn get_base_type() -> gfx_core::shade::BaseType {
i32::get_base_type()
}
}
/// A Sprite is a fragment of an atom's appearance.
///
/// Every atom has a default sprite, which may be disabled, and a list of
/// overlays.
#[derive(Debug, Clone)]
pub struct Sprite<'s> {
// filtering
pub category: Category,
// visual appearance
pub icon: &'s str,
pub icon_state: &'s str,
pub dir: Dir,
pub color: [u8; 4], // [r, g, b, a]
// position
pub ofs_x: i32, // pixel_x + pixel_w + step_x
pub ofs_y: i32, // pixel_y + pixel_z + step_y
// sorting
pub plane: i32,
pub layer: Layer,
}
impl<'s> Sprite<'s> {
pub fn from_vars<V: GetVar<'s> + ?Sized>(objtree: &'s ObjectTree, vars: &V) -> Sprite<'s> {
let pixel_x = vars.get_var("pixel_x", objtree).to_int().unwrap_or(0);
let pixel_y = vars.get_var("pixel_y", objtree).to_int().unwrap_or(0);
let pixel_w = vars.get_var("pixel_w", objtree).to_int().unwrap_or(0);
let pixel_z = vars.get_var("pixel_z", objtree).to_int().unwrap_or(0);
let step_x = vars.get_var("step_x", objtree).to_int().unwrap_or(0);
let step_y = vars.get_var("step_y", objtree).to_int().unwrap_or(0);
Sprite {
category: Category::from_path(vars.get_path()),
icon: vars.get_var("icon", objtree).as_path_str().unwrap_or(""),
icon_state: vars.get_var("icon_state", objtree).as_str().unwrap_or(""),
dir: vars.get_var("dir", objtree).to_int().and_then(Dir::from_int).unwrap_or(Dir::default()),
color: color_of(objtree, vars),
ofs_x: pixel_x + pixel_w + step_x,
ofs_y: pixel_y + pixel_z + step_y,
plane: plane_of(objtree, vars),
layer: layer_of(objtree, vars),
}
}
}
impl<'s> Default for Sprite<'s> {
fn default() -> Self {
Sprite {
category: Category::default(),
icon: "",
icon_state: "",
dir: Dir::default(),
color: [255, 255, 255, 255],
ofs_x: 0,
ofs_y: 0,
plane: 0,
layer: Layer::default(),
}
}
}
fn plane_of<'s, T: GetVar<'s> + ?Sized>(objtree: &'s ObjectTree, atom: &T) -> i32 {
match atom.get_var("plane", objtree) {
&Constant::Int(i) => i,
other => {
eprintln!("not a plane: {:?} on {:?}", other, atom.get_path());
0
}
}
}
pub(crate) fn layer_of<'s, T: GetVar<'s> + ?Sized>(objtree: &'s ObjectTree, atom: &T) -> Layer {
match atom.get_var("layer", objtree) {
&Constant::Int(i) => Layer::from(i),
&Constant::Float(f) => Layer::from(f),
other => {
eprintln!("not a layer: {:?} on {:?}", other, atom.get_path());
Layer::from(2)
}
}
}
pub fn color_of<'s, T: GetVar<'s> + ?Sized>(objtree: &'s ObjectTree, atom: &T) -> [u8; 4] {
let alpha = match atom.get_var("alpha", objtree) {
&Constant::Int(i) if i >= 0 && i <= 255 => i as u8,
_ => 255,
};
match atom.get_var("color", objtree) {
&Constant::String(ref color) if color.starts_with("#") => {
let mut sum = 0;
for ch in color[1..color.len()].chars() {
sum = 16 * sum + ch.to_digit(16).unwrap_or(0);
}
if color.len() == 7 { // #rrggbb
[(sum >> 16) as u8, (sum >> 8) as u8, sum as u8, alpha]
} else if color.len() == 4 { // #rgb
[
(0x11 * ((sum >> 8) & 0xf)) as u8,
(0x11 * ((sum >> 4) & 0xf)) as u8,
(0x11 * (sum & 0xf)) as u8,
alpha,
]
} else {
[255, 255, 255, alpha] // invalid
}
}
&Constant::String(ref color) => match html_color(color) {
Some([r, g, b]) => [r, g, b, alpha],
None => [255, 255, 255, alpha],
}
// TODO: color matrix support?
_ => [255, 255, 255, alpha],
}
}
fn html_color(name: &str) -> Option<[u8; 3]> {
Some(match name {
// from "tags (text)" in the DM reference
"black" => [0, 0, 0],
"silver" => [0xc0, 0xc0, 0xc0],
"gray" | "grey" => [0x80, 0x80, 0x80],
"white" => [0xff, 0xff, 0xff],
"maroon" => [0x80, 0, 0],
"red" => [0xff, 0, 0],
"purple" => [0x80, 0, 0x80],
"fuchsia" | "magenta" => [0xff, 0, 0xff],
"green" => [0, 0xc0, 0],
"lime" => [0, 0xff, 0],
"olive" | "gold" => [0x80, 0x80, 0],
"yellow" => [0xff, 0xff, 0],
"navy" => [0, 0, 0x80],
"blue" => [0, 0, 0xff],
"teal" => [0, 0x80, 0x80],
"aqua" | "cyan" => [0, 0xff, 0xff],
_ => return None,
})
}