1
1
use std:: { collections:: HashSet , iter:: repeat_with} ;
2
2
3
- use petgraph :: stable_graph :: NodeIndex ;
3
+ use eyre :: ensure ;
4
4
5
5
use crate :: {
6
6
graph:: {
7
+ logic:: LogicGraph ,
7
8
world:: {
8
9
builder:: { PlaceBound , PropagateType } ,
9
10
WorldGraph ,
10
11
} ,
11
- GraphNodeId , GraphNodeKind , SubGraphWithGraph ,
12
+ GraphNodeKind ,
12
13
} ,
13
14
world:: { block:: Block , position:: Position , world:: World3D } ,
14
15
} ;
@@ -35,61 +36,52 @@ impl PlacedNode {
35
36
. into_iter ( )
36
37
. collect ( )
37
38
}
39
+
40
+ pub fn has_conflict_with ( & self , other : & Self ) -> bool {
41
+ todo ! ( )
42
+ }
43
+
44
+ // pub fn
38
45
}
39
46
40
- pub struct LocalPlacer < ' a > {
41
- graph : SubGraphWithGraph < ' a > ,
42
- try_count : usize ,
43
- max_width_size : usize ,
44
- max_height_size : usize ,
47
+ pub struct LocalPlacer {
48
+ graph : LogicGraph ,
45
49
}
46
50
47
51
pub const K_MAX_LOCAL_PLACE_NODE_COUNT : usize = 25 ;
48
52
49
- impl < ' a > LocalPlacer < ' a > {
50
- // you should not pass side effected sub-graph
51
- pub fn new (
52
- graph : SubGraphWithGraph < ' a > ,
53
- try_count : usize ,
54
- max_width_size : Option < usize > ,
55
- max_height_size : Option < usize > ,
56
- ) -> eyre:: Result < Self > {
57
- assert ! ( try_count > 0 ) ;
58
-
59
- Self :: verify ( & graph) ;
60
-
61
- Ok ( Self {
62
- graph,
63
- try_count,
64
- max_width_size : max_width_size. unwrap_or ( usize:: MAX ) ,
65
- max_height_size : max_height_size. unwrap_or ( usize:: MAX ) ,
66
- } )
53
+ impl LocalPlacer {
54
+ pub fn new ( graph : LogicGraph ) -> eyre:: Result < Self > {
55
+ let result = Self { graph } ;
56
+ result. verify ( ) ?;
57
+ Ok ( result)
67
58
}
68
59
69
- pub fn verify ( graph : & SubGraphWithGraph < ' a > ) {
70
- assert ! ( graph. nodes. len( ) > 0 ) ;
71
- assert ! ( graph. nodes. len( ) <= K_MAX_LOCAL_PLACE_NODE_COUNT ) ;
72
-
73
- for node_id in & graph. nodes {
74
- assert ! ( matches!(
75
- graph. graph. find_node_by_id( * node_id) . unwrap( ) . kind,
76
- GraphNodeKind :: Logic { .. }
77
- ) ) ;
60
+ fn verify ( & self ) -> eyre:: Result < ( ) > {
61
+ ensure ! ( self . graph. nodes. len( ) > 0 , "" ) ;
62
+ ensure ! ( self . graph. nodes. len( ) <= K_MAX_LOCAL_PLACE_NODE_COUNT , "" ) ;
63
+
64
+ for node_id in & self . graph . nodes {
65
+ ensure ! (
66
+ matches!(
67
+ self . graph. find_node_by_id( node_id. id) . unwrap( ) . kind,
68
+ GraphNodeKind :: Logic { .. }
69
+ ) ,
70
+ ""
71
+ ) ;
78
72
}
79
- }
80
73
81
- pub fn generate ( & mut self ) -> WorldGraph {
82
- let try_count = self . try_count ;
74
+ Ok ( ( ) )
75
+ }
83
76
84
- // TODO: make parallel
77
+ pub fn generate ( & mut self ) -> World3D {
85
78
repeat_with ( || self . next_place ( ) )
86
- . take ( try_count)
87
79
. min_by_key ( |( _, c) | * c)
88
80
. unwrap ( )
89
81
. 0
90
82
}
91
83
92
- fn next_place ( & mut self ) -> ( WorldGraph , usize ) {
84
+ fn next_place ( & mut self ) -> ( World3D , usize ) {
93
85
todo ! ( )
94
86
}
95
87
}
@@ -109,3 +101,34 @@ impl<'a> LocalPlacerCostEstimator<'a> {
109
101
todo ! ( )
110
102
}
111
103
}
104
+
105
+ #[ cfg( test) ]
106
+ mod tests {
107
+
108
+ use crate :: {
109
+ graph:: {
110
+ graphviz:: ToGraphvizGraph ,
111
+ logic:: { builder:: LogicGraphBuilder , LogicGraph } ,
112
+ } ,
113
+ nbt:: { NBTRoot , ToNBT } ,
114
+ transform:: placer:: LocalPlacer ,
115
+ } ;
116
+
117
+ fn build_graph_from_stmt ( stmt : & str , output : & str ) -> eyre:: Result < LogicGraph > {
118
+ LogicGraphBuilder :: new ( stmt. to_string ( ) ) . build ( output. to_string ( ) )
119
+ }
120
+
121
+ #[ test]
122
+ fn test_generate_component_and ( ) -> eyre:: Result < ( ) > {
123
+ let logic_graph = build_graph_from_stmt ( "a&b" , "c" ) ?. prepare_place ( ) ?;
124
+ println ! ( "{}" , logic_graph. to_graphviz( ) ) ;
125
+
126
+ let mut placer = LocalPlacer :: new ( logic_graph) ?;
127
+ let world3d = placer. generate ( ) ;
128
+
129
+ let nbt: NBTRoot = world3d. to_nbt ( ) ;
130
+ nbt. save ( "test/and-gate-new.nbt" ) ;
131
+
132
+ Ok ( ( ) )
133
+ }
134
+ }
0 commit comments