-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWedge.cpp
76 lines (61 loc) · 1.73 KB
/
Wedge.cpp
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
//
// Wedge.cpp
// Fission
//
// Created by C0deH4cker on 2/24/14.
// Copyright (c) 2014 C0deH4cker. All rights reserved.
//
#include "Wedge.h"
#include <cstdint>
#include "common.h"
#include "Grid.h"
using namespace fsn;
Wedge::Wedge(Token type, Grid& grid)
: Component(type), DirectedComponent(type), grid(grid), saved{2, 0} {}
Direction Wedge::getDir() const {
switch(type) {
case Token::WEDGE_UP: return UP;
case Token::WEDGE_LEFT: return LEFT;
case Token::WEDGE_DOWN: return DOWN;
case Token::WEDGE_RIGHT: return RIGHT;
default:
fatal("Cell '%c' is not a Wedge.", (char)type);
}
}
bool Wedge::onHit(Atom& atom) {
Direction dir(getDir());
if(atom.dir == dir) {
// Hit the inside of the fork, so consume the atom
saved = atom;
// Store 1 instead of 0 to prevent div by zero
if(saved.mass == 0) {
++saved.mass;
}
return true;
}
if((atom.dir ^ 2) == dir) {
// The atom hit the vertex, so split the atom.
//
// WARNING: DO NOT TRY THIS AT HOME. SPLITTING ATOMS IS ONLY TO BE DONE
// BY TRAINED PROFESSIONALS. BY USING THE PRODUCT YOU ACKNOWLEDGE THAT
// THERE IS NO WARRANTY, EXPRESS OR IMPLIED, AND THAT THE AUTHOR IS NOT
// LIABLE FOR ANY INJURIES, DECAPITATIONS, OR SPONTANEOUS COMBUSTIONS
// THAT MAY RESULT FROM YOUR USE OF THE PRODUCT. IF THE WORLD AND/OR
// UNIVERSE COME TO A FIERY END AS A RESULT OF SPLITTING ATOMS WITH
// THE PRODUCT, THE AUTHOR IS NOT TO BE HELD RESPONSIBLE IN ANY WAY.
//
// Have fun!
int64_t left = atom.mass / saved.mass;
atom.mass -= left;
atom.energy -= saved.energy;
atom.dir = (dir + 1) & 3;
Atom split(atom);
split.mass = left;
split.dir = atom.dir ^ 2;
grid.spawn(split);
}
else {
atom.dir = dir;
}
return false;
}