/
Numenera_Natha.js
1051 lines (1012 loc) · 41.5 KB
/
Numenera_Natha.js
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
/* read Help.txt */
var NathaNumenera = NathaNumenera || (function () {
'use strict';
var version = 4.8,
releasedate= "2015-10-10",
schemaversion = 1.0,
author="Natha (roll20userid:75857)",
warning = "Sheet must be in version 4.8+ : chat outputs and error messages are managed through the sheet's templates.",
//-----------------------------------------------------------------------------
checkInstall = function() {
log(""+author+"'s Numenera API script version "+version+" ("+releasedate+") installed.");
log(warning);
log("This script works with both the French and English sheet:");
log("English sheet : https://github.com/Roll20/roll20-character-sheets/tree/master/Numenera_NathasNumenera_English");
log("French sheet: https://github.com/Roll20/roll20-character-sheets/tree/master/Numenera_NathasNumenera_French");
log("Enjoy!");
},
//-----------------------------------------------------------------------------
checkCharStates = function (characterObj) {
// Check the character and token states (damage track) based on her current stat pools attributes
// Find the character's token object
var might = 0;
var speed = 0;
var intellect = 0;
var specdmg = 0;
var damage = 0;
var CurrentDamage = 0;
// reading all relevant stats and attributes from the character
might = parseInt(getAttrByName(characterObj.id, "might", "current")) || 0;
speed = parseInt(getAttrByName(characterObj.id, "speed", "current")) || 0;
intellect = parseInt(getAttrByName(characterObj.id, "intellect", "current")) || 0;
specdmg = parseInt(getAttrByName(characterObj.id, "SpecialDamage", "current")) || 0;
// calculating the damage track depending on the stats
// and setting the right markers depending on the damage track
damage += specdmg;
if (might <= 0) {damage += 1};
if (speed <= 0) {damage += 1};
if (intellect <= 0) {damage += 1};
damage = Math.min(damage,3);
CurrentDamage = parseInt(getAttrByName(characterObj.id, "damage-track", "current")) || 0;
//sendChat("character|"+characterObj.get("id"), "Debug : Might = " + might + ", Damage Track = " + CurrentDamage + ", Damage = " + damage);
if (CurrentDamage != damage) {
//sendChat("GM","Damage track : "+CurrentDamage+" Calculated: "+damage); //DEBUG
var attDmgArray = findObjs({
_type: 'attribute',
name: "damage-track",
_characterid: characterObj.id
});
if(attDmgArray.length){
attDmgArray[0].set("current",damage);
}else{
createObj("attribute", {
name: "damage-track",
current: damage,
characterid: characterObj.id
});
};
var tokens = findObjs({
_pageid: Campaign().get("playerpageid"),
_type: "graphic",
layer:"objects",
represents: characterObj.id
});
if (tokens.length > 0) {
switch (damage) {
case 0 : //normal state
tokens[0].set("status_yellow",false);
tokens[0].set("status_red",false);
tokens[0].set("status_dead",false);
break;
case 1 : // impaired
tokens[0].set("status_yellow",true);
tokens[0].set("status_red",false);
tokens[0].set("status_dead",false);
break;
case 2 : // disabled
tokens[0].set("status_yellow",true);
tokens[0].set("status_red",true);
tokens[0].set("status_dead",false);
break;
case 3 : // dead
tokens[0].set("status_yellow",false);
tokens[0].set("status_red",false);
tokens[0].set("status_dead",true);
break;
}
};
};
return;
},
//-----------------------------------------------------------------------------
restChar = function (characterObj) {
//Complete rest/reset of the character
// MIGHT
var attObjArray = findObjs({
_type: 'attribute',
name: "might",
_characterid: characterObj.id
});
if(attObjArray.length>0){
attObjArray[0].set("current", attObjArray[0].get("max"));
} else {
var might = parseInt(getAttrByName(characterObj.id, "might", "max")) || 11;
createObj("attribute", {
name: "might",
current: might,
max: might,
characterid: characterObj.id
});
};
// SPEED, taking account of the armor speed reduction
attObjArray = findObjs({
_type: 'attribute',
name: "speed",
_characterid: characterObj.id
});
if(attObjArray.length>0){
var maxspeed = parseInt(attObjArray[0].get("max")) || 0;
attObjArray[0].set("current", maxspeed);
} else {
var speed = parseInt(getAttrByName(characterObj.id, "speed", "max")) || 10;
createObj("attribute", {
name: "speed",
current: speed,
max: speed,
characterid: characterObj.id
});
};
// INTELLECT
attObjArray = findObjs({
_type: 'attribute',
name: "intellect",
_characterid: characterObj.id
});
if(attObjArray.length>0){
attObjArray[0].set("current", attObjArray[0].get("max"));
} else {
var intellect = parseInt(getAttrByName(characterObj.id, "intellect", "max")) || 7;
createObj("attribute", {
name: "intellect",
current: intellect,
max: intellect,
characterid: characterObj.id
});
};
//Recovery rolls
attObjArray = findObjs({
_type: 'attribute',
name: "recovery-rolls",
_characterid: characterObj.id
});
if(attObjArray.length>0){
attObjArray[0].set("current", "0");
} else {
createObj("attribute", {
name: "recovery-rolls",
current: 0,
characterid: characterObj.id
});
};
// Special Damage
var attObjArray = findObjs({
_type: 'attribute',
name: "SpecialDamage",
_characterid: characterObj.id
});
if(attObjArray.length>0){
attObjArray[0].set("current", "0");
} else {
createObj("attribute", {
name: "SpecialDamage",
current: 0,
characterid: characterObj.id
});
};
//Markers & States & Damage track
checkCharStates(characterObj);
//output
sendChat("character|" + characterObj.id, "&{template:nathaNumRecovery} {{fullRest=1}}");
},
//-----------------------------------------------------------------------------
modStat = function (characterObj,statName,statCost,showMsg) {
// checking the stat
showMsg = showMsg || 0;
var stat1 = "";
if(statName == "might" || statName == "speed" || statName == "intellect") {
stat1 = statName;
} else {
sendChat("character|"+charId, "&{template:nathaNumMsg} {{modStat=1}} {{noAttribute=" + statName + "}}");
return;
};
//getting the stat values
var pool1 = 0;
var max1 = 0;
var finalPool = 0;
var objArray = findObjs({
_type: 'attribute',
name: stat1,
_characterid: characterObj.id
});
var obj1;
if (!objArray.length) {
pool1 = parseInt(getAttrByName(characterObj.id, stat1, "current")) || 0;
max1 = parseInt(getAttrByName(characterObj.id, stat1, "max")) || 0;
obj1 = createObj("attribute", {
name: stat1,
current: pool1,
max: max1,
characterid: characterObj.id
});
} else {
obj1 = objArray[0];
pool1=parseInt(obj1.get("current")) || 0;
};
if(statCost > pool1){
//several stats will be diminished
var pool2, pool3, max2, max3 = 0;
var stat2, stat3 = '';
var obj2, obj3;
switch(statName){
case "might":
stat2 = 'speed';
stat3 = 'intellect';
break;
case "speed":
stat2 = 'might';
stat3 = 'intellect';
break;
case "intellect":
stat2 = 'might';
stat3 = 'speed';
break;
}
objArray = findObjs({
_type: 'attribute',
name: stat2,
_characterid: characterObj.id
});
if (!objArray.length) {
pool2 = parseInt(getAttrByName(characterObj.id, stat2, "current")) || 0;
max2 = parseInt(getAttrByName(characterObj.id, stat2, "max")) || 0;
obj2 = createObj("attribute", {
name: stat2,
current: pool2,
max: max2,
characterid: characterObj.id
});
} else {
obj2= objArray[0];
pool2=parseInt(obj2.get("current")) || 0;
};
objArray = findObjs({
_type: 'attribute',
name: stat3,
_characterid: characterObj.id
});
if (!objArray.length) {
pool3 = parseInt(getAttrByName(characterObj.id, stat3, "current")) || 0;
max3 = parseInt(getAttrByName(characterObj.id, stat3, "max")) || 0;
obj3 = createObj("attribute", {
name: stat3,
current: pool3,
max: max3,
characterid: characterObj.id
});
} else {
obj3 = objArray[0];
pool3=parseInt(obj3.get("current")) || 0;
};
// calculus
statCost = statCost - pool1;
obj1.set("current",0);
if(statCost > pool2){
statCost = statCost - pool2;
obj2.set("current",0);
if(statCost > pool3){
obj3.set("current",0);
if(showMsg == 1){
sendChat("character|"+characterObj.id, "He's dead, Jim! Might, Speed and Intellect down to 0.");
};
}else{
finalPool = pool3 - statCost;
obj3.set("current",finalPool);
if(showMsg == 1){
sendChat("character|"+characterObj.id, "" + stat1 + " and " + stat2 + " down to 0. " + stat3 + ": " + pool3 + "-" + statCost + "=" + finalPool);
};
};
}else{
finalPool = pool2 - statCost;
obj2.set("current",finalPool);
if(showMsg == 1){
sendChat("character|"+characterObj.id, "" + stat1 + " down to 0. " + stat2 + ": " + pool2 + "-" + statCost + "=" + finalPool);
};
};
}else{
//just the current stat is diminished
finalPool = pool1 - statCost;
obj1.set("current",finalPool);
if(showMsg == 1){
sendChat("character|"+characterObj.id, "" + stat1 + ": " + pool1 + "-" + statCost + "=" + finalPool);
};
};
checkCharStates(characterObj);
},
//-----------------------------------------------------------------------------
resetAction = function (characterObj) {
//Resets (sets to 0) the action parameters
// rollVarDiff - Difficulty
var attrName = "rollVarDiff";
var objArray = findObjs({
_type: 'attribute',
name: attrName,
_characterid: characterObj.id
});
if(objArray.length>0){
objArray[0].set("current", 0);
} else {
createObj("attribute", {
name: attrName,
current: 0,
characterid: characterObj.id
});
};
// rollVarCost - Cost
attrName = "rollVarCost";
objArray = findObjs({
_type: 'attribute',
name: attrName,
_characterid: characterObj.id
});
if(objArray.length>0){
objArray[0].set("current", 0);
} else {
createObj("attribute", {
name: attrName,
current: 0,
characterid: characterObj.id
});
};
// rollVarBonus - Bonus
attrName = "rollVarBonus";
objArray = findObjs({
_type: 'attribute',
name: attrName,
_characterid: characterObj.id
});
if(objArray.length>0){
objArray[0].set("current", 0);
} else {
createObj("attribute", {
name: attrName,
current: 0,
characterid: characterObj.id
});
};
// rollVarRollEff - Roll effort
attrName = "rollVarRollEff";
objArray = findObjs({
_type: 'attribute',
name: attrName,
_characterid: characterObj.id
});
if(objArray.length>0){
objArray[0].set("current", 0);
} else {
createObj("attribute", {
name: attrName,
current: 0,
characterid: characterObj.id
});
};
// rollVarRollDmg - Damage effort
attrName = "rollVarRollDmg";
objArray = findObjs({
_type: 'attribute',
name: attrName,
_characterid: characterObj.id
});
if(objArray.length>0){
objArray[0].set("current", 0);
} else {
createObj("attribute", {
name: attrName,
current: 0,
characterid: characterObj.id
});
};
// rollVarAsset - Asset
attrName = "rollVarAsset";
objArray = findObjs({
_type: 'attribute',
name: attrName,
_characterid: characterObj.id
});
if(objArray.length>0){
objArray[0].set("current", 0);
} else {
createObj("attribute", {
name: attrName,
current: 0,
characterid: characterObj.id
});
};
// rollVarSkill - Skill level
attrName = "rollVarSkill";
objArray = findObjs({
_type: 'attribute',
name: attrName,
_characterid: characterObj.id
});
if(objArray.length>0){
objArray[0].set("current", 0);
} else {
createObj("attribute", {
name: attrName,
current: 0,
characterid: characterObj.id
});
};
//End
sendChat("character|"+characterObj.id,"Action parameters reset done.");
},
//-----------------------------------------------------------------------------
initRoll = function (characterObj) {
/*
BEWARE : Not the standard Numenera initiative roll !
The initiative roll is meant to be sorted/compared to (Level*3) of the NPCs/Creatures
The function :
- Rolls 1d20, + the optional efforts (*3) + rollbonus + skill level (*3)
- Expends the optional speed points from effort(s) and initial cost.
- Then add the character, or its token (if on the map), to the tracker,
or replace the initiative value if it's already in the tracker
Every necessary parameters for this function are attributes of the character sheet.
*/
var charId = characterObj.get("id");
//getting the stats values
var speedPool = parseInt(getAttrByName(characterObj.id, "speed", "current")) || 10;
var speedEdge = parseInt(getAttrByName(characterObj.id, "speededge", "current")) || 0;
var attrEffort = parseInt(getAttrByName(characterObj.id, "effort", "current")) || 0;
var damagetrack = parseInt(getAttrByName(characterObj.id, "damage-track", "current")) || 0;
var bonusToRoll = parseInt(getAttrByName(characterObj.id, "InitVarBonus", "current")) || 0;
var statExpense = parseInt(getAttrByName(characterObj.id, "InitVarCost", "current")) || 0;
var effortsUsed = parseInt(getAttrByName(characterObj.id, "InitVarEffort", "current")) || 0;
var assetsUsed = parseInt(getAttrByName(characterObj.id, "InitVarAsset", "current")) || 0;
var skillLevel = parseInt(getAttrByName(characterObj.id, "InitVarSkill", "current")) || 0;
// Rolling the dice
var diceRoll = randomInteger(20);
// checking for appliable effort and calculating statpool cost
var effortCost = 0;
var totalCost = 0;
var speedPoolInit=speedPool;
if ((effortsUsed > 0) || (statExpense > 0)) {
if (effortsUsed > attrEffort) {
sendChat("character|"+charId, "&{template:nathaNumMsg} {{nathaNumInitRoll=1}} {{tooManyEfforts=" + effortsUsed + "}}");
return;
} else {
effortCost = 1 + (effortsUsed * (2+damagetrack) );
};
// effort cost is spent if the roll is less than 20
if (diceRoll != 20) {
totalCost = effortCost + statExpense - speedEdge;
speedPool = Math.max(speedPoolInit - totalCost,0);
modStat(characterObj,"speed",totalCost,0);
};
};
// Calculating initiative
var finalRoll = diceRoll + (effortsUsed*3) + (skillLevel*3) + (assetsUsed*3) + bonusToRoll;
//Retrieving token, to add it to the turn tracker
var tokenObj;
var turnId;
var turnCustom;
var tokens = findObjs({
_pageid: Campaign().get("playerpageid"),
_type: "graphic",
layer:"objects",
represents: charId
});
if (tokens.length > 0) {
tokenObj = tokens[0];
turnId=""+tokenObj.id;
turnCustom="";
} else {
//No token, use the character's name as custom option
turnId="-1";
turnCustom=characterObj.get("name");
};
//Handling the turn tracker
var turnorder;
if(Campaign().get("turnorder") == "") {
turnorder = [];
} else turnorder = JSON.parse(Campaign().get("turnorder"));
//Searching if the token/character is already in the turn tracker
var turnFound=0;
for(var x in turnorder){
if( (turnorder[x].id == "-1") && (turnorder[x].custom=="turnCustom")){
//it's a character without token on the map, already in the tracker
//replace the value
turnorder[x].pr = finalRoll;
turnFound=1;
break;
};
if(turnorder[x].id == turnId){
//it's the token of a character, already in the tracker
//replace the value
turnorder[x].pr = finalRoll;
turnFound=1;
break;
};
}
if (!turnFound) {
//add the character or its token to the tracker
turnorder.push({
id: turnId,
"pr": finalRoll,
"custom":turnCustom
});
};
//Sorting the turn tracker
var first, second;
turnorder.sort(function(a,b) {
first = a.pr;
second = b.pr;
return second - first;
});
//Updating the turn tracker
Campaign().set("turnorder", JSON.stringify(turnorder));
//output
var tmplt="&{template:nathaNumInit} {{finalRoll=[["+finalRoll+"]]}} {{diceRoll=[["+diceRoll+"]]}} {{speedEdge=[["+speedEdge+"]]}}";
if(bonusToRoll>0) tmplt+=" {{bonusToRoll=[["+bonusToRoll+"]]}}";
if(effortsUsed>0) tmplt+=" {{effortsUsed=[["+effortsUsed+"]]}} {{effortCost=[["+effortCost+"]]}}";
if(statExpense>0) tmplt+=" {{statExpense=[["+statExpense+"]]}}";
if(assetsUsed>0) tmplt+=" {{assets=[["+assetsUsed+"]]}}";
if (skillLevel == -1) tmplt += " {{skilled=[[-3]]}}";
if (skillLevel == 1) tmplt += " {{skilled=[[3]]}}";
if (skillLevel == 2) tmplt += " {{skilled=[[6]]}}";
if(totalCost>0) tmplt+=" {{totalCost=[["+totalCost+"]]}} {{attrPool=[["+speedPool+"]]}} {{attrPoolInit=[["+speedPoolInit+"]]}}";
//log(tmplt);
sendChat("character|"+charId, ""+tmplt);
},
//-----------------------------------------------------------------------------
statRollFromSheet= function (characterObj, statName,whoRolled) {
var difficulty=parseInt(getAttrByName(characterObj.id, "rollVarDiff", "current")) || 0;
var assets=parseInt(getAttrByName(characterObj.id, "rollVarAsset", "current")) || 0;
var statexp=parseInt(getAttrByName(characterObj.id, "rollVarCost", "current")) || 0;
var effortsOnRoll=parseInt(getAttrByName(characterObj.id, "rollVarRollEff", "current")) || 0;
var effortsOnDmg=parseInt(getAttrByName(characterObj.id, "rollVarRollDmg", "current")) || 0;
var rollBonus=parseInt(getAttrByName(characterObj.id, "rollVarBonus", "current")) || 0;
var skillLevel=parseInt(getAttrByName(characterObj.id, "rollVarSkill", "current")) || 0;
statRoll(characterObj,statName,whoRolled,difficulty,statexp,assets,effortsOnRoll,effortsOnDmg,rollBonus,skillLevel);
},
//-----------------------------------------------------------------------------
statRoll = function (characterObj,statName,whoRolled,difficulty,statexp,assets,effortsOnRoll,effortsOnDmg,rollBonus,skillLevel) {
/*
Might/speed/intellect roll with eventual roll effort(s), additionnal cost,
damage effort(s), bonus to the roll (<3), skill level, against a difficulty (optional).
Every necessary parameters for this function are attributes of the character sheet
(except the stat name).
Difficulty is the level of difficulty, not the target number.
The target number is calculated by the function.
If difficulty is 0, the roll still happens, but is not confronted to any difficulty.
Instead of calculating success, it calculates the highest difficulty beaten.
Unless d20 rolls a natural 20, pool points cost is calculted (taking the stat edge)
and expended, if necessary and if possible.
Damage track and token state are checked/updated by calling nathaNumCheckCharStates()
*/
var charId = characterObj.get("id");
//checking the character damage track
var damagetrack=parseInt(getAttrByName(characterObj.id, "damage-track", "current")) || 0;
if (damagetrack > 1) {
sendChat("character|"+charId, "&{template:nathaNumMsg} {{nathaNumeneRoll=1}} {{pcDying=1}}");
return;
};
// checking the stat
var attributeName = "";
var edgename = "";
if(statName == "might" || statName == "speed" || statName == "intellect") {
attributeName = statName;
edgename = statName + "edge";
} else {
sendChat("character|"+charId, "&{template:nathaNumMsg} {{nathaNumeneRoll=1}} {{noAttribute=" + statName + "}}");
return;
};
//getting the stat values
var attrPool = parseInt(getAttrByName(characterObj.id, attributeName, "current")) || 0;;
var attrEdge = parseInt(getAttrByName(characterObj.id, edgename, "current")) || 0;
var attrEffort = parseInt(getAttrByName(characterObj.id, "effort", "current")) || 0;
//Checking the bonus to roll
var bonusToRoll = parseInt(rollBonus) || 0;
if( Math.abs(bonusToRoll) > 2 ) {
sendChat("character|"+charId, "&{template:nathaNumMsg} {{nathaNumeneRoll=1}} {{badBonus=" + bonusToRoll + "}}");
return;
};
// Rolling the dice
var diceRoll = randomInteger(20);
var finalRoll = diceRoll + bonusToRoll;
// checking for appliable effort and calculating statpool cost
var effortsUsed = 0;
var effortRoll = parseInt(0 || effortsOnRoll);
var effortDmg = parseInt(0 || effortsOnDmg),
effortsUsed = effortRoll + effortDmg;
var statExpense = parseInt(statexp) || 0;
var effortCost = 0;
var totalCost = 0;
var attrPoolInit= attrPool;
if ((effortsUsed > 0) || (statExpense > 0)) {
if (effortsUsed > 0) {
if (effortsUsed > attrEffort) {
sendChat("character|"+charId, "&{template:nathaNumMsg} {{nathaNumeneRoll=1}} {{tooManyEfforts=" + effortsUsed + "}}");
return;
} else {
effortCost = 1 + (effortsUsed * (2+damagetrack) );
};
};
// effort cost is spent if the roll is not 20
if (diceRoll != 20) {
totalCost = Math.max(0,(effortCost + statExpense - attrEdge));
attrPoolInit = attrPool;
attrPool = Math.max(attrPoolInit - totalCost,0);
modStat(characterObj,attributeName,totalCost,0);
};
};
// beginning output calculation
skillLevel = parseInt(0 || skillLevel);
var tmplt="&{template:nathaNumRoll} {{"+attributeName+"="+attributeName+"}} {{attrEdge="+attrEdge+"}} {{finalRoll="+finalRoll+"}} {{diceRoll="+diceRoll+"}} {{skilled="+ skillLevel +"}}";
if (bonusToRoll>0) tmplt += " {{bonusToRoll="+bonusToRoll+"}}";
var assetsUsed = parseInt(0 || assets);
if (assetsUsed>0) tmplt += " {{assets="+assetsUsed+"}}";
if (skillLevel == 0) tmplt += " {{Untrained=1}}";
if (skillLevel == 1) tmplt += " {{Trained=1}}";
if (skillLevel == 2) tmplt += " {{Specialized=1}}";
if (skillLevel == -1) tmplt += " {{Inability=1}}";
//computing final difficulty / target task
var initDiff=parseInt(0 || difficulty);
if (initDiff > 0) {
var target = initDiff*3;
var finalDiff= Math.max(0, initDiff-effortRoll-assetsUsed-skillLevel);
var targetRoll = finalDiff*3;
tmplt += "{{difficulty=" + initDiff + "}} {{target="+target+"}} {{finalDiff="+finalDiff+"}} {{targetRoll="+targetRoll+"}}";
} else {
var rollBeats=Math.max(0, Math.floor(finalRoll/3)+effortRoll+assetsUsed+skillLevel);
tmplt += " {{rollBeats="+rollBeats+" ("+(rollBeats*3)+")"+"}}";
};
// Checking result
var bonusDmg=0;
if ((effortsUsed > 0) || (statExpense > 0)) {
tmplt= tmplt + " {{totalCost=" + totalCost + "}}";
if (effortRoll > 0) {
tmplt += " {{effortCost=" + effortCost + "}} {{effortRoll=" + effortRoll + "}}";
};
if (effortDmg > 0) {
bonusDmg=bonusDmg + (effortDmg*3);
tmplt += " {{effortDmg=" + effortDmg + "}}";
};
if (statExpense > 0) {
tmplt += " {{statExpense=" + statExpense + "}}";
};
};
if (bonusToRoll != 0) {
tmplt += " {{bonusToRoll=" + bonusToRoll + "}}";
};
// If not an automatic success or a known difficulty
if (initDiff > 0) {
if( finalRoll >= targetRoll) {
tmplt += " {{boolOK=1}}";
}
else{
tmplt += " {{boolKO=1}}";
};
};
//special dice roll results treatment
if (diceRoll == 1) {
tmplt += " {{freeGMI=1}}";
} else {
if (damagetrack == 0) {
// if character is haled, special success is possible
switch (diceRoll) {
case 17:
bonusDmg += 1;
break;
case 18:
bonusDmg += 2;
break;
case 19:
bonusDmg += 3;
tmplt += " {{minorEffect=1}}";
break;
case 20:
bonusDmg += 4;
tmplt += " {{majorEffect=1}} {{noExpense=1}}";
break;
}
} else if (diceRoll == 20) {
// if character is impaired
tmplt += " {{noExpense=1}}";
};
};
//ending output
if(bonusDmg>0) tmplt += " {{bonusDmg="+bonusDmg+"}}";
if(totalCost==0) {
tmplt += " {{noExpense=1}}";
}
else {
tmplt += " {{attrPool="+attrPool+"}} {{attrPoolInit="+attrPoolInit+"}}"
};
sendChat("character|"+charId, ""+tmplt);
},
//-----------------------------------------------------------------------------
recoveryRoll = function (characterObj) {
// Rolls a recovery rolls and advances the recovery track
var charId = characterObj.get("id");
var recrolls=parseInt(getAttrByName(charId, "recovery-rolls", "current")) || 0;
var recovObjArray = findObjs({
_type: 'attribute',
name: "recovery-rolls",
_characterid: charId
});
var obj;
if (!recovObjArray.length) {
obj = createObj("attribute", {
name: "recovery-rolls",
current: recrolls,
max: recrolls,
characterid: charId
});
}else{
obj= recovObjArray[0];
};
var recovbonus = parseInt(getAttrByName(charId, "recoverybonus", "current")) || 0;
var curRecLib = "";
var nextRecLib = "";
switch (recrolls) {
case 0 :
curRecLib = "1 ACTION";
nextRecLib = "10 MINS";
recrolls=1;
break;
case 1 :
curRecLib = "10 MINS";
nextRecLib = "1 HOUR";
recrolls=2;
break;
case 2 :
curRecLib = "1 HOUR";
nextRecLib = "10 HOURS";
recrolls=3;
break;
case 3 :
curRecLib = "10 HOURS";
nextRecLib = "1 ACTION";
recrolls=0;
break;
}
obj.set("current",recrolls);
var recovery = randomInteger(6)+recovbonus;
sendChat("character|" + charId, "&{template:nathaNumRecovery} {{recoverPoints=1d6+"+recovbonus+" ("+recovery+")}} {{currentAction="+curRecLib+"}} {{nextAction="+nextRecLib+"}}");
},
//-----------------------------------------------------------------------------
npcDamage = function (tokenObj,characterObj,dmgDealt, applyArmor) {
// Apply damage (or healing if dmdDeal is negative ...) to Numenera NPC/Creature
// And set "death" marker if health is 0 or less.
// The Mook or Non Player full Character must have the following attributes :
// - Level (token bar1)
// - Health (token bar2)
// - Armor (token bar3)
// Armor will diminish damage unless "applyArmor"='n'
var dmg = parseInt(dmgDealt) || 0;
var npcName = characterObj.get("name");
var npcHealth = 0;
var npcMaxHealth = 0;
var npcArmor=0;
if (applyArmor!="n"){
npcArmor = parseInt(getAttrByName(characterObj.id, "Armor", "current")) || 0;
//DEBUG
//sendChat("GM", "/w gm npcDamage() Debug : Armor of ('"+npcName+"', char id:"+characterObj.id+", token id:"+tokenObj.id+") = "+npcArmor);
};
// Is the token linked to the character ("full NPC") or a Mook ?
var isChar = tokenObj.get("bar1_link");
if (isChar == "") {
// It's a Mook : get the bars value
npcHealth = parseInt(tokenObj.get("bar2_value"));
npcMaxHealth = parseInt(tokenObj.get("bar2_max"));
} else {
// It's a "full" character NPC : get the attributes values
var attObjArray = findObjs({
_type: "attribute",
name: "Health",
_characterid: characterObj.id
});
if (attObjArray == false) {
sendChat("GM", "/w gm npcDamage() Error : this character ('"+npcName+"') has no Health attribute!");
return false;
} else {
npcHealth=parseInt(attObjArray[0].get("current")) || 0;
npcMaxHealth=parseInt(attObjArray[0].get("max")) || 0;
};
};
// In case the Health attribute / bar has no maximum value
npcMaxHealth = Math.max(npcHealth, npcMaxHealth);
if (dmg > 0) {
dmg = Math.max((dmg - npcArmor),0);
};
var npcHealthFinal = Math.min(Math.max((npcHealth - dmg),0),npcMaxHealth);
if (isChar == "") {
// Mook : update bars onbly
tokenObj.set("bar2_max", npcMaxHealth);
tokenObj.set("bar2_value",npcHealthFinal);
} else {
// Update character attributes
attObjArray[0].set("max",npcMaxHealth);
attObjArray[0].set("current",npcHealthFinal);
};
tokenObj.set("status_dead", (npcHealthFinal == 0));
if (dmgDealt>0) {
sendChat("GM", "/w gm " + npcName + " takes " + dmg + " damage (" + dmgDealt + " - " + npcArmor + " Armor). Health: " + npcHealth + "->" + npcHealthFinal + ".");
} else {
sendChat("GM", "/w gm " + npcName + " is healed for " + dmg + " points. Health: " + npcHealth + "->" + npcHealthFinal + ".");
};
return;
},
//-----------------------------------------------------------------------------
handleAttributeEvent = function(obj, prev) {
/*
Check and set character states according to stats pools attributes
and special damage, when their current values are manually
changed on the sheet or directly in the character window.
Note that this event isn't fired when attributes are modified by API functions,
that's why some functions here call checkCharStates() too.
*/
var attrName = obj.get("name");
if ( attrName=="might" || attrName=="speed" || attrName=="intellect" || attrName=="SpecialDamage") {
checkCharStates(getObj("character", obj.get("_characterid")));
};
return;
},
//-----------------------------------------------------------------------------
handleInput = function(msg) {
if (msg.type !== "api") {
return;
};
if (msg.content.indexOf("!nathanum-") !== 0) {
return;
} else {
if (parseInt(msg.content.indexOf(" ")) ==-1) {
//every function requires at least one parameter
return;
} else {
var functionCalled, obj;
var paramArray= new Array(1);
functionCalled = msg.content.split(" ")[0];
paramArray[0] = msg.content.split(" ")[1];
//log("Function called:"+functionCalled+" Parameters:"+paramArray[0]); //DEBUG
if (parseInt(paramArray[0].indexOf("|")) !=-1) {
//more than 1 parameter (supposedly character_id as first paramater)
paramArray = paramArray[0].split("|");
};
obj = getObj("character", paramArray[0]);
};
};
switch(functionCalled) {
case '!nathanum-numeneroll':
if (!obj) {
sendChat("GM", "&{template:nathaNumMsg} {{chatmessage=nathanum-numeneroll}} {{notaCharacter="+paramArray[0]+"}}");
return false;
};
if (paramArray.length != 2) {
//this function requires 2 parameters : character_id|attribute_name
sendChat("GM", "&{template:nathaNumMsg} {{chatmessage=nathanum-numeneroll}} {{wtfAttribute="+msg.content+"}}");
return false;
};
statRollFromSheet(obj,paramArray[1],msg.who);
break;
case '!nathanum-macroroll':
// macroroll can be called with a token_id or character_id
if(!obj) {
//not a character_id
obj = findObjs({
_pageid: Campaign().get("playerpageid"),
_type: "graphic",
layer:"objects",
_id: paramArray[0]
})[0];
};
if (!obj) {
//not a token either
sendChat("GM", "&{template:nathaNumMsg} {{chatmessage=nathanum-macroroll}} {{noTokNoChar= "+msg.content+"}}");
return false;
} else {
if(obj.get("type")=="graphic") {
// it's a token but does it represents a character ?
obj = getObj("character", obj.get("represents"));
if (!obj) {
//not a token either
sendChat("GM", "&{template:nathaNumMsg} {{chatmessage=nathanum-macroroll}} {{noTokNoChar= "+msg.content+"}}");
return false;
};
};
};
if (paramArray.length != 9) {
//this function requires more parameters
sendChat("GM", "&{template:nathaNumMsg} {{chatmessage=nathanum-macroroll}} {{genericMsg=Requires 9 paramaters : token|stat name|difficulty|assets|Cost|Effort on Roll|Effort on Damage|Roll Bonus|Skill level (-1 to 2)}}");
return false;
};
statRoll(obj,paramArray[1],msg.who,paramArray[2],paramArray[3],paramArray[4],paramArray[5],paramArray[6],paramArray[7],paramArray[8])
break;
case '!nathanum-recoveryroll':
if (!obj) {
sendChat("GM", "&{template:nathaNumMsg} {{chatmessage=nathanum-recoveryroll}} {{noCharacter="+msg.content+"}}");
return false;
};
recoveryRoll(obj);
break;
case '!nathanum-initroll':
if (!obj) {
sendChat("GM", "&{template:nathaNumMsg} {{chatmessage=nathanum-initroll}} {{noCharacter="+msg.content+"}}");
return false;
};
initRoll(obj);
break;
case '!nathanum-checkchar':
if (!obj) {
sendChat("GM", "&{template:nathaNumMsg} {{chatmessage=nathanum-checkpcstate}} {{noCharacter="+msg.content+"}}");
return false;
};
checkCharStates(obj);
break;
case '!nathanum-restchar':
// restchar can be called with a token_id or character_id
if(!obj) {
//not a character_id
obj = findObjs({
_pageid: Campaign().get("playerpageid"),
_type: "graphic",
layer:"objects",
_id: paramArray[0]
})[0];
};
if (!obj) {
//not a token either
sendChat("GM", "&{template:nathaNumMsg} {{chatmessage=nathanum-restchar}} {{noTokNoChar= "+msg.content+"}}");
return false;
} else {
if(obj.get("type")=="graphic") {
// it's a token but does it represents a character ?
obj = getObj("character", obj.get("represents"));
if (!obj) {
//not a token either
sendChat("GM", "&{template:nathaNumMsg} {{chatmessage=nathanum-restchar}} {{noTokNoChar= "+msg.content+"}}");
return false;
};
};
};
restChar(obj);
break;
case '!nathanum-npcdmg':
//this function requires 3 parameters : token_id|damage|apply armor y/n
if(paramArray.length != 3) {
sendChat("GM", "&{template:nathaNumMsg} {{chatmessage=nathanum-npcdmg}} {{genericMsg=Wrong parameters (expected: token_id|damage|apply armor y/n): '"+Parameters+"''}}");
return false;
};
//
obj=getObj("graphic", paramArray[0]);