Oolite 1.91.0.7645-241119-222d325
Loading...
Searching...
No Matches
OOJSClock.m
Go to the documentation of this file.
1/*
2
3OOJSClock.m
4
5
6Oolite
7Copyright (C) 2004-2013 Giles C Williams and contributors
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
26#import "OOJSClock.h"
28#import "Universe.h"
29#import "OOJSPlayer.h"
30#import "PlayerEntity.h"
32#import "OOStringParsing.h"
33#import "OODebugStandards.h"
34
35static JSBool ClockGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
36
37// Methods
38static JSBool JSClockToString(JSContext *context, uintN argc, jsval *vp);
39static JSBool ClockClockStringForTime(JSContext *context, uintN argc, jsval *vp);
40static JSBool ClockAddSeconds(JSContext *context, uintN argc, jsval *vp);
41
42
43static JSClass sClockClass =
44{
45 "Clock",
46 JSCLASS_HAS_PRIVATE,
47
48 JS_PropertyStub, // addProperty
49 JS_PropertyStub, // delProperty
50 ClockGetProperty, // getProperty
51 JS_StrictPropertyStub, // setProperty
52 JS_EnumerateStub, // enumerate
53 JS_ResolveStub, // resolve
54 JS_ConvertStub, // convert
55 JS_FinalizeStub, // finalize
56 JSCLASS_NO_OPTIONAL_MEMBERS
57};
58
59
60enum
61{
62 // Property IDs
63 kClock_absoluteSeconds, // game real time clock, double, read-only
64 kClock_seconds, // game clock time, double, read-only
65 kClock_minutes, // game clock time minutes (rounded down), integer double, read-only
66 kClock_hours, // game clock time hours (rounded down), integer double, read-only
67 kClock_days, // game clock time days (rounded down), int, read-only
68 kClock_secondsComponent, // second component of game clock time, double, read-only
69 kClock_minutesComponent, // minute component of game clock time (rounded down), int, read-only
70 kClock_hoursComponent, // hour component of game clock time (rounded down), int, read-only
71 kClock_daysComponent, // day component of game clock time (rounded down), int, read-only
72 kClock_clockString, // game clock time as display string, string, read-only
73 kClock_isAdjusting, // clock is adjusting, boolean, read-only
74 kClock_adjustedSeconds, // game clock time, including adjustments, double, read-only
75 kClock_legacy_scriptTimer // legacy scriptTimer_number, double, read-only
76};
77
78
79static JSPropertySpec sClockProperties[] =
80{
81 // JS name ID flags
82 { "absoluteSeconds", kClock_absoluteSeconds, OOJS_PROP_READONLY_CB },
87 { "secondsComponent", kClock_secondsComponent, OOJS_PROP_READONLY_CB },
88 { "minutesComponent", kClock_minutesComponent, OOJS_PROP_READONLY_CB },
89 { "hoursComponent", kClock_hoursComponent, OOJS_PROP_READONLY_CB },
90 { "daysComponent", kClock_daysComponent, OOJS_PROP_READONLY_CB },
91 { "clockString", kClock_clockString, OOJS_PROP_READONLY_CB },
92 { "isAdjusting", kClock_isAdjusting, OOJS_PROP_READONLY_CB },
93 { "adjustedSeconds", kClock_adjustedSeconds, OOJS_PROP_READONLY_CB },
94 { "legacy_scriptTimer", kClock_legacy_scriptTimer, OOJS_PROP_READONLY_CB },
95 { 0 }
96};
97
98
99static JSFunctionSpec sClockMethods[] =
100{
101 // JS name Function min args
102 { "toString", JSClockToString, 0 },
103 { "clockStringForTime", ClockClockStringForTime, 1 },
104 { "addSeconds", ClockAddSeconds, 1 },
105 { 0 }
106};
107
108
109void InitOOJSClock(JSContext *context, JSObject *global)
110{
111 JSObject *clockPrototype = JS_InitClass(context, global, NULL, &sClockClass, OOJSUnconstructableConstruct, 0, sClockProperties, sClockMethods, NULL, NULL);
112 JS_DefineObject(context, global, "clock", &sClockClass, clockPrototype, OOJS_PROP_READONLY);
113}
114
115
116static JSBool ClockGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
117{
118 if (!JSID_IS_INT(propID)) return YES;
119
120 OOJS_NATIVE_ENTER(context)
121
123 double clockTime;
124
125 clockTime = [player clockTime];
126
127 switch (JSID_TO_INT(propID))
128 {
130 return JS_NewNumberValue(context, [UNIVERSE getTime], value);
131
132 case kClock_seconds:
133 return JS_NewNumberValue(context, clockTime, value);
134
135 case kClock_minutes:
136 return JS_NewNumberValue(context, floor(clockTime / 60.0), value);
137
138 case kClock_hours:
139 return JS_NewNumberValue(context, floor(clockTime /3600.0), value);
140 return YES;
141
143 *value = INT_TO_JSVAL(fmod(clockTime, 60.0));
144 return YES;
145
147 *value = INT_TO_JSVAL(fmod(floor(clockTime / 60.0), 60.0));
148 return YES;
149
151 *value = INT_TO_JSVAL(fmod(floor(clockTime / 3600.0), 24.0));
152 return YES;
153
154 case kClock_days:
156 *value = INT_TO_JSVAL(floor(clockTime / 86400.0));
157 return YES;
158
160 *value = OOJSValueFromNativeObject(context, [player dial_clock]);
161 return YES;
162
164 *value = OOJSValueFromBOOL([player clockAdjusting]);
165 return YES;
166
168 return JS_NewNumberValue(context, [player clockTimeAdjusted], value);
169
171 OOStandardsDeprecated(@"The legacy_scriptTimer property is deprecated");
172 return JS_NewNumberValue(context, [player scriptTimer], value);
173
174 default:
175 OOJSReportBadPropertySelector(context, this, propID, sClockProperties);
176 return NO;
177 }
178
180}
181
182
183// *** Methods ***
184
185// toString() : String
186static JSBool JSClockToString(JSContext *context, uintN argc, jsval *vp)
187{
188 OOJS_NATIVE_ENTER(context)
189
191
193}
194
195
196// clockStringForTime(time : Number) : String
197static JSBool ClockClockStringForTime(JSContext *context, uintN argc, jsval *vp)
198{
199 OOJS_NATIVE_ENTER(context)
200
201 double time;
202
203 if (EXPECT_NOT(argc < 1 || !JS_ValueToNumber(context, OOJS_ARGV[0], &time)))
204 {
205 jsval arg = JSVAL_VOID;
206 if (argc > 0) arg = OOJS_ARGV[0];
207 OOJSReportBadArguments(context, @"Clock", @"clockStringForTime", 1, &arg, nil, @"number");
208 return NO;
209 }
210
212
214}
215
216
217// addSeconds(seconds : Number) : String
218static JSBool ClockAddSeconds(JSContext *context, uintN argc, jsval *vp)
219{
220 OOJS_NATIVE_ENTER(context)
221
222 double time;
223 const double kMaxTime = 30.0 * 24.0 * 3600.0; // 30 days
224
225 if (EXPECT_NOT(argc < 1 || !JS_ValueToNumber(context, OOJS_ARGV[0], &time)))
226 {
227 jsval arg = JSVAL_VOID;
228 if (argc > 0) arg = OOJS_ARGV[0];
229 OOJSReportBadArguments(context, @"Clock", @"addSeconds", 1, &arg, nil, @"number");
230 return NO;
231 }
232
233 if (time > kMaxTime || time < 1.0 || !isfinite(time))
234 {
236 }
237
238 [OOPlayerForScripting() addToAdjustTime:time];
239
240 OOJS_RETURN_BOOL(YES);
241
243}
void OOStandardsDeprecated(NSString *message)
#define EXPECT_NOT(x)
static JSBool ClockClockStringForTime(JSContext *context, uintN argc, jsval *vp)
Definition OOJSClock.m:197
@ kClock_absoluteSeconds
Definition OOJSClock.m:63
@ kClock_isAdjusting
Definition OOJSClock.m:73
@ kClock_daysComponent
Definition OOJSClock.m:71
@ kClock_adjustedSeconds
Definition OOJSClock.m:74
@ kClock_days
Definition OOJSClock.m:67
@ kClock_seconds
Definition OOJSClock.m:64
@ kClock_clockString
Definition OOJSClock.m:72
@ kClock_hours
Definition OOJSClock.m:66
@ kClock_secondsComponent
Definition OOJSClock.m:68
@ kClock_hoursComponent
Definition OOJSClock.m:70
@ kClock_minutes
Definition OOJSClock.m:65
@ kClock_minutesComponent
Definition OOJSClock.m:69
@ kClock_legacy_scriptTimer
Definition OOJSClock.m:75
static JSBool ClockAddSeconds(JSContext *context, uintN argc, jsval *vp)
Definition OOJSClock.m:218
static JSClass sClockClass
Definition OOJSClock.m:43
static JSPropertySpec sClockProperties[]
Definition OOJSClock.m:79
void InitOOJSClock(JSContext *context, JSObject *global)
Definition OOJSClock.m:109
static JSBool ClockGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
Definition OOJSClock.m:116
static JSFunctionSpec sClockMethods[]
Definition OOJSClock.m:99
static JSBool JSClockToString(JSContext *context, uintN argc, jsval *vp)
Definition OOJSClock.m:186
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
PlayerEntity * OOPlayerForScripting(void)
Definition OOJSPlayer.m:191
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
#define OOJS_RETURN_OBJECT(o)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
#define OOJS_RETURN_BOOL(v)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
#define OOJS_PROP_READONLY
#define OOJS_ARGV
OOINLINE jsval OOJSValueFromBOOL(int b) INLINE_CONST_FUNC
void OOJSReportBadArguments(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, NSString *message, NSString *expectedArgsDescription)
#define OOJS_PROP_READONLY_CB
return nil
NSString * ClockToString(double clock, BOOL adjusting)
#define UNIVERSE
Definition Universe.h:833