-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.lua
1484 lines (1316 loc) · 30.1 KB
/
code.lua
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
-- title: Lockdown
-- author: BOOtak
-- desc: Keep yourself alive crafting food from your inventory
-- script: lua
T = 8
W = 240
H = 136
W3D=80
H3D=45
sf = string.format
UP=0
DOWN=1
LEFT=2
RIGHT=3
BTN_Z=4
BTN_X=5
-- helpers
function safe1( f, arg )
if f ~= nil then f(arg) end
end
function plural( i,one,pl )
local res = sf("%d %s", i, pl)
if i == 1 then res = sf("1 %s",one) end
return res
end
function deepcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[deepcopy(orig_key)] = deepcopy(orig_value)
end
setmetatable(copy, deepcopy(getmetatable(orig)))
else
copy = orig
end
return copy
end
function removeFrom(tab,obj,toDel)
if not table.contains(tab, obj) then
trace("table does not contain object!!!")
end
obj.__rem=true
obj.__del=toDel
end
function cleanup(tab)
for i = #tab, 1, -1 do
obj=tab[i]
if obj.__rem then
obj.__rem=false
table.remove(tab, i)
if obj.__del then obj=nil end
end
end
end
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
function table.matches( table1, table2 )
for i,v in ipairs(table1) do
if not table.contains(table2, v) then return false end
end
for i,v in ipairs(table2) do
if not table.contains(table1, v) then return false end
end
return true
end
-- draw helpers
function make_tex(c0,w,h)
tex={}
for i=1,h do
tex[i]={}
for j=1,w do
tex[i][j]=c0 + (j-1) + (i-1)*16
end
end
return tex
end
function draw_entity( e )
if e.sp == nil then return end
local offx,offy = e.offx, e.offy
if offx == nil then offx = 0 end
if offy == nil then offy = 0 end
for i,t in ipairs(e.sp) do
for j,v in ipairs(t) do
spr(v, e.x+(j-1)*T + offx, e.y+(i-1)*T + offy, 0)
end
end
end
function draw_entity_up( e )
if e.sp == nil then return end
local offx,offy = e.offx, e.offy
if offx == nil then offx = 0 end
if offy == nil then offy = 0 end
for i,t in ipairs(e.sp) do
for j,v in ipairs(t) do
spr(v, e.x+(j-1)*T + offx, e.y-(#t-i+1)*T + offy, 0)
end
end
end
function printframe(text,x,y,c,fc,small)
c = c or 15
fc = fc or 0
small = small or false
print(text,x-1,y,fc,false,1,small)
print(text,x+1,y,fc,false,1,small)
print(text,x,y-1,fc,false,1,small)
print(text,x,y+1,fc,false,1,small)
print(text,x-1,y-1,fc,false,1,small)
print(text,x+1,y-1,fc,false,1,small)
print(text,x-1,y+1,fc,false,1,small)
print(text,x+1,y+1,fc,false,1,small)
print(text,x,y,c,false,1,small)
end
-- log
function splittokens(s)
local res = {}
for w in s:gmatch("%S+") do
res[#res+1] = w
end
return res
end
function textwrap(text, linewidth)
if not linewidth then
linewidth = 75
end
local spaceleft = linewidth
local res = {}
local line = {}
for _, word in ipairs(splittokens(text)) do
if #word + 1 > spaceleft then
table.insert(res, table.concat(line, ' '))
line = {word}
spaceleft = linewidth - #word
else
table.insert(line, word)
spaceleft = spaceleft - (#word + 1)
end
end
table.insert(res, table.concat(line, ' '))
return res
end
LOGX,LOGY=10,110
LOG={}
LOGSTR_LIFETIME=180
CUR_HEIGHT=0
function add_log( str )
local tokens = textwrap(str,42)
for i,t in ipairs(tokens) do
table.insert(LOG,{str=t,t=0})
end
end
function update_log()
for i,v in ipairs(LOG) do
v.t = v.t + 1
if v.t > LOGSTR_LIFETIME then
removeFrom(LOG,v,true)
end
end
cleanup(LOG)
end
function draw_log()
local height = #LOG * T
if CUR_HEIGHT < height then
CUR_HEIGHT = CUR_HEIGHT + 1
elseif CUR_HEIGHT > height then
CUR_HEIGHT = height
end
for i,v in ipairs(LOG) do
printframe(v.str,LOGX,H-CUR_HEIGHT+(i-1)*T)
end
end
-- 3d
cam={
x=0,
y=0,
z=0
}
function point_3d( x,y,z )
local dz = z - cam.z
if dz <= 0.00001 then return nil,nil end
local x2d,y2d = (x-cam.x)/dz, (y-cam.y)/dz
local xnorm, ynorm = (x2d + W3D/2) / W3D, (y2d + H3D/2) / H3D
local xproj, yproj = xnorm * W, (-ynorm + 1) * H
return xproj, yproj
end
function line_3d( x1,y1,z1,x2,y2,z2,c )
xn1,yn1 = point_3d(x1,y1,z1)
xn2,yn2 = point_3d(x2,y2,z2)
if xn1 == nil or xn2 == nil then return end
line(xn1,yn1,xn2,yn2,c)
end
function v3( x,y,z )
return {x=x,y=y,z=z}
end
function sq( v )
return v*v
end
function v2dist( v1,v2 )
local p1,p2 = sq(v1.x-v2.x),sq(v1.y-v2.y)
local res = math.sqrt(p1+p2)
return res
end
function v3add( v1,v2 )
return {x=v1.x+v2.x,y=v1.y+v2.y,z=v1.z+v2.z}
end
function line_3dv( v1,v2,c )
line_3d(v1.x,v1.y,v1.z,v2.x,v2.y,v2.z,c)
end
function line_3dvv( vecs,c )
for i=1,#vecs-1 do
line_3dv(vecs[i], vecs[i+1], c)
end
end
function circb_3dv( v,r,c )
local cx,cy = point_3d(v.x,v.y,v.z)
if cx == nil then return end
local dx=v3(r,0,0)
local p = v3add(v,dx)
local px,py = point_3d(p.x,p.y,p.z)
if px == nil then return end
local r2d = math.abs(px-cx)
circb(cx,cy,r2d,c)
end
function rect_3d( x,y,z,w,h )
line_3d(x, y, z, x + w, y, z)
line_3d(x, y, z, x, y - h, z)
line_3d(x + w, y - h, z, x, y - h, z)
line_3d(x + w, y - h, z, x + w, y, z)
end
Names={
bw="Buckwheat",
tp="Toilet Paper",
bwp="Buckwheat porridge",
w="Water",
chr="Charcoal powder",
mlk="Milk",
bt="Butter",
flw="Buckwheat Flour",
brd="Bread",
ink="Ink",
pap="Paper",
scrl="Scroll of Wisdom",
ndls="Noodles",
snw="Sandwich",
crk="Cracker",
dftp="Deep Fried Toilet Paper",
mush="Mushy goo",
vngr="Vinegar",
chs="Cheese"
}
Items={
{
name=Names.tp,
nutr=0,
sp = make_tex(0, 2, 2),
spoil=-1
},
{
name=Names.bw,
nutr=10,
sp = make_tex(2, 2, 2),
spoil=-1
},
{
name=Names.bwp,
nutr=20,
sp = make_tex(6, 2, 2),
spoil=3
},
{
name=Names.w,
nutr=1,
sp = make_tex(4, 2, 2),
spoil=-1
},
{
name=Names.chr,
nutr=0,
sp=make_tex(8, 2, 2),
spoil=-1
},
{
name=Names.mlk,
nutr=5,
sp=make_tex(10, 2, 2),
spoil=5
},
{
name=Names.bt,
nutr=6,
sp=make_tex(12, 2, 2),
spoil=10
},
{
name=Names.flw,
nutr=3,
sp=make_tex(14, 2, 2),
spoil=20
},
{
name=Names.brd,
nutr=8,
sp=make_tex(32, 2, 2),
spoil=15
},
{
name=Names.ink,
nutr=0,
sp=make_tex(34, 2, 2),
spoil=-1
},
{
name=Names.pap,
nutr=0,
sp=make_tex(36, 2, 2),
spoil=-1
},
{
name=Names.scrl,
nutr=0,
sp=make_tex(38, 2, 2),
spoil=-1
},
{
name=Names.ndls,
nutr=10,
sp=make_tex(40, 2, 2),
spoil=50
},
{
name=Names.dftp,
nutr=8,
sp=make_tex(42, 2, 2),
spoil=-1
},
{
name=Names.snw,
nutr=10,
sp=make_tex(44, 2, 2),
spoil=10
},
{
name=Names.crk,
nutr=6,
sp=make_tex(46, 2, 2),
spoil=-1
},
{
name=Names.mush,
nutr=5,
sp=make_tex(64, 2, 2),
spoil=1
},
{
name=Names.vngr,
nutr=0,
sp=make_tex(66, 2, 2),
spoil=-1
},
{
name=Names.chs,
nutr=12,
sp=make_tex(68, 2, 2),
spoil=30
}
}
Button={
x=0,
y=0,
w=0,
h=0,
pressed=false,
hover=false,
color=0,
sp={},
offx=0,
offy=0,
on_enter=nil,
on_hover=nil,
on_press=nil,
on_release=nil,
on_leave=nil
}
Inventory={
name="Inventory",
items={},
size=15
}
Hand={
name="Hand",
items={},
size=1
}
Craftstable={
name="Craftstable",
items={},
size=2
}
ALL_INVENTORIES={
Inventory,
Hand,
Craftstable
}
function enhance_craftstable()
if Craftstable.size == 4 then return end
Craftstable.size = 4
add_log("The wisdom of the scroll allows you to craft more complicated recepies!")
init_buttons()
end
Recepies={
{
items={Names.bw, Names.w},
res=Names.bwp
},
{
items={Names.tp},
res=Names.chr
},
{
items={Names.mlk},
res=Names.bt
},
{
items={Names.bw},
res=Names.flw
},
{
items={Names.tp, Names.w},
res=Names.pap
},
{
items={Names.flw, Names.mlk},
res=Names.brd
},
{
items={Names.flw, Names.w},
res=Names.ndls
},
{
items={Names.chr, Names.w},
res=Names.ink
},
{
items={Names.ink, Names.pap},
res=Names.scrl
},
{
items={Names.tp, Names.bt, Names.flw},
res=Names.dftp
},
{
items={Names.scrl},
res={magic=enhance_craftstable}
},
{
items={Names.brd, Names.bt},
res=Names.snw
},
{
items={Names.brd},
res=Names.crk
},
{
items={Names.crk,Names.mlk},
res=Names.mush
},
{
items={Names.mush,Names.w},
res=Names.vngr
},
{
items={Names.vngr,Names.mlk},
res=Names.chs
},
}
HEALTH=100
TIME=0
-- buttons
function g_press( btn )
btn.color = 6
end
function g_release( btn )
btn.color = 7
end
function g_hover( btn )
btn.color = 8
end
function g_leave( btn )
btn.color = btn.orig_c
end
function make_button( x, y, w, h, color, on_hover, on_leave, on_press, on_release, on_enter, sp, offx, offy, on_draw )
local btn = deepcopy(Button)
btn.x, btn.y = x,y
btn.w, btn.h = w,h
btn.color = color
btn.orig_c = color
btn.on_hover = on_hover
btn.on_leave = on_leave
btn.on_press = on_press
btn.on_release = on_release
btn.on_enter = on_enter
btn.sp = sp
btn.offx = offx
btn.offy = offy
btn.on_draw = on_draw
return btn
end
function check_button( btn, mx, my, md )
local x,y,r,d = btn.x, btn.y, btn.x + btn.w, btn.y + btn.h
local old_hover = btn.hover
local old_pressed = btn.pressed
if x <= mx and r >= mx and y <= my and d >= my then
btn.hover = true
if md then
btn.pressed = true
else
btn.pressed = false
end
else
btn.hover = false
btn.pressed = false
end
if btn.hover then
safe1(btn.on_hover, btn)
end
if old_hover and not btn.hover then
safe1(btn.on_leave, btn)
elseif not old_hover and btn.hover then
safe1(btn.on_enter, btn)
end
if old_pressed and not btn.pressed then
safe1(btn.on_release, btn)
elseif btn.pressed and not old_pressed then
safe1(btn.on_press, btn)
end
end
function draw_text_btn( btn,text )
rect(btn.x, btn.y, btn.w, btn.h, btn.color)
rectb(btn.x, btn.y, btn.w, btn.h, 2)
local dx,dy=0,0
if btn.pressed then
rectb(btn.x + 1, btn.y + 1, btn.w - 1, btn.h - 1, 2)
dx,dy=1,1
else
rectb(btn.x, btn.y, btn.w - 1, btn.h - 1, 2)
end
local tw,th = print(text, W,H),8
printframe(text, btn.x + btn.w/2 - tw/2 + dx, btn.y + btn.h/2 - th/2 + dy, 6)
end
function draw_craft_btn( btn )
draw_text_btn(btn,"Craft!")
end
function draw_eat_btn( btn )
draw_text_btn(btn,"I'm ready!")
end
function draw_button( btn )
if btn.on_draw ~= nil then
btn.on_draw(btn)
return
end
rect(btn.x, btn.y, btn.w, btn.h, btn.color)
rectb(btn.x, btn.y, btn.w, btn.h, 2)
rectb(btn.x + 1, btn.y + 1, btn.w - 1, btn.h - 1, 2)
if btn.item ~= nil then
local item,x,y,w,h = btn.item,btn.x,btn.y,btn.w,btn.h
local ent = {x=x, y=y, sp=item.sp, offx=btn.offx, offy=btn.offy}
draw_entity(ent)
local count = item.count
local width = print(count,W,H,15,false,1,true)
printframe(count,x+w-width-1,y+h-6,15,0,true)
end
end
-- inventory
function make_item( name,count )
for i,it in ipairs(Items) do
if it.name == name then
new_item = deepcopy(it)
new_item.count = count
return new_item
end
end
end
function inventory_get( inv, item )
for i,it in ipairs(inv.items) do
if it.name == item.name and it.spoil == item.spoil then return it end
end
return nil
end
function inventory_get_count( inv, item, count )
local it = inventory_get(inv, item)
if it.count >= count then
return it
else
return nil
end
end
function inventory_take( inv, item, count )
for i,it in ipairs(inv.items) do
if it.name == item.name and it.spoil == item.spoil then
local taken = math.min(count, it.count)
local new_item = make_item(it.name, taken)
new_item.spoil = it.spoil
it.count = it.count - taken
if it.count == 0 then
removeFrom(inv.items, it, true)
end
return new_item
end
end
return nil
end
function inventory_get_by_name( inv, name )
res = {}
for i,it in ipairs(inv.items) do
if it.name == name then table.insert(res, it) end
end
return res
end
function inventory_add_item( inv, item )
local same_items = inventory_get_by_name(inv, item.name)
for i,same in ipairs(same_items) do
if same.spoil == item.spoil then
same.count = same.count + item.count
return true
end
end
if inv.size > #inv.items then
table.insert(inv.items, item)
return true
end
return false
end
function inventory_add( inv, name, count )
local new_item = make_item(name, count)
return inventory_add_item(inv, new_item)
end
function check_recepie( names )
for i,recepie in ipairs(Recepies) do
if table.matches(recepie.items, names) then return recepie.res end
end
return nil
end
function make_recepie( inv )
names = {}
if #inv.items == 0 then
add_log("Place ingredients here to craft new item")
return
end
namestr = ""
min_item_count = -1
for i,it in ipairs(inv.items) do
table.insert(names, it.name)
if string.len(namestr) == 0 then namestr = it.name
else namestr = sf("%s, %s ", namestr, it.name) end
if min_item_count == -1 or min_item_count > it.count then min_item_count = it.count end
end
if min_item_count == 0 then
trace("unable to make 0 items")
return false
end
local res = check_recepie(names)
if res ~= nil then
res_count = min_item_count * #inv.items
-- check if have all items to remove
for i,it in ipairs(inv.items) do
local test = inventory_get_count(inv, it, min_item_count)
if test == nil or test.count == 0 then return false end
end
-- remove items to make room for new item
for i,it in ipairs(inv.items) do
inventory_take(inv, it, min_item_count)
end
-- cleanup
cleanup(inv.items)
-- add new item
if res.magic ~= nil then
res.magic()
return true
elseif not inventory_add(inv, res, res_count) then
trace(sf("Unable to add %s to inventory", res))
return false
end
else
add_log(sf("No recepie with %s!", namestr))
return false
end
for i,itm in ipairs(Items) do
if itm.name == res and not itm.discovered then
itm.discovered = true
add_log(sf("New discovery: %s!", res))
end
end
return true
end
function update_spoil( inv )
for i,it in ipairs(inv.items) do
if it.spoil ~= -1 then
it.spoil = it.spoil - 1
if it.spoil == 0 then
add_log(sf("%d pcs of %s have spoiled!", it.count, it.name))
removeFrom(inv.items, it, true)
end
end
end
end
function on_inventory_hover( btn )
g_hover(btn)
if btn.item ~= nil and btn.item.count > 0 then
if #Hand.items > 0 then return end
local dx,dy = 5, 5
local mx,my = mouse()
local it = btn.item
local exp_str = sf("in %d days", it.spoil)
if it.spoil == -1 then exp_str = "never"
elseif it.spoil == 1 then exp_str = "in 1 day" end
local text = sf("%s\nNutrition: %d\nExpires: %s", it.name, it.nutr, exp_str)
local width = print(text, W, H)
local x,y,w,h=math.min(W-width, mx + dx), my+dy, width+6, 24
rect(x-4,y-4,w,h,8)
rectb(x-3,y-3,w,h,2)
printframe(text, x,y)
end
end
function move_item( to, from, item, count )
local temp_item = inventory_get_count(from, item, count)
if temp_item ~= nil then
local new_item = deepcopy(temp_item)
new_item.count = count
if inventory_add_item(to, new_item) then
inventory_take(from, item, count)
return true
else
return false
end
end
return false
end
after_craft = false
function on_inventory_click( btn )
if btn.item ~= nil and btn.inv ~= nil then
local inv_item = inventory_get(btn.inv, btn.item)
local to_take = 1
if btn.inv.name == "Craftstable" and after_craft then
to_take=inv_item.count
after_craft = false
end
if #Hand.items == 0 then
move_item(Hand, btn.inv, inv_item, to_take)
else
if Hand.prev_click == nil or Hand.prev_click == btn.inv then
if not move_item(Hand, btn.inv, inv_item, to_take) then
local hand_item = Hand.items[1]
if inventory_add_item(btn.inv, hand_item) then
Hand.items = {}
move_item(Hand, btn.inv, inv_item, to_take)
end
end
else
move_item(btn.inv, Hand, Hand.items[1], Hand.items[1].count)
end
end
elseif #Hand.items > 0 then
move_item(btn.inv, Hand, Hand.items[1], Hand.items[1].count)
end
Hand.prev_click = btn.inv
end
function on_craft_click( btn )
if make_recepie(Craftstable) then
after_craft = true
end
end
function on_eat_click( btn )
state = EATING
end
function draw_buttons( btns )
for i,v in ipairs(btns) do
draw_button(v)
end
end
function update_buttons( btns )
local mx,my,md = mouse()
draw_buttons(btns)
for i,v in ipairs(btns) do
check_button(v, mx, my, md)
end
end
function draw_inventory( inv )
for i,btn in ipairs(INV_BUTTONS) do
btn.item=nil
btn.inv=inv
end
for i,it in ipairs(inv.items) do
INV_BUTTONS[i].item=it
end
end
function draw_craft_table( inv )
for i,btn in ipairs(CRAFT_BUTTONS) do
btn.item=nil
btn.inv=inv
end
for i,it in ipairs(inv.items) do
CRAFT_BUTTONS[i].item=it
end
end
function draw_hand( inv )
local mx,my = mouse()
local dx,dy = -15, -15
if #inv.items == 1 and inv.items[1].count > 0 then
local item = inv.items[1]
draw_entity({x=mx+dx,y=my+dy,sp=item.sp})
local width = print(item.count, W, H, 0, false, 1, true)
printframe(item.count, mx+dx+16-width, my+dy+10, 15, 0, true)
end
end
angle=0
angle1=0
a_speed=0.015
w_speed=0.01
function move_cam()
angle = angle + a_speed
angle1=angle1 + w_speed
cam.x = math.cos(angle) * AMP_XY
cam.y = math.sin(angle) * AMP_XY
cam.z = 0.2+math.sin(angle1) * AMP_Z
end
function map_to( oldmin,oldmax,newmin,newmax,val )
local norm = (val-oldmin) / (oldmax-oldmin)
return norm * (newmax-newmin) + newmin
end
function draw_person(b1,fdup,anim)
fdup=fdup or 0
anim=anim or false
t2=v3add(b1,v3(10,10,0))
t3=v3add(b1,v3(8,10,-0.2))
b2=v3add(b1,v3(12,0,0))
b3=v3add(b1,v3(10,0,-0.25))
line_3dvv({b1,t2,b2},2)
line_3dvv({b1,t3,b3},2)
-- top part
t1=v3add(b1,v3(0,18,0))
local r=4
dy=v3(0,r,0)
local h = v3add(dy,t1)
b2=v3add(t1,v3(6,0,0))
b3=v3add(t1,v3(4,0,-0.2))
t2=v3add(t1,v3(2,5,0))
t3=v3add(t1,v3(0,5,-0.1))
pangle = map_to(-1,1,min_angle,max_angle,math.sin(pdangle))
pdangle = pdangle + pspeed
local to_tilt = {t1,b2,b3,t2,t3,h}
if fdup > 0 then to_tilt = {b2,b3,t2,t3,h} end
for i,v in ipairs(to_tilt) do
local dst = v2dist(v,b1)
local dangle = math.atan(v.y-b1.y,v.x-b1.x)
local center = b1
local ms,mc = math.sin,math.cos
if fdup > 1 then center = t1 end
local temp = v3add(center, v3(dst*mc(dangle+pangle),dst*ms(dangle+pangle), 0))
v.x,v.y=temp.x,temp.y
end
circb_3dv(h,r,2)
line_3dvv({t1,b2,t2},2)
line_3dvv({t1,b3,t3},2)
line_3dv(b1,t1,2)
end
FH=20
FW=18
TW=24
TD=0.5
LX = -W3D / 2 - 10
TY = H3D / 2 + 10
RX = W3D / 2 + 10
BY = -H3D / 2 - 10
function draw_table(p0, drop)
local l1,l2,l3,l4 = p0, v3add(p0, v3(0,0,TD)), v3add(p0, v3(TW,0,TD)), v3add(p0, v3(TW,0,0))
local dy = v3(0,FH,0)
local t1,t2,t3,t4 = v3add(l1,dy), v3add(l2,dy), v3add(l3,dy), v3add(l4,dy)
if drop then t1,t2,t3,t4,l1,l2,l3,l4=l1,l2,l3,l4,t1,t2,t3,t4 end
line_3dv(l1, t1, 2)
line_3dv(l2, t2, 2)
line_3dv(l3, t3, 2)
line_3dv(l4, t4, 2)
line_3dvv({t1, t2, t3, t4, t1}, 2)
end
function draw_room(stick)
stick = stick or false
-- walls
local tlx, tly = LX, TY
local trx, try = RX, TY