Oolite
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"
27#import "OODebugStandards.h"
28#import "OOJSPlayer.h"
30#import "OOStringParsing.h"
31#import "PlayerEntity.h"
33#import "Universe.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
42static JSClass sClockClass = {
43 "Clock",
44 JSCLASS_HAS_PRIVATE,
45
46 JS_PropertyStub, // addProperty
47 JS_PropertyStub, // delProperty
48 ClockGetProperty, // getProperty
49 JS_StrictPropertyStub, // setProperty
50 JS_EnumerateStub, // enumerate
51 JS_ResolveStub, // resolve
52 JS_ConvertStub, // convert
53 JS_FinalizeStub, // finalize
54 JSCLASS_NO_OPTIONAL_MEMBERS
55};
56
57enum {
58 // Property IDs
59 kClock_absoluteSeconds, // game real time clock, double, read-only
60 kClock_seconds, // game clock time, double, read-only
61 kClock_minutes, // game clock time minutes (rounded down), integer double, read-only
62 kClock_hours, // game clock time hours (rounded down), integer double, read-only
63 kClock_days, // game clock time days (rounded down), int, read-only
64 kClock_secondsComponent, // second component of game clock time, double, read-only
65 kClock_minutesComponent, // minute component of game clock time (rounded down), int, read-only
66 kClock_hoursComponent, // hour component of game clock time (rounded down), int, read-only
67 kClock_daysComponent, // day component of game clock time (rounded down), int, read-only
68 kClock_clockString, // game clock time as display string, string, read-only
69 kClock_isAdjusting, // clock is adjusting, boolean, read-only
70 kClock_adjustedSeconds, // game clock time, including adjustments, double, read-only
71 kClock_legacy_scriptTimer // legacy scriptTimer_number, double, read-only
72};
73
74static JSPropertySpec sClockProperties[] = {
75 // JS name ID flags
76 { "absoluteSeconds", kClock_absoluteSeconds, OOJS_PROP_READONLY_CB },
81 { "secondsComponent", kClock_secondsComponent, OOJS_PROP_READONLY_CB },
82 { "minutesComponent", kClock_minutesComponent, OOJS_PROP_READONLY_CB },
83 { "hoursComponent", kClock_hoursComponent, OOJS_PROP_READONLY_CB },
84 { "daysComponent", kClock_daysComponent, OOJS_PROP_READONLY_CB },
85 { "clockString", kClock_clockString, OOJS_PROP_READONLY_CB },
86 { "isAdjusting", kClock_isAdjusting, OOJS_PROP_READONLY_CB },
87 { "adjustedSeconds", kClock_adjustedSeconds, OOJS_PROP_READONLY_CB },
88 { "legacy_scriptTimer", kClock_legacy_scriptTimer, OOJS_PROP_READONLY_CB },
89 { 0 }
90};
91
92static JSFunctionSpec sClockMethods[] = {
93 // JS name Function min args
94 { "toString", JSClockToString, 0 },
95 { "clockStringForTime", ClockClockStringForTime, 1 },
96 { "addSeconds", ClockAddSeconds, 1 },
97 { 0 }
98};
99
100void InitOOJSClock(JSContext* context, JSObject* global)
101{
102 JSObject* clockPrototype = JS_InitClass(context, global, NULL, &sClockClass, OOJSUnconstructableConstruct, 0, sClockProperties, sClockMethods, NULL, NULL);
103 JS_DefineObject(context, global, "clock", &sClockClass, clockPrototype, OOJS_PROP_READONLY);
104}
105
106static JSBool ClockGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value)
107{
108 if (!JSID_IS_INT(propID))
109 return YES;
110
111 OOJS_NATIVE_ENTER(context)
112
114 double clockTime;
115
116 clockTime = [player clockTime];
117
118 switch (JSID_TO_INT(propID)) {
120 return JS_NewNumberValue(context, [UNIVERSE getTime], value);
121
122 case kClock_seconds:
123 return JS_NewNumberValue(context, clockTime, value);
124
125 case kClock_minutes:
126 return JS_NewNumberValue(context, floor(clockTime / 60.0), value);
127
128 case kClock_hours:
129 return JS_NewNumberValue(context, floor(clockTime / 3600.0), value);
130 return YES;
131
133 *value = INT_TO_JSVAL(fmod(clockTime, 60.0));
134 return YES;
135
137 *value = INT_TO_JSVAL(fmod(floor(clockTime / 60.0), 60.0));
138 return YES;
139
141 *value = INT_TO_JSVAL(fmod(floor(clockTime / 3600.0), 24.0));
142 return YES;
143
144 case kClock_days:
146 *value = INT_TO_JSVAL(floor(clockTime / 86400.0));
147 return YES;
148
150 *value = OOJSValueFromNativeObject(context, [player dial_clock]);
151 return YES;
152
154 *value = OOJSValueFromBOOL([player clockAdjusting]);
155 return YES;
156
158 return JS_NewNumberValue(context, [player clockTimeAdjusted], value);
159
161 OOStandardsDeprecated(@"The legacy_scriptTimer property is deprecated");
162 return JS_NewNumberValue(context, [player scriptTimer], value);
163
164 default:
165 OOJSReportBadPropertySelector(context, this, propID, sClockProperties);
166 return NO;
167 }
168
170}
171
172// *** Methods ***
173
174// toString() : String
175static JSBool JSClockToString(JSContext* context, uintN argc, jsval* vp)
176{
177 OOJS_NATIVE_ENTER(context)
178
180
182}
183
184// clockStringForTime(time : Number) : String
185static JSBool ClockClockStringForTime(JSContext* context, uintN argc, jsval* vp)
186{
187 OOJS_NATIVE_ENTER(context)
188
189 double time;
190
191 if (EXPECT_NOT(argc < 1 || !JS_ValueToNumber(context, OOJS_ARGV[0], &time))) {
192 jsval arg = JSVAL_VOID;
193 if (argc > 0)
194 arg = OOJS_ARGV[0];
195 OOJSReportBadArguments(context, @"Clock", @"clockStringForTime", 1, &arg, nil, @"number");
196 return NO;
197 }
198
200
202}
203
204// addSeconds(seconds : Number) : String
205static JSBool ClockAddSeconds(JSContext* context, uintN argc, jsval* vp)
206{
207 OOJS_NATIVE_ENTER(context)
208
209 double time;
210 const double kMaxTime = 30.0 * 24.0 * 3600.0; // 30 days
211
212 if (EXPECT_NOT(argc < 1 || !JS_ValueToNumber(context, OOJS_ARGV[0], &time))) {
213 jsval arg = JSVAL_VOID;
214 if (argc > 0)
215 arg = OOJS_ARGV[0];
216 OOJSReportBadArguments(context, @"Clock", @"addSeconds", 1, &arg, nil, @"number");
217 return NO;
218 }
219
220 if (time > kMaxTime || time < 1.0 || !isfinite(time)) {
222 }
223
225
226 OOJS_RETURN_BOOL(YES);
227
229}
void OOStandardsDeprecated(NSString *message)
#define EXPECT_NOT(x)
@ kClock_absoluteSeconds
Definition OOJSClock.m:59
@ kClock_isAdjusting
Definition OOJSClock.m:69
@ kClock_daysComponent
Definition OOJSClock.m:67
@ kClock_adjustedSeconds
Definition OOJSClock.m:70
@ kClock_days
Definition OOJSClock.m:63
@ kClock_seconds
Definition OOJSClock.m:60
@ kClock_clockString
Definition OOJSClock.m:68
@ kClock_hours
Definition OOJSClock.m:62
@ kClock_secondsComponent
Definition OOJSClock.m:64
@ kClock_hoursComponent
Definition OOJSClock.m:66
@ kClock_minutes
Definition OOJSClock.m:61
@ kClock_minutesComponent
Definition OOJSClock.m:65
@ kClock_legacy_scriptTimer
Definition OOJSClock.m:71
static JSBool ClockClockStringForTime(JSContext *context, uintN argc, jsval *vp)
Definition OOJSClock.m:185
static JSBool ClockAddSeconds(JSContext *context, uintN argc, jsval *vp)
Definition OOJSClock.m:205
static JSClass sClockClass
Definition OOJSClock.m:42
static JSPropertySpec sClockProperties[]
Definition OOJSClock.m:74
void InitOOJSClock(JSContext *context, JSObject *global)
Definition OOJSClock.m:100
static JSBool ClockGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
Definition OOJSClock.m:106
static JSFunctionSpec sClockMethods[]
Definition OOJSClock.m:92
static JSBool JSClockToString(JSContext *context, uintN argc, jsval *vp)
Definition OOJSClock.m:175
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
PlayerEntity * OOPlayerForScripting(void)
Definition OOJSPlayer.m:175
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:827
void addToAdjustTime:(double seconds)