Skip to content

Commit

Permalink
tgting: save 406 bytes with bit tricks
Browse files Browse the repository at this point in the history
  • Loading branch information
serprex committed Mar 11, 2024
1 parent c79624e commit eb85f71
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/rs/src/skill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,22 +677,23 @@ impl Tgt {

pub fn check(self, ctx: &Game, c: i16, t: i16) -> bool {
let mut x = self.0.get();
let mut ops = [(0u8, 0u8); 4];
let mut ops = 0u32;
let mut opnum = 0;
let mut vals = [false; 4];
let mut valnum = 0u8;
let mut vals = 0u32;
let mut valnum = 0u32;
let mut ignore = 0;

loop {
if (x & 1) == 0 {
let f = (x >> 1) & 31;
x >>= 6;
if f == 0 {
ops[opnum] = (1, valnum);
ops = ops << 6 | 1 << 3 | valnum;
opnum += 1;
continue;
}
vals[valnum as usize] = ignore == 0 && match f {
valnum += 1;
vals = (vals << 1) | (ignore == 0 && match f {
Tgt::_own => ctx.get_owner(c) == ctx.get_owner(t),
Tgt::_notself => c != t,
Tgt::_card => c != t && ctx.get_kind(t) == Kind::Spell,
Expand Down Expand Up @@ -791,42 +792,45 @@ impl Tgt {
.is_some()
}
_ => unsafe { core::hint::unreachable_unchecked() },
};
valnum += 1;
}) as u32;
} else {
ops[opnum] = ((x & 2) as u8, valnum);
ops = ops << 6 | ((x & 2) as u32) << 3 | valnum;
opnum += 1;
x >>= 2;
continue;
}
loop {
if opnum == 0 {
return vals[valnum as usize - 1];
return vals & 1 == 1;
}
if valnum <= ops[opnum - 1].1 {
if valnum <= ops & 7 {
break
}
let op = ops[opnum - 1].0;
let op = (ops >> 3) & 7;
match op {
0 | 2 => { // AND | OR
if (op == 0) != vals[valnum as usize - 1] {
if (op == 0) != (vals & 1 == 1) {
ignore += 1;
ops[opnum - 1] = (4, valnum);
ops = (ops & !0x3f) | 4 << 3 | valnum;
} else {
valnum -= 1;
ops[opnum - 1] = (3, valnum);
ops = (ops & !0x3f) | 3 << 3 | valnum;
}
}
1 => { // NOT
ops >>= 6;
opnum -= 1;
vals[valnum as usize - 1] = !vals[valnum as usize - 1];
vals ^= 1;
}
3 => { // NEXT
ops >>= 6;
opnum -= 1;
}
4 => { // DROP
ops >>= 6;
opnum -= 1;
valnum -= 1;
vals >>= 1;
ignore -= 1;
}
_ => unsafe { core::hint::unreachable_unchecked() },
Expand Down

0 comments on commit eb85f71

Please sign in to comment.