forked from doublespeakgames/adarkroom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
room.js
1168 lines (1094 loc) · 29.8 KB
/
room.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
/**
* Module that registers the simple room functionality
*/
var Room = {
// times in (minutes * seconds * milliseconds)
_FIRE_COOL_DELAY: 5 * 60 * 1000, // time after a stoke before the fire cools
_ROOM_WARM_DELAY: 30 * 1000, // time between room temperature updates
_BUILDER_STATE_DELAY: 0.5 * 60 * 1000, // time between builder state updates
_STOKE_COOLDOWN: 10, // cooldown to stoke the fire
_NEED_WOOD_DELAY: 15 * 1000, // from when the stranger shows up, to when you need wood
buttons:{},
Craftables: {
'trap': {
name: _('trap'),
button: null,
maximum: 10,
availableMsg: _('builder says she can make traps to catch any creatures might still be alive out there'),
buildMsg: _('more traps to catch more creatures'),
maxMsg: _("more traps won't help now"),
type: 'building',
cost: function() {
var n = $SM.get('game.buildings["trap"]', true);
return {
'wood': 10 + (n*10)
};
}
},
'cart': {
name: _('cart'),
button: null,
maximum: 1,
availableMsg: _('builder says she can make a cart for carrying wood'),
buildMsg: _('the rickety cart will carry more wood from the forest'),
type: 'building',
cost: function() {
return {
'wood': 30
};
}
},
'hut': {
name: _('hut'),
button: null,
maximum: 20,
availableMsg: _("builder says there are more wanderers. says they'll work, too."),
buildMsg: _('builder puts up a hut, out in the forest. says word will get around.'),
maxMsg: _('no more room for huts.'),
type: 'building',
cost: function() {
var n = $SM.get('game.buildings["hut"]', true);
return {
'wood': 100 + (n*50)
};
}
},
'lodge': {
name: _('lodge'),
button: null,
maximum: 1,
availableMsg: _('villagers could help hunt, given the means'),
buildMsg: _('the hunting lodge stands in the forest, a ways out of town'),
type: 'building',
cost: function() {
return {
wood: 200,
fur: 10,
meat: 5
};
}
},
'trading post': {
name: _('trading post'),
button: null,
maximum: 1,
availableMsg: _("a trading post would make commerce easier"),
buildMsg: _("now the nomads have a place to set up shop, they might stick around a while"),
type: 'building',
cost: function() {
return {
'wood': 400,
'fur': 100
};
}
},
'tannery': {
name: _('tannery'),
button: null,
maximum: 1,
availableMsg: _("builder says leather could be useful. says the villagers could make it."),
buildMsg: _('tannery goes up quick, on the edge of the village'),
type: 'building',
cost: function() {
return {
'wood': 500,
'fur': 50
};
}
},
'smokehouse': {
name: _('smokehouse'),
button: null,
maximum: 1,
availableMsg: _("should cure the meat, or it'll spoil. builder says she can fix something up."),
buildMsg: _('builder finishes the smokehouse. she looks hungry.'),
type: 'building',
cost: function() {
return {
'wood': 600,
'meat': 50
};
}
},
'workshop': {
name: _('workshop'),
button: null,
maximum: 1,
availableMsg: _("builder says she could make finer things, if she had the tools"),
buildMsg: _("workshop's finally ready. builder's excited to get to it"),
type: 'building',
cost: function() {
return {
'wood': 800,
'leather': 100,
'scales': 10
};
}
},
'steelworks': {
name: _('steelworks'),
button: null,
maximum: 1,
availableMsg: _("builder says the villagers could make steel, given the tools"),
buildMsg: _("a haze falls over the village as the steelworks fires up"),
type: 'building',
cost: function() {
return {
'wood': 1500,
'iron': 100,
'coal': 100
};
}
},
'armoury': {
name: _('armoury'),
button: null,
maximum: 1,
availableMsg: _("builder says it'd be useful to have a steady source of bullets"),
buildMsg: _("armoury's done, welcoming back the weapons of the past."),
type: 'building',
cost: function() {
return {
'wood': 3000,
'steel': 100,
'sulphur': 50
};
}
},
'torch': {
name: _('torch'),
button: null,
type: 'tool',
buildMsg: _('a torch to keep the dark away'),
cost: function() {
return {
'wood': 1,
'cloth': 1
};
}
},
'waterskin': {
name: _('waterskin'),
button: null,
type: 'upgrade',
maximum: 1,
buildMsg: _('this waterskin\'ll hold a bit of water, at least'),
cost: function() {
return {
'leather': 50
};
}
},
'cask': {
name: _('cask'),
button: null,
type: 'upgrade',
maximum: 1,
buildMsg: _('the cask holds enough water for longer expeditions'),
cost: function() {
return {
'leather': 100,
'iron': 20
};
}
},
'water tank': {
name: _('water tank'),
button: null,
type: 'upgrade',
maximum: 1,
buildMsg: _('never go thirsty again'),
cost: function() {
return {
'iron': 100,
'steel': 50
};
}
},
'bone spear': {
name: _('bone spear'),
button: null,
type: 'weapon',
buildMsg: _("this spear's not elegant, but it's pretty good at stabbing"),
cost: function() {
return {
'wood': 100,
'teeth': 5
};
}
},
'rucksack': {
name: _('rucksack'),
button: null,
type: 'upgrade',
maximum: 1,
buildMsg: _('carrying more means longer expeditions to the wilds'),
cost: function() {
return {
'leather': 200
};
}
},
'wagon': {
name: _('wagon'),
button: null,
type: 'upgrade',
maximum: 1,
buildMsg: _('the wagon can carry a lot of supplies'),
cost: function() {
return {
'wood': 500,
'iron': 100
};
}
},
'convoy': {
name: _('convoy'),
button: null,
type: 'upgrade',
maximum: 1,
buildMsg: _('the convoy can haul mostly everything'),
cost: function() {
return {
'wood': 1000,
'iron': 200,
'steel': 100
};
}
},
'l armour': {
name: _('l armour'),
type: 'upgrade',
maximum: 1,
buildMsg: _("leather's not strong. better than rags, though."),
cost: function() {
return {
'leather': 200,
'scales': 20
};
}
},
'i armour': {
name: _('i armour'),
type: 'upgrade',
maximum: 1,
buildMsg: _("iron's stronger than leather"),
cost: function() {
return {
'leather': 200,
'iron': 100
};
}
},
's armour': {
name: _('s armour'),
type: 'upgrade',
maximum: 1,
buildMsg: _("steel's stronger than iron"),
cost: function() {
return {
'leather': 200,
'steel': 100
};
}
},
'iron sword': {
name: _('iron sword'),
button: null,
type: 'weapon',
buildMsg: _("sword is sharp. good protection out in the wilds."),
cost: function() {
return {
'wood': 200,
'leather': 50,
'iron': 20
};
}
},
'steel sword': {
name: _('steel sword'),
button: null,
type: 'weapon',
buildMsg: _("the steel is strong, and the blade true."),
cost: function() {
return {
'wood': 500,
'leather': 100,
'steel': 20
};
}
},
'rifle': {
name: _('rifle'),
type: 'weapon',
buildMsg: _("black powder and bullets, like the old days."),
cost: function() {
return {
'wood': 200,
'steel': 50,
'sulphur': 50
};
}
}
},
TradeGoods: {
'scales': {
type: 'good',
cost: function() {
return { fur: 150 };
}
},
'teeth': {
type: 'good',
cost: function() {
return { fur: 300 };
}
},
'iron': {
type: 'good',
cost: function() {
return {
'fur': 150,
'scales': 50
};
}
},
'coal': {
type: 'good',
cost: function() {
return {
'fur': 200,
'teeth': 50
};
}
},
'steel': {
type: 'good',
cost: function() {
return {
'fur': 300,
'scales': 50,
'teeth': 50
};
}
},
'medicine': {
type: 'good',
cost: function() {
return {
'scales': 50, 'teeth': 30
};
}
},
'bullets': {
type: 'good',
cost: function() {
return {
'scales': 10
};
}
},
'energy cell': {
type: 'good',
cost: function() {
return {
'scales': 10,
'teeth': 10
};
}
},
'bolas': {
type: 'weapon',
cost: function() {
return {
'teeth': 10
};
}
},
'grenade': {
type: 'weapon',
cost: function() {
return {
'scales': 100,
'teeth': 50
};
}
},
'bayonet': {
type: 'weapon',
cost: function() {
return {
'scales': 500,
'teeth': 250
};
}
},
'alien alloy': {
type: 'good',
cost: function() {
return {
'fur': 1500,
'scales': 750,
'teeth': 300
};
}
},
'compass': {
type: 'special',
maximum: 1,
cost: function() {
return {
fur: 400,
scales: 20,
teeth: 10
};
}
}
},
MiscItems: {
'laser rifle': {
type: 'weapon'
}
},
name: _("Room"),
init: function(options) {
this.options = $.extend(
this.options,
options
);
Room.pathDiscovery = Boolean($SM.get('stores["compass"]'));
if(Engine._debug) {
this._ROOM_WARM_DELAY = 5000;
this._BUILDER_STATE_DELAY = 5000;
this._STOKE_COOLDOWN = 0;
this._NEED_WOOD_DELAY = 5000;
}
if(typeof $SM.get('features.location.room') == 'undefined') {
$SM.set('features.location.room', true);
$SM.set('game.builder.level', -1);
}
// If this is the first time playing, the fire is dead and it's freezing.
// Otherwise grab past save state temp and fire level.
$SM.set('game.temperature', $SM.get('game.temperature.value')===undefined?this.TempEnum.Freezing:$SM.get('game.temperature'));
$SM.set('game.fire', $SM.get('game.fire.value')===undefined?this.FireEnum.Dead:$SM.get('game.fire'));
// Create the room tab
this.tab = Header.addLocation(_("A Dark Room"), "room", Room);
// Create the Room panel
this.panel = $('<div>')
.attr('id', "roomPanel")
.addClass('location')
.appendTo('div#locationSlider');
Engine.updateSlider();
// Create the light button
new Button.Button({
id: 'lightButton',
text: _('light fire'),
click: Room.lightFire,
cooldown: Room._STOKE_COOLDOWN,
width: '80px',
cost: {'wood': 5}
}).appendTo('div#roomPanel');
// Create the stoke button
new Button.Button({
id: 'stokeButton',
text: _("stoke fire"),
click: Room.stokeFire,
cooldown: Room._STOKE_COOLDOWN,
width: '80px',
cost: {'wood': 1}
}).appendTo('div#roomPanel');
// Create the stores container
$('<div>').attr('id', 'storesContainer').prependTo('div#roomPanel');
//subscribe to stateUpdates
$.Dispatch('stateUpdate').subscribe(Room.handleStateUpdates);
Room.updateButton();
Room.updateStoresView();
Room.updateIncomeView();
Room.updateBuildButtons();
Room._fireTimer = Engine.setTimeout(Room.coolFire, Room._FIRE_COOL_DELAY);
Room._tempTimer = Engine.setTimeout(Room.adjustTemp, Room._ROOM_WARM_DELAY);
/*
* Builder states:
* 0 - Approaching
* 1 - Collapsed
* 2 - Shivering
* 3 - Sleeping
* 4 - Helping
*/
if($SM.get('game.builder.level') >= 0 && $SM.get('game.builder.level') < 3) {
Room._builderTimer = Engine.setTimeout(Room.updateBuilderState, Room._BUILDER_STATE_DELAY);
}
if($SM.get('game.builder.level') == 1 && $SM.get('stores.wood', true) < 0) {
Engine.setTimeout(Room.unlockForest, Room._NEED_WOOD_DELAY);
}
Engine.setTimeout($SM.collectIncome, 1000);
Notifications.notify(Room, _("the room is {0}", Room.TempEnum.fromInt($SM.get('game.temperature.value')).text));
Notifications.notify(Room, _("the fire is {0}", Room.FireEnum.fromInt($SM.get('game.fire.value')).text));
},
options: {}, // Nothing for now
onArrival: function(transition_diff) {
Room.setTitle();
if(Room.changed) {
Notifications.notify(Room, _("the fire is {0}", Room.FireEnum.fromInt($SM.get('game.fire.value')).text));
Notifications.notify(Room, _("the room is {0}", Room.TempEnum.fromInt($SM.get('game.temperature.value')).text));
Room.changed = false;
}
if($SM.get('game.builder.level') == 3) {
$SM.add('game.builder.level', 1);
$SM.setIncome('builder', {
delay: 10,
stores: {'wood' : 2 }
});
Room.updateIncomeView();
Notifications.notify(Room, _("the stranger is standing by the fire. she says she can help. says she builds things."));
}
Engine.moveStoresView(null, transition_diff);
},
TempEnum: {
fromInt: function(value) {
for(var k in this) {
if(typeof this[k].value != 'undefined' && this[k].value == value) {
return this[k];
}
}
return null;
},
Freezing: { value: 0, text: _('freezing') },
Cold: { value: 1, text: _('cold') },
Mild: { value: 2, text: _('mild') },
Warm: { value: 3, text: _('warm') },
Hot: { value: 4, text: _('hot') }
},
FireEnum: {
fromInt: function(value) {
for(var k in this) {
if(typeof this[k].value != 'undefined' && this[k].value == value) {
return this[k];
}
}
return null;
},
Dead: { value: 0, text: _('dead') },
Smoldering: { value: 1, text: _('smoldering') },
Flickering: { value: 2, text: _('flickering') },
Burning: { value: 3, text: _('burning') },
Roaring: { value: 4, text: _('roaring') }
},
setTitle: function() {
var title = $SM.get('game.fire.value') < 2 ? _("A Dark Room") : _("A Firelit Room");
if(Engine.activeModule == this) {
document.title = title;
}
$('div#location_room').text(title);
},
updateButton: function() {
var light = $('#lightButton.button');
var stoke = $('#stokeButton.button');
if($SM.get('game.fire.value') == Room.FireEnum.Dead.value && stoke.css('display') != 'none') {
stoke.hide();
light.show();
if(stoke.hasClass('disabled')) {
Button.cooldown(light);
}
} else if(light.css('display') != 'none') {
stoke.show();
light.hide();
if(light.hasClass('disabled')) {
Button.cooldown(stoke);
}
}
if(!$SM.get('stores.wood')) {
light.addClass('free');
stoke.addClass('free');
} else {
light.removeClass('free');
stoke.removeClass('free');
}
},
_fireTimer: null,
_tempTimer: null,
lightFire: function() {
var wood = $SM.get('stores.wood');
if(wood < 5) {
Notifications.notify(Room, _("not enough wood to get the fire going"));
Button.clearCooldown($('#lightButton.button'));
return;
} else if(wood > 4) {
$SM.set('stores.wood', wood - 5);
}
$SM.set('game.fire', Room.FireEnum.Burning);
Room.onFireChange();
},
stokeFire: function() {
var wood = $SM.get('stores.wood');
if(wood === 0) {
Notifications.notify(Room, _("the wood has run out"));
Button.clearCooldown($('#stokeButton.button'));
return;
}
if(wood > 0) {
$SM.set('stores.wood', wood - 1);
}
if($SM.get('game.fire.value') < 4) {
$SM.set('game.fire', Room.FireEnum.fromInt($SM.get('game.fire.value') + 1));
}
Room.onFireChange();
},
onFireChange: function() {
if(Engine.activeModule != Room) {
Room.changed = true;
}
Notifications.notify(Room, _("the fire is {0}", Room.FireEnum.fromInt($SM.get('game.fire.value')).text), true);
if($SM.get('game.fire.value') > 1 && $SM.get('game.builder.level') < 0) {
$SM.set('game.builder.level', 0);
Notifications.notify(Room, _("the light from the fire spills from the windows, out into the dark"));
Engine.setTimeout(Room.updateBuilderState, Room._BUILDER_STATE_DELAY);
}
window.clearTimeout(Room._fireTimer);
Room._fireTimer = Engine.setTimeout(Room.coolFire, Room._FIRE_COOL_DELAY);
Room.updateButton();
Room.setTitle();
},
coolFire: function() {
var wood = $SM.get('stores.wood');
if($SM.get('game.fire.value') <= Room.FireEnum.Flickering.value &&
$SM.get('game.builder.level') > 3 && wood > 0) {
Notifications.notify(Room, _("builder stokes the fire"), true);
$SM.set('stores.wood', wood - 1);
$SM.set('game.fire',Room.FireEnum.fromInt($SM.get('game.fire.value') + 1));
}
if($SM.get('game.fire.value') > 0) {
$SM.set('game.fire',Room.FireEnum.fromInt($SM.get('game.fire.value') - 1));
Room._fireTimer = Engine.setTimeout(Room.coolFire, Room._FIRE_COOL_DELAY);
Room.onFireChange();
}
},
adjustTemp: function() {
var old = $SM.get('game.temperature.value');
if($SM.get('game.temperature.value') > 0 && $SM.get('game.temperature.value') > $SM.get('game.fire.value')) {
$SM.set('game.temperature',Room.TempEnum.fromInt($SM.get('game.temperature.value') - 1));
Notifications.notify(Room, _("the room is {0}" , Room.TempEnum.fromInt($SM.get('game.temperature.value')).text), true);
}
if($SM.get('game.temperature.value') < 4 && $SM.get('game.temperature.value') < $SM.get('game.fire.value')) {
$SM.set('game.temperature', Room.TempEnum.fromInt($SM.get('game.temperature.value') + 1));
Notifications.notify(Room, _("the room is {0}" , Room.TempEnum.fromInt($SM.get('game.temperature.value')).text), true);
}
if($SM.get('game.temperature.value') != old) {
Room.changed = true;
}
Room._tempTimer = Engine.setTimeout(Room.adjustTemp, Room._ROOM_WARM_DELAY);
},
unlockForest: function() {
$SM.set('stores.wood', 4);
Outside.init();
Notifications.notify(Room, _("the wind howls outside"));
Notifications.notify(Room, _("the wood is running out"));
Engine.event('progress', 'outside');
},
updateBuilderState: function() {
var lBuilder = $SM.get('game.builder.level');
if(lBuilder === 0) {
Notifications.notify(Room, _("a ragged stranger stumbles through the door and collapses in the corner"));
lBuilder = $SM.setget('game.builder.level', 1);
Engine.setTimeout(Room.unlockForest, Room._NEED_WOOD_DELAY);
}
else if(lBuilder < 3 && $SM.get('game.temperature.value') >= Room.TempEnum.Warm.value) {
var msg = "";
switch(lBuilder) {
case 1:
msg = _("the stranger shivers, and mumbles quietly. her words are unintelligible.");
break;
case 2:
msg = _("the stranger in the corner stops shivering. her breathing calms.");
break;
}
Notifications.notify(Room, msg);
if(lBuilder < 3) {
lBuilder = $SM.setget('game.builder.level', lBuilder + 1);
}
}
if(lBuilder < 3) {
Engine.setTimeout(Room.updateBuilderState, Room._BUILDER_STATE_DELAY);
}
Engine.saveGame();
},
updateStoresView: function() {
var stores = $('div#stores');
var resources = $('div#resources');
var special = $('div#special');
var weapons = $('div#weapons');
var needsAppend = false, rNeedsAppend = false, sNeedsAppend = false, wNeedsAppend = false, newRow = false;
if(stores.length === 0) {
stores = $('<div>').attr({
'id': 'stores',
'data-legend': _('stores')
}).css('opacity', 0);
needsAppend = true;
}
if(resources.length === 0) {
resources = $('<div>').attr({
id: 'resources'
}).css('opacity', 0);
rNeedsAppend = true;
}
if(special.length === 0) {
special = $('<div>').attr({
id: 'special'
}).css('opacity', 0);
sNeedsAppend = true;
}
if(weapons.length === 0) {
weapons = $('<div>').attr({
'id': 'weapons',
'data-legend': _('weapons')
}).css('opacity', 0);
wNeedsAppend = true;
}
for(var k in $SM.get('stores')) {
var type = null;
if(Room.Craftables[k]) {
type = Room.Craftables[k].type;
} else if(Room.TradeGoods[k]) {
type = Room.TradeGoods[k].type;
} else if (Room.MiscItems[k]) {
type = Room.MiscItems[k].type;
}
var location;
switch(type) {
case 'upgrade':
// Don't display upgrades on the Room screen
continue;
case 'weapon':
location = weapons;
break;
case 'special':
location = special;
break;
default:
location = resources;
break;
}
var id = "row_" + k.replace(' ', '-');
var row = $('div#' + id, location);
var num = $SM.get('stores["'+k+'"]');
if(typeof num != 'number' || isNaN(num)) {
// No idea how counts get corrupted, but I have reason to believe that they occassionally do.
// Build a little fence around it!
num = 0;
$SM.set('stores["'+k+'"]', 0);
}
var lk = _(k);
// thieves?
if(typeof $SM.get('game.thieves') == 'undefined' && num > 5000 && $SM.get('features.location.world')) {
$SM.startThieves();
}
if(row.length === 0 && num > 0) {
row = $('<div>').attr('id', id).addClass('storeRow');
$('<div>').addClass('row_key').text(lk).appendTo(row);
$('<div>').addClass('row_val').text(Math.floor(num)).appendTo(row);
$('<div>').addClass('clear').appendTo(row);
var curPrev = null;
location.children().each(function(i) {
var child = $(this);
var cName = child.children('.row_key').text();
if(cName < lk) {
curPrev = child.attr('id');
}
});
if(curPrev == null) {
row.prependTo(location);
} else {
row.insertAfter(location.find('#' + curPrev));
}
newRow = true;
} else if(num>= 0){
$('div#' + row.attr('id') + ' > div.row_val', location).text(Math.floor(num));
}
}
if(rNeedsAppend && resources.children().length > 0) {
resources.prependTo(stores);
resources.animate({opacity: 1}, 300, 'linear');
}
if(sNeedsAppend && special.children().length > 0) {
special.appendTo(stores);
special.animate({opacity: 1}, 300, 'linear');
}
if(needsAppend && stores.find('div.storeRow').length > 0) {
stores.appendTo('div#storesContainer');
stores.animate({opacity: 1}, 300, 'linear');
}
if(wNeedsAppend && weapons.children().length > 0) {
weapons.appendTo('div#storesContainer');
weapons.animate({opacity: 1}, 300, 'linear');
}
if(newRow) {
Room.updateIncomeView();
}
if($("div#outsidePanel").length) {
Outside.updateVillage();
}
if($SM.get('stores.compass') && !Room.pathDiscovery){
Room.pathDiscovery = true;
Path.openPath();
}
},
updateIncomeView: function() {
var stores = $('div#resources');
var totalIncome = {};
if(stores.length === 0 || typeof $SM.get('income') == 'undefined') return;
$('div.storeRow', stores).each(function(index, el) {
el = $(el);
$('div.tooltip', el).remove();
var tt = $('<div>').addClass('tooltip bottom right');
var storeName = el.attr('id').substring(4).replace('-', ' ');
for(var incomeSource in $SM.get('income')) {
var income = $SM.get('income["'+incomeSource+'"]');
for(var store in income.stores) {
if(store == storeName && income.stores[store] !== 0) {
$('<div>').addClass('row_key').text(_(incomeSource)).appendTo(tt);
$('<div>')
.addClass('row_val')
.text(Engine.getIncomeMsg(income.stores[store], income.delay))
.appendTo(tt);
if (!totalIncome[store] || totalIncome[store].income === undefined) {
totalIncome[store] = { income: 0 };
}
totalIncome[store].income += Number(income.stores[store]);
totalIncome[store].delay = income.delay;
}
}
}
if(tt.children().length > 0) {
var total = totalIncome[storeName].income;
$('<div>').addClass('total row_key').text(_('total')).appendTo(tt);
$('<div>').addClass('total row_val').text(Engine.getIncomeMsg(total, totalIncome[storeName].delay)).appendTo(tt);
tt.appendTo(el);
}
});
},
buy: function(buyBtn) {
var thing = $(buyBtn).attr('buildThing');
var good = Room.TradeGoods[thing];
var numThings = $SM.get('stores["'+thing+'"]', true);
if(numThings < 0) numThings = 0;
if(good.maximum <= numThings) {
return;
}
var storeMod = {};
var cost = good.cost();
for(var k in cost) {
var have = $SM.get('stores["'+k+'"]', true);
if(have < cost[k]) {
Notifications.notify(Room, _("not enough " + k));
return false;
} else {
storeMod[k] = have - cost[k];
}
}
$SM.setM('stores', storeMod);
Notifications.notify(Room, good.buildMsg);
$SM.add('stores["'+thing+'"]', 1);
},
build: function(buildBtn) {
var thing = $(buildBtn).attr('buildThing');
if($SM.get('game.temperature.value') <= Room.TempEnum.Cold.value) {
Notifications.notify(Room, _("builder just shivers"));
return false;
}
var craftable = Room.Craftables[thing];
var numThings = 0;
switch(craftable.type) {
case 'good':
case 'weapon':
case 'tool':
case 'upgrade':
numThings = $SM.get('stores["'+thing+'"]', true);
break;
case 'building':
numThings = $SM.get('game.buildings["'+thing+'"]', true);
break;
}
if(numThings < 0) numThings = 0;
if(craftable.maximum <= numThings) {
return;
}
var storeMod = {};
var cost = craftable.cost();
for(var k in cost) {
var have = $SM.get('stores["'+k+'"]', true);
if(have < cost[k]) {
Notifications.notify(Room, _("not enough "+k));
return false;
} else {
storeMod[k] = have - cost[k];
}
}
$SM.setM('stores', storeMod);
Notifications.notify(Room, craftable.buildMsg);
switch(craftable.type) {
case 'good':
case 'weapon':
case 'upgrade':
case 'tool':
$SM.add('stores["'+thing+'"]', 1);
break;
case 'building':
$SM.add('game.buildings["'+thing+'"]', 1);
break;
}
},