-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsbot.m
More file actions
132 lines (115 loc) · 3.6 KB
/
Copy pathjsbot.m
File metadata and controls
132 lines (115 loc) · 3.6 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#import "vector.h"
#import "jsbot.h"
#import "console.h"
#import "game.h"
#include <math.h>
@implementation jsBot
@synthesize jsContext=_jsContext;
@synthesize position=_position;
@synthesize speed = _speed;
@synthesize angle = _angle;
@synthesize name = _name;
@synthesize thread, game;
@synthesize returnValues, scanAngle, scanResolution, index, shootArguments;
- (id) initWithFile: (NSString *) botFile Position: (jsVector*) pos{
self = [[jsBot alloc] init];
if(self){
//set up some initial variables
_position = pos;
_console = [[jsConsole alloc] init];
_name = botFile;
returnValues = 0;
}
return self;
}
- (void) update{
_position = [_position add:[jsVector vectorWithMag: _speed/30 angle: _angle*M_PI/180]];
}
-(void) driveSpeed: (int) speed Angle: (int) angle{
if(_speed <= 50){
if(speed > 100){
speed = 100;
}
_speed = speed;
while(angle < 0 || angle > 359){
if(angle < 0){
angle += 360;
}
if(angle > 359){
angle = angle % 360;
}
}
_angle = angle;
}
}
-(int) scanInDirection: (int) direction WithResolution: (int) resolution{
if(resolution < 0) resolution *= -1;
if(resolution > 10){
resolution = 10;
}
while(direction < 0 || direction > 359){
if(direction < 0){
direction += 360;
}
if(direction > 359){
direction = direction % 360;
}
}
scanResolution = resolution;
scanAngle = direction;
[game performSelectorOnMainThread:@selector(scan:) withObject:self waitUntilDone:YES];
return returnValues;
}
-(int) shootInDirection: (int) direction atDistance: (int) distance{
//setup some arguments to be sent
shootArguments.direction = direction;
shootArguments.distance = distance;
//call the shoot function on the main thread
[game performSelectorOnMainThread:@selector(shoot:) withObject:self waitUntilDone:YES];
return self.returnValues;
}
- (void) makeSimpleFunction {
_jsContext[@"shoot"] = ^(int direction, int distance){
//get the robot object. If I don't do this I end up with a retain cycle
jsBot *bot = [[JSContext currentContext][@"mybot"] toObjectOfClass: [jsBot class]];
//call the function to shoot
return [bot shootInDirection: direction atDistance: distance];
};
_jsContext[@"scan"] = ^(int direction, int resolution){
//get the robot object. If I don't do this I end up with a retain cycle
jsBot *bot = [[JSContext currentContext][@"mybot"] toObjectOfClass: [jsBot class]];
//call the function to shoot
return [bot scanInDirection: direction WithResolution: resolution];
};
_jsContext[@"drive"] = ^(int speed, int angle){
//get the robot object. If I don't do this I end up with a retain cycle
jsBot *bot = [[JSContext currentContext][@"mybot"] toObjectOfClass: [jsBot class]];
//call the function to shoot
return [bot driveSpeed: speed Angle: angle];
};
}
//This is where the thread for the robot will enter
- (void) main{
//setup js virtual machine
JSVirtualMachine* machine = [[JSVirtualMachine alloc] init];
//setup javascript context
_jsContext = [[JSContext alloc] initWithVirtualMachine: machine];
NSError *error;
NSString *jsFile = [NSString stringWithContentsOfFile:_name encoding:NSUTF8StringEncoding error:&error];
if(error){
NSLog(@"Error reading file: %@", error.localizedDescription);
}
//Setup variables on the global object in the context
_jsContext[@"Vector"] = [jsVector class];
_jsContext[@"mybot"] = self;
_jsContext[@"console"] = _console;
[self makeSimpleFunction];
//Evaluate the script
[_jsContext evaluateScript: jsFile];
JSValue *main = _jsContext[@"main"];
[main callWithArguments: nil];
}
@end
@implementation jsBullet
@synthesize fromBot, startPosition, position, shootArguments;
@end