Skip to content

Commit 3dabafe

Browse files
committed
Implement lookup, add content data from mimex
1 parent 264ee62 commit 3dabafe

File tree

15 files changed

+650
-36
lines changed

15 files changed

+650
-36
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "submodules/mimex-data"]
2+
path = submodules/mimex-data
3+
url = https://github.com/cardillan/mimex-data

Cargo.lock

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2024"
77
base64 = "0.22.1"
88
binrw = "0.15.0"
99
cesu8 = "1.1.0"
10+
csv = "1.3.1"
1011
flate2 = { version = "1.1.2", features = ["zlib-rs"], default-features = false }
1112
indexmap = "2.10.0"
1213
itertools = "0.14.0"
@@ -17,7 +18,7 @@ num-traits = "0.2.19"
1718
rand = "0.9.2"
1819
regex = "1.11.1"
1920
seekable_reader = "0.1.2"
20-
serde = "1.0.219"
21+
serde = { version = "1.0.219", features = ["derive"] }
2122
serde_json = "1.0.141"
2223
strum = "0.27.2"
2324
strum_macros = "0.27.2"

src/logic/ast.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ pub enum Instruction {
5151
if_true: Value,
5252
if_false: Value,
5353
},
54+
Lookup {
55+
content_type: ContentType,
56+
result: Value,
57+
id: Value,
58+
},
5459
PackColor {
5560
result: Value,
5661
r: Value,
@@ -257,3 +262,5 @@ macro_rules! optional_args {
257262
}
258263

259264
pub(super) use optional_args;
265+
266+
use crate::types::ContentType;

src/logic/grammar.lalrpop

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// -*- mode: Rust; -*-
22

33
use super::ast::*;
4-
use crate::types::colors::{COLORS, to_double_bits};
4+
use crate::types::{colors::{COLORS, to_double_bits}, ContentType};
55

66
grammar;
77

@@ -21,6 +21,7 @@ match {
2121
"set",
2222
"op",
2323
"select",
24+
"lookup",
2425
"packcolor",
2526
"unpackcolor",
2627
"noop",
@@ -99,6 +100,25 @@ match {
99100
"acos",
100101
"atan",
101102

103+
"item",
104+
"block",
105+
"mech_UNUSED",
106+
"bullet",
107+
"liquid",
108+
"status",
109+
"unit",
110+
"weather",
111+
"effect_UNUSED",
112+
"sector",
113+
"loadout_UNUSED",
114+
"typeid_UNUSED",
115+
"error",
116+
"planet",
117+
"ammo_UNUSED",
118+
"team",
119+
"unitCommand",
120+
"unitStance",
121+
102122
// https://github.com/Anuken/Arc/blob/071fdffaf220cd57cf971a0ee58db2f321f92ee1/arc-core/src/arc/util/Strings.java#L495
103123
// for example, -+1.--0. is a valid number literal
104124
r"(?x)
@@ -191,6 +211,9 @@ Instruction: Instruction = {
191211
"select" <result:Value> <op:ConditionOp2> <x:Value> <y:Value> <if_true:Value> <if_false:Value> =>
192212
Instruction::Select { <> },
193213

214+
"lookup" <content_type:ContentType> <result:Value> <id:Value> =>
215+
Instruction::Lookup { <> },
216+
194217
"packcolor" <result:Value> <r:Value> <g:Value> <b:Value> <a:Value> =>
195218
Instruction::PackColor { <> },
196219

@@ -330,6 +353,27 @@ LogicOp2: LogicOp = {
330353
"logn" => LogicOp::Logn,
331354
};
332355

356+
ContentType: ContentType = {
357+
"item" => ContentType::Item,
358+
"block" => ContentType::Block,
359+
"mech_UNUSED" => ContentType::Mech,
360+
"bullet" => ContentType::Bullet,
361+
"liquid" => ContentType::Liquid,
362+
"status" => ContentType::Status,
363+
"unit" => ContentType::Unit,
364+
"weather" => ContentType::Weather,
365+
"effect_UNUSED" => ContentType::Effect,
366+
"sector" => ContentType::Sector,
367+
"loadout_UNUSED" => ContentType::Loadout,
368+
"typeid_UNUSED" => ContentType::TypeID,
369+
"error" => ContentType::Error,
370+
"planet" => ContentType::Planet,
371+
"ammo_UNUSED" => ContentType::Ammo,
372+
"team" => ContentType::Team,
373+
"unitCommand" => ContentType::UnitCommand,
374+
"unitStance" => ContentType::UnitStance,
375+
}
376+
333377
Value: Value = {
334378
<s:STRING> =>
335379
Value::String(s[1..s.len() - 1].replace(r"\n", "\n")),
@@ -396,6 +440,7 @@ Symbol = {
396440
"set",
397441
"op",
398442
"select",
443+
"lookup",
399444
"packcolor",
400445
"unpackcolor",
401446
"noop",
@@ -473,4 +518,23 @@ Symbol = {
473518
"asin",
474519
"acos",
475520
"atan",
521+
522+
"item",
523+
"block",
524+
"mech_UNUSED",
525+
"bullet",
526+
"liquid",
527+
"status",
528+
"unit",
529+
"weather",
530+
"effect_UNUSED",
531+
"sector",
532+
"loadout_UNUSED",
533+
"typeid_UNUSED",
534+
"error",
535+
"planet",
536+
"ammo_UNUSED",
537+
"team",
538+
"unitCommand",
539+
"unitStance",
476540
};

src/logic/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
pub mod ast;
22
mod vm;
33

4-
lalrpop_util::lalrpop_mod!(grammar, "/logic/grammar.rs");
4+
lalrpop_util::lalrpop_mod!(
5+
#[allow(deprecated)]
6+
grammar,
7+
"/logic/grammar.rs"
8+
);
59

610
pub use grammar::LogicParser;
711
pub use vm::*;

src/logic/vm/blocks.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use super::{
77
LogicVM, VMLoadError, VMLoadResult,
88
processor::{Processor, ProcessorBuilder},
99
};
10-
use crate::types::{Object, ProcessorConfig, SchematicTile};
10+
use crate::types::{Object, ProcessorConfig, SchematicTile, content};
1111

1212
const MESSAGE_MAX_LEN: usize = 220;
1313
const MESSAGE_MAX_LINES: usize = 24;
@@ -79,6 +79,7 @@ impl Block {
7979
privileged: false,
8080
running_processors: Rc::clone(&vm.running_processors),
8181
time: Rc::clone(&vm.time),
82+
globals: &vm.globals,
8283
config,
8384
}
8485
.build()?,
@@ -93,6 +94,7 @@ impl Block {
9394
privileged: false,
9495
running_processors: Rc::clone(&vm.running_processors),
9596
time: Rc::clone(&vm.time),
97+
globals: &vm.globals,
9698
config,
9799
}
98100
.build()?,
@@ -107,6 +109,7 @@ impl Block {
107109
privileged: false,
108110
running_processors: Rc::clone(&vm.running_processors),
109111
time: Rc::clone(&vm.time),
112+
globals: &vm.globals,
110113
config,
111114
}
112115
.build()?,
@@ -121,6 +124,7 @@ impl Block {
121124
privileged: true,
122125
running_processors: Rc::clone(&vm.running_processors),
123126
time: Rc::clone(&vm.time),
127+
globals: &vm.globals,
124128
config,
125129
}
126130
.build()?,
@@ -148,7 +152,10 @@ impl Block {
148152
block: block.clone(),
149153
config: config.clone(),
150154
},
151-
1,
155+
content::blocks::FROM_NAME
156+
.get(block.as_str())
157+
.map(|v| v.size)
158+
.unwrap_or(1),
152159
)),
153160
}
154161
}

src/logic/vm/instructions.rs

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ use noise::{NoiseFn, Simplex};
66
use super::{
77
LogicVM, VMLoadError, VMLoadResult,
88
processor::{MAX_TEXT_BUFFER, ProcessorState},
9-
variables::{LValue, LVar, RAD_DEG},
9+
variables::{Content, LValue, LVar, RAD_DEG},
1010
};
1111
use crate::{
1212
logic::{
1313
ast::{self, ConditionOp, LogicOp},
1414
vm::variables::{F64_DEG_RAD, F64_RAD_DEG},
1515
},
16-
types::colors::{f32_to_double_bits, f64_from_double_bits},
16+
types::{
17+
ContentType, Team,
18+
colors::{f32_to_double_bits, f64_from_double_bits},
19+
content,
20+
},
1721
};
1822

1923
const MAX_IPT: usize = 1000;
@@ -27,18 +31,23 @@ lazy_static! {
2731
pub fn parse_instruction(
2832
instruction: ast::Instruction,
2933
variables: &mut HashMap<String, LVar>,
34+
globals: &HashMap<String, LVar>,
3035
labels: &HashMap<String, usize>,
3136
privileged: bool,
3237
num_instructions: usize,
3338
) -> VMLoadResult<Box<dyn Instruction>> {
3439
// helpers
3540

3641
let mut lvar = |value| match value {
37-
ast::Value::Variable(name) => variables.get(&name).cloned().unwrap_or_else(|| {
38-
let var = LVar::new_variable();
39-
variables.insert(name, LVar::clone(&var));
40-
var
41-
}),
42+
ast::Value::Variable(name) => {
43+
if let Some(var) = globals.get(&name).or_else(|| variables.get(&name)) {
44+
var.clone()
45+
} else {
46+
let var = LVar::new_variable();
47+
variables.insert(name, LVar::clone(&var));
48+
var
49+
}
50+
}
4251
ast::Value::String(value) => LVar::Constant(LValue::String(value.into())),
4352
ast::Value::Number(value) => LVar::Constant(value.into()),
4453
ast::Value::None => LVar::Constant(LValue::Null),
@@ -101,6 +110,15 @@ pub fn parse_instruction(
101110
if_true: lvar(if_true),
102111
if_false: lvar(if_false),
103112
}),
113+
ast::Instruction::Lookup {
114+
content_type,
115+
result,
116+
id,
117+
} => Box::new(Lookup {
118+
content_type,
119+
result: lvar(result),
120+
id: lvar(id),
121+
}),
104122
ast::Instruction::PackColor { result, r, g, b, a } => Box::new(PackColor {
105123
result: lvar(result),
106124
r: lvar(r),
@@ -186,6 +204,8 @@ impl Print {
186204
})
187205
}
188206
LValue::String(s) => Cow::Borrowed(s),
207+
LValue::Content(content) => Cow::Borrowed(content.name()),
208+
LValue::Team(team) => team.name(),
189209
}
190210
}
191211
}
@@ -368,6 +388,46 @@ impl SimpleInstruction for Select {
368388
}
369389
}
370390

