DEADSOFTWARE

04446b3bf80dc50ba51180b00352480d03a26965
[cavecraft.git] / src / CAVE.mpsrc
1 program CAVE;
3 uses
4 sign,
5 jsr75i,
6 drop,
7 invui,
8 vars,
9 particles_store,
10 randoms,
11 memory,
12 worldgen,
13 canvas,
14 items,
15 safeload,
16 mob,
17 console,
18 effects,
19 particles,
20 keyboard,
21 maps,
22 phy,
23 utils,
24 func,
25 CellUI,
26 furnace,
27 player,
28 items_logic,
29 chest,
30 inv,
31 items_store,
32 video;
34 const
35 version = 'BETA 9';
36 version_map = 9;
38 var
39 keymode,updx,updy:integer;
40 seed,nextseed:integer;
41 free_ram:real;
43 light:array [0..15] of image;
44 bg:array[0..1] of image;
45 osad:array [0..1,0..7] of image;
46 back:array [0..8] of image;
47 gui:array [0..34] of image;
48 tue:array [0..9] of image;
50 sign_im:image;
51 sky:image;
52 sun:image;
53 moon:image;
54 moon_phase:integer;
55 toolus,toolind:integer;
56 osadki_ani:integer;
57 sd,sav_fl:string;
58 global_light:integer;
59 world_typ:integer;
60 float:image;
62 msg:array [0..4] of string;
63 msg_time:array[0..4] of integer;
64 last_sleep_x,last_sleep_y:integer;
66 ifminimap:boolean;
67 gt:integer;
69 del,dt,time:integer;
71 deb:boolean;
73 procedure newworld;
74 var
75 ix,iy:integer;
76 begin
77 if nextseed=0 then seed:=getrelativetimems; else seed:=nextseed;
78 nextseed:=0;
79 srand(seed);
80 if world_typ=0 then genworld;
81 else
82 if world_typ=1 then genflat;
83 world_typ:=0;
84 player.setX(get_spawn_x*16+4);
85 player.setY(get_spawn_y*16);
86 pl_world:=0;
87 end;
89 procedure drwrect(x,y,w,h,t:integer);
90 var
91 i:integer;
92 begin
93 for i:=0 to t do
94 drawrect(x+i,y+i,w-i*2,h-i*2);
95 end;
97 procedure proc_fps;
98 begin
99 dt:=GetRelativeTimeMs - time;
100 time:=GetRelativeTimeMs;
101 fps:=1000/dt;
102 end;
104 procedure maxfps;
105 begin
106 if fps<s_max_fps then if del>0 then del:=del-1;
107 if fps>s_max_fps then if del<200 then del:=del+1;
108 delay(del);
109 end;
111 procedure cleartextures;
112 var
113 no:image;
114 ix,iy,iz:integer;
115 begin
116 bg[0]:=no;
117 bg[1]:=no;
118 float:=no;
119 compas:=no;
120 for ix:=0 to 7 do clock[ix]:=no;
122 {for ix:=0 to maxBlockTex do
123 begin
124 tex[ix]:=no;
125 tex8[ix]:=no;
126 end;
127 for ix:=0 to maxItemTex do
128 begin
129 item[ix]:=no;
130 item8[ix]:=no;
131 end;}
132 for ix:=0 to 8 do back[ix]:=no;
134 player.freeSkin;
136 for ix:=0 to 34 do
137 gui[ix]:=no;
139 for ix:=0 to 9 do tue[ix]:=no;
141 for ix:=0 to 7 do osad[0,ix]:=no;
142 for ix:=0 to 7 do osad[1,ix]:=no;
144 for ix:=0 to 15 do light[ix]:=no;
146 for ix:=0 to 7 do
147 begin
148 pr_1[ix]:=no;
149 pr_2[ix]:=no;
150 pr_3[ix]:=no;
151 pr_4[ix]:=no;
152 pr_5[ix]:=no;
153 end;
154 bubble:=no;
156 for ix:=0 to 15 do
157 pr_boom[ix]:=no;
159 mob.freeSkin;
161 sky:=no;
163 sun:=no;
164 moon:=no;
166 sign_im:=no;
168 for ix:=0 to CONST_MAX_CURS do
169 LoadCurImg(no, ix);
171 //resetVirtualKeyboard(-1);
172 end;
174 procedure create_msg(s:string);
175 var
176 i,t:integer;
177 begin
178 for i:=3 downto 0 do
179 begin
180 if i=0 then break;
181 msg_time[i]:=msg_time[i-1];
182 msg[i]:=msg[i-1];
183 end;
184 msg_time[0]:=getrelativetimems;
185 msg[0]:=s;
186 end;
188 procedure loadtexture(path:string);
189 var
190 ix,iy,iz:integer;
191 im:image;
192 begin
193 cleartextures;
194 im:=ld_tex('background.png',path,'title/');
195 for ix:=0 to 1 do
196 bg[ix]:=rotate_image_from_image(im,ix*16,0,16,16,0);
197 float:=ld_tex('float.png',path,'gui/');
198 compas:=ld_tex('compass.png',path,'gui/');
199 im:=ld_tex('clock.png',path,'gui/');
200 for ix:=0 to 7 do
201 clock[ix]:=rotate_image_from_image(im,ix*16,0,16,16,0);
202 if load_back_tex then
203 begin
204 im:=ld_tex('back.png',path,'');
205 for ix:=0 to 8 do back[ix]:=rotate_image_from_image(im,ix*16,0,16,16,0);
206 end;
208 player.loadSkin('char_ani.png', path);
209 mob.loadSkin(path);
211 im:=ld_tex('gui.png',path,'gui/');
212 gui[0]:=rotate_image_from_image(im,0,0,16,16,0);
213 LoadCurImg(gui[0], CUR_SELECT1);
214 gui[1]:=rotate_image_from_image(im,0,16,168,21,0);
215 gui[3]:=rotate_image_from_image(im,16,0,16,16,0);
216 LoadCurImg(gui[3], CUR_SELECT2);
217 gui[6]:=rotate_image_from_image(im,0,56,120,12,0);
218 gui[7]:=rotate_image_from_image(im,0,68,120,12,0);
219 gui[8]:=rotate_image_from_image(im,0,80,120,12,0);
220 gui[13]:=rotate_image_from_image(im,1,93,9,9,0);//hp
221 gui[14]:=rotate_image_from_image(im,12,93,9,9,0);//hp
222 gui[15]:=rotate_image_from_image(im,23,93,9,9,0);//hp
223 gui[17]:=rotate_image_from_image(im,120,56,18,18,0);
225 gui[26]:=rotate_image_from_image(im,89,93,9,9,0);//hunger
226 gui[27]:=rotate_image_from_image(im,100,93,9,9,0);//hunger
227 gui[28]:=rotate_image_from_image(im,111,93,9,9,0);//hunger
229 gui[29]:=rotate_image_from_image(im,67,93,9,9,0);//air
230 gui[30]:=rotate_image_from_image(im,78,93,9,9,0);//air
232 gui[31]:=rotate_image_from_image(im,122,93,9,9,0);//hp hardcore
233 gui[32]:=rotate_image_from_image(im,133,93,9,9,0);//hp hardcore
234 gui[33]:=rotate_image_from_image(im,144,93,9,9,0);//hp hardcore
236 gui[20]:=rotate_image_from_image(im,121,75,9,13,0);
237 gui[21]:=rotate_image_from_image(im,130,75,9,13,0);
238 gui[22]:=rotate_image_from_image(im,0,103,120,12,0);
240 if load_minimap_tex then gui[16]:=ld_tex('mapbg.png',path,'gui/');
242 for ix:=0 to 9 do tue[ix]:=rotate_image_from_image(im,16*ix,40,16,16,0);
244 if load_weather_tex then
245 begin
246 im:=ld_tex('rain.png',path,'terrain/');
247 for ix:=0 to 7 do osad[0,ix]:=rotate_image_from_image(im,16*ix,0,16,16,0);
248 im:=ld_tex('snow.png',path,'terrain/');
249 for ix:=0 to 7 do osad[1,ix]:=rotate_image_from_image(im,16*ix,0,16,16,0);
250 end;
252 if load_light_tex then
253 begin
254 im:=ld_tex('light.png',path,'terrain/');
255 for ix:=0 to 15 do light[ix]:=rotate_image_from_image(im,16*ix,0,16,16,0);
256 end;
258 if load_particles_tex then
259 begin
260 im:=ld_tex('particles.png',path,'terrain/');
261 for ix:=0 to 7 do
262 begin
263 pr_1[ix]:=rotate_image_from_image(im,8*ix,0,8,8,0);
264 pr_2[ix]:=rotate_image_from_image(im,8*ix,8,8,8,0);
265 pr_3[ix]:=rotate_image_from_image(im,8*ix,16,8,8,0);
266 pr_4[ix]:=rotate_image_from_image(im,8*ix,24,8,8,0);
267 pr_5[ix]:=rotate_image_from_image(im,8*ix,32,8,8,0);
268 end;
269 bubble:=rotate_image_from_image(im,0,40,8,8,0);
270 im:=ld_tex('explosion.png',path,'terrain/');
271 for ix:=0 to 15 do
272 pr_boom[ix]:=rotate_image_from_image(im,32*ix,0,32,32,0);
273 end;
275 im:=ld_tex('partition.png',path,'gui/');
276 gui[18]:=rotate_image_from_image(im,0,0,84,42,0);
277 gui[19]:=rotate_image_from_image(im,0,42,4,4,0);
279 if load_sky_siz<=0 then
280 sky:=ld_tex('sky.png',path,'terrain/');
281 else
282 sky:=resize_image(ld_tex('sky.png',path,'terrain/'),load_sky_siz,2);
284 if load_sm=1 then
285 begin
286 sun:=ld_tex('sun.png',path,'terrain/');
287 moon:=ld_tex('moon_phase_0.png',path,'terrain/moon_phases/');
288 end; else
289 if load_sm=2 then
290 begin
291 sun:=resize_image(ld_tex('sun.png',path,'terrain/'),(getWidth+getHeight)/5,(getWidth+getHeight)/5);
292 moon:=resize_image(ld_tex('moon_phase_0.png',path,'terrain/moon_phases/'),(getWidth+getHeight)/5,(getWidth+getHeight)/5);
293 end;
295 //load_virt_tex(ld_tex('touch.png',path,'gui/'));
296 end;
298 procedure cleargame;
299 var
300 ix,iy:integer;
301 begin
302 jmp:=false;
303 keymode:=0;
304 updx:=0;
305 updy:=0;
306 toolus:=0;
307 toolind:=0;
308 osadki_ani:=0;
309 global_light:=15;
310 pl_world:=0;
312 camx:=0;
313 camy:=0;
314 curx:=0;
315 cury:=0;
317 player.setX(0);
318 player.setY(0);
319 last_sleep_x:=0;
320 last_sleep_y:=0;
322 fly:=false;
323 game_time:=0;
324 clock_stage:=0;
325 hp:=20;
326 hunger:=20;
327 air:=21;
328 moon_phase:=0;
329 posi:=0;
330 velx:=0;
331 vely:=0;
332 invslot:=0;
334 osadki:=false;
336 //anim_del2:=0;
338 inv.resetData;
340 chest.resetData;
342 furnace.resetData;
344 drop.resetData;
346 mob.resetData;
348 //gb_up_pa:=0;
349 reset_particles(max_particles+1);
351 for ix:=0 to 255 do
352 begin
353 setBackMap(0, ix);
354 setBiomMap(0, ix);
355 for iy:=0 to 127 do
356 begin
357 setmap(0,ix,iy);
358 setmapinfo(0,ix,iy);
359 setmaplight(0,ix,iy);
360 end;
361 end;
363 for ix:=0 to 31 do
364 begin
365 b_sign[ix]:=false;
366 t_sign[ix]:='';
367 end;
369 for ix:=0 to CONST_MAX_LAYERS do
370 ClearLayer(ix);
371 end;
373 procedure drw_load_line(s:string;percent:integer);
374 var
375 ix,iy:integer;
376 begin
377 for ix:=0 to getWidth/16 do
378 for iy:=0 to getHeight/16 do
379 drawimage(bg[0],ix*16,iy*16);
380 drawfonttext(s,(getWidth/2)-(length(s)*8/2),(getHeight/2)-8);
381 setcolor(128,128,128);
382 fillrect(getwidth/2-50,getheight/2+6,100,3);
383 setcolor(128,255,128);
384 fillrect(getwidth/2-50,getheight/2+6,percent,3);
385 drawfonttext(integertostring(percent)+'%',getwidth/2-(length(integertostring(percent)+'%')*8)/2,getheight/2+12);
386 drawVideo;
387 end;
389 function getFlatMap(i:integer):integer;
390 var
391 ix, iy:integer;
392 begin
393 iy:=i div 256;
394 ix:=i-iy*256;
395 getFlatMap:=getmap(ix,iy);
396 end;
398 procedure setFlatMap(n, i:integer);
399 var
400 ix, iy:integer;
401 begin
402 iy:=i div 256;
403 ix:=i-iy*256;
404 setmap(n,ix,iy);
405 end;
407 function getFlatMapInfo(i:integer):integer;
408 var
409 ix, iy:integer;
410 begin
411 iy:=i div 256;
412 ix:=i-iy*256;
413 getFlatMapInfo:=getmapinfo(ix,iy);
414 end;
416 procedure setFlatMapInfo(n, i:integer);
417 var
418 ix, iy:integer;
419 begin
420 iy:=i div 256;
421 ix:=i-iy*256;
422 setmapinfo(n,ix,iy);
423 end;
425 procedure SaveMapRLE;
426 var
427 i:integer;
428 id, s:integer;
429 begin
430 repeat
431 id:=getFlatMap(i);
432 for s:=0 to 255 do
433 if (id<>getFlatMap(i+s)) or (i+s>32767) then
434 break;
436 write_byte(id);
437 write_byte(s-1);
439 i:=i+s;
440 until i>32767;
441 end;
443 procedure SaveMapInfoRLE;
444 var
445 i:integer;
446 id, s:integer;
447 begin
448 repeat
449 id:=getFlatMapInfo(i);
450 for s:=0 to 255 do
451 if (id<>getFlatMapInfo(i+s)) or (i+s>32767) then
452 break;
454 write_byte(id);
455 write_byte(s-1);
457 i:=i+s;
458 until i>32767;
459 end;
461 procedure LoadMapRLE;
462 var
463 i:integer;
464 id, s, j:integer;
465 begin
466 repeat
467 id:=read_byte;
468 s:=read_byte;
469 for j:=0 to s do
470 setFlatMap(id, i+j);
472 i:=i+s+1;
473 until i>32767;
474 end;
476 procedure LoadMapInfoRLE;
477 var
478 i:integer;
479 id, s, j:integer;
480 begin
481 repeat
482 id:=read_byte;
483 s:=read_byte;
484 for j:=0 to s do
485 setFlatMapInfo(id, i+j);
487 i:=i+s+1;
488 until i>32767;
489 end;
491 procedure saveworld(path:string);
492 var
493 ix,iy:integer;
494 world_name:string;
495 begin
496 if pl_world=0 then world_name:='world.sav';
497 else
498 if pl_world=1 then world_name:='nether.sav';
499 if file_exists(path)<>1 then if create_folder(path)=1 then debug('Folder created!');
500 if file_exists(path+world_name)>0 then delete_file(path+world_name);
501 if file_exists(path+world_name)<>1 then file_create(path+world_name);
502 if open_file(path+world_name)=1 then
503 begin
504 writeint(player.getX);
505 writeint(player.getY);
506 //Matrix
507 SaveMapRLE;
508 SaveMapInfoRLE;
509 drw_load_line('Background',50);
510 //Background and biomes
511 for ix:=0 to 255 do
512 begin
513 write_byte(getBackMap(ix));
514 write_byte(getBiomMap(ix));
515 end;
516 drw_load_line('Chests',55);
517 //Chests
518 chest.saveData;
519 drw_load_line('Furnaces',60);
520 //Furnaces
521 furnace.saveData;
522 drw_load_line('Mobs',70);
523 //Mobs
524 mob.saveData;
525 drw_load_line('Drop',80);
526 //Drop
527 drop.saveData;
528 drw_load_line('Particles',85);
529 //Particles
530 writeint(max_particles);
531 write_byte(gb_up_pa);
532 for ix:=0 to max_particles do
533 begin
534 write_byte(get_particle_type(ix));
535 write_byte(get_particle_ani(ix));
536 writeint(get_particle_x(ix));
537 writeint(get_particle_y(ix));
538 end;
539 drw_load_line('Other',90);
540 //Other
541 write_byte(updx);
542 write_byte(updy);
543 writebool(osadki);
544 write_byte(osadki_ani);
545 write_byte(global_light);
546 writebool(fly);
547 writeint(game_time);
548 write_byte(clock_stage);
549 for ix:=0 to 31 do
550 begin
551 writebool(b_sign[ix]);
552 writestr(t_sign[ix]);
553 end;
554 flush;
555 drw_load_line('Ready',100);
556 if close_file(path+world_name)=1 then debug('World Saved!');
557 end;
558 end;
560 procedure savegame(path:string);
561 var
562 ix,iy:integer;
563 begin
564 drw_load_line('Basic',0);
565 if file_exists(path+'player.dat')=1 then delete_file(path+'player.dat');
566 if file_exists(path)<>1 then if create_folder(path)=1 then debug('Folder created!');
567 if file_exists(path+'player.dat')<>1 then file_create(path+'player.dat');
568 if open_file(path+'player.dat')=1 then
569 begin
570 //Head
571 write_byte(version_map);
572 write_byte(gamemode);
573 writebool(cheats);
574 writeint(seed);
575 write_byte(pl_world);
576 //Player
577 writeint(last_sleep_x);
578 writeint(last_sleep_y);
579 write_byte(velx);
580 write_byte(vely);
581 write_byte(invslot);
582 write_byte(posi);
583 write_byte(hp);
584 write_byte(hunger);
585 write_byte(moon_phase);
586 writebool(jmp);
587 inv.saveData;
588 if close_file(path+'player.dat')=1 then debug('Saved!');
589 drw_load_line('Matrix',10);
590 saveworld(path);
591 end;
592 end;
595 function version_err(ver:integer):boolean;
596 var
597 ix,iy:integer;
598 begin
599 for ix:=0 to getWidth/16 do
600 for iy:=0 to getHeight/16 do
601 begin
602 drawimage(bg[0],ix*16,iy*16);
603 end;
604 if version_map=ver then version_err:=true; else
605 if version_map>ver then
606 begin
607 drawfonttext('Old save format!',(getWidth/2)-60,(getHeight/2)-4);
608 drawVideo;
609 delay(3000);
610 version_err:=false;
611 end; else
612 if version_map<ver then
613 begin
614 drawfonttext('New save format!',(getWidth/2)-60,(getHeight/2)-4);
615 drawVideo;
616 delay(3000);
617 version_err:=false;
618 end;
619 end;
622 function loadworld(path:string):boolean;
623 var
624 ix,iy,ver:integer;
625 world_name:string;
626 begin
627 if pl_world=0 then world_name:='world.sav';
628 else
629 if pl_world=1 then world_name:='nether.sav';
630 if file_exists(path)=1 then
631 begin
632 if file_exists(path+world_name)=1 then
633 begin
634 if open_file(path+world_name)=1 then
635 begin
636 player.setX(readint);
637 player.setY(readint);
638 //Matrix
639 LoadMapRLE;
640 LoadMapInfoRLE;
641 drw_load_line('Background',50);
642 //Background and biomes
643 for ix:=0 to 255 do
644 begin
645 setBackMap(read_byte, ix);
646 setBiomMap(read_byte, ix);
647 end;
648 drw_load_line('Chests',55);
649 //Chests
650 chest.loadData;
651 drw_load_line('Furnaces',60);
652 //Furnaces
653 furnace.loadData;
654 //Mobs
655 mob.loadData;
656 drw_load_line('Drop',80);
657 //Drop
658 drop.loadData;
659 drw_load_line('Particles',85);
660 //Particles
661 max_particles:=readint;
662 reset_particles(max_particles+1);
663 gb_up_pa:=read_byte;
664 for ix:=0 to max_particles do
665 begin
666 set_particle_type(ix,read_byte);
667 set_particle_ani(ix,read_byte);
668 set_particle_x(ix,readint);
669 set_particle_y(ix,readint);
670 end;
671 drw_load_line('Other',90);
672 //Other
673 updx:=read_byte;
674 updy:=read_byte;
675 osadki:=readbool;
676 osadki_ani:=read_byte;
677 global_light:=read_byte;
678 fly:=readbool;
679 game_time:=readint;
680 clock_stage:=read_byte;
681 for ix:=0 to 31 do
682 begin
683 b_sign[ix]:=readbool;
684 t_sign[ix]:=readstr;
685 end;
686 drw_load_line('Ready',100);
687 ///////////////////////
688 if close_file(path+world_name)=1 then begin loadworld:=true; debug('World loaded!'); end;
689 end; else begin loadworld:=false; debug('File not opened!'); end;
690 end; else begin loadworld:=false; debug('File not exists!'); end;
691 end; else begin loadworld:=false; debug('Folder not exists!'); end;
692 end;
694 function loadgame(path:string):boolean;
695 var
696 ix,iy,ver:integer;
697 begin
698 drw_load_line('Basic',0);
699 if file_exists(path)=1 then
700 begin
701 if file_exists(path+'player.dat')=1 then
702 begin
703 if open_file(path+'player.dat')=1 then
704 begin
705 //Head
706 ver:=read_byte;
707 if version_err(ver)=false then
708 begin
709 if close_file(path+'player.dat')=1 then loadgame:=false;
710 exit;
711 end;
712 gamemode:=read_byte;
713 cheats:=readbool;
714 seed:=readint;
715 pl_world:=read_byte;
716 //Player;
717 last_sleep_x:=readint;
718 last_sleep_y:=readint;
719 velx:=read_byte;
720 vely:=read_byte;
721 invslot:=read_byte;
722 posi:=read_byte;
723 hp:=read_byte;
724 hunger:=read_byte;
725 moon_phase:=read_byte;
726 jmp:=readbool;
727 inv.loadData;
728 drw_load_line('Matrix',10);
729 if close_file(path+'player.dat')=1 then loadgame:=loadworld(path);
730 end; else loadgame:=false;
731 end; else loadgame:=false;
732 end; else loadgame:=false;
733 end;
735 procedure drawdeadlogo;
736 var
737 dead:image;
738 begin
739 setcolor(0,0,0);
740 fillrect(0,0,getWidth,getHeight);
741 dead:=loadimage('/dl');
742 drawimage(dead,(getWidth/2)-(getimagewidth(dead)/2),(getHeight/2)-(getimageheight(dead)/2));
743 drawfonttext(#119+#119+#119+#46+#100+#101+#97+#100+#115+#111+#102+#116+#119+#97+#114+#101+#46+#114+#117,getWidth/2-80,(getHeight/2)+(getimageheight(dead)/2));
744 drawfonttext('Loading...',(getWidth/2)-(40),getHeight-8);
745 drawVideo;
746 delay(500);
747 end;
749 procedure newgame;
750 var
751 ix,iy:integer;
752 begin
753 for ix:=0 to getWidth/16 do
754 for iy:=0 to getHeight/16 do
755 begin
756 drawimage(bg[0],ix*16,iy*16);
757 end;
758 drawfonttext('Generation World...',(getWidth/2)-(76),(getHeight/2)-4);
759 drawVideo;
760 delay(1);
761 cleargame;
762 game_time:=45000;
763 newworld;
764 //if s_spawn_mob then begin if pl_world=0 then begin megaspawn; end; else begin netherspawn; end; end;
765 if gamemode=1 then fly:=true else fly:=false;
766 debug('New game!');
767 end;
769 function gettext(text,text_f:string; max,typ:integer;):string;
770 var
771 textField_id:integer;
772 exitCmd,cli:command;
773 begin
774 clearForm;
775 exitCmd:=createCommand('Ok',CM_OK,1);
776 showForm;
777 addCommand(exitCmd);
778 textField_id:=formAddTextField(text,text_f,max,typ);
779 drawVideo;
780 delay(100);
781 repeat
782 cli:=getClickedCommand;
783 until cli=exitCmd;
784 gettext:=formGetText(textField_id);
785 showCanvas;
786 end;
788 procedure drw_btn(text:string; cur,ccur,h,ty:integer);
789 var
790 m_x,m_y,i:integer;
791 begin
792 m_x:=(getWidth/2)-(120/2);
793 m_y:=h+((getHeight/2))-((getimageheight(gui[7])-2*cur)/2)+((getimageheight(gui[7])+2)*cur);
794 if ty=0 then
795 begin
796 if ccur=cur then drawimage(gui[22],m_x+i*4,m_y); else drawimage(gui[6],m_x+i*8,m_y);
797 end; else
798 if ty=1 then
799 begin
800 if ccur=cur then drawimage(gui[8],m_x+i*8,m_y); else drawimage(gui[7],m_x+i*8,m_y);
801 end;
802 setclip(0,0,getwidth,getheight);
803 m_x:=(getWidth/2)-(length(text)*8/2);
804 m_y:=h+((getHeight/2))-((getimageheight(gui[7])-2*cur)/2)+((getimageheight(gui[7])+2)*cur)+((getimageheight(gui[7])-8)/2);
805 drawfonttext(text,m_x,m_y);
806 end;
808 procedure drw_txt(str:string; xx,n,t:integer);
809 var
810 m_x,m_y:integer;
811 begin
812 m_x:=(getWidth/2)-(length(str)*8/2);
813 m_y:=xx+32+(8*n);
814 if t=0 then drawfonttext(str,0,m_y); else
815 if t=1 then drawfonttext(str,m_x,m_y);
816 end;
818 function pos_end(s:string; c:char):integer;
819 var
820 i:integer;
821 begin
822 for i:=length(s)-1 downto 0 do
823 begin
824 if getchar(s,i)=c then
825 begin
826 pos_end:=i;
827 exit;
828 end;
829 end;
830 pos_end:=-1;
831 end;
833 function getroot(cancel_b:boolean):string;
834 var
835 m_cur,max_r,pars,ix,iy:integer;
836 rr:string;
837 im:image;
838 roots:array [0..15] of string;
839 begin
840 im:=rotate_image_from_image(ld_tex('background.png','/'+sd+'/cavecraft/texturepacks/'+tex_pack+'/','title/'),16,0,16,16,0);
841 rr:=get_roots;
842 if cancel_b then roots[0]:='<CANCEL>';
843 if cancel_b then max_r:=1; else max_r:=0;
844 while pos(rr,'|')<>-1 do
845 begin
846 pars:=pos(rr,'|');
847 roots[max_r]:=copy(rr,0,pars-1);
848 rr:=copy(rr,pars+1,length(rr));
849 max_r:=max_r+1;
850 end;
851 max_r:=max_r-1;
852 repeat
853 updateKeys;
854 if clickedKey(KEY_FM_UP) then begin m_cur:=m_cur-1; if m_cur<0 then m_cur:=max_r; end;
855 if clickedKey(KEY_FM_DOWN) then begin m_cur:=m_cur+1; if m_cur>max_r then m_cur:=0; end;
856 if clickedKey(KEY_FM_SELECT) then
857 begin
858 if (m_cur=0) and (cancel_b) then
859 begin
860 getroot:='';
861 exit;
862 end;
863 else
864 begin
865 getroot:=roots[m_cur];
866 exit;
867 end;
868 end;
870 for ix:=0 to getWidth/16 do
871 for iy:=0 to getHeight/16 do
872 drawimage(im,ix*16,iy*16);
874 setcolor(0,0,0);
875 fillrect(0,m_cur*8,getWidth-1,8);
876 for ix:=0 to max_r do
877 begin
878 drawfonttext(roots[ix],0,ix*8);
879 end;
880 setcolor(255,255,255);
881 drawrect(0,m_cur*8,getWidth-1,8);
883 drawVideo;
884 delay(1);
885 until false;
886 end;
888 function filemanager(cancel_b:boolean):string;
889 var
890 m_cur,ix,iy,max_r,pars:integer;
891 im:image;
892 last,rr,root,path:string;
893 names:array [0..255] of string;
894 begin
895 im:=rotate_image_from_image(ld_tex('background.png','/'+sd+'/cavecraft/texturepacks/'+tex_pack+'/','title/'),16,0,16,16,0);
896 names[0]:='<..>';
897 names[1]:='<OK>';
898 root:=getroot(cancel_b);
899 if root='' then
900 begin
901 filemanager:='';
902 exit;
903 end;
904 path:=root;
905 if cancel_b then names[2]:='<CANCEL>';
906 rr:=get_dirs(path);
907 if cancel_b then max_r:=3; else max_r:=2;
908 while pos(rr,'|')<>-1 do
909 begin
910 pars:=pos(rr,'|');
911 names[max_r]:=copy(rr,0,pars-1);
912 rr:=copy(rr,pars+1,length(rr));
913 max_r:=max_r+1;
914 end;
915 max_r:=max_r-1;
917 repeat
918 updateKeys;
919 if clickedKey(KEY_FM_UP) then begin m_cur:=m_cur-1; if m_cur<0 then m_cur:=max_r; end;
920 if clickedKey(KEY_FM_DOWN) then begin m_cur:=m_cur+1; if m_cur>max_r then m_cur:=0; end;
921 if clickedKey(KEY_FM_SELECT) then
922 begin
923 if m_cur=0 then
924 begin
925 debug('path:'+path);
926 debug('lol:'+pos_end(path,'/'));
927 if (path=root) or (path=root+'/') then
928 begin
929 root:=getroot(cancel_b);
930 if root='' then
931 begin
932 filemanager:='';
933 exit;
934 end;
935 path:=root;
936 end;
937 else
938 begin
939 path:=copy(path,0,pos_end(path,'/'));
940 if cancel_b then names[2]:='<CANCEL>';
941 rr:=get_dirs(path);
942 if cancel_b then max_r:=3; else max_r:=2;
943 while pos(rr,'|')<>-1 do
944 begin
945 pars:=pos(rr,'|');
946 names[max_r]:=copy(rr,0,pars-1);
947 rr:=copy(rr,pars+1,length(rr));
948 max_r:=max_r+1;
949 end;
950 max_r:=max_r-1;
951 end;
952 end; else
953 if m_cur=1 then
954 begin
955 debug('<OK>');
956 filemanager:=path;
957 exit;
958 end; else
959 if (m_cur=2) and (cancel_b) then
960 begin
961 debug('<CANCEL>');
962 filemanager:='';
963 exit;
964 end; else
965 begin
966 path:=path+'/'+names[m_cur];
968 if cancel_b then names[2]:='<CANCEL>';
969 rr:=get_dirs(path);
970 if cancel_b then max_r:=3; else max_r:=2;
971 while pos(rr,'|')<>-1 do
972 begin
973 pars:=pos(rr,'|');
974 names[max_r]:=copy(rr,0,pars-1);
975 rr:=copy(rr,pars+1,length(rr));
976 max_r:=max_r+1;
977 end;
978 max_r:=max_r-1;
979 m_cur:=0;
980 debug('path:'+path);
981 end;
982 end;
984 for ix:=0 to getWidth/16 do
985 for iy:=0 to getHeight/16 do
986 drawimage(im,ix*16,iy*16);
988 setcolor(0,0,0);
989 fillrect(0,m_cur*8,getWidth-1,8);
990 for ix:=0 to max_r do
991 begin
992 drawfonttext(names[ix],0,ix*8);
993 end;
994 setcolor(255,255,255);
995 drawrect(0,m_cur*8,getWidth-1,8);
997 drawVideo;
998 delay(1);
1000 until false;
1001 end;
1003 procedure setsd(cancel_b:boolean);
1004 var
1005 s:string;
1006 t:integer;
1007 rs:recordstore;
1008 begin
1009 s:=filemanager(cancel_b);
1010 if s<>'' then
1011 begin
1012 sd:=s;
1013 if file_exists('/'+sd+'/cavecraft')<>1 then
1014 if create_folder('/'+sd+'/cavecraft')=1 then debug('/cavecraft created!');
1016 if file_exists('/'+sd+'/cavecraft/saves')<>1 then
1017 if create_folder('/'+sd+'/cavecraft/saves')=1 then debug('/saves created!');
1019 if file_exists('/'+sd+'/cavecraft/screenshots')<>1 then
1020 if create_folder('/'+sd+'/cavecraft/screenshots')=1 then debug('/screenshots created!');
1022 if file_exists('/'+sd+'/cavecraft/texturepacks')<>1 then
1023 if create_folder('/'+sd+'/cavecraft/texturepacks')=1 then debug('/texturepacks created!');
1025 deleteRecordStore('SD');
1026 rs:=openRecordStore('SD');
1027 t:=addRecordStoreEntry(rs,sd);
1028 closeRecordStore(rs);
1029 end;
1030 end;
1032 procedure start_uu;
1033 var
1034 rs:recordstore;
1035 ss:string;
1036 begin
1037 drawdeadlogo;
1038 //Load SD
1039 rs:=openRecordStore('SD');
1040 sd:=readRecordStoreEntry(rs,1);
1041 closeRecordStore(rs);
1042 console.exec('autoexec.cfg', 'AUTO', true);
1043 if sd='' then
1044 begin
1045 init_touch;
1046 if touchscreen then
1047 load_key_tex:=1;
1048 else
1049 load_key_tex:=0;
1050 //load_virt_tex(loadimage('/gui/touch.png'));
1051 setsd(false);
1052 end;
1054 if file_exists('/'+sd+'/cavecraft')<>1 then
1055 if create_folder('/'+sd+'/cavecraft')=1 then debug('/cavecraft created!');
1057 if file_exists('/'+sd+'/cavecraft/saves')<>1 then
1058 if create_folder('/'+sd+'/cavecraft/saves')=1 then debug('/saves created!');
1060 if file_exists('/'+sd+'/cavecraft/screenshots')<>1 then
1061 if create_folder('/'+sd+'/cavecraft/screenshots')=1 then debug('/screenshots created!');
1063 if file_exists('/'+sd+'/cavecraft/texturepacks')<>1 then
1064 if create_folder('/'+sd+'/cavecraft/texturepacks')=1 then debug('/texturepacks created!');
1066 rs:=openRecordStore('TX');
1067 ss:=readRecordStoreEntry(rs,1);
1068 closeRecordStore(rs);
1070 if ss<>'' then
1071 begin
1072 tex_pack:=ss;
1073 LoadDrawFont('/'+sd+'/cavecraft/texturepacks/'+tex_pack+'/');
1074 loadtexture('/'+sd+'/cavecraft/texturepacks/'+tex_pack+'/');
1075 end;
1076 else
1077 begin
1078 LoadDrawFont('/');
1079 loadtexture('/');
1080 end;
1081 end;
1083 function question(text:string):boolean;
1084 var
1085 ix,iy,m_cur:integer;
1086 begin
1087 repeat
1088 updateKeys;
1089 if clickedKey(KEY_MENU_UP) then begin m_cur:=m_cur-1; if m_cur<0 then m_cur:=1; end;
1090 if clickedKey(KEY_MENU_DOWN) then begin m_cur:=m_cur+1; if m_cur>1 then m_cur:=0; end;
1091 if clickedKey(KEY_MENU_SELECT) then begin if m_cur=0 then question:=true; else question:=false; exit; end;
1092 for ix:=0 to getwidth/16 do for iy:=0 to getheight/16 do drawimage(bg[0],ix*16,iy*16);
1093 drawfonttext(text,getwidth/2-(length(text)*8)/2,getheight/2-16);
1094 drw_btn('Yes',0,m_cur,0,1);
1095 drw_btn('No',1,m_cur,0,1);
1096 drawVideo;
1097 delay(1);
1098 until false;
1099 end;
1101 function menu_game_new:boolean;
1102 var
1103 m_cur,tmp_gm,ix,iy,lol:integer;
1104 tmp_cheats,tmp_bon_chest:boolean;
1105 newgametxt:array[0..4] of string;
1106 name,tmp:string;
1107 begin
1108 m_cur:=-2;
1109 newgametxt[0]:='Survival';
1110 newgametxt[1]:='Creative';
1111 newgametxt[2]:='Hardcore';
1112 newgametxt[3]:='Normal';
1113 newgametxt[4]:='Flat';
1114 name:='New World';
1115 repeat
1116 updateKeys;
1117 if clickedKey(KEY_MENU_UP) then begin m_cur:=m_cur-1; if m_cur<-2 then m_cur:=5; end;
1118 if clickedKey(KEY_MENU_DOWN) then begin m_cur:=m_cur+1; if m_cur>5 then m_cur:=-2; end;
1119 if clickedKey(KEY_MENU_SELECT) then
1120 begin
1121 if m_cur=-2 then
1122 begin
1123 name:=gettext('Name:',name,10,TF_ANY);
1124 end; else
1125 if m_cur=-1 then
1126 begin
1127 tmp_gm:=tmp_gm+1; if tmp_gm>2 then tmp_gm:=0;
1128 if tmp_gm=0 then tmp_cheats:=false;
1129 if tmp_gm=1 then tmp_cheats:=true;
1130 if tmp_gm=2 then begin tmp_cheats:=false; tmp_bon_chest:=false; end;
1131 end; else
1132 if m_cur=0 then
1133 begin
1134 nextseed:=stringtointeger(gettext('Seed:',''+nextseed,10,TF_NUMERIC));
1135 end; else
1136 if m_cur=1 then
1137 begin
1138 world_typ:=world_typ+1;
1139 if world_typ>1 then world_typ:=0;
1140 end; else
1141 if m_cur=2 then
1142 begin
1143 if tmp_gm<2 then tmp_cheats:= not tmp_cheats;
1144 end; else
1145 if m_cur=3 then
1146 begin
1147 if tmp_gm<2 then tmp_bon_chest:= not tmp_bon_chest;
1148 end; else
1149 if m_cur=4 then
1150 begin
1151 gamemode:=tmp_gm;
1152 cheats:=tmp_cheats;
1153 bon_chest:=tmp_bon_chest;
1154 tmp:=name;
1155 while file_exists('/'+sd+'/cavecraft/saves/'+name)=1 do
1156 begin
1157 lol:=lol+1;
1158 name:=tmp+' '+lol;
1159 end;
1160 sav_fl:=name;
1161 newgame;
1162 menu_game_new:=true;
1163 break;
1164 end; else
1165 if m_cur=5 then
1166 begin
1167 break;
1168 end; else
1169 end;
1170 for ix:=0 to getWidth/16 do
1171 for iy:=0 to getHeight/16 do
1172 drawimage(bg[0],ix*16,iy*16);
1174 drw_btn('Name:'+name,-2,m_cur,0,1);
1175 drw_btn('Mode:'+newgametxt[tmp_gm],-1,m_cur,0,1);
1176 drw_btn('Seed:'+nextseed,0,m_cur,0,1);
1177 drw_btn('Type:'+newgametxt[world_typ+3],1,m_cur,0,1);
1178 if tmp_gm<2 then
1179 drw_btn('Cheats:'+tmp_cheats,2,m_cur,0,1);
1180 else
1181 drw_btn('Cheats:'+tmp_cheats,2,m_cur,0,0);
1182 if tmp_gm<2 then
1183 drw_btn('Chest:'+tmp_bon_chest,3,m_cur,0,1);
1184 else
1185 drw_btn('Chest:'+tmp_bon_chest,3,m_cur,0,0);
1186 drw_btn('Create',4,m_cur,0,1);
1187 drw_btn('Cancel',5,m_cur,0,1);
1189 drawVideo;
1190 delay(1);
1191 until false;
1192 end;
1194 procedure deleteworld(path:string);
1195 begin
1196 if file_exists(path+'player.dat')=1 then delete_file(path+'player.dat');
1197 if file_exists(path+'world.sav')=1 then delete_file(path+'world.sav');
1198 if file_exists(path+'nether.sav')=1 then delete_file(path+'nether.sav');
1199 if file_exists(path+'pic.png')=1 then delete_file(path+'pic.png');
1200 if file_exists(path)=1 then delete_file(path);
1201 if file_exists(path)=0 then debug('World deleted!');
1202 end;
1204 function menu_game:boolean;
1205 var
1206 ix,iy,pars,max_r,cur_name,m_cur:integer;
1207 mm_t_b:boolean;
1208 im_game:image;
1209 rr:string;
1210 names:array[0..255] of string;
1211 begin
1212 rr:=get_dirs('/'+sd+'/cavecraft/saves/');
1213 while pos(rr,'|')<>-1 do
1214 begin
1215 pars:=pos(rr,'|');
1216 names[max_r]:=copy(rr,0,pars-1);
1217 rr:=copy(rr,pars+1,length(rr));
1218 if file_exists('/'+sd+'/cavecraft/saves/'+names[max_r]+'/player.dat')=1 then max_r:=max_r+1;
1219 end;
1220 max_r:=max_r-1;
1221 if file_exists('/'+sd+'/cavecraft/saves/'+names[cur_name]+'/pic.png')=1 then
1222 im_game:=safely_load_image_fs('/'+sd+'/cavecraft/saves/'+names[cur_name]+'/pic.png');
1223 else im_game:=ld_tex('maps.png','/'+sd+'/cavecraft/texturepacks/'+tex_pack+'/','');
1225 if file_exists('/'+sd+'/cavecraft/saves/'+names[cur_name]+'/player.dat')=1 then
1226 mm_t_b:=true; else mm_t_b:=false;
1228 repeat
1229 updateKeys;
1230 if clickedKey(KEY_MENU_UP) then
1231 begin
1232 m_cur:=m_cur-1;
1233 if m_cur<0 then m_cur:=3;
1234 end;
1235 if clickedKey(KEY_MENU_DOWN) then
1236 begin
1237 m_cur:=m_cur+1;
1238 if m_cur>3 then m_cur:=0;
1239 end;
1241 if (max_r >= 0) and clickedKey(KEY_MENU_LEFT) then
1242 begin
1243 cur_name:=cur_name-1;
1244 if cur_name<0 then cur_name:=0;
1245 if file_exists('/'+sd+'/cavecraft/saves/'+names[cur_name]+'/pic.png')=1 then
1246 im_game:=safely_load_image_fs('/'+sd+'/cavecraft/saves/'+names[cur_name]+'/pic.png');
1247 else im_game:=ld_tex('maps.png','/'+sd+'/cavecraft/texturepacks/'+tex_pack+'/','');
1249 if file_exists('/'+sd+'/cavecraft/saves/'+names[cur_name]+'/player.dat')=1 then
1250 end;
1251 if (max_r >= 0) and clickedKey(KEY_MENU_RIGHT) then
1252 begin
1253 cur_name:=cur_name+1;
1254 if cur_name>max_r then cur_name:=max_r;
1255 if file_exists('/'+sd+'/cavecraft/saves/'+names[cur_name]+'/pic.png')=1 then
1256 im_game:=safely_load_image_fs('/'+sd+'/cavecraft/saves/'+names[cur_name]+'/pic.png');
1257 else im_game:=ld_tex('maps.png','/'+sd+'/cavecraft/texturepacks/'+tex_pack+'/','');
1259 if file_exists('/'+sd+'/cavecraft/saves/'+names[cur_name]+'/player.dat')=1 then
1260 end;
1261 if clickedKey(KEY_MENU_SELECT) then
1262 begin
1263 if m_cur=0 then
1264 begin
1265 if max_r>-1 then
1266 if loadgame('/'+sd+'/cavecraft/saves/'+names[cur_name]+'/') then
1267 begin
1268 sav_fl:=names[cur_name];
1269 menu_game:=true;
1270 break;
1271 end;
1272 end; else
1273 if m_cur=1 then
1274 begin
1275 if menu_game_new then begin menu_game:=true; break; end;
1276 end; else
1277 if m_cur=2 then
1278 begin
1279 debug('/'+sd+'/cavecraft/saves/'+names[cur_name]+'/');
1280 if max_r>-1 then
1281 if question('Are you sure?')=true then deleteworld('/'+sd+'/cavecraft/saves/'+names[cur_name]+'/');
1282 cur_name:=0;
1283 max_r:=0;
1284 rr:=get_dirs('/'+sd+'/cavecraft/saves/');
1285 while pos(rr,'|')<>-1 do
1286 begin
1287 pars:=pos(rr,'|');
1288 names[max_r]:=copy(rr,0,pars-1);
1289 rr:=copy(rr,pars+1,length(rr));
1290 max_r:=max_r+1;
1291 end;
1292 max_r:=max_r-1;
1293 if file_exists('/'+sd+'/cavecraft/saves/'+names[cur_name]+'/pic.png')=1 then
1294 im_game:=safely_load_image_fs('/'+sd+'/cavecraft/saves/'+names[cur_name]+'/pic.png');
1295 else im_game:=ld_tex('maps.png','/'+sd+'/cavecraft/texturepacks/'+tex_pack+'/','');
1296 end; else
1297 if m_cur=3 then
1298 begin
1299 break;
1300 end;
1301 end;
1303 for ix:=0 to getWidth/16 do
1304 for iy:=1 to 7 do
1305 drawimage(bg[1],ix*16,iy*16);
1306 for ix:=0 to getWidth/16 do
1307 drawimage(bg[0],ix*16,0);
1308 for ix:=0 to getWidth/16 do
1309 for iy:=8 to getHeight/16 do
1310 drawimage(bg[0],ix*16,iy*16);
1312 if max_r>-1 then
1313 begin
1314 setcolor(0,0,0);
1315 fillrect(32,24,getWidth-64,96);
1316 setclip(32,24,getWidth-64,96);
1317 drawimage(im_game,(getWidth/2)-(32),32);
1318 drawfonttext(names[cur_name],(getWidth/2)-(length(names[cur_name])*8/2),104);
1319 setclip(0,0,getWidth,getHeight);
1320 setcolor(128,128,128);
1321 drawrect(32,24,getWidth-64,96);
1322 if cur_name>0 then drawimage(gui[20],0,58);
1323 if cur_name<max_r then drawimage(gui[21],getWidth-9,58);
1324 end;
1325 else
1326 begin
1327 setcolor(0,0,0);
1328 fillrect(32,24,getWidth-64,96);
1329 setclip(32,24,getWidth-64,96);
1330 drawfonttext('No saves!',(getWidth/2)-(length('No saves!')*8/2),104);
1331 setclip(0,0,getWidth,getHeight);
1332 setcolor(128,128,128);
1333 drawrect(32,24,getWidth-64,96);
1334 end;
1336 if mm_t_b then drw_btn('Play',0,m_cur,32,1); else drw_btn('Play',0,m_cur,32,0);
1337 drw_btn('Create new',1,m_cur,32,1);
1338 if mm_t_b then drw_btn('Delete',2,m_cur,32,1); else drw_btn('Delete',2,m_cur,32,0);
1339 drw_btn('Back',3,m_cur,32,1);
1340 if not mm_t_b then cur_name:=cur_name+1;
1342 drawVideo;
1343 delay(1);
1344 until false;
1345 end;
1347 procedure menu_tex;
1348 var
1349 ix,iy,pars,max_r,cur_name,m_cur,t:integer;
1350 im_game:image;
1351 rr:string;
1352 names:array[0..255] of string;
1353 rs:recordstore;
1354 begin
1355 rr:=get_dirs('/'+sd+'/cavecraft/texturepacks/');
1356 names[0]:='Default';
1357 max_r:=1;
1358 while pos(rr,'|')<>-1 do
1359 begin
1360 pars:=pos(rr,'|');
1361 names[max_r]:=copy(rr,0,pars-1);
1362 rr:=copy(rr,pars+1,length(rr));
1363 max_r:=max_r+1;
1364 end;
1365 max_r:=max_r-1;
1366 im_game:=loadimage('/pack.png');
1368 repeat
1369 updateKeys;
1370 if clickedKey(KEY_MENU_UP) then
1371 begin
1372 m_cur:=m_cur-1;
1373 if m_cur<0 then m_cur:=1;
1374 end;
1375 if clickedKey(KEY_MENU_DOWN) then
1376 begin
1377 m_cur:=m_cur+1;
1378 if m_cur>1 then m_cur:=0;
1379 end;
1381 if clickedKey(KEY_MENU_LEFT) then
1382 begin
1383 cur_name:=cur_name-1;
1384 if cur_name<0 then cur_name:=0;
1385 im_game:=ld_tex('pack.png','/'+sd+'/cavecraft/texturepacks/'+names[cur_name]+'/','');
1386 end;
1387 if clickedKey(KEY_MENU_RIGHT) then
1388 begin
1389 cur_name:=cur_name+1;
1390 if cur_name>max_r then cur_name:=max_r;
1391 im_game:=ld_tex('pack.png','/'+sd+'/cavecraft/texturepacks/'+names[cur_name]+'/','');
1392 end;
1393 if clickedKey(KEY_MENU_SELECT) then
1394 begin
1395 if m_cur=0 then begin
1396 if cur_name>0 then
1397 begin
1398 tex_pack:=names[cur_name];
1399 LoadDrawFont('/'+sd+'/cavecraft/texturepacks/'+tex_pack+'/');
1400 loadtexture('/'+sd+'/cavecraft/texturepacks/'+tex_pack+'/');
1401 deleteRecordStore('TX');
1402 rs:=openRecordStore('TX');
1403 t:=addRecordStoreEntry(rs,tex_pack);
1404 closeRecordStore(rs);
1405 end;
1406 else
1407 begin
1408 tex_pack:='';
1409 loadtexture('/');
1410 deleteRecordStore('TX');
1411 rs:=openRecordStore('TX');
1412 t:=addRecordStoreEntry(rs,'');
1413 closeRecordStore(rs);
1414 end;
1415 end;
1416 if m_cur=1 then break;
1417 break;
1418 end;
1420 for ix:=0 to getWidth/16 do
1421 for iy:=1 to 7 do
1422 drawimage(bg[1],ix*16,iy*16);
1423 for ix:=0 to getWidth/16 do
1424 drawimage(bg[0],ix*16,0);
1425 for ix:=0 to getWidth/16 do
1426 for iy:=8 to getHeight/16 do
1427 drawimage(bg[0],ix*16,iy*16);
1429 if max_r>-1 then
1430 begin
1431 setcolor(0,0,0);
1432 fillrect(32,24,getWidth-64,96);
1433 setclip(32,24,getWidth-64,96);
1434 drawimage(im_game,(getWidth/2)-(32),32);
1435 drawfonttext(names[cur_name],(getWidth/2)-(length(names[cur_name])*8/2),104);
1436 setclip(0,0,getWidth,getHeight);
1437 setcolor(128,128,128);
1438 drawrect(32,24,getWidth-64,96);
1439 if cur_name>0 then drawimage(gui[20],0,58);
1440 if cur_name<max_r then drawimage(gui[21],getWidth-9,58);
1441 end;
1443 drw_btn('Done',0,m_cur,32,1);
1444 drw_btn('Back',1,m_cur,32,1);
1446 drawVideo;
1447 delay(1);
1448 until false;
1449 end;
1451 procedure menu_sett;
1452 var
1453 ix,iy,m_cur:integer;
1454 begin
1455 m_cur:=-3;
1456 repeat
1457 updateKeys;
1458 if clickedKey(KEY_MENU_UP) then
1459 begin
1460 m_cur:=m_cur-1;
1461 if m_cur<-3 then m_cur:=5;
1462 end;
1463 if clickedKey(KEY_MENU_DOWN) then
1464 begin
1465 m_cur:=m_cur+1;
1466 if m_cur>5 then m_cur:=-3;
1467 end;
1468 if clickedKey(KEY_MENU_SELECT) then
1469 begin
1470 if m_cur=-3 then
1471 begin
1472 light_type:=light_type+1;
1473 if light_type>2 then light_type:=0;
1474 end; else
1475 if m_cur=-2 then
1476 begin
1477 ifosad:=not ifosad;
1478 end; else
1479 if m_cur=-1 then
1480 begin
1481 s_particles:=not s_particles;
1482 end; else
1483 if m_cur=0 then
1484 begin
1485 drawgui:=not drawgui;
1486 end; else
1487 if m_cur=1 then
1488 begin
1489 if question('Are you sure?')=true then
1490 begin
1491 if load_key_tex=0 then
1492 begin
1493 load_key_tex:=1;
1494 init_touch;
1495 //load_virt_tex(ld_tex('touch.png','/'+sd+'/cavecraft/texturepacks/'+tex_pack+'/','gui/'));
1496 end;
1497 else
1498 if load_key_tex=1 then
1499 begin
1500 load_key_tex:=0;
1501 resetVirtualKeyboard(-1);
1502 end;
1503 end;
1504 end;
1505 if m_cur=2 then
1506 begin
1507 s_jpeg_quality:=stringtointeger(gettext('JPEG quality:',''+s_jpeg_quality,3,TF_NUMERIC));
1508 if s_jpeg_quality>100 then s_jpeg_quality:=100; else
1509 if s_jpeg_quality<0 then s_jpeg_quality:=0;
1510 end; else
1511 if m_cur=3 then
1512 begin
1513 setsd(true);
1514 end; else
1515 if m_cur=4 then
1516 begin
1517 deleteRecordStore('S');
1518 deleteRecordStore('SD');
1519 deleteRecordStore('TX');
1520 halt;
1521 end; else
1522 if m_cur=5 then
1523 begin
1524 save_settings;
1525 break;
1526 end; else
1527 end;
1529 for ix:=0 to getWidth/16 do
1530 for iy:=0 to getHeight/16 do
1531 drawimage(bg[0],ix*16,iy*16);
1533 drw_btn('Light:'+light_type,-3,m_cur,0,1);
1534 drw_btn('Weather:'+ifosad,-2,m_cur,0,1);
1535 drw_btn('Particles:'+s_particles,-1,m_cur,0,1);
1536 drw_btn('Hide GUI:'+not drawgui,0,m_cur,0,1);
1537 drw_btn('Touch:'+(load_key_tex>0),1,m_cur,0,1);
1538 drw_btn('JPEG:'+s_jpeg_quality,2,m_cur,0,1);
1539 drw_btn('Set Root',3,m_cur,0,1);
1540 drw_btn('Reset',4,m_cur,0,1);
1541 drw_btn('Back',5,m_cur,0,1);
1543 drawVideo;
1544 delay(1);
1545 until false;
1546 end;
1548 procedure menu_help;
1549 var
1550 ix,iy,cur:integer;
1551 begin
1552 repeat
1553 updateKeys;
1554 if clickedKey(KEY_MENU_SELECT) then break;
1555 if pressedKey(KEY_MENU_UP) then cur:=cur+1;
1556 if pressedKey(KEY_MENU_DOWN) then cur:=cur-1;
1558 for ix:=0 to getWidth/16 do
1559 for iy:=2 to (getHeight/16)-2 do
1560 drawimage(bg[1],ix*16,iy*16);
1562 for ix:=0 to getWidth/16 do
1563 for iy:=0 to 1 do
1564 drawimage(bg[0],ix*16,iy*16);
1566 for ix:=0 to getWidth/16 do
1567 for iy:=(getHeight/16)-2 to getHeight/16 do
1568 drawimage(bg[0],ix*16,iy*16);
1570 setclip(0,32,getWidth,(getHeight/16-4)*16);
1572 drw_txt('Controls:',cur,0,1);
1573 drw_txt('Left - Move left',cur,1,0);
1574 drw_txt('Right - Move right',cur,2,0);
1575 drw_txt('Up - Jump',cur,3,0);
1576 drw_txt('Down - Put a block under you',cur,4,0);
1577 drw_txt('1 - Inventory',cur,5,0);
1578 drw_txt('Duble 1 - Craft',cur,6,0);
1579 drw_txt('3 - Use block',cur,7,0);
1580 drw_txt('7 - Debug info',cur,8,0);
1581 drw_txt('Hold 7 - Console',cur,9,0);
1582 drw_txt('9 - Pause',cur,10,0);
1583 drw_txt('*, # - Browse inventory',cur,11,0);
1584 drw_txt('0 - Edit mode',cur,12,0);
1586 drw_txt('In inventory/chest:',cur,14,1);
1587 drw_txt('3 - Enject object',cur,15,0);
1588 drw_txt('Hold 3 - Enject stack',cur,16,0);
1589 drw_txt('5 - Move the stack',cur,17,0);
1590 drw_txt('Hold 5 - Divide stack',cur,18,0);
1592 setclip(0,0,getWidth,getHeight);
1594 drw_btn('Back',0,0,getHeight-getHeight/2-16-6,1);
1596 drawVideo;
1597 delay(1);
1598 until false;
1599 end;
1601 procedure menu_about;
1602 var
1603 ix,iy,cur:integer;
1604 begin
1605 repeat
1606 updateKeys;
1607 if clickedKey(KEY_MENU_SELECT) then break;
1608 if pressedKey(KEY_MENU_UP) then cur:=cur+1;
1609 if pressedKey(KEY_MENU_DOWN) then cur:=cur-1;
1611 for ix:=0 to getWidth/16 do
1612 for iy:=2 to (getHeight/16)-2 do
1613 drawimage(bg[1],ix*16,iy*16);
1615 for ix:=0 to getWidth/16 do
1616 for iy:=0 to 1 do
1617 drawimage(bg[0],ix*16,iy*16);
1619 for ix:=0 to getWidth/16 do
1620 for iy:=(getHeight/16)-2 to getHeight/16 do
1621 drawimage(bg[0],ix*16,iy*16);
1623 setclip(0,32,getWidth,(getHeight/16-4)*16);
1625 drw_txt('Developers:',cur,0,1);
1626 drw_txt(#68+#101+#97+#68+#68+#111+#111+#77+#69+#82+' - Programmer',cur,1,0);//dead
1627 drw_txt(#102+#114+#101+#100+#45+#98+#111+#121+' - Programmer',cur,2,0);//fred-boy
1628 drw_txt(#65+#110+#100+#114+#101+#121+#53+#57+' - Programmer',cur,3,0);//andrey59
1629 drw_txt(#89+#117+#82+#97+#78+#110+#78+#122+#90+#90+' - Artist',cur,4,0);//yura
1630 drw_txt(#83+#97+#115+#104+#97+#71+' - Artist and idea generator',cur,5,0);//sasha
1631 drw_txt(#66+#97+#74+#108+#101+#72+#84+#105+#72+' - Artist and tester',cur,6,0);//valentin
1632 drw_txt(#65+#103+#114+#101+#115+#115+#111+#82+' - Tester',cur,7,0);//agressor
1633 drw_txt(#118+#111+#108+#121+#97+#95+#110+#97+#115+#116+#97+#110+#101+' - Tester',cur,8,0);//volya
1634 drw_txt(#97+#98+#97+#100+#111+#110+' - Tester',cur,9,0);//abadon
1635 drw_txt(#77+#111+#110+#111+#103+#114+#111+#109+' - Tester',cur,10,0);//monogrom
1636 drw_txt(#75+#97+#108+#116+#101+#114+' - Tester',cur,11,0);//kalter
1638 drw_txt('Thanks:',cur,12,1);
1639 drw_txt('Piligrim and 0vZ - Lib_jsr75i',cur,13,0);
1640 drw_txt('Piligrim - Lib_effects',cur,14,0);
1641 drw_txt('Kurdt - Lib_canvas',cur,15,0);
1642 drw_txt('ViNT - Lib_png and Lib_bmp',cur,16,0);
1643 drw_txt('aleshka - Lib_jpeg',cur,17,0);
1644 drw_txt('Roman_V - Lib_safeload',cur,18,0);
1646 drw_txt('Site: '+#104+#116+#116+#112+#58+#47+#47+#100+#101+#97+#100+#115+#111+#102+#116+#119+#97+#114+#101+#46+#114+#117,cur,21,0);
1647 drw_txt(#68+#101+#97+#68+#83+#111+#102+#116+#87+#97+#114+#101+' 2012-'+getyear(getcurrenttime),cur,22,0);
1649 drw_txt('Hello! :D',cur,100,1);
1651 setclip(0,0,getWidth,getHeight);
1653 drw_btn('Back',0,0,getHeight-getHeight/2-16-6,1);
1655 drawVideo;
1656 delay(1);
1657 until false;
1658 end;
1660 function sm_siz:integer;
1661 begin
1662 sm_siz:=(getWidth+getHeight)/5;
1663 end;
1665 function sm_siz4:integer;
1666 begin
1667 sm_siz4:=sm_siz/4;
1668 end;
1670 procedure draw_menu_back;
1671 var
1672 ix, iy:integer;
1673 begin
1674 for ix:=0 to getWidth/16 do
1675 for iy:=0 to getHeight/16 do
1676 drawimage(bg[0],ix*16,iy*16);
1677 end;
1679 procedure menu;
1680 var
1681 m_cur,ix,iy,iz:integer;
1682 key,spl_i:integer;
1683 splash:string;
1684 res:resource;
1685 time:integer;
1686 cavelogo:image;
1687 spl_y,spl_del:integer;
1688 spl_y_b:boolean;
1689 begin
1690 cavelogo:=ld_tex('cavelogo.png','/'+sd+'/cavecraft/texturepacks/'+tex_pack+'/','title/');
1691 splash:='#ERROR';
1692 spl_y:=getimageheight(cavelogo);
1693 spl_y_b:=true;
1694 spl_del:=getrelativetimems;
1695 res:=openResource('/title/splashes.txt');
1696 if ResourceAvailable(res) then
1697 begin
1698 spl_i:=stringtointeger(readline(res));
1699 iy:=random(spl_i-1)+1;
1700 debug('SPLASH #'+iy);
1701 for ix:=1 to iy do
1702 splash:=readline(res);
1703 closeresource(res);
1704 end;
1705 time:=getcurrenttime;
1706 if (getmonth(time)=5) and (getday(time)=7) then splash:='Happy birthday, DeaDDooMER!';
1707 if ((getmonth(time)=0) and (getday(time)<3)) or ((getmonth(time)=11) and (getday(time)>29)) then splash:='Happy New Year!';
1708 repeat
1709 proc_fps;
1710 updateKeys;
1711 if clickedKey(KEY_MENU_UP) then
1712 begin
1713 m_cur:=m_cur-1;
1714 if m_cur<0 then m_cur:=5;
1715 end;
1716 if clickedKey(KEY_MENU_DOWN) then
1717 begin
1718 m_cur:=m_cur+1;
1719 if m_cur>5 then m_cur:=0;
1720 end;
1721 if clickedKey(KEY_MENU_SELECT) then
1722 begin
1723 if m_cur=0 then begin if menu_game then break; end;
1724 else
1725 if m_cur=1 then menu_tex;
1726 else
1727 if m_cur=2 then menu_sett;
1728 else
1729 if m_cur=3 then menu_help;
1730 else
1731 if m_cur=4 then menu_about;
1732 else
1733 if m_cur=5 then begin save_settings; halt; end;
1734 end;
1736 draw_menu_back;
1738 drawimage(cavelogo,(getWidth/2)-(getimagewidth(cavelogo)/2),0);
1739 setcolor(255,255,0);
1741 DrawFontTextSpec(splash, (getWidth/2)-(length(splash)*FONT_SYM_SIZE/2), spl_y, FONT_YELLOW_COLOR, true);
1743 if spl_y_b then begin if getrelativetimems-spl_del>64 then begin spl_y:=spl_y+1; spl_del:=getrelativetimems; end; if spl_y>getimageheight(cavelogo)+2 then spl_y_b:=false; end;
1744 if not spl_y_b then begin if getrelativetimems-spl_del>64 then begin spl_y:=spl_y-1; spl_del:=getrelativetimems; end; if spl_y<getimageheight(cavelogo)+1 then spl_y_b:=true; end;
1745 drawfonttext(version,0,getHeight-8);
1747 drw_btn('Singleplayer',0,m_cur,0,1);
1748 drw_btn('Texture Packs',1,m_cur,0,1);
1749 drw_btn('Options',2,m_cur,0,1);
1750 drw_btn('Help',3,m_cur,0,1);
1751 drw_btn('About',4,m_cur,0,1);
1752 drw_btn('Quit Game',5,m_cur,0,1);
1754 drawVideo;
1755 maxfps;
1756 until false;
1757 end;
1759 function setBlock(invcur, x, y:integer):boolean;
1760 var
1761 item, sum, block, sblock:integer;
1762 begin
1763 item:=inv.getItem(invcur);
1764 sum:=inv.getSum(invcur);
1765 block:=getMap(x, y);
1766 sblock:=getItemInfo(item);
1768 if getItemType(item)=ITEM_TYPE_BLOCK then
1769 if inv.isNull(invcur)=false then
1770 if getBlockSet(block)=true then
1771 // if (coll_xy(x, y)=false) or (getBlockColl(item)=0) then
1772 begin
1773 if set_block_code(sblock, x, y)=false then
1774 begin
1776 setmap(sblock, x, y);
1777 if gamemode<>1 then
1778 begin
1779 inv.setSum(inv.getSum(invcur)-1, invcur);
1780 inv.fixNull(invcur);
1781 end;
1783 setBlock:=true;
1784 end;
1785 end;
1786 end;
1788 procedure fishing;
1789 var
1790 x, y:integer;
1791 begin
1792 x:=player.getX;
1793 y:=player.getY;
1794 if (getrelativetimems-fish_time>5000) and (getmap(fx,fy)=50) then
1795 begin
1796 if (random(3)=2) and (getmap(fx,fy)=50) then
1797 begin
1798 drop.create(210,1,x,y);
1799 fish:=false;
1800 end;
1801 inv.setSum(inv.getSum(invslot)-1, invslot);
1802 inv.fixNull(invslot);
1803 fish:=false;
1804 end; else
1805 if (getrelativetimems-fish_time<5000) and (getmap(fx,fy)=50) and (fish=true) then
1806 begin
1807 if getmap(fx,fy-1)=50 then fy:=fy-1;
1808 end;
1809 end;
1811 procedure drawminimap;
1812 var
1813 ix,iy,tmp_gx,tmp_gy,minx,miny,maxx,maxy,loc_camx,loc_camy,x,y:integer;
1814 begin
1815 x:=player.getX;
1816 y:=player.getY;
1817 if load_minimap_tex then
1818 begin
1819 tmp_gx:=(getWidth/2)-(getimagewidth(gui[16])/2);
1820 tmp_gy:=(getHeight/2)-(getimageheight(gui[16])/2);
1821 end;
1822 else
1823 begin
1824 tmp_gx:=(getWidth/2)-(64/2);
1825 tmp_gy:=(getHeight/2)-(64/2);
1826 end;
1828 loc_camx:=(x+4)-(864/2);
1829 loc_camy:=(y+4)-(864/2);
1830 if loc_camx<0 then loc_camx:=0;
1831 if loc_camx>4096-864 then loc_camx:=4096-864;
1832 if loc_camy<0 then loc_camy:=0;
1833 if loc_camy>2048-864 then loc_camy:=2048-864;
1834 minx:=loc_camx/16;
1835 miny:=loc_camy/16;
1836 maxx:=(loc_camx+864)/16;
1837 maxy:=(loc_camy+864)/16;
1838 if minx<0 then minx:=0;
1839 if miny<0 then miny:=0;
1840 if maxx>255 then maxx:=255;
1841 if maxy>127 then maxy:=127;
1843 if load_minimap_tex then
1844 drawimage(gui[16],tmp_gx,tmp_gy);
1845 else
1846 begin
1847 setcolor(214,190,150);
1848 fillrect(tmp_gx,tmp_gy,64,64);
1849 end;
1851 for ix:=minx to maxx do
1852 for iy:=miny to maxy do
1853 begin
1854 if (getmap(ix,iy)=1) or (getmap(ix,iy)=2) or (getmap(ix,iy)=74) then setcolor(121,85,58); else
1855 if (getmap(ix,iy)=4) or (getmap(ix,iy)=9) or (getmap(ix,iy)=11) or (getmap(ix,iy)=28) or (getmap(ix,iy)=29) or ((getmap(ix,iy)>=77) and (getmap(ix,iy)<=86)) or ((getmap(ix,iy)>=88) and (getmap(ix,iy)<=101)) then setcolor(188,152,98); else
1856 if (getmap(ix,iy)=50) or (getmap(ix,iy)=62) then setcolor(38,92,255); else
1857 if getmap(ix,iy)=51 then setcolor(255,0,0); else
1858 if (getmap(ix,iy)=61) or (getmap(ix,iy)=32) then setcolor(255,255,255); else
1859 setcolor(127,127,127);
1861 if (getmap(ix,iy)<>0) then plot(tmp_gx+4+ix-minx,tmp_gy+4+iy-miny);
1862 end;
1863 setcolor(0,0,255);
1864 end;
1866 procedure draw_sign;
1867 var
1868 tmp:string;
1869 str:array[0..3] of string;
1870 i,j,tmp_gx,tmp_gy:integer;
1871 begin
1872 if load_gui_tex then
1873 begin
1874 tmp_gx:=(getWidth/2)-(getimagewidth(sign_im)/2);
1875 tmp_gy:=(getHeight/2)-(getimageheight(sign_im)/2);
1876 end;
1877 else
1878 begin
1879 tmp_gx:=(getWidth/2)-(120/2);
1880 tmp_gy:=(getHeight/2)-(60/2);
1881 end;
1883 tmp:=t_sign[getmapinfo(curx,cury)];
1884 while pos(tmp,#13)<>-1 do
1885 begin
1886 i:=pos(tmp,#13);
1887 str[j]:=copy(tmp,0,i);
1888 tmp:=copy(tmp,i+1,length(tmp));
1889 j:=j+1;
1890 end;
1892 if load_gui_tex then
1893 drawimage(sign_im,tmp_gx,tmp_gy);
1894 else
1895 begin
1896 setcolor(159,132,77);
1897 fillrect(tmp_gx,tmp_gy,120,60);
1898 end;
1900 drawfonttext(str[0],tmp_gx+(120/2)-(length(str[0])*8/2),tmp_gy+(60/4/2)+4);
1901 drawfonttext(str[1],tmp_gx+(120/2)-(length(str[1])*8/2),tmp_gy+(60/4/2)+8+4);
1902 drawfonttext(str[2],tmp_gx+(120/2)-(length(str[2])*8/2),tmp_gy+(60/4/2)+16+4);
1903 drawfonttext(str[3],tmp_gx+(120/2)-(length(str[3])*8/2),tmp_gy+(60/4/2)+24+4);
1904 end;
1906 procedure draw_back(ix,iy:integer);
1907 begin
1908 if getBiomMap(ix)=0 then
1909 begin
1910 if (getBackMap(ix)=iy) then drawimage(back[0],(ix*16)-camx,(iy*16)-camy); else
1911 if (getBackMap(ix)+1=iy) then drawimage(back[1],(ix*16)-camx,(iy*16)-camy); else
1912 if (getBackMap(ix)+2=iy) then drawimage(back[1],(ix*16)-camx,(iy*16)-camy); else
1913 if (getBackMap(ix)+3=iy) then drawimage(back[1],(ix*16)-camx,(iy*16)-camy); else
1914 if (getBackMap(ix)<iy) then drawimage(back[2],(ix*16)-camx,(iy*16)-camy);
1915 end; else
1916 if getBiomMap(ix)=1 then
1917 begin
1918 if (getBackMap(ix)=iy) then drawimage(back[4],(ix*16)-camx,(iy*16)-camy); else
1919 if (getBackMap(ix)+1=iy) then drawimage(back[4],(ix*16)-camx,(iy*16)-camy); else
1920 if (getBackMap(ix)+2=iy) then drawimage(back[6],(ix*16)-camx,(iy*16)-camy); else
1921 if (getBackMap(ix)+3=iy) then drawimage(back[6],(ix*16)-camx,(iy*16)-camy); else
1922 if (getBackMap(ix)<iy) then drawimage(back[2],(ix*16)-camx,(iy*16)-camy);
1923 end; else
1924 if getBiomMap(ix)=2 then
1925 begin
1926 if (getBackMap(ix)=iy) then drawimage(back[5],(ix*16)-camx,(iy*16)-camy); else
1927 if (getBackMap(ix)+1=iy) then drawimage(back[1],(ix*16)-camx,(iy*16)-camy); else
1928 if (getBackMap(ix)+2=iy) then drawimage(back[1],(ix*16)-camx,(iy*16)-camy); else
1929 if (getBackMap(ix)+3=iy) then drawimage(back[1],(ix*16)-camx,(iy*16)-camy); else
1930 if (getBackMap(ix)<iy) then drawimage(back[2],(ix*16)-camx,(iy*16)-camy);
1931 end; else
1932 if getBiomMap(ix)=3 then
1933 begin
1934 if (getBackMap(ix)=iy) then drawimage(back[7],(ix*16)-camx,(iy*16)-camy); else
1935 if (getBackMap(ix)+1=iy) then drawimage(back[1],(ix*16)-camx,(iy*16)-camy); else
1936 if (getBackMap(ix)+2=iy) then drawimage(back[1],(ix*16)-camx,(iy*16)-camy); else
1937 if (getBackMap(ix)+3=iy) then drawimage(back[1],(ix*16)-camx,(iy*16)-camy); else
1938 if (getBackMap(ix)<iy) then drawimage(back[2],(ix*16)-camx,(iy*16)-camy);
1939 end; else
1940 if getBiomMap(ix)=4 then
1941 begin
1942 drawimage(back[8],(ix*16)-camx,(iy*16)-camy);
1943 end;
1944 end;
1946 procedure draw;
1947 var
1948 ix,iy,iz,minx,miny,maxx,maxy,tmp_ax,tim,smy,pa_xo, x, y:integer;
1949 begin
1950 x:=player.getX;
1951 y:=player.getY;
1952 {===================[sky]===================}
1953 tim:=10000*getimagewidth(sky)/600000*game_time/10000;
1954 setcolor(effects.get(sky,tim,0,1),effects.get(sky,tim,0,2),effects.get(sky,tim,0,3));
1955 fillrect(0,0,getWidth,getHeight);
1956 srand(seed);
1957 if drw_stars then
1958 begin
1959 setcolor(effects.get(sky,tim,2,1),effects.get(sky,tim,2,2),effects.get(sky,tim,2,3));
1960 smy:=10000*getWidth*2/600000*game_time/10000;
1961 for iz:=1 to getWidth do
1962 begin
1963 ix:=rnd(getWidth*2)-getWidth+smy;
1964 if ix>getWidth then ix:=ix-getWidth*2;
1965 iy:=rnd(getHeight);
1966 if (ix>-1) and (ix<getWidth) then plot(ix,iy);
1967 end;
1968 end;
1970 if drw_sm then
1971 begin
1972 if load_sm>0 then
1973 begin
1974 smy:=10000*(getWidth+getimagewidth(sun))/300000*game_time/10000;
1975 drawimage(sun,smy-getimagewidth(sun),getHeight/2-getimageheight(sun)/2);
1976 end;
1977 else
1978 begin
1979 setcolor(255,213,74);
1980 smy:=10000*(getWidth+sm_siz)/300000*game_time/10000;
1981 fillrect(smy-sm_siz+sm_siz4,getHeight/2-sm_siz/2+sm_siz4,sm_siz-sm_siz4,sm_siz-sm_siz4);
1982 end;
1984 if load_sm>0 then
1985 begin
1986 smy:=10000*(getWidth+getimagewidth(moon))/300000*(game_time-300000)/10000;
1987 drawimage(moon,smy-getimagewidth(moon),getHeight/2-getimageheight(moon)/2);
1988 end;
1989 else
1990 begin
1991 setcolor(175,184,204);
1992 smy:=10000*(getWidth+sm_siz)/300000*(game_time-300000)/10000;
1993 fillrect(smy-sm_siz+sm_siz4,getHeight/2-sm_siz/2+sm_siz4,sm_siz-sm_siz4,sm_siz-sm_siz4);
1994 end;
1995 end;
1996 {===================[camera]===================}
1997 camx:=(x+4)-(getWidth/2);
1998 camy:=(y+4)-(getHeight/2);
1999 if camx<0 then camx:=0;
2000 if camx>4096-getWidth then camx:=4096-getWidth;
2001 if camy>2048-getHeight then camy:=2048-getHeight;
2002 {===================[max_draw]===================}
2003 minx:=camx/16;
2004 miny:=camy/16;
2005 maxx:=(camx+getWidth)/16;
2006 maxy:=(camy+getHeight)/16;
2007 if minx<0 then minx:=0;
2008 if miny<0 then miny:=0;
2009 if maxx>255 then maxx:=255;
2010 if maxy>127 then maxy:=127;
2011 {===================[draw_back]===================}
2012 for ix:=minx to maxx do
2013 for iy:=miny to maxy do
2014 begin
2015 if ifosad then if (getBackMap(ix)>=iy) and (osadki=true) and getBlockTrans(getmap(ix,iy)) then
2016 begin
2017 if getBiomMap(ix)=0 then drawimage(osad[0,osadki_ani],(ix*16)-camx,(iy*16)-camy);
2018 else
2019 if getBiomMap(ix)=2 then drawimage(osad[1,osadki_ani],(ix*16)-camx,(iy*16)-camy);
2020 else
2021 if getBiomMap(ix)=3 then drawimage(osad[0,osadki_ani],(ix*16)-camx,(iy*16)-camy);
2022 end;
2023 if drw_back then
2024 if getBlockTrans(getmap(ix,iy)) then
2025 draw_back(ix,iy);
2027 if getBlockFore(getmap(ix,iy))=false then
2028 draw_block(ix,iy);
2029 end;
2030 {===================[drop]===================}
2031 drop.draw(camx, camy);
2032 {===================[particles]===================}
2033 if s_particles then draw_particle;
2034 {===================[player]===================}
2035 if hp>0 then
2036 begin
2037 player.draw(camx, camy);
2038 end;
2040 {===================[mobs]===================}
2041 mob.draw(camx, camy);
2042 {===================[draw_blocks]===================}
2043 for ix:=minx to maxx do
2044 for iy:=miny to maxy do
2045 begin
2046 if getBlockFore(getmap(ix, iy)) then
2047 draw_block(ix, iy);
2049 setcolor(0, 0, 0);
2050 if light_type = 1 then
2051 begin
2052 if getmaplight(ix,iy) = 0 then
2053 fillrect((ix * 16) - camx, (iy * 16) - camy, 16, 16);
2054 end;
2055 else if light_type = 2 then
2056 begin
2057 if getmaplight(ix, iy) < 15 then
2058 drawimage(light[getmaplight(ix, iy)], (ix * 16) - camx, (iy * 16) - camy);
2059 end;
2060 end;
2062 // debug
2063 // drawimage(light[getmaplight(player.getX div 16, player.getY div 16)], 0, 0);
2065 if (toolus > 0) and (toolind < 10) and (getmap(curx,cury) > 0) then
2066 begin
2067 if toolind > 9 then
2068 toolind:=9;
2069 drawimage(tue[toolind], curx * 16 - camx, cury * 16 - camy);
2070 end;
2071 {===================[gui]===================}
2072 if drawgui then
2073 begin
2074 tmp_ax:=(getWidth/2)-(getImageWidth(gui[1])/2);
2076 if keymode=1 then
2077 drawimage(gui[0],curx*16-camx,cury*16-camy);
2079 DrawWindows;
2080 /*if keymode=2 then
2081 if gamemode<>1 then
2082 DrawPlayerInventory;
2083 else
2084 drawinv_c; else
2085 if keymode=3 then drawcraft; else
2086 if keymode=4 then drawchest; else
2087 if keymode=5 then draw_sign; else
2088 if keymode=6 then drawfurnace; else
2089 if ifminimap then drawminimap;*/
2091 drawimage(gui[1],tmp_ax,0);
2092 for ix:=0 to 8 do
2093 begin
2094 drawItem(inv.getItem(ix), inv.getSum(ix), (ix*16)+tmp_ax+ix*2+4, 1, true);
2095 end;
2096 drawimage(gui[17],(invslot*16)+tmp_ax+invslot*2+2,0);
2098 if gamemode<>1 then
2099 begin
2100 if gamemode=0 then
2101 begin
2102 for ix:=0 to (hp div 2)-1 do drawimage(gui[13],ix*9,getHeight-9);
2103 if (hp mod 2)<>0 then begin drawimage(gui[14],ix*9,getHeight-9); ix:=ix+1 end;
2104 for ix:=ix to 9 do drawimage(gui[15],ix*9,getHeight-9);
2105 end; else
2106 if gamemode=2 then
2107 begin
2108 for ix:=0 to (hp div 2)-1 do drawimage(gui[31],ix*9,getHeight-9);
2109 if (hp mod 2)<>0 then begin drawimage(gui[32],ix*9,getHeight-9); ix:=ix+1 end;
2110 for ix:=ix to 9 do drawimage(gui[33],ix*9,getHeight-9);
2111 end;
2113 for ix:=0 to (hunger div 2)-1 do drawimage(gui[26],ix*9,getHeight-18);
2114 if (hunger mod 2)<>0 then begin drawimage(gui[27],ix*9,getHeight-18); ix:=ix+1 end;
2115 for ix:=ix to 9 do drawimage(gui[28],ix*9,getHeight-18);
2117 if getmap((x+4)/16,y/16)=50 then
2118 begin
2119 for ix:=0 to (air div 2)-1 do drawimage(gui[29],ix*9,getHeight-27);
2120 if (air mod 2)<>0 then begin drawimage(gui[30],ix*9,getHeight-27); ix:=ix+1 end;
2121 end;
2122 end;
2124 for ix:=0 to 3 do
2125 begin
2126 drawfonttext(msg[ix],0,getHeight-20-ix*9);
2127 if getrelativetimems-msg_time[ix]>5000 then msg[ix]:='';
2128 end;
2129 end;
2130 end;
2132 procedure load_moon(path:string;phase:integer);
2133 var
2134 no:image;
2135 begin
2136 moon:=no;
2137 if load_sm=1 then
2138 begin
2139 moon:=ld_tex('moon_phase_'+phase+'.png',path,'terrain/moon_phases/');
2140 end; else
2141 if load_sm=2 then
2142 begin
2143 moon:=resize_image(ld_tex('moon_phase_'+phase+'.png',path,'terrain/moon_phases/'),(getWidth+getHeight)/5,(getWidth+getHeight)/5);
2144 end;
2145 end;
2147 procedure sleep;
2148 var
2149 i,ix,iy:integer;
2150 begin
2151 if game_time>300000 then
2152 begin
2153 if load_light_tex then
2154 begin
2155 keymode:=0;
2156 i:=15;
2157 while i>0 do
2158 begin
2159 for iy:=0 to getheight/16 do
2160 for ix:=0 to getwidth/16 do
2161 drawimage(light[i],ix*16,iy*16);
2162 i:=i-1;
2163 drawVideo;
2164 delay(100);
2165 end;
2166 moon_phase:=moon_phase+1;
2167 if moon_phase>7 then moon_phase:=0;
2168 load_moon('/'+sd+'/cavecraft/texturepacks/'+tex_pack+'/',moon_phase);
2169 game_time:=50000;
2170 last_sleep_x:=curx;
2171 last_sleep_y:=cury-1;
2172 i:=1;
2173 while i<15 do
2174 begin
2175 draw;
2176 for iy:=0 to getheight/16 do
2177 for ix:=0 to getwidth/16 do
2178 drawimage(light[i],ix*16,iy*16);
2179 i:=i+1;
2180 drawVideo;
2181 delay(100);
2182 end;
2183 end;
2184 else
2185 begin
2186 keymode:=0;
2187 i:=0;
2188 while i<=getheight do
2189 begin
2190 setcolor(0,0,0);
2191 fillrect(0,0,getwidth,i);
2192 i:=i+5;
2193 drawVideo;
2194 delay(50);
2195 end;
2196 moon_phase:=moon_phase+1;
2197 if moon_phase>7 then moon_phase:=0;
2198 load_moon('/'+sd+'/cavecraft/texturepacks/'+tex_pack+'/',moon_phase);
2199 game_time:=50000;
2200 last_sleep_x:=curx;
2201 last_sleep_y:=cury-1;
2202 i:=0;
2203 while i<=getheight do
2204 begin
2205 draw;
2206 setcolor(0,0,0);
2207 fillrect(0,i,getwidth,getheight);
2208 i:=i+5;
2209 drawVideo;
2210 delay(50);
2211 end;
2212 end;
2213 end; else create_msg('You can sleep only at night');
2214 end;
2216 procedure resetToolProgerss;
2217 begin
2218 toolus:=0;
2219 toolind:=0;
2220 end;
2222 procedure usetools(invcur, x, y:integer);
2223 var
2224 item, sum, block:integer;
2225 begin
2226 block:=getmap(x, y);
2227 item:=inv.getItem(invcur);
2228 sum:=inv.getSum(invcur);
2230 if gamemode=1 then
2231 destroy_block_cr(block, x, y);
2232 else
2233 if (block>0) then
2234 begin
2235 if (getItemType(item)=ITEM_TYPE_TOOL) and (getBlockTool(block)=getToolType(item)) then
2236 toolus:=toolus+getToolSpeed(item);
2237 else
2238 toolus:=toolus+1;
2240 if toolus>=getBlockHP(block) then
2241 begin
2242 if ((getItemType(item)=ITEM_TYPE_TOOL) and (getBlockTool(block)=getToolType(item)) and (getToolLvl(item)>=getBlockLvl(block))) or (getBlockLvl(block)<=0) then
2243 begin
2244 setMap(0, x, y);
2245 destroy_block_1(block, x, y);
2246 setMapInfo(0, x, y);
2247 end;
2248 else
2249 begin
2250 setMap(0, x, y);
2251 destroy_block_0(block, x, y);
2252 setMapInfo(0, x, y);
2253 end;
2255 if getItemType(item)=ITEM_TYPE_TOOL then
2256 begin
2257 inv.setSum(inv.getSum(invcur)-1, invcur);
2258 inv.fixNull(invcur);
2259 end;
2261 toolus:=0;
2262 toolind:=0;
2263 end;
2264 end;
2266 if (toolus>0) and (toolus<=getBlockHP(block)) then
2267 toolind:=((toolus*100) div getBlockHP(block)) div 10;
2268 end;
2270 function rt_useweap:boolean;
2271 var
2272 x, y, w, h, i, damg:integer;
2273 item:integer;
2274 begin
2275 x:=player.getX;
2276 y:=player.getY;
2277 w:=player.getW;
2278 h:=player.getH;
2279 item:=inv.getItem(invslot);
2281 if getItemType(item)=1 then
2282 damg:=getToolDamg(item);
2283 else
2284 damg:=1;
2286 if posi=0 then
2287 i:=mob.findAndHit(damg, x-TILE_SIZE, y, TILE_SIZE+(w/2), h, -2, -3);
2288 else
2289 i:=mob.findAndHit(damg, x+(w/2), y, TILE_SIZE+(w/2), h, 2, -3);
2291 if i<>-1 then
2292 begin
2293 inv.setSum(inv.getSum(invslot)-1, invslot);
2294 inv.fixNull(invslot);
2295 rt_useweap:=true;
2296 end;
2297 end;
2299 procedure rt_usetools;
2300 var
2301 x, y:integer;
2302 begin
2303 x:=player.getX;
2304 y:=player.getY;
2306 if posi=0 then curx:=(x div 16)-1; else curx:=(x div 16)+1;
2307 cury:=y div 16;
2308 if getmap(curx,cury)=0 then cury:=cury+1;
2310 if curx<0 then curx:=0;
2311 if curx>255 then curx:=255;
2312 if cury<0 then cury:=0;
2313 if cury>127 then cury:=127;
2315 usetools(invslot, curx, cury);
2316 end;
2318 procedure rt_usemob;
2319 var
2320 x, y:integer;
2321 begin
2322 x:=player.getX;
2323 y:=player.getY;
2325 if posi=0 then curx:=(x div 16)-1; else curx:=(x div 16)+1;
2326 cury:=y div 16;
2327 if getmap(curx,cury)=0 then cury:=cury+1;
2329 if curx<0 then curx:=0;
2330 if curx>255 then curx:=255;
2331 if cury<0 then cury:=0;
2332 if cury>127 then cury:=127;
2334 //usemob(curx,cury);
2335 end;
2337 procedure actionUse(invcur, x, y:integer);
2338 begin
2339 if setBlock(invcur, x, y)=false then
2340 if useBlock(invcur, x, y) then
2341 if useItem(invcur, x, y) then
2342 end;
2344 procedure rt_useblock;
2345 var
2346 x, y:integer;
2347 begin
2348 x:=player.getX;
2349 y:=player.getY;
2351 if posi=0 then curx:=(x div 16)-1; else curx:=(x div 16)+1;
2352 cury:=y div 16;
2353 if getmap(curx,cury)=0 then cury:=cury+1;
2355 if curx<0 then curx:=0;
2356 if curx>255 then curx:=255;
2357 if cury<0 then cury:=0;
2358 if cury>127 then cury:=127;
2360 actionUse(invslot, curx, cury);
2361 end;
2363 procedure go_to_nether;
2364 var
2365 ix:integer;
2366 begin
2367 portal_time:=getrelativetimems;
2368 savegame('/'+sd+'/cavecraft/saves/'+sav_fl+'/');
2369 pl_world:=1;
2370 drw_load_line('Matrix',10);
2371 if loadworld('/'+sd+'/cavecraft/saves/'+sav_fl+'/')=true then
2372 begin
2373 player.setX(get_spawn_x*16+4);
2374 player.setY((get_up(get_spawn_x)-1)*16);
2375 end;
2376 else
2377 begin
2378 {for ix:=0 to 31 do
2379 begin
2380 mob[ix].m_type:=0;
2381 mob[ix].m_x:=0;
2382 mob[ix].m_y:=0;
2383 mob[ix].m_posi:=0;
2384 mob[ix].m_velx:=0;
2385 mob[ix].m_vely:=0;
2386 mob[ix].m_ani:=0;
2387 mob[ix].m_min_vely:=0;
2388 mob[ix].m_hp:=0;
2389 mob[ix].m_del:=0;
2390 mob[ix].m_fall:=false;
2391 mob[ix].m_jmp:=false;
2392 mob[ix].m_velani:=false;
2393 end;}
2395 drop.resetData;
2396 gennether;
2397 //netherspawn;
2398 end;
2399 end;
2401 procedure go_to_world;
2402 begin
2403 portal_time:=getrelativetimems;
2404 savegame('/'+sd+'/cavecraft/saves/'+sav_fl+'/');
2405 pl_world:=0;
2406 drw_load_line('Matrix',10);
2407 if loadworld('/'+sd+'/cavecraft/saves/'+sav_fl+'/')=true then
2408 begin
2409 pl_world:=0;
2410 end;
2411 else
2412 begin
2413 debug('ERROR!!!');
2414 pl_world:=0;
2415 genworld;
2416 //megaspawn;
2417 end;
2418 end;
2420 procedure plr_is_dead_hardcore;
2421 begin
2422 keymode:=0;
2423 toolus:=0;
2424 toolind:=0;
2426 repeat
2427 updateKeys;
2428 if clickedKey(KEY_MENU_SELECT) then
2429 begin
2430 deleteworld('/'+sd+'/cavecraft/saves/'+sav_fl+'/');
2431 menu;
2432 exit;
2433 end;
2434 draw;
2435 drawfonttext('You died!',getwidth/2-32,getheight/2-16);
2437 drw_btn('Delete world',0,0,0,1);
2439 drawVideo;
2440 delay(1);
2441 until false;
2443 end;
2445 procedure plr_is_dead;
2446 var
2447 m_cur:integer;
2448 x, y:integer;
2449 begin
2450 x:=player.getX;
2451 y:=player.getY;
2452 keymode:=0;
2453 toolus:=0;
2454 toolind:=0;
2455 repeat
2456 updateKeys;
2457 if clickedKey(KEY_MENU_UP) then
2458 begin
2459 m_cur:=m_cur-1;
2460 if m_cur<0 then m_cur:=1;
2461 end;
2462 if clickedKey(KEY_MENU_DOWN) then
2463 begin
2464 m_cur:=m_cur+1;
2465 if m_cur>1 then m_cur:=0;
2466 end;
2467 if clickedKey(KEY_MENU_SELECT) then
2468 begin
2469 if m_cur=0 then
2470 begin
2471 if pl_world=0 then
2472 begin
2473 if last_sleep_x=0 then
2474 begin
2475 x:=get_spawn_x*16+4;
2476 y:=get_spawn_y*16;
2477 end; else
2478 begin
2479 x:=last_sleep_x*16+4;
2480 y:=last_sleep_y*16;
2481 end;
2482 end; else
2483 if pl_world=1 then
2484 begin
2485 go_to_world;
2486 if last_sleep_x=0 then
2487 begin
2488 x:=get_spawn_x*16+4;
2489 y:=get_spawn_y*16;
2490 end; else
2491 begin
2492 x:=last_sleep_x*16+4;
2493 y:=last_sleep_y*16;
2494 end;
2495 end;
2496 player.setX(x);
2497 player.setY(y);
2498 posi:=0;
2499 curx:=0;
2500 cury:=0;
2501 vely:=0;
2502 jmp:=false;
2503 hp:=20;
2504 hunger:=20;
2505 exit;
2506 end;
2507 else
2508 if m_cur=1 then begin menu; exit; end;
2509 end;
2511 draw;
2513 drawfonttext('You died!',getwidth/2-32,getheight/2-16);
2515 drw_btn('Respawn',0,m_cur,0,1);
2516 drw_btn('Main menu',1,m_cur,0,1);
2518 drawVideo;
2519 delay(1);
2520 until false;
2521 end;
2523 procedure fast_menu;
2524 var
2525 m_cur,i:integer;
2526 begin
2527 repeat
2528 updateKeys;
2529 if clickedKey(KEY_MENU_UP) then begin m_cur:=m_cur-1; if m_cur<0 then m_cur:=3; end;
2530 if clickedKey(KEY_MENU_DOWN) then begin m_cur:=m_cur+1; if m_cur>3 then m_cur:=0; end;
2531 if clickedKey(KEY_MENU_SELECT) then
2532 begin
2533 if m_cur=0 then
2534 begin
2535 exit;
2536 end; else
2537 if m_cur=1 then
2538 begin
2539 savegame('/'+sd+'/cavecraft/saves/'+sav_fl+'/');
2540 exit;
2541 end; else
2542 if m_cur=2 then
2543 begin
2544 menu_sett;
2545 end; else
2546 if m_cur=3 then
2547 begin
2548 //clear_gui(keymode);
2549 menu;
2550 exit;
2551 end;
2552 end;
2553 draw;
2554 drawfonttext('Game menu',getwidth/2-36,getheight/2-20);
2555 drw_btn('Back to Game',0,m_cur,0,1);
2556 drw_btn('Save Game',1,m_cur,0,1);
2557 drw_btn('Options',2,m_cur,0,1);
2558 drw_btn('Quit to Title',3,m_cur,0,1);
2560 drawVideo;
2561 delay(1);
2562 until false;
2563 end;
2565 procedure keyFastInv;
2566 begin
2567 if clickedKey(KEY_FASTINV_NEXT) then
2568 begin
2569 invslot:=invslot-1;
2570 if invslot<0 then
2571 invslot:=8;
2572 end;
2574 if clickedKey(KEY_FASTINV_PREV) then
2575 begin
2576 invslot:=invslot+1;
2577 if invslot>8 then
2578 invslot:=0;
2579 end;
2580 end;
2582 procedure keyConsole;
2583 begin
2584 if clickedKey(KEY_CHAT) then
2585 SetTimer(1000,T_CONSOLE);
2587 if pressedKey(KEY_CHAT) then
2588 begin
2589 if GetTimer(T_CONSOLE)=TIMER_OK then
2590 begin
2591 call_console;
2592 ResetTimer(T_CONSOLE);
2593 end;
2594 end;
2595 else
2596 begin
2597 if GetTimer(T_CONSOLE)>TIMER_OK then
2598 begin
2599 deb:= not deb;
2600 ResetTimer(T_CONSOLE);
2601 end;
2602 end;
2603 end;
2605 procedure keyInventory;
2606 begin
2607 if clickedKey(KEY_PLR_OPENINV) then
2608 if gamemode<>1 then
2609 OpenPlayerInventory;
2610 else
2611 OpenCreativeWindow;
2612 end;
2614 procedure keyhandler;
2615 begin
2616 updateKeys;
2618 if WindowKeyHanler then
2619 begin
2620 if keymode=0 then
2621 begin
2622 keyInventory;
2623 keyFastInv;
2624 keyConsole;
2626 if clickedKey(KEY_PLR_EDITMODE) then
2627 begin
2628 keymode:=1;
2629 curx:=player.getX div 16;
2630 cury:=player.getY div 16;
2631 end;
2633 if clickedKey(KEY_PLR_USE) then
2634 rt_usemob;
2636 if clickedKey(KEY_MENU) then
2637 fast_menu;
2639 if clickedKey(KEY_PLR_ATTACK) then
2640 if rt_useweap=false then
2641 debug('useweap');
2643 if pressedKey(KEY_PLR_ATTACK) then
2644 begin
2645 rt_usetools;
2646 playAnim(ANIM_HAND);
2647 end;
2648 else
2649 begin
2650 resetToolProgerss;
2651 cancelAnim(ANIM_HAND);
2652 end;
2654 if pressedKey(KEY_PLR_UP) then
2655 player.gotoUP;
2656 if pressedKey(KEY_PLR_DOWN) then
2657 player.gotoDOWN;
2658 if pressedKey(KEY_PLR_LEFT) then
2659 player.gotoLEFT;
2660 if pressedKey(KEY_PLR_RIGHT) then
2661 player.gotoRIGHT;
2662 end;
2663 else
2664 if keymode=1 then
2665 begin
2666 keyInventory;
2667 keyFastInv;
2668 keyConsole;
2670 if clickedKey(KEY_PLR_UP) then
2671 begin
2672 cury:=cury-1;
2673 if cury<0 then
2674 cury:=0;
2675 end;
2676 if clickedKey(KEY_PLR_DOWN) then
2677 begin
2678 cury:=cury+1;
2679 if cury>127 then
2680 cury:=127;
2681 end;
2682 if clickedKey(KEY_PLR_LEFT) then
2683 begin
2684 curx:=curx-1;
2685 if curx<0 then
2686 curx:=0;
2687 end;
2688 if clickedKey(KEY_PLR_RIGHT) then
2689 begin
2690 curx:=curx+1;
2691 if curx>255 then
2692 curx:=255;
2693 end;
2695 if clickedKey(KEY_PLR_EDITMODE) then
2696 keymode:=0;
2698 if clickedKey(KEY_PLR_USE) then
2699 actionUse(invslot, curx, cury);
2701 if pressedKey(KEY_PLR_ATTACK) then
2702 begin
2703 usetools(invslot, curx, cury);
2704 playAnim(ANIM_HAND);
2705 end;
2706 else
2707 begin
2708 resetToolProgerss;
2709 cancelAnim(ANIM_HAND);
2710 end;
2712 if clickedKey(KEY_MENU) then
2713 fast_menu;
2714 end;
2716 end;
2718 end;
2720 procedure phyhandler;
2721 var
2722 i:integer;
2723 begin
2724 player.calcPhysics;
2725 mob.updatePhy;
2726 drop.calcPhy;
2727 end;
2729 procedure light_fillrect(l,x,y,r:integer);
2730 var
2731 ix,iy,ym,yp,xm,xp:integer;
2732 begin
2733 ym:=y-r;
2734 yp:=y+r;
2735 xm:=x-r;
2736 xp:=x+r;
2737 for ix:=xm to xp do
2738 for iy:=ym to yp do
2739 setmaplight(getmaplight(ix,iy)+l,ix,iy);
2740 end;
2742 procedure light_rect(l,x,y,r:integer);
2743 var
2744 ix,iy,ym,yp,xm,xp:integer;
2745 begin
2746 ym:=y-r;
2747 yp:=y+r;
2748 xm:=x-r;
2749 xp:=x+r;
2750 for ix:=xm to xp do
2751 begin
2752 setmaplight(getmaplight(ix,ym)+l,ix,ym);
2753 setmaplight(getmaplight(ix,yp)+l,ix,yp);
2754 end;
2755 for iy:=ym+1 to yp-1 do
2756 begin
2757 setmaplight(getmaplight(xm,iy)+l,xm,iy);
2758 setmaplight(getmaplight(xp,iy)+l,xp,iy);
2759 end;
2760 end;
2762 procedure calc_light(m,x,y:integer);
2763 var
2764 ix,iy,l,ss,sf:integer;
2765 begin
2766 if light_type=1 then light_fillrect(m,x,y,m/2); else
2767 for l:=m downto 1 do
2768 begin
2769 if ss mod 2=0 then light_rect(l,x,y,ss/2);
2770 ss:=ss+1;
2771 end;
2772 end;
2774 procedure calc_sun(ix,m:integer);
2775 var
2776 iy,ss:integer;
2777 begin
2778 ss:=m;
2779 for iy:=0 to 127 do
2780 begin
2781 setmaplight(ss,ix,iy);
2782 if ss=0 then break;
2783 ss:=ss-getBlockTr(getmap(ix,iy));
2784 if ss<0 then ss:=0;
2785 end;
2786 for iy:=iy+1 to 127 do
2787 begin
2788 setmaplight(0,ix,iy);
2789 end;
2790 end;
2792 procedure kill_plr;
2793 var
2794 i:integer;
2795 begin
2796 hp:=0;
2797 for i:=0 to INV_SIZE do
2798 begin
2799 if inv.isNull(i)=false then
2800 player.dropItem(inv.getItem(i), inv.getSum(i));
2801 inv.setItem(0, i);
2802 inv.setSum(0, i);
2803 end;
2804 if gamemode<2 then plr_is_dead; else plr_is_dead_hardcore;
2805 end;
2807 procedure hunger_and_air;
2808 var
2809 x, y:integer;
2810 begin
2811 x:=player.getX;
2812 y:=player.getY;
2813 if gamemode<>1 then
2814 begin
2815 if getrelativetimems-hung_time>=90000/(gamemode+1) then
2816 begin
2817 hung_time:=getrelativetimems;
2818 hunger:=hunger-1;
2819 end;
2820 if getrelativetimems-hp_time>=5000*(gamemode+1) then
2821 begin
2822 if hunger>16 then
2823 begin
2824 hp_time:=getrelativetimems;
2825 hp:=hp+1;
2826 if hp>20 then hp:=20;
2827 end; else
2828 if hunger<1 then
2829 begin
2830 hp_time:=getrelativetimems;
2831 hp:=hp-1;
2832 if hp<1 then if gamemode<2 then hp:=1;
2833 end;
2834 end;
2836 if getmap((x+4)/16,y/16)=50 then
2837 begin
2838 if getrelativetimems-air_time>=500 then
2839 begin
2840 air:=air-1;
2841 air_time:=getrelativetimems;
2842 if air<1 then
2843 begin
2844 hp_time:=getrelativetimems;
2845 hp:=hp-2;
2846 end;
2847 end;
2848 end; else
2849 air:=21;
2850 end;
2851 end;
2853 procedure game;
2854 var
2855 ix,iy,minx,maxx,miny,maxy,fps_t,tim, x, y:integer;
2856 begin
2857 x:=player.getX;
2858 y:=player.getY;
2860 hunger_and_air;
2862 if hunger<0 then hunger:=0;
2863 if air<0 then air:=0;
2865 fps_t:=fps;
2866 if fps_t<1 then fps_t:=1;
2868 if bl_ani5_d then
2869 if bl_ani5_v=false then
2870 begin
2871 bl_ani5:=bl_ani5+1;
2872 if bl_ani5>4 then
2873 begin
2874 bl_ani5:=4;
2875 bl_ani5_v:=not bl_ani5_v;
2876 end;
2877 end;
2878 else
2879 begin
2880 bl_ani5:=bl_ani5-1;
2881 if bl_ani5<0 then
2882 begin
2883 bl_ani5:=0;
2884 bl_ani5_v:=not bl_ani5_v;
2885 end;
2886 end;
2887 bl_ani5_d:=not bl_ani5_d;
2889 drop.reflux;
2890 player.getDrop;
2892 game_time:=game_time+(600000 div (fps_t*1000));
2894 // Ускорение игрового времени в 10 раз
2895 // game_time := game_time + (600000 div (fps_t*100));
2897 if (game_time>600000) or (game_time<0) then
2898 begin
2899 game_time:=0;
2900 moon_phase:=moon_phase+1;
2901 if moon_phase>7 then moon_phase:=0;
2902 load_moon('/'+sd+'/cavecraft/texturepacks/'+tex_pack+'/',moon_phase);
2903 end;
2905 tim := 10000 * getimagewidth(sky) / 600000 * game_time / 10000;
2906 global_light := effects.get(sky, tim, 1, 1) div 16;
2908 if clock_stage<>game_time div 75000 then begin clock_stage:=clock_stage+1; if clock_stage>7 then clock_stage:=0; end;
2910 if x<0 then x:=0;
2911 if x+8>4094 then x:=4086;
2912 if getBlockColl(getmap(x div 16, y div 16))>0 then hp:=hp-1;
2913 if random(4096)=random(4096) then begin osadki:=not osadki; end;
2914 osadki_ani:=osadki_ani+1;
2915 if osadki_ani>7 then osadki_ani:=0;
2917 if random(20)=random(20) then create_particle(6,x,y);
2919 if gamemode=1 then begin hp:=666; hunger:=666; end;
2921 if inv.getItem(invslot)=186 then
2922 ifminimap:=true;
2923 else
2924 ifminimap:=false;
2926 //if s_spawn_mob then if random(4096)=1547 then megaspawn;
2928 mob.update;
2930 if particle_upd then update_particle;
2932 if light_type>0 then
2933 begin
2934 minx:=camx/16-1;
2935 maxx:=(camx+getWidth)/16+1;
2936 if minx<0 then minx:=0;
2937 if maxx>255 then maxx:=255;
2938 for ix:=minx to maxx do calc_sun(ix,global_light);
2939 end;
2941 minx:=camx/16-8;
2942 maxx:=(camx+getWidth)/16+16;
2943 miny:=camy/16-8;
2944 maxy:=(camy+getHeight)/16+16;
2945 if minx<0 then minx:=0;
2946 if maxx>255 then maxx:=255;
2947 if miny<0 then miny:=0;
2948 if maxy>127 then maxy:=127;
2949 for ix:=minx to maxx do
2950 for iy:=miny to maxy do
2951 begin
2952 if light_type=0 then setmaplight(15,ix,iy);
2953 else
2954 if light_type>0 then if getBlockLi(getmap(ix,iy))>0 then calc_light(getBlockLi(getmap(ix,iy)),ix,iy);
2955 end;
2957 minx:=(camx/16)-((getwidth/2)/16);
2958 miny:=(camy/16)-((getheight/2)/16);
2959 maxx:=(camx+(getWidth+getWidth/2))/16;
2960 maxy:=(camy+(getHeight+getHeight/2))/16;
2961 if minx<0 then minx:=0;
2962 if miny<0 then miny:=0;
2963 if maxx>255 then maxx:=255;
2964 if maxy>127 then maxy:=127;
2965 if updx<minx then updx:=minx;
2966 if updy<miny then updy:=miny;
2967 if updx>maxx then updx:=minx;
2968 if updy>maxy then updy:=miny;
2970 if bl_upd>0 then
2971 begin
2972 for ix:=0 to (((2*getWidth/16)*(2*getHeight/16))-1) div ((fps_t*bl_upd)) do
2973 begin
2974 updateBlock(updx, updy);
2975 updx:=updx+1;
2976 if updx>maxx then
2977 begin
2978 updx:=minx;
2979 updy:=updy+1;
2980 if updy>maxy then updy:=miny;
2981 end;
2982 end;
2983 end;
2985 {if coll_bl(110)=true then
2986 begin
2987 if getrelativetimems-portal_time>5000 then
2988 begin
2989 if pl_world=0 then begin go_to_nether; portal_time:=getrelativetimems; end; else
2990 if pl_world=1 then begin go_to_world; portal_time:=getrelativetimems; end;
2991 end;
2992 end;
2994 if coll_bl(51)=true then hp:=hp-1;
2996 if coll_bl(59)=true then
2997 begin
2998 if vely<0 then vely:=-1;
2999 else
3000 if vely>0 then vely:=1;
3001 end;}
3003 if gamemode<>1 then
3004 begin
3005 if hp>20 then hp:=20;
3006 if hunger>20 then hunger:=20;
3007 end;
3009 if y>2048 then kill_plr;
3010 if (hp<1) and (gamemode<>1) then kill_plr;
3011 if fish=true then fishing;
3013 if (fish=true) and (inv.getItem(invslot)<>152) then fish:=false;
3015 //if (keymode>3) and (cury<(y div 16)-4) then begin clear_gui(keymode); keymode:=0; curx:=x div 16; cury:=y div 16; end;
3017 if gamemode<>1 then
3018 begin
3019 if cury<(y div 16)-4 then cury:=(y div 16)-4;
3020 if cury>(y div 16)+5 then cury:=(y div 16)+5;
3021 if curx<(x div 16)-4 then curx:=(x div 16)-4;
3022 if curx>(x div 16)+4 then curx:=(x div 16)+4;
3023 end;
3025 UpdateFurnaces;
3026 end;
3028 procedure qt_start;
3029 var
3030 i:integer;
3031 begin
3032 drawgui:=true;
3033 LoadDrawFont('/'+sd+'/cavecraft/texturepacks/'+tex_pack+'/');
3034 drawdeadlogo;
3035 start_uu;
3036 if load_key_tex=1 then init_touch;
3037 for i:=0 to 4 do
3038 begin
3039 msg[i]:='';
3040 msg_time[i]:=getrelativetimems;
3041 end;
3042 InitMap(MAP_W, MAP_H);
3043 menu;
3044 end;
3046 procedure draw_debug;
3047 var
3048 i : Integer;
3049 begin
3050 //drawfonttext('X:'+(x div 16-128),0,0);
3051 //drawfonttext('Y:'+integertostring(127-(y div 16)),0,8);
3052 drawfonttext('CURX:'+(curx-128),0,16);
3053 drawfonttext('CURY:'+cury,0,24);
3054 drawfonttext('UPDX:'+updx,0,32);
3055 drawfonttext('UPDY:'+updy,0,40);
3056 drawfonttext('FPS:'+fps,0,56);
3057 drawfonttext('Free RAM:'+free_ram/1024+' KB',0,64);
3058 drawfonttext('Total RAM:'+memory.get_totalmemory div 1024+' KB',0,72);
3059 drawfonttext('SEED:'+seed,0,88);
3060 drawfonttext('Game time:'+game_time,0,96);
3061 drawfonttext('Global light:' + global_light, 0, 104);
3063 for i := 0 to 15 do
3064 drawImage(light[i], getWidth - 16, 16 * i);
3065 end;
3067 begin
3068 qt_start;
3069 hung_time:=getrelativetimems;
3070 hp_time:=getrelativetimems;
3071 air_time:=getrelativetimems;
3072 portal_time:=getrelativetimems;
3073 repeat
3074 proc_fps;
3075 keyhandler;
3076 phyhandler;
3077 game;
3078 draw;
3079 drawfonttext(version,getWidth-(length(version)*8),getHeight-8);
3080 if getrelativetimems-msg_time[4]>500 then begin free_ram:=memory.get_freememory; if free_ram<0 then free_ram:=-free_ram; msg_time[4]:=getrelativetimems; end;
3081 if deb = true then
3082 draw_debug;
3084 drawVideo;
3085 maxfps;
3087 until false;
3088 end.