@@ -11,7 +11,8 @@ use mindy::{
1111 parser:: LogicParser ,
1212 types:: { LAccess , Object , ProcessorConfig , ProcessorLinkConfig , content} ,
1313 vm:: {
14- Building , BuildingData , EmbeddedDisplayData , InstructionResult , LVar , LogicVM ,
14+ Building , BuildingData , Content , EmbeddedDisplayData , InstructionResult , LValue , LVar ,
15+ LogicVM ,
1516 buildings:: { MESSAGE , SWITCH } ,
1617 variables:: Constants ,
1718 } ,
@@ -154,6 +155,19 @@ impl WebLogicVM {
154155 . map_err ( |e| e. to_string ( ) )
155156 }
156157
158+ pub fn add_sorter ( & mut self , position : u32 ) -> Result < ( ) , String > {
159+ self . vm
160+ . add_building (
161+ Building :: new (
162+ content:: blocks:: FROM_NAME [ "sorter" ] ,
163+ unpack_point ( position) ,
164+ WebSorterData :: new ( self . on_building_change . clone ( ) ) . into ( ) ,
165+ ) ,
166+ & self . globals ,
167+ )
168+ . map_err ( |e| e. to_string ( ) )
169+ }
170+
157171 pub fn add_switch ( & mut self , position : u32 ) -> Result < ( ) , String > {
158172 self . vm
159173 . add_building (
@@ -235,6 +249,47 @@ impl WebLogicVM {
235249 Ok ( names)
236250 }
237251
252+ pub fn set_sorter_config (
253+ & mut self ,
254+ position : u32 ,
255+ logic_id : Option < i32 > ,
256+ ) -> Result < ( ) , String > {
257+ let position = unpack_point ( position) ;
258+
259+ let item = match logic_id {
260+ Some ( logic_id) => Content :: Item (
261+ * content:: items:: FROM_LOGIC_ID
262+ . get ( & logic_id)
263+ . ok_or_else ( || format ! ( "invalid logic id: {logic_id}" ) ) ?,
264+ )
265+ . into ( ) ,
266+ None => LValue :: NULL ,
267+ } ;
268+
269+ let building = self
270+ . vm
271+ . building ( position)
272+ . ok_or_else ( || format ! ( "building does not exist: {position}" ) ) ?;
273+
274+ let BuildingData :: Custom ( custom) = & mut * building. data . borrow_mut ( ) else {
275+ return Err ( format ! (
276+ "expected switch at {position} but got {}" ,
277+ building. block. name
278+ ) ) ;
279+ } ;
280+
281+ let _ = custom. control (
282+ building,
283+ & self . vm ,
284+ LAccess :: Config ,
285+ Cow :: Owned ( item) ,
286+ Default :: default ( ) ,
287+ Default :: default ( ) ,
288+ ) ;
289+
290+ Ok ( ( ) )
291+ }
292+
238293 pub fn set_switch_enabled ( & mut self , position : u32 , value : bool ) -> Result < ( ) , String > {
239294 let position = unpack_point ( position) ;
240295 let building = self
0 commit comments