@@ -307,6 +307,12 @@ static bool DisallowAllAssignment(Value key, Value value) {
307
307
throw new RuntimeException ( "Assignment to protected map" ) ;
308
308
}
309
309
310
+ static bool RequireBot ( Shell sh , string methodName ) {
311
+ if ( sh . bot != null ) return false ;
312
+ sh . PrintLine ( $ "me.{ methodName } is only valid for bots") ;
313
+ return true ;
314
+ }
315
+
310
316
static ValMap meModule ;
311
317
static HashSet < string > botProtectedKeys ;
312
318
public static ValMap MeModule ( ) {
@@ -330,37 +336,51 @@ public static ValMap MeModule() {
330
336
} ;
331
337
meModule [ "name" ] = f . GetFunc ( ) ;
332
338
339
+
333
340
f = Intrinsic . Create ( "" ) ;
334
341
f . code = ( context , partialResult ) => {
335
342
Shell sh = context . interpreter . hostData as Shell ;
343
+ if ( RequireBot ( sh , "facing" ) ) return Intrinsic . Result . Null ;
336
344
return new Intrinsic . Result ( new ValNumber ( sh . bot . facingDirection ) ) ;
337
345
} ;
338
346
meModule [ "facing" ] = f . GetFunc ( ) ;
339
347
340
348
f = Intrinsic . Create ( "" ) ;
341
349
f . code = ( context , partialResult ) => {
342
350
Shell sh = context . interpreter . hostData as Shell ;
351
+ if ( RequireBot ( sh , "currentToolIndex" ) ) return Intrinsic . Result . Null ;
343
352
return new Intrinsic . Result ( new ValNumber ( sh . bot . currentToolIndex ) ) ;
344
353
} ;
345
354
meModule [ "currentToolIndex" ] = f . GetFunc ( ) ;
346
355
347
356
f = Intrinsic . Create ( "" ) ;
348
357
f . code = ( context , partialResult ) => {
349
358
Shell sh = context . interpreter . hostData as Shell ;
359
+ if ( RequireBot ( sh , "energy" ) ) return Intrinsic . Result . Null ;
350
360
return new Intrinsic . Result ( new ValNumber ( sh . bot . energy ) ) ;
351
361
} ;
352
362
meModule [ "energy" ] = f . GetFunc ( ) ;
353
363
354
364
f = Intrinsic . Create ( "" ) ;
355
365
f . code = ( context , partialResult ) => {
356
366
Shell sh = context . interpreter . hostData as Shell ;
367
+ if ( RequireBot ( sh , "statusColor" ) ) return Intrinsic . Result . Null ;
357
368
return new Intrinsic . Result ( new ValString ( sh . bot . statusColor . ToHexString ( ) ) ) ;
358
369
} ;
359
370
meModule [ "statusColor" ] = f . GetFunc ( ) ;
360
371
361
372
f = Intrinsic . Create ( "" ) ;
362
373
f . code = ( context , partialResult ) => {
363
374
Shell sh = context . interpreter . hostData as Shell ;
375
+ if ( sh . bot == null ) return new Intrinsic . Result ( new ValString ( sh . console . backColor . ToHexString ( ) ) ) ;
376
+ return new Intrinsic . Result ( new ValString ( sh . bot . screenColor . ToHexString ( ) ) ) ;
377
+ } ;
378
+ meModule [ "screenColor" ] = f . GetFunc ( ) ;
379
+
380
+ f = Intrinsic . Create ( "" ) ;
381
+ f . code = ( context , partialResult ) => {
382
+ Shell sh = context . interpreter . hostData as Shell ;
383
+ if ( RequireBot ( sh , "forward" ) ) return Intrinsic . Result . Null ;
364
384
if ( partialResult == null ) {
365
385
// Just starting our move; tell the bot and return partial result
366
386
sh . bot . MoveForward ( ) ;
@@ -376,6 +396,7 @@ public static ValMap MeModule() {
376
396
f = Intrinsic . Create ( "" ) ;
377
397
f . code = ( context , partialResult ) => {
378
398
Shell sh = context . interpreter . hostData as Shell ;
399
+ if ( RequireBot ( sh , "inventory" ) ) return Intrinsic . Result . Null ;
379
400
ValList result = new ValList ( ) ;
380
401
if ( sh . bot . inventory != null ) {
381
402
foreach ( var item in sh . bot . inventory ) {
@@ -389,6 +410,7 @@ public static ValMap MeModule() {
389
410
f = Intrinsic . Create ( "" ) ;
390
411
f . code = ( context , partialResult ) => {
391
412
Shell sh = context . interpreter . hostData as Shell ;
413
+ if ( RequireBot ( sh , "left" ) ) return Intrinsic . Result . Null ;
392
414
sh . bot . Rotate ( - 1 ) ;
393
415
return Intrinsic . Result . Null ;
394
416
} ;
@@ -397,6 +419,7 @@ public static ValMap MeModule() {
397
419
f = Intrinsic . Create ( "" ) ;
398
420
f . code = ( context , partialResult ) => {
399
421
Shell sh = context . interpreter . hostData as Shell ;
422
+ if ( RequireBot ( sh , "position" ) ) return Intrinsic . Result . Null ;
400
423
var pos = sh . bot . TileLocation ;
401
424
var loc = sh . bot . currentLocation ;
402
425
var result = new ValMap ( ) ;
@@ -410,6 +433,7 @@ public static ValMap MeModule() {
410
433
f = Intrinsic . Create ( "" ) ;
411
434
f . code = ( context , partialResult ) => {
412
435
Shell sh = context . interpreter . hostData as Shell ;
436
+ if ( RequireBot ( sh , "right" ) ) return Intrinsic . Result . Null ;
413
437
sh . bot . Rotate ( 1 ) ;
414
438
return Intrinsic . Result . Null ;
415
439
} ;
@@ -420,6 +444,7 @@ public static ValMap MeModule() {
420
444
// For now, we'll just always place as many as possible.
421
445
f . code = ( context , partialResult ) => {
422
446
Shell sh = context . interpreter . hostData as Shell ;
447
+ if ( RequireBot ( sh , "placeItem" ) ) return Intrinsic . Result . Null ;
423
448
int itemsPlaced = sh . bot . PlaceItem ( ) ;
424
449
return new Intrinsic . Result ( itemsPlaced ) ;
425
450
} ;
@@ -429,6 +454,7 @@ public static ValMap MeModule() {
429
454
f . AddParam ( "slot" , 0 ) ;
430
455
f . code = ( context , partialResult ) => {
431
456
Shell sh = context . interpreter . hostData as Shell ;
457
+ if ( RequireBot ( sh , "takeItem" ) ) return Intrinsic . Result . Null ;
432
458
bool result = sh . bot . TakeItem ( context . GetLocalInt ( "slot" ) ) ;
433
459
return result ? Intrinsic . Result . True : Intrinsic . Result . False ;
434
460
} ;
@@ -437,6 +463,7 @@ public static ValMap MeModule() {
437
463
f = Intrinsic . Create ( "" ) ;
438
464
f . code = ( context , partialResult ) => {
439
465
Shell sh = context . interpreter . hostData as Shell ;
466
+ if ( RequireBot ( sh , "useTool" ) ) return Intrinsic . Result . Null ;
440
467
441
468
if ( partialResult == null ) {
442
469
// Just starting our tool use; tell the bot and return partial result
@@ -453,6 +480,7 @@ public static ValMap MeModule() {
453
480
f = Intrinsic . Create ( "" ) ;
454
481
f . code = ( context , partialResult ) => {
455
482
Shell sh = context . interpreter . hostData as Shell ;
483
+ if ( RequireBot ( sh , "harvest" ) ) return Intrinsic . Result . Null ;
456
484
457
485
bool result = sh . bot . Harvest ( ) ;
458
486
return result ? Intrinsic . Result . True : Intrinsic . Result . False ;
@@ -477,11 +505,23 @@ public static ValMap MeModule() {
477
505
}
478
506
return true ;
479
507
} else if ( keyStr == "statusColor" ) {
480
- Shell . runningInstance . bot . statusColor = value . ToString ( ) . ToColor ( ) ;
481
- if ( Context . IsMultiplayer ) Shell . runningInstance . bot . data . Update ( ) ;
508
+ var sh = Shell . runningInstance ;
509
+ if ( RequireBot ( sh , keyStr ) ) return true ;
510
+ sh . bot . statusColor = value . ToString ( ) . ToColor ( ) ;
511
+ if ( Context . IsMultiplayer ) sh . bot . data . Update ( ) ;
512
+ return true ;
513
+ } else if ( keyStr == "screenColor" ) {
514
+ var sh = Shell . runningInstance ;
515
+ sh . console . backColor = value . ToString ( ) . ToColor ( ) ;
516
+ if ( sh . bot != null ) {
517
+ sh . bot . screenColor = value . ToString ( ) . ToColor ( ) ;
518
+ if ( Context . IsMultiplayer ) sh . bot . data . Update ( ) ;
519
+ }
482
520
return true ;
483
521
} else if ( keyStr == "currentToolIndex" ) {
484
- Shell . runningInstance . bot . currentToolIndex = value . IntValue ( ) ;
522
+ var sh = Shell . runningInstance ;
523
+ if ( RequireBot ( sh , keyStr ) ) return true ;
524
+ sh . bot . currentToolIndex = value . IntValue ( ) ;
485
525
return true ;
486
526
} else if ( botProtectedKeys . Contains ( keyStr ) ) return true ;
487
527
return false ; // allow the assignment
0 commit comments