summaryrefslogtreecommitdiff
path: root/scripts/toy.groovy
blob: 9a745818a8dd592bf93b88cbe6c89b0b2f2f773f (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
//
// Toy
//
// by rascalDan
// This script is a WIP and is always likely to be so.
// All polite suggestions are welcomed :)
//
// This script, from https://ss.deviatenow.com, is protected by this licence :
// CC by-NC, see http://creativecommons.org/licenses/by-nc/3.0/
//
setInfos(9, "Toy", "Become my new plaything.", "rascalDan", "WIP", 0xFFFFFF, "en", ["bondage", "femaledom", "formale", "long", "pain", "toys", "joi"]);
final VERSION = 1;
final DAY = 86400;
final HOUR = 3600.0;
final soonFormatter = java.time.format.DateTimeFormatter.ofPattern("EEEE 'at' h:mma");
// Notes
// toys.<name> ankle_cuffs, ballgag, blindfold, chastity_belt, clothespins, cockring, dog_collar, handcuffs, hood, nipple_clamps, ring_gag
final BALLGAG = "ballgag", COLLAR = "dog_collar", CLAMPS = "nipple_clamps", CHASTITY = "chastity_belt", HANDCUFFS = "handcuffs";
// fetish.<name>
final BONDAGE = "bondage", CBT = "cbt", CHORES = "chores", PAIN = "pain";
// toy.permission.<name>
final CUM = "cum", EDGE = "edge"
final PERMIT = "permit";
final PLAY = "play";
// toy.state.<name>
final CHASTE = "chaste", COLLARED = "collared", CUFFED = "cuffed", CLAMPED = "clamped", GAGGED = "gagged", NAKED = "naked";
final REDRESS = "redress";
final TOYTOYS = [CUFFED, COLLARED, CLAMPED, GAGGED];
final RELEASEFROM = "releaseFrom";
final LUNCH = "lunch", SHOPPING = "shopping", PARTY = "party", SLEEPING = "sleeping";
final BEDTIME = "bedtime", WAKEUP = "wakeup";
final BEGINPLAN = "beginPlan", ENDPLAN = "endPlan";
final FRIENDS = [ "Tori", "Sophie", "Krystal" ];
// toy.position
final KNEELING = "kneeling", ALLFOURS = "allfours", STANDING = "standing";
def OWNER = null;
final SITTER = "sarah-james";
final SOFT = "soft";

// Imagery
final DRESSED = "dressed", TITS = "tits", PUSSY = "pussy", LINGERIE = "lingerie", TEASE = "tease", SIT = "sit", BOOTS = "boots", KNEEL = "kneel", STOOD = "stood", MEAN = "mean", CROP = "crop", ASS = "ass", SQUAT = "squat", SSSH = "sssh";
final nDRESSED = "!$DRESSED", nTITS = "!$TITS", nTEASE = "!$TEASE";
final IMAGEDATA = {
	return new File(getDataFolder() + "/images/toy")
		.listFiles()
		.findAll { d -> d.isDirectory(); }
		.collect { d ->
			[
				domme: d.getName(),
				sets: d.listFiles()
					.findAll { s -> s.isDirectory(); }
					.collect { s -> [ sub: s, f: new File(s.getPath() + "/tags")] }
					.findAll { s -> s.f.canRead() }
					.collect { s ->
						[
							set: s.sub.getName(),
							images: s.f
								.readLines()
								.collect { l ->
									def fields = l.split(":");
									return [
										image: fields[0],
										tags: fields[1]
											.split(",")
											.findAll { t -> !t.isEmpty() }
									];
								}
						]
					}
			]
		};
}();
def selectImage = { domme, set, spec ->
	def meets = { i ->
		return spec.every({ s ->
			return (s[0] == "!") ? i.tags.indexOf(s.drop(1)) == -1 : i.tags.indexOf(s) != -1;
		});
	};
	def matches = IMAGEDATA
		.find({ d -> d.domme == domme });
	if (!matches) return null;
	matches = matches
		.sets.find({ s -> s.set == set });
	if (!matches) return null;
	matches = matches
		.images.findAll({ i -> meets(i) });
	if (matches.isEmpty()) return null;
	return matches[getRandom(matches.size())];
};
def showImage = { spec ->
	def outfit = loadString("toy.owner.outfit");
	def image = selectImage(OWNER, outfit, spec);
	if (image) {
		setImage("toy/$OWNER/$outfit/${image.image}.jpg");
		return image.tags;
	}
	else {
		setImage(null);
		return null;
	}
};
def showLounge = {
	setImage("toy/$OWNER/lounge.jpg");
};
def selectImageSet = { domme, specs ->
	def matches = IMAGEDATA
		.find({ d -> d.domme == domme });
	if (!matches) return null;
	matches = matches.sets.findAll({ s -> specs.every({ spec -> selectImage(domme, s.set, spec)})});
	return matches[getRandom(matches.size())];
};
def dress = { specs ->
	def outfit = loadString("toy.owner.outfit");
	def outfitTime = loadInteger("toy.owner.outfitTime");
	if (outfitTime > getTime() - 7200) { // Recent, check
		def prev = IMAGEDATA
			.find({ d -> d.domme == OWNER });
		if (prev) {
			prev = prev.sets.find({ s -> s.set == outfit });
			if (prev && specs.every({ spec -> selectImage(OWNER, prev.set, spec)})) {
				return outfit;
			}
		}
	}
	outfit = selectImageSet(OWNER, specs);
	if (!outfit) {
		showPopup("No outfit for $OWNER : $specs");
		save("toy.owner.outfit", null);
		save("toy.owner.outfitTime", null);
		return;
	}
	save("toy.owner.outfit", outfit.set);
	save("toy.owner.outfitTime", getTime());
};
// Utils
def localTimeOffset = {
	return java.time.ZoneId.systemDefault()
		.getRules()
		.getOffset(java.time.Instant.now())
		.getTotalSeconds();
};
def localTimeOf = { s ->
	java.time.LocalDateTime.ofInstant(
			java.time.Instant.ofEpochSecond(s), java.time.ZoneId.systemDefault())
}
def localTime = {
	// A float between 4.0 (4am) and 28.0 (4am)
	def wallclock = ((getTime() + localTimeOffset()) % DAY) / HOUR;
	if (wallclock < 4) {
		wallclock += 24;
	}
	return wallclock;
};
def getDay = { (int)(getTime() / DAY) * DAY };
def has = {i -> loadBoolean("toys.$i") == true};
def likes = {i -> loadBoolean("fetish.$i") == true};
def can = {i -> loadBoolean("toy.permission.$i") == true};
def givePermission = {i -> save("toy.permission.$i", true)};
def revokePermission = {i -> save("toy.permission.$i", false)};
def is = {i -> loadBoolean("toy.state.$i") == true};
def set = {i, s -> save("toy.state.$i", s)};
def positioned = { i -> loadString("toy.position") == i };
def getPunish = { loadInteger("toy.punishment") ?: 0; };
def adjustPunish = { p -> save("toy.punishment", Math.max(getPunish() + (int)p, 0)); };
def getAway = { loadString("toy.owner.away") };
def setAway = { a -> save("toy.owner.away", a) };
def namedEvents;
def loadEvents = {
	return (loadMap("toy.events") ?: [:]);
}
def saveEvents = { events ->
	save("toy.events", events);
}
def nextEvent = { events ->
	def first = null;
	events.each{ k, v ->
		if (!first || first.event.time > v.time) {
			first = [ name: k, event: v ];
		}
	};
	return first;
};
def setEvent = { events, name, time, func, arg = null ->
	if (namedEvents.containsKey(func)) {
		events[name] = [
			time: time,
			func: func,
			arg: arg
		];
		saveEvents(events);
		return true;
	}
	else {
		showPopup("No such event $func");
		return false;
	}
};
def addEvent = { name, time, func, arg = null ->
	def events = loadEvents();
	return setEvent(events, name, time, func, arg);
}
def addEventIfMissing = { name, time, func, arg = null ->
	def events = loadEvents();
	if (!events.containsKey(name)) {
		return setEvent(events, name, time, func, arg);
	}
};
def removeEvent = { name ->
	def events = loadEvents();
	events.remove(name);
	saveEvents(events);
};
def execEvents = { rt ->
	for (def e = nextEvent(loadEvents()); e && e.event.time <= getTime(); e = nextEvent(loadEvents())) {
		def f = namedEvents[e.event.func];
		removeEvent(e.name);
		if (f) {
			f(e.name, e.event.arg, e.event.time, rt);
		}
	}
};
def sessionAborted = null;
def gagText = { t, p ->
	if (!is(GAGGED)) return t;
	return t.split(/\s+/)
		.collect {
			'm' * getRandom((int)Math.max(1.0, it.length() * 1.0)) +
			'p' * getRandom((int)Math.max(1.0, it.length() * 0.4)) +
			'h' * getRandom((int)Math.max(1.0, it.length() * 0.8))
		}
	.join(" ")
		.capitalize() + " ($p)";
};
def showButtonG = { s, p, t = null ->
	return t != null ? showButton(gagText(s, p), t) : showButton(gagText(s, p));
};
def present = { imageSpec, texts ->
	if (texts) {
		show(texts.collect {
			t -> t[getRandom(t.size())]
		}.join(" ").capitalize());
	}
	if (imageSpec) {
		return showImage(imageSpec);
	}
	return null;
};
def showButtonGT = { s, p, t, pc ->
	def tt = showButton(gagText(s, p), t);
	if (tt == t) {
		present(null, [
				["Don't keep me waiting!", "Hurry up!", "Quicker, toy!"]]);
		def pt = showButton(gagText(s, p));
		adjustPunish(pt * pc);
		return pt + tt;
	}
	return tt;
};

def harden = { imageSpec ->
	present(imageSpec, [
			["I want to see you hard.", "I need you hard now, very hard."],
			["Stroke it, slowly...", "Slow worship strokes..."],
			["don't edge...", "no edging..."],
			["and do NOT cum.", "and definitely no cumming."],
			["Not yet.", "Maybe soon."]]);
	playBackgroundSound("toy/90bpm.mp3", 2); //45sec
	if (showButtonG("Hard, mistress", "hard", 60) == 60) {
		playBackgroundSound(null);
		present(imageSpec, [
				["What's taking so long!?", "Come on!", "Don't disappoint me."],
				["Slap it about a bit!.", "Get it hard, now!", "Pinch your nipples."]]);
		playBackgroundSound("toy/165bpm.mp3");
		if (showButtonG("Hard, mistress", "hard", 30) == 30) {
			playBackgroundSound("toy/180bpm.mp3");
			if (showButtonG("Hard, mistress", "hard", 30) == 30) {
				sessionAborted = SOFT;
				present(imageSpec, [
						["Bah!", "Pathetic!"],
						["That's no good to me!", "How do I have fun with that!?"]]);
				adjustPunish(100);
				showButtonG("Sorry, mistress", "sorry");
			}
		}
	}
	playBackgroundSound(null);
};
def expose = { imageSpec ->
	if (!is(NAKED)) {
		present(imageSpec, [
				["Get that cock out, toy;", "Let's see that cock of mine."],
				["I want to torment it.", "It's playtime!"]]);
		wait(10);
	}
};
// Return whether toy came or not
def mightCum = { time, reason = "without permission" ->
	if (showButtonG("Sorry, mistress, I'm cumming $reason", "cumming", time) < time) {
		playBackgroundSound(null);
		sessionAborted = CUM;
		present(null, [
				["Bah!", "Pfft. I'm very disappointed in you."],
				["Ruin it.", "Don't touch it."]]);
		adjustPunish(100);
		wait(10);
		present([DRESSED], [["Clean up your mess!"]]);
		showButtonG("Yes, mistress", "ok");
		wait(10);
		showButtonG("Cleaned up, mistress, back", "back");
		return true;
	}
	return false;
};
// Return whether toy came or not, despite being denied
def cumChanceDenied = {
	present([TEASE], [
			["Stop!", "Nope!", "Haha! No!"],
			["Let go.", "Hands off!", "Hands behind your back."],
			["Not this time.", "Maaaaybe next time, toy."]]);
	return mightCum(15);
};
def cumChanceCountdown = {
	present([TEASE], [
			["OK, toy, I'm going to count you down.", "Would you like a countdown, toy?"],
			["Get stroking!", "Jerk it!"]]);
	if (mightCum(5 + getRandom(5))) return true;
	present([TITS], []);
	def numbers = "";
	for (def n = 10; n > 0; n--) {
		numbers += "${n}... ";
		show(numbers);
		mightCum(1, "too soon");
		if (getRandom(100) > 85 || !can(CUM)) {
			return cumChanceDenied();
		}
	}
	present([TITS], [
			["Cum, cum", "Cum for me"],
			["you little slut.", "my lucky toy.", "you dirty boy."]]);
	wait(10);
	showButtonG("Thank you, mistress.", "ok");
	return true;
};
def cumChanceWindow = {
	present([TEASE], [
			["OK, toy, I'm going to give you a chance to cum.", "Would you like a small chance to cum, toy?"],
			["Get stroking!", "Stroke it, you'll need to be close!"],
			["You won't get long.", "You might not get long.", "Wait for my say so..."]]);
	if (mightCum(15 + getRandom(60), "too soon")) return true;
	if (getRandom(5) > 0 || !can(CUM)) {
		return cumChanceDenied();
	}
	present([TITS], [
			["Cum, cum", "Cum for me"],
			["you little slut.", "my lucky toy.", "you dirty boy."]]);
	def w = 4 + getRandom(4);
	if (showButtonG("Cumming, mistress", "cumming", w) == w) {
		w = 3;
		present(null, [
				["Quickly now!", "Hurry!"]]);
		if (showButtonG("Cumming, mistress", "cumming", w) == w) {
			present([TEASE], [
					["Stop!", "Let go.", "Hands off!"],
					["Time's up!", "You had your chance."]]);
			return mightCum(15, "too late");
		}
	}
	wait(10);
	showButtonG("Thank you, mistress.", "ok");
	return true;
};
def cumChanceRuin = {
	present([TEASE], [
			["Stroke!", "Jerk it."],
			["Get close to the edge, toy...", "Get yourself close..."]]);
	if (mightCum(5 + getRandom(10), "too soon")) return true;
	if (getRandom(5) > 0 || !can(CUM)) {
		return cumChanceDenied();
	}
	for (def n = 3 + getRandom(6); n >= 0; n--) {
		present([TITS], [
				["Try to cum!"]]);
		def w = 3 + getRandom(8);
		if (showButtonG("Cumming, mistress", "cumming", w) < w) {
			present([TEASE], [
					["Hands off!", "Leave it."],
					["Let it all ooze out.", "You're not getting a proper release."]]);
			wait(10);
			showButtonG("Thank you, mistress.", "ok");
			return true;
		}
		if (n > 0) {
			present([TITS], [
					["Stop!", "Not now!", "Wait!"],
					["Hands off!", "Hands by your side."]]);
			w = (w - 2) + getRandom(8);
			if (mightCum(w, "but it's ruined")) return true;
		}
	}
	return cumChanceDenied();
};
def edge = { amount, imageSpec ->
	def allowedTime = 128;
	(getRandom(amount) + 2).times {
		if (sessionAborted) return;
		present(imageSpec, [
				["Stroke to edge now, toy...", "Edge!"],
				["Don't cum.", "No cumming.", "No accidents though."]]);
		showButtonGT("Edging, mistress", "edging", allowedTime + 20, 1);
		if (getRandom(2) == 1) {
			present(imageSpec, [
					["Hold it...", "And hold...", "Keeping going..."]]);
			if (mightCum(getRandom(20) + 5)) return;
		}
		present(imageSpec, [
				["Hands off!", "Stop!"]]);
		if (mightCum(getRandom(5) + 5)) return;
		allowedTime /= 2;
	};
};
def strokes = { amount, imageSpec ->
	present(imageSpec, [
			["Stroke to the beat, toy.", "Follow the beat.", "Beat it!"],
			["No cumming unless I say so...", "Don't cum without my say so,"],
			["Tell me if you get too close.", "so tell me if you're edging.", "I don't want any messes."]]);
	final BEATS = [
		// bpm:30, len:60 ],
		[ bpm:90, len:45 ],
		[ bpm:165, len:30 ],
		[ bpm:180, len:30 ],
		[ bpm:240, len:20 ],
		];
	def sto = loadInteger("toy.strokeTeaseOffset");
	def edges = 0;
	(getRandom(amount) + 5).times {
		if (sessionAborted) return;
		def beatNo = getRandom(BEATS.size());
		def beat = BEATS[beatNo];
		playBackgroundSound("toy/${beat.bpm}bpm.mp3", 10); // Lots, to cover offset
		def len = getRandom(Math.max(1, beat.len + (sto * beatNo)));
		if (showButtonG("Edging, mistress", "edging", len) < len) {
			sto -= 1;
			edges += 1;
			playBackgroundSound(null);
			switch (getRandom(3)) {
				case 0:
					present(imageSpec, [
							["Stop!", "Let go!", "Hands off!"]]);
					if (mightCum(getRandom(8) + 10)) return;
					break;
				case 1:
					present(imageSpec, [
							["Careful now...", "No accidents..."],
							["slow it down...", "slowly now..."]]);
					playBackgroundSound("toy/30bpm.mp3");
					if (mightCum(getRandom(20) + 5)) {
						playBackgroundSound(null);
						return;
					}
				case 2:
					switch(getSelectedValue("Would you like to cum, toy?", [
								gagText("Please, mistress, may I cum!?", "yes"),
								gagText("No, mistress, please torment me more!", "no")
					])) {
						case 0:
							if (can(CUM)) {
								present(imageSpec, [
										["Maybe...", "Perhaps.", "We'll see."]]);
							}
							else {
								present(imageSpec, [
										["No you may not!", "Nope.", "Not a chance."]]);
								adjustPunish(5);
							}
							break;
						case 1:
							present(imageSpec, [
									["Good boy.", "Very well.", "OK then!"]]);
							adjustPunish(-5);
							break;
					}
					if (mightCum(getRandom(8) + 5)) {
						playBackgroundSound(null);
						return;
					}
					playBackgroundSound(null);
			}
			present(imageSpec, [
					["Back to stroking, toy...", "Get to it again...."],
					["follow the beat..."],
					["no accidents.", "and concentrate."]]);
		}
		else {
			playBackgroundSound(null);
		}
	}
	if (edges == 0) {
		sto += 1;
	}
	save("toy.strokeTeaseOffset", sto);
}
def clampPulls = { amount ->
	getRandom(1 + amount).times {
		if (getRandom(2)) {
			present([DRESSED], [
					["Take them off.", "Remove them."]]);
			wait(6 + getRandom(8));
			if (getRandom(2)) {
				present([DRESSED], [
						["And put them back on", "Put them back"],
						["right where they came from.", "where they were."]]);
			}
			else {
				present([DRESSED], [
						["Flip them", "Turn them"],
						["90 degrees", "around"],
						["and put them back.", "and replace them."]]);
			}
			wait(10 + getRandom(8));
		}
		else if (getRandom(2)) {
			present([DRESSED], [
					["Twist them"],
					["for me.", "... twist those nipples."]]);
			wait(6 + getRandom(5));
			if (getRandom(2)) {
				present(null, [
						["More!", "A little more!", "Further!"]]);
				wait(getRandom(10) + 2);
			}
		}
		present([DRESSED], [
				["Pull them tight.", "Pull them!"]]);
		wait(getRandom(10) + 5);
		if (getRandom(5) == 0) {
			present(null, [
					["Tighter!", "Harder!", "Further!"]]);
			wait(getRandom(10) + 2);
			present(null, [
					["Haha!", "Ooo, they must hurt.", "Do they hurt?"]]);
			wait(3);
		}
		present(null, [
				["Release them.", "Let them go."]]);
		wait(getRandom(5) + 3);
	};
	return amount;
};
// Pre-tease
def preRelease = {
	if (!is(CHASTE)) return;
	if (!is(NAKED)) {
		present([DRESSED], [
				["Reach down... and through your clothes...", "Stay clothed,"],
				["rub that chastity device...", "run your hand around that locked cock..."],
				["imagine if that was me doing it.", "that looks frustrating."]]);
		wait(15);
		present(null, [["Show me.", "Let me see it."]]);
		wait(10);
	}
	else {
		present([DRESSED], [
				["Look what we have here.", "Hnmm, look at that."],
				["Locked away and useless.", "Under lock and key as it should be."],
				["Too bad I have the keys.", "And I have the keys somewhere safe."]]);
		wait(10);
	}
	present([DRESSED], [
			["Play with your balls,", "Tease those balls a little,"],
			["I want to see how horny you are", "see how hard you can get"],
			["before I let you out.", "before I hand over the keys."],
			["You should be", "When you're"],
			["practically breaking out,", "about to break it for me,"],
			["we'll continue.", "then I'll see about letting it out."]]);
	showButtonG("Hard, mistress, please let me out", "hard");
	present([DRESSED,TEASE], [
			["OK then.", "Fine, fine."],
			["I'm feeling generous, toy...", "Well, it's no good to me like that..."],
			["I have the keys right here,", "I have the keys here somewhere... OK,"],
			["unlock yourself and", "take your chastity device off"],
			["let me see my cock.", "let's see it."]]);
	showButtonG("Yes, mistress", "ok");
	wait(15);
	set(CHASTE, false);
	showButtonGT("Thank you, mistress", "ok", 80, 1);
};
def preEdge = {
	if (is(CHASTE)) return;
	expose([DRESSED]);
	harden([DRESSED]);
	edge(4, [DRESSED]);
	present([DRESSED], [
			["And relax...", "Hands off..."],
			["cool down a little.", "but keep it hard for me!", "for now!"]]);
	wait(getRandom(5) + 5);
};
def preGag = {
	if (is(GAGGED)) return 1.2;
	if (!has(BALLGAG)) return;
	present([DRESSED], [
			["Go fetch your gag, toy...", "Bring me your gag, toy..."],
			["Quickly!", "Hurry back!"]]);
	showButton("Yes, mistress");
	show(null);
	wait(10);
	showButtonGT("Back, mistress", "back", 50, 2);
	present([DRESSED], [
			["Good boy.", "Very good."],
			["Put it on..."],
			["Nice and tight.", "Tight!"],
			["I don't want to a hear a word from you.", "A quiet toy is a good toy."]]);
	set(GAGGED, true);
	showButtonGT("Gagged, mistress", "gagged", 20, 2);
	return 1.2;
};
def clamps = {
	if (is(CLAMPED)) return;
	present([DRESSED],
			likes(PAIN) ? [
			["We both like to see you suffer.", "Pain is fun."],
			["Isn't that right, toy?", "Don't you agree?"]]
			: [
			["I'm sorry, toy,"],
			["but seeing you suffer is too much fun.", "but I need to find my amusement somewhere."]]);
	wait(10);
	present([DRESSED],[
			["Go put your nipple clamps on...", "I want those nipples clamped..."],
			["but on your way back...", "no walking though..."],
			["crawl, down on all fours.", "on your knees."]]);
	showButtonG("Yes, mistress", "ok");
	show(null);
	wait(10);
	set(CLAMPED, true);
	showButtonGT("Back, mistress", "back", 60, 1);
	present([DRESSED], [
			["On your knees,", "Kneel before me"],
			["let me see.", "hands behind your back."]]);
	wait(getRandom(10) + 5);
};
def clampsShow = {
	present([DRESSED,TEASE], [
			["Let me see", "Show me"],
			["those nipple clamps,", "those nasty clamps,"],
			["they look painful.", "and jiggle them about for me!"]]);
	wait(10);
};
def preClamps = {
	if (is(CLAMPED)) {
		clampsShow();
	}
	else {
		clamps();
	}
	clampPulls(getRandom(4));
	return 1.8;
};
def preCollar = {
	if (is(COLLARED)) return 1.1;
	if (!has(COLLAR)) return;
	present([DRESSED], [
			["You look underdressed, toy.", "A toy without a collar doesn't look right."],
			["Go put your collar on", "I want a collar around your neck"],
			["and crawl back like the slutty puppy dog you are.", "and kneel before me."]]);
	showButtonG("Yes, mistress", "ok");
	show(null);
	wait(10);
	set(COLLARED, true);
	showButtonGT("Back, mistress", "back", 60, 1);
	return 1.1;
};
def preStrip = { imageSpec = [DRESSED] ->
	if (is(NAKED)) return 1.1;
	present(imageSpec, [
			["Clothes off!", "Get naked, slut!"]]);
	wait(10);
	present(imageSpec, [
			["All of them off!", "And the rest!", "Keep going."]]);
	set(NAKED, true);
	showButtonGT("Naked, mistress", "naked", 60, 5);
	present(null, [
			["Very good. Now...", "I see..."],
			["on your knees before me,", "kneel,", "stand up straight,", "on your feet,"],
			["hands behind your head,", "arms up,", "hands high up,"],
			["let me see you properly.", "let's see what we have.", "I want a good view."]]);
	wait(10);
	return 1.1;
};

// Post
def postEdge = {
	if (is(CHASTE)) return;
	if (!can(EDGE)) return;
	edge(6, [TEASE]);
};
def postCum = {
	if (is(CHASTE)) return;
	present([TEASE], [
			["How about I let you cum?", "Maybe I should let you cum."],
			["Would you like that?", "Let some of that frustration out?"]]);
	wait(getRandom(5) + 5);
	def ways = [cumChanceRuin, cumChanceWindow, cumChanceCountdown];
	if (ways[getRandom(ways.size())]()) {
		// Once toy has cum, revoke permission for a while
		revokePermission(CUM);
	}
};
def chastity = { pre ->
	if (is(CHASTE)) return;
	if (!has(CHASTITY)) return;
	if (pre) {
		present([DRESSED,TEASE], [
				["Fun time is over for you.", "You'd have enough pleasure lately, my turn."]]);
	}
	else {
		present([TEASE], [
				["I can't keep an eye on you all time.", "It's not that I don't trust you."],
				["But...", "I just like to be sure."]]);
	}
	wait(getRandom(5) + 10);
	present([DRESSED,TEASE], [
			["Go to your room,", "Run along now,"],
			["get your chastity device and"],
			["lock it on.", "get that cock secured away."],
			["Bring back the key.", "I want the key of course."]]);
	wait(10);
	show(null);
	showButtonGT("Locked, mistress. Here's the key.", "locked", 180, 1);
	set(CHASTE, true);
	present([DRESSED,TEASE], [
			["Good boy.", "Thank you, toy."]]);
	wait(5);
}
def preChastity = {
	chastity(true);
}
def postChastity = {
	chastity(false);
};

// Play
def playStrokes = {
	preRelease();
	harden([TITS]);
	strokes(15, [TITS]);
};
def playEdges = {
	preRelease();
	harden([TITS]);
	edge(6, [TITS]);
};
def playNothing = {
	(getRandom(3) + 2).times {
		def tags = showImage([TITS]).intersect(
				[ASS, CROP, BOOTS, PUSSY, SSSH]);
		Collections.shuffle(tags);
		if (tags.isEmpty()) {
			show(null);
		}
		else {
			switch (tags[0]) {
				case ASS:
					if (!is(GAGGED))
						present(null, [
								["Come closer,", "Don't be shy,"],
								["kiss it!", "two kisses on each cheek."]]);
					break;
				case CROP:
					present(null, [
							["You know what this is for?", "Good boys get rewards, bad ones getting a beating.", "Would you like your ass a darker shade?"]]);
					break;
				case BOOTS:
					if (is(GAGGED))
						present(null, [
								["I know you'd like to worship them.", "Too bad that tongue can't be put to good use."]]);
					else
						break;
				case PUSSY:
					if (is(GAGGED))
						present(null, [
								["I know you'd like to pleasure me.", "Too bad that tongue can't be put to good use."]]);
					else
						present(null, [
								["How about you start licking it?", "How much would like your tongue in there?"]]);
					break;
				case SSSH:
					present(null, [
							["Quiet now.", "Sssssh.", "Patience."]]);
					break;
				default:
					show(null);
			}
		}
		wait(getRandom(10) + 10);
	};
};
def playCbt = {
	present([DRESSED], [
			["Go to the kitchen...", "From the kitchen..."],
			["bring me", "get yourself"],
			["a wooden spoon or similar."]]);
	showButtonG("Yes, mistress", "ok");
	wait(5);
	final BEATS = [
		// bpm:30, len:60 ],
		[ bpm:90, len:45 ],
		[ bpm:165, len:30 ],
		[ bpm:180, len:30 ],
		[ bpm:240, len:20 ],
		];
	showButtonGT("Back, mistress", "back", 20, 1);
	present([DRESSED], [
			["Good.", "Right."],
			["Take those balls in one hand", "In one hand, hold your balls"],
			["and in the other, get ready to beat them.", "and get ready to beat them with other."]]);
	showButtonGT("Yes, mistress", "ok", 10, 1);
	present([DRESSED], [
			["You concentrate on beating,", "Don't worry,"],
			["I'll be keeping count.", "I'll count them for you."]]);
	wait(3);
	present(null, [["Ready"]]);
	wait(1);
	present(null, [["Set"]]);
	wait(1);
	present(null, [["Go!"]]);
	def beats = 0;
	(4 + getRandom(7)).times({
		def beatNo = getRandom(BEATS.size());
		def beat = BEATS[beatNo];
		def len = getRandom(beat.len - 5) + 5;
		playBackgroundSound("toy/${beat.bpm}bpm.mp3");
		wait(len);
		playBackgroundSound(null);
		beats += (int)((beat.bpm * len) / 60.0);
		present(null, [
				["$beats.", "That's $beats.", "$beats so far."],
				["Keep going.", "Don't stop!", "Just a few more.", "Harder!"]]);
	});
	present([DRESSED], [
			["Stop!", "Ok, you can stop now."],
			["They look a little sore.", "Is that a nice tingly feeling?"]]);
	showButtonG("Thank you, mistress.", "ok");
	show(null);
	wait(5);
	return beats / 10.0;
};
def playKneel = {
	present([DRESSED], [
			["Right, slut.", "Come here, toy."],
			["On the floor, right there.", "Kneel at my feet."]]);
	showButtonGT("Yes, mistress.", "ok", 20, 1);
	present([DRESSED], [
			["When I say go,", "In a moment,"],
			["you'll crawl", "turn and crawl"],
			["away until I ring my bell."],
			["You'll stay there,", "And then you won't move,"],
			["head bowed", "face down"],
			["until I call you back.", "until you're summoned."]]);
	showButtonG("Yes, mistress.", "ok");
	present(null, [["Wait..."]]);
	wait(getRandom(10) + 10);
	present([DRESSED], [["Ok, go!", "Now! Off you go."]]);
	wait(getRandom(5) + 5);
	playBackgroundSound("bell.wav");
	show(null);
	def kneelTime = 60 + getRandom(240);
	wait(kneelTime);
	while (playBackgroundSound("bell.wav") || true) {
		if (showButtonG("Back, mistress", "back", 20) != 20) break;
	}
	present([DRESSED], [
			["Good boy.", "Very good."],
			["Stay down there.", "Don't move."]]);
	wait(getRandom(5) + 5);
	return kneelTime / 20.0;
};
def playBondage = {
	if (!has(HANDCUFFS)) return;
	def prep = [ preStrip, preCollar, preClamps, preGag ];
	Collections.shuffle(prep);
	prep.collect { p -> p(); };
	present([DRESSED, nTEASE], [
			["Go fetch your handcuffs"]]);
	showButtonG("Yes, mistress", "ok");
	wait(10);
	showButtonGT("Back, mistress", "back", 60, 1);
	present([DRESSED, nTEASE], [
			["Good."],
			["Down on the floor.", "On your knees.", "Down there, where you are."]]);
	wait(15);
	present([DRESSED, nTEASE], [
			["You know what's next.", "Take your cuffs."],
			["Cuff yourself,", "Hands cuffed,"],
			["behind your back of course", "behind you."]]);
	wait(15);
	present([DRESSED, TEASE], [
			["Don't go anywhere!", "Do not move!"]]);
	wait(15);
	present([STOOD], [
			["I'll be back for you soon.", "I won't leave you too long."]]);
	wait(15);
	setImage(null);
	show(null);
	def waitTime = 500 + getRandom(1000);
	wait(waitTime);
	playBackgroundSound("bell.wav");
	present([DRESSED, nTEASE], [
			["Hi slave!", "Hello again."],
			["You don't look very comfortable.", "That looks quite awkward."],
			["Maybe you've been there long enough.", "It's been a while now."]]);
	wait(20);
	present([DRESSED, TEASE], [
			["OK,", "Mmmm,"],
			["soon.", "just a while longer."]]);
	wait(60 + getRandom(60));
	playBackgroundSound("bell.wav");
	present([DRESSED, TEASE], [
			["Go find the keys,", "Very well,"],
			["let yourself out,", "unlock the cuffs,"],
			["and come back.", "and hurry back."]]);
	wait(10);
	setImage(null);
	showButtonGT("Freed, mistress", "freed", 120, 1);
	return waitTime;
};
def playClamps = {
	if (!has(CLAMPS)) return;
	if (!is(CLAMPED)) clamps();
	clampsShow();
	return clampPulls(3 + getRandom(10));
};
def intClamps = {
	if (!has(CLAMPS)) return;
	if (!is(CLAMPED)) return;
	clampsShow();
	return clampPulls(getRandom(5));
};
def intSqueeze = {
	(2 + getRandom(4)).times { n ->
		if (n > 0) {
			present([DRESSED,TEASE], [
					["And again,", "Again,", "Once more,"],
					["harder!", "tighter!"]]);
		}
		else {
			present([DRESSED,TEASE], [
					["Squeeze yours balls", "Grab your balls and squeeze them"],
					["good and tight", "firm and hard"],
					["until they hurt a little.", "as if I was doing it."]]);
		}
		wait(3 + getRandom(6));
		present(null, [
				["Let them go.", "Hands off."]]);
		wait(3 + getRandom(6));
	};
};
def stateToyName = { state ->
	switch (state) {
		case CUFFED:
			return "cuffs";
		case COLLARED:
			return "collar";
		case CLAMPED:
			return "nipple clamps";
		case GAGGED:
			return "gag";
	}
	return null;
};
def removeToy = { toy, imageSpec ->
	if (!is(toy)) return;
	def toyName = stateToyName(toy);
	present(imageSpec, [
			["OK,"],
			["you may remove your $toyName.", "the $toyName can come off.", "take the $toyName off."]]);
	showButtonG("Thank you, mistress", "ok");
	wait(5);
	set(toy, false);
	removeEvent("$RELEASEFROM-$toy");
	showButtonGT("Removed, mistress", "removed", 20, 1);
};
// Session
def sessionSummon = { imageSpec ->
	final limit = 5;
	final timeMax = 10;

	for (def n = 1; n <= limit; n++) {
		switch (n) {
			case limit:
				present(imageSpec, [
						["TOY!", "SLAVE!", "SLUT!"],
						["Last chance!", "Final warning!"]]);
				adjustPunish(5);
				break;
			case limit - 1:
			case limit - 2:
				present(imageSpec, [
						["Toy!", "Slave!", "Slut!"],
						["Where are you!?", "Get yourself here, now!"]]);
				break;
			default:
				present(imageSpec, [
						["Toy!", "Slave!", "Slut!"]]);
				break;
		}
		playBackgroundSound(n == limit ? "shortwhip.wav" : "bell.wav");
		if (showButton("Yes, Miss?", timeMax) < timeMax) {
			return true;
		};
	}
	showLounge();
	show(null);
	adjustPunish(5);
	return false;
};
def sessionPlay = {
	def ph = (float)1.0;
	def pm = { val ->
		if (val) {
			ph *= val;
		}
	};
	def ap = { val ->
		if (val) {
			adjustPunish(-val * ph);
		}
	};
	def apply = { activitity, use = null ->
		if (!activitity) return;
		def val = (Float)activitity();
		if (use) {
			use(val);
		}
	};
	def interval = {
		def activities = [null, intClamps, intSqueeze];
		apply(activities[getRandom(activities.size())], null);
	};
	def playRepeat = { amount, activities, use = null ->
		amount.times {
			if (!sessionAborted) {
				apply(activities[getRandom(activities.size())], use);
			}
		};
	};
	def playTake = { amount, activities, use = null ->
		Collections.shuffle(activities);
		activities.take(amount).each {
			if (!sessionAborted) {
				apply(it, use);
			}
		};
	};
	def cp = getPunish();
	if (cp > 50) {
		// Punish only: bad toy
		preChastity();
		def pr = 1 + getRandom(2 + (int)(cp / 100));
		playRepeat(pr, [preGag, preClamps, preCollar, preStrip], pm);
		playRepeat(pr, [playKneel, playCbt, playClamps, playNothing], ap);
		playTake(pr, [playBondage, playCbt, playClamps]);
	}
	else {
		// Sub pleasure: good toy
		playRepeat(1 + getRandom(3), [preRelease, preEdge, preGag, preClamps, preCollar, preStrip]);
		playRepeat(1 + getRandom(3), [playStrokes, playEdges], interval);
		playTake(getRandom(3), [postCum, postChastity]);
		if (!sessionAborted && !can(CUM)) {
			addEventIfMissing(CUM, getTime() + (DAY * 2) + getRandom(DAY * 2), PERMIT, CUM); // 2 - 4 days from now
		}
	}
};
def sessionRelease = { goodToy ->
	if (goodToy) {
		present([DRESSED], [
				["Ahhh,", "OK,"],
				["I'm done with you for now.", "That was fun... for me at least."]]);
	}
	else {
		present([DRESSED], [
				["I don't ask much of you, toy."]]);
		wait(getRandom(10) + 5);
		postChastity();
	}
	wait(getRandom(10) + 5);
	def toys = TOYTOYS.findAll { t -> is(t) };
	Collections.shuffle(toys);
	if (toys) {
		if (!goodToy) {
			present([DRESSED], [
					["I should leave you like that.", "I suppose you still want letting free?"],
					["Maybe.", "Let me ponder on it."]]);
			wait(getRandom(20) + 20);
		}
		toys.each { toy ->
			if (getRandom(100) > ((goodToy && (getPunish() < 100)) ? 10 : 80)) {
				removeToy(toy, [DRESSED]);
			}
			else {
				addEvent("$RELEASEFROM-$toy", getTime() + 100 + getRandom(10 * (getPunish() + 1)),
						RELEASEFROM, toy);
				adjustPunish(-5);
			}
		};
	}
	if (is(NAKED)) {
		if (goodToy && getRandom(1)) {
			present([DRESSED], [
					["Put some clothes back on.", "Cover yourself up!"]]);
			wait(getRandom(10) + 15);
			set(NAKED, false);
		}
		else {
			present([DRESSED], [
					["No clothes now for a while,", "Keep those clothes off,"],
					["you don't get clothes until I say.", "I like seeing you naked."]]);
			addEvent(REDRESS, getTime() + 600 + getRandom(1200), "redress");
			showButtonG("Yes, mistress", "ok");
		}
	}
	if (goodToy) {
		present([DRESSED], [
				["Off you go.", "You may leave."]]);
	}
	else {
		present([DRESSED], [
				["Get out of my sight.", "Go!"]]);
		removeEvent(CUM);
		revokePermission(CUM);
	}
	wait(10);
	showLounge();
	sessionAborted = null;
	if (goodToy) {
		adjustPunish(-20);
	}
	show(null);
};
def playWait = {
	present([DRESSED,nTEASE], [
			["Hello,", "Hi,", "Hey there,"],
			["toy.", "slut.", "slave."],
			["On your knees,", "On the floor,", "Down... at my feet,",],
			["wait there.", "wait patiently.", "don't move."],
			["I'll be with you shortly.", "I won't be a moment."]]);
	showButtonGT("Yes, mistress", "ok", 10, 1);
	showLounge();
	show(null);
	wait(20 + getRandom(20));
};
def playSchedule = { lastPlay ->
	if (getAway()) return;
	if (lastPlay < getTime() - (4 * HOUR)) {
		addEventIfMissing(PLAY, getTime() + 120 + getRandom(480), PLAY, true); // 2-10mins
	}
	else {
		addEventIfMissing(PLAY, getTime() + 1200 + getRandom(5400), PLAY, false); // 20-90mins
	}
};
def playEvent = { rt = true, first = true ->
	if (!rt || getAway()) return;
	dress([[DRESSED,nTEASE],[DRESSED,TEASE],[TITS],[STOOD]]);
	if (sessionSummon([DRESSED,nTEASE])) {
		if (first.asBoolean()) {
			playWait();
		}
		sessionPlay();
		sessionRelease(sessionAborted == null);
		save("toy.lastPlay", getTime());
	}
	else {
		adjustPunish(25);
	}
	playSchedule(getTime());
	showLounge();
}
def sleepSchedule = {
	def t = (int)(getDay() + localTimeOffset() + (25.1 * HOUR) + getRandom((int)HOUR));
	addEventIfMissing(SLEEPING, t, BEDTIME);
};
def bedtime = { rt, schedTime ->
	dress([[LINGERIE,nTITS]]);
	if (rt && sessionSummon([LINGERIE,nTITS])) {
		preStrip([LINGERIE,nTITS]);
		def toys = TOYTOYS.findAll { t -> is(t) };
		Collections.shuffle(toys);
		toys.each { removeToy(it, [LINGERIE,nTITS]) };
		present([LINGERIE,nTITS], [
				["Very good, toy.", "OK"],
				["Bedtime!", "Off to bed with you!", "Bed! Now!"]]);
		showButtonG("Good night, mistress", "night");
	}
	// Assume toy will return dressed tomorrow
	removeEvent(REDRESS);
	set(NAKED, false);
	// Assume toy will release himself
	TOYTOYS.findAll { t -> is(t) }.each {
		removeEvent("$RELEASEFROM-$it");
		set(it, false);
	};
	setAway(SLEEPING);
	def t = schedTime + (6 * (int)HOUR) + getRandom(2 * (int)HOUR);
	addEvent(SLEEPING, t, WAKEUP);
	removeEvent(PLAY);
	if (rt) {
		exit();
	}
};
def wakeup = { rt ->
	setAway(null);
	playSchedule(loadInteger("toy.lastPlay") ?: 0);
	sleepSchedule();
};
def redress = { rt ->
	if (!rt || !sessionSummon([DRESSED])) {
		addEvent(REDRESS, getTime() + 300, "redress");
		return;
	}
	present([DRESSED], [
			["OK, toy,"],
			["you can put some clothes back on now.", "get yourself covered up."]]);
	wait(getRandom(10) + 15);
	showButtonG("Thank you, mistress", "ok");
	set(NAKED, false);
	showLounge();
};
def releaseFrom = { rt, arg, name ->
	if (!rt || !sessionSummon([DRESSED])) {
		addEvent(name, getTime() + 300, RELEASEFROM, arg);
		return;
	}
	removeToy(arg, [DRESSED]);
	showLounge();
};
def requestClothes = {
	adjustPunish(5);
	present([DRESSED], [
			["Really?", "Disappointing."],
			["You want to clothes back on?", "You wish to cover up?"]]);
	if (getBoolean(null)) {
		present([DRESSED], [["Hmm, very well.", "If you must."]]);
		adjustPunish(50);
		set(NAKED, false);
		removeEvent(REDRESS);
	}
	else {
		present([DRESSED], [["Good boy."]]);
	}
	showButtonG("Thank you, mistress", "ok");
	showLounge();
};
def requestRelease = { toy ->
	adjustPunish(5);
	present([DRESSED], [
			["Really?", "Disappointing."],
			["You wish to be un-$toy?"]]);
	if (getBoolean(null)) {
		adjustPunish(10);
		if (getRandom(100) > getPunish()) {
			present([DRESSED], [["No.", "You may not."],
					["Not yet.", "Not until later."]]);
		}
		else {
			present([DRESSED], [["Hmm, very well.", "If you must."]]);
			set(toy, false);
			removeEvent("$RELEASEFROM-$toy");
		}
	}
	else {
		present([DRESSED], [["Good boy."]]);
	}
	showButtonG("Thank you, mistress", "ok");
	showLounge();
};
// Confession
def confessChastityRelease = {
	adjustPunish(75);
	present([DRESSED, nTEASE], [
			["Bad toy!", "Naughty little slut."],
			["I don't keep it locked for nothing.", "It's locked for a reason."],
			["It better have been a good reason.", "I'll presume you had a valid excuse."],
			["Is it locked again now?", "Are you back in chastity now?"]]);
	if (getBoolean(null)) {
		present([DRESSED, nTEASE], [
				["Good!", "There's that at least."]]);
		showButtonG("Yes, mistress", "ok");
	}
	else {
		adjustPunish(300);
		present([DRESSED, nTEASE], [
				["disobedient", "dirty", "cheeky"],
				["little"],
				["slut!", "whore!", "brat!"],
				["Get it locked back up", "Back in chastity"],
				["right now!", "this instant!"]]);
		showButtonGT("Locked, mistress. Here's the key.", "locked", 180, 1);
	}
	if (getBoolean("Did you get erect whilst out?")) {
		adjustPunish(50);
		if (getBoolean("Did you cum!?")) {
			adjustPunish(200);
		}
	}
	adjustPunish(getInteger("How many hours were you out?", 1) * 10);
};
def confessStroked = {
	adjustPunish(40);
	present([DRESSED,nTEASE], [
			["Awww, poor little toy.", "Naught little toy!"],
			["You know you aren't allowed.", "You get to stroke only when I say."]]);
	showButtonG("Sorry, mistress", "sorry");
	if (getBoolean("Did you get to the edge?")) {
		adjustPunish(40);
		adjustPunish(getInteger("How many times?", 1) * 5);
		if (getBoolean("Did you cum!?")) {
			adjustPunish(200);
		}
	}
};
def confess = {
	// 5 points just for feeling the need to confess
	def op = getPunish();
	adjustPunish(5);
	dress([[DRESSED,nTEASE], [MEAN]]);
	while (true) {
		present([DRESSED, nTEASE], [
				["I'm not going to like this, am I?", "There is no way this ends well for you."],
				["Out with it.", "Let me hear it."]]);
		def opts = [ ];
		if (is(CHASTE)) {
			opts.push([lbl: "Freed from chastity", act: confessChastityRelease]);
		}
		if (!is(CHASTE)) {
			opts.push([lbl: "Stroked", act: confessStroked]);
		}
		opts.push([ lbl: "Nothing listed (phew!)", act: null ]);
		def opt = getSelectedValue(null, opts.collect { it.lbl });
		def act = opts[opt].act;
		if (!act) {
			showLounge();
			return;
		}
		act();
		if (!getBoolean("Anything else?")) break;
	}
	// If sufficient points accrued, punish immediately.. with a bonus
	if (getPunish() > 150 + op) {
		present([MEAN], [
				["I'm very disappointed in you.", "You've been a very bad toy!"],
				["Immediate punishment!", "So you will suffer... right now!"]]);
		adjustPunish(100);
		showButtonG("Yes, mistress", "ok");
		removeEvent(PLAY);
		sessionPlay();
		sessionRelease(sessionAborted == null);
		playSchedule(getTime());
	}
	else {
		present([DRESSED, nTEASE], [
				["Good!", "There better not be!"]]);
		showButtonG("Yes, mistress", "ok");
		showLounge();
	}
};

// Setup
// Plans - cannot overlap
def setupPlan = { plan, float startmin, float startmax, float endmin, float endmax ->
	def timeWindow = { float min, float max -> (int)((min * HOUR) + localTimeOffset() + getRandom((int)((max - min) * HOUR))) };
	final friendName = FRIENDS[getRandom(FRIENDS.size())];
	def day = getDay() + ((getRandom(3) + 1) * DAY);
	addEventIfMissing(plan, day + timeWindow(startmin, startmax), BEGINPLAN,
			[friendName: friendName, returnTime: day + timeWindow(endmin, endmax)]);
}
def setupPlans = {
	save("toy.plan", null);
	setupPlan(LUNCH, 11.5, 12.5, 13.0, 14.0)
	setupPlan(SHOPPING, 14.5, 15.5, 16.0, 18.0)
	setupPlan(PARTY, 19, 20, 22.0, 25.0)
};
def setupEvents = {
	playSchedule(loadInteger("toy.lastPlay") ?: 0);
	sleepSchedule();
	setupPlans();
};
def setDefault = { prop, val ->
	if (loadString(prop) == null) {
		save(prop, val);
	}
};
def leaveNote = { msg, instr ->
	save("toy.note", msg);
	save("toy.noteInstr", instr);
};
def readNote = {
	leaveNote(null, null);
};
def plannedCheck = { plan ->
	return (getTime() > loadInteger("toy.plan.${plan}.${START}") && getTime() < loadInteger("toy.plan.${plan}.${END}"));
}
def beginPlan = { rt, plan, args ->
	addEvent(plan, args.returnTime, ENDPLAN, args.friendName);
	dress([[DRESSED,nTEASE], [DRESSED,TEASE]]);
	if (rt && sessionSummon([DRESSED,nTEASE])) {
		present([DRESSED,nTEASE], [
			[ "I'm going out now, toy.", "$plan time!" ],
			[ "Will you be good while I'm gone?", "You will behave as I expect?" ]]);
		wait(10);
		postChastity();
		def toys = TOYTOYS.findAll { t -> is(t) };
		Collections.shuffle(toys);
		toys.each { removeToy(it, [DRESSED,nTEASE]) };
		present([DRESSED,TEASE], [
				[ "Back later!", "See you soon, toy" ]]);
		wait(5);
	}
	else if (has(CHASTITY)) {
		leaveNote("I want you in chastity until I return!", CHASTE);
	}
	setAway(plan);
	removeEvent(PLAY);
	if (rt) {
		exit();
	}
};
def endPlan = { rt, plan ->
	save("toy.owner.outfitTime", getTime());
	setAway(null);
	if (rt && sessionSummon([DRESSED])) {
		present([DRESSED], [["Hello, toy!"], ["I'm back home now."]]);
		wait(10);
		showLounge();
	}
	readNote();
	playSchedule(getTime());
	setupPlans();
};
// Initial - basic checks and defaults
namedEvents = [
	bedtime: { name, arg, schedTime, rt -> bedtime(rt, schedTime) },
	wakeup: { name, arg, schedTime, rt -> wakeup(rt) },
	beginPlan: { name, arg, schedTime, rt -> beginPlan(rt, name, arg) },
	endPlan: { name, arg, schedTime, rt -> endPlan(rt, name) },
	play: { name, arg, schedTime, rt -> playEvent(rt, arg) },
	releaseFrom: { name, arg, schedTime, rt -> releaseFrom(rt, arg, name) },
	redress: { name, arg, schedTime, rt -> redress(rt) },
	permit: { name, arg, schedTime, rt -> givePermission(arg) }
];
def setupInitial = {
	if (loadInteger("toy.version") == null) {
		// Welcome
		dress([[DRESSED,nTEASE], [DRESSED,TEASE], [TITS]]);
		def name = loadString("intro.name");
		present([DRESSED,nTEASE], [
				["Welcome, $name, to your new life as my plaything, my toy."]]);
		showButton("Thank you, mistress");
		present([DRESSED,nTEASE], [
				["My rules are simple, do what I say, when I say, and you'll be rewarded... eventually."]]);
		showButton("Thank you, mistress");
		present([DRESSED,nTEASE], [
				["But if you disappointment me, disobey me or disrespect me, then you'll be punished."]]);
		showButton("Yes, mistress");
		present([DRESSED,TEASE], [
				["I will tease you... and I will torment you..."]]);
		showButton("Yes, mistress");
		present([TITS], [
				["... and I will break... and then you will suffer!"]]);
		showButton("Thank you, mistress");
		present([TITS], [
				["Now, your devotion is important to me."],
				["I have expectations my toy be available to play with as much as possible."]]);
		showButton("Yes, mistress");
		present([TITS], [
				["You can indicate your availability by running this script."],
				["I will summon you when I wish to play."]]);
		showButton("Understood, mistress");
		present([DRESSED,TEASE], [
				["Would you like to set this script as default?"]]);
		if (getBoolean(null)) {
			save("intro.start_script", "toy");
			present([DRESSED,nTEASE], [
					["Good boy."]]);
			showButton("Thank you, mistress");
		}
		present([DRESSED,nTEASE], [
				["Run along now, toy."],
				["I'll summon you with a bell when I want you."],
				["Lisen carefully!"]]);
		showButton("Yes, mistress");
		present(null, [
				["All images are the property of their respective owners, see watermarks (OnlyTease, ForeverVamp).\n"],
				["Don't do anything you aren't 100% comfortable doing.\n"],
				["Be sure to have configured your toys!"]]);
		showButton("OK");
	}
	save("toy.version", VERSION);
	setDefault("toy.strokeTeaseOffset", 0);
	setDefault("toy.punishment", 0);
	execEvents(false);
	setupEvents();
};
def setupShowState = {
	show(
			[CHASTE, COLLARED, CUFFED, CLAMPED, GAGGED, NAKED].collect {
				def v = is(it);
				return "$it: $v\n".capitalize()
			}.join());
	showButton("OK");
	show(
			[BALLGAG, COLLAR, CLAMPS, CHASTITY, HANDCUFFS].collect {
				def v = has(it);
				return "$it: $v\n".capitalize()
			}.join());
	showButton("OK");
	show(
			[BONDAGE, CBT, CHORES, PAIN].collect {
				def v = likes(it);
				return "$it: $v\n".capitalize()
			}.join());
	showButton("OK");
};
def setupShowCalendar = {
	final events = loadEvents();
	show(
			[LUNCH, SHOPPING, PARTY].collect {
				final event = events[it];
				if (!event) return "";
				def s = event.time;
				def f = event.arg.friendName;
				def timeStr = localTimeOf(s).format(soonFormatter);
				return "$it with $f on $timeStr.\n".capitalize();
			}.join());
	showButton("OK");
};

// GO!
setDefault("toy.owner", "ancilla");
OWNER = loadString("toy.owner");
def getDayNum = { Math.floorDiv(getTime() + localTimeOffset() - 14400, 86400) };
def getAvail = { dayNum -> loadInteger("toy.availability.$dayNum") ?: 0 };
def addAvail = { amount ->
	def dayNum = getDayNum();
	save("toy.availability.$dayNum", (int)(getAvail(dayNum) + amount));
};
def tidyAvail = {
	def p = load("toy.availability");
	if (!p) return;
	def p2 = [:];
	final firstDay = getDayNum() - 20;
	p.eachWithIndex{ avail, day ->
		if (day >= firstDay && avail > 0) {
			p2["$day"] = avail;
		}
	}
	save("toy.availability", p2);
};
setupInitial();
tidyAvail();

final cycleTime = 60;
showLounge();
while (true) {
	def away = getAway();
	def e = nextEvent(loadEvents());
	if (!e) return "toy"; // Force restart, should create some events

	if (away) {
		def note = loadString("toy.note");
		def noteInstr = loadString("toy.noteInstr");
		if (note) {
			show("Mistress is away ($away)... but you find a note:\n$note");
			showButton("OK");
			if (noteInstr) {
				set(noteInstr, true);
			}
			readNote();
		}
		else {
			show("Mistress is away ($away)...");
		}
		if (e && e.event && e.event.time > getTime()) {
			wait(e.event.time - getTime());
		}
	}
	else {
		if (is("DEBUG")) {
			def timeStr = localTimeOf(e.event.time).format(soonFormatter);
			show("$e.name: $timeStr -> ${e.event.func}(${e.event.arg})");
		}
		else {
			show("Mistress will summon you when required.");
		}
		final waitTime = Math.min(cycleTime, e.event.time - getTime());
		final clickTime = showButton("Please, Miss?", waitTime);
		if (clickTime < waitTime) {
			def opts = [
				[ lbl: "Calendar", act: setupShowCalendar ],
				[ lbl: "Confess", act: confess ],
			];
			if (is("DEBUG")) {
				opts.push([ lbl: "Status", act: setupShowState ]);
				opts.push([ lbl: "Play", act: playEvent, arg: true ]);
			}
			if (is(NAKED)) {
				opts.push([ lbl: "May I wear clothes", act: requestClothes ]);
			}
			TOYTOYS.findAll { is(it) }.each {
				opts.push([ lbl: "May I be un-$it".toString(), act: requestRelease, arg: it ]);
			};
			opts.push([ lbl: "Back", act: null ]);
			def opt = getSelectedValue("Yes, toy?", opts.collect { it.lbl });
			def act = opts[opt].act;
			if (act)
				act(opts[opt].arg);
			else
				return "toy";
		}
		addAvail(clickTime);
	}
	def eStart = getTime();
	execEvents(true);
	addAvail(getTime() - eStart);
}


/*
 * Resources
 * scripts/toy.groovy
 * images/toy/ancilla/corset/1.jpg
 * images/toy/ancilla/corset/10.jpg
 * images/toy/ancilla/corset/11.jpg
 * images/toy/ancilla/corset/12.jpg
 * images/toy/ancilla/corset/13.jpg
 * images/toy/ancilla/corset/14.jpg
 * images/toy/ancilla/corset/15.jpg
 * images/toy/ancilla/corset/16.jpg
 * images/toy/ancilla/corset/2.jpg
 * images/toy/ancilla/corset/3.jpg
 * images/toy/ancilla/corset/4.jpg
 * images/toy/ancilla/corset/5.jpg
 * images/toy/ancilla/corset/6.jpg
 * images/toy/ancilla/corset/7.jpg
 * images/toy/ancilla/corset/8.jpg
 * images/toy/ancilla/corset/9.jpg
 * images/toy/ancilla/corset/tags
 * images/toy/ancilla/crop/1.jpg
 * images/toy/ancilla/crop/10.jpg
 * images/toy/ancilla/crop/11.jpg
 * images/toy/ancilla/crop/12.jpg
 * images/toy/ancilla/crop/13.jpg
 * images/toy/ancilla/crop/14.jpg
 * images/toy/ancilla/crop/15.jpg
 * images/toy/ancilla/crop/16.jpg
 * images/toy/ancilla/crop/2.jpg
 * images/toy/ancilla/crop/3.jpg
 * images/toy/ancilla/crop/4.jpg
 * images/toy/ancilla/crop/5.jpg
 * images/toy/ancilla/crop/6.jpg
 * images/toy/ancilla/crop/7.jpg
 * images/toy/ancilla/crop/8.jpg
 * images/toy/ancilla/crop/9.jpg
 * images/toy/ancilla/crop/tags
 * images/toy/ancilla/dress/1.jpg
 * images/toy/ancilla/dress/10.jpg
 * images/toy/ancilla/dress/11.jpg
 * images/toy/ancilla/dress/12.jpg
 * images/toy/ancilla/dress/13.jpg
 * images/toy/ancilla/dress/14.jpg
 * images/toy/ancilla/dress/15.jpg
 * images/toy/ancilla/dress/2.jpg
 * images/toy/ancilla/dress/3.jpg
 * images/toy/ancilla/dress/4.jpg
 * images/toy/ancilla/dress/5.jpg
 * images/toy/ancilla/dress/6.jpg
 * images/toy/ancilla/dress/7.jpg
 * images/toy/ancilla/dress/8.jpg
 * images/toy/ancilla/dress/9.jpg
 * images/toy/ancilla/dress/tags
 * images/toy/ancilla/pink/1.jpg
 * images/toy/ancilla/pink/10.jpg
 * images/toy/ancilla/pink/11.jpg
 * images/toy/ancilla/pink/12.jpg
 * images/toy/ancilla/pink/13.jpg
 * images/toy/ancilla/pink/14.jpg
 * images/toy/ancilla/pink/15.jpg
 * images/toy/ancilla/pink/2.jpg
 * images/toy/ancilla/pink/3.jpg
 * images/toy/ancilla/pink/4.jpg
 * images/toy/ancilla/pink/5.jpg
 * images/toy/ancilla/pink/6.jpg
 * images/toy/ancilla/pink/7.jpg
 * images/toy/ancilla/pink/8.jpg
 * images/toy/ancilla/pink/9.jpg
 * images/toy/ancilla/pink/tags
 * images/toy/ancilla/lounge.jpg
 * sounds/toy/165bpm.mp3
 * sounds/toy/180bpm.mp3
 * sounds/toy/240bpm.mp3
 * sounds/toy/30bpm.mp3
 * sounds/toy/90bpm.mp3
 */