391+
struct Lookup {
392+
content_type: ContentType,
393+
result: LVar,
394+
id: LVar,
395+
}
396+
397+
impl SimpleInstruction for Lookup {
398+
fn execute(&self, state: &mut ProcessorState, _: &LogicVM) {
399+
let id = self.id.get(state).num() as i32;
400+
401+
let result = match self.content_type {
402+
ContentType::Block => content::blocks::FROM_LOGIC_ID
403+
.get(&id)
404+
.map(|v| Content::Block(v))
405+
.into(),
406+
407+
ContentType::Item => content::items::FROM_LOGIC_ID
408+
.get(&id)
409+
.map(|v| Content::Item(v))
410+
.into(),
411+
412+
ContentType::Liquid => content::liquids::FROM_LOGIC_ID
413+
.get(&id)
414+
.map(|v| Content::Liquid(v))
415+
.into(),
416+
417+
ContentType::Unit => content::units::FROM_LOGIC_ID
418+
.get(&id)
419+
.map(|v| Content::Unit(v))
420+
.into(),
421+
422+
ContentType::Team => id.try_into().ok().map(Team::from_id).into(),
423+
424+
_ => LValue::Null,
425+
};
426+
427+
self.result.set(state, result);
428+
}
429+
}
430+
371431
struct PackColor {
372432
result: LVar,
373433
r: LVar,

0 commit comments

Comments
 (0)