Repository navigation
Expand file tree
/
Copy pathworldtask.cpp
More file actions
1326 lines (1154 loc) · 45.9 KB
/
Copy pathworldtask.cpp
File metadata and controls
1326 lines (1154 loc) · 45.9 KB
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
#include <sys/stat.h>
#ifndef _TINSPIRE
#include <SDL/SDL.h>
#endif
#include "worldtask.h"
#include "aabb.h"
#include "blockrenderer.h"
#include "blocklisttask.h"
#include "menutask.h"
#include "settingstask.h"
#include "fastmath.h"
#include "font.h"
#include "inventory.h"
#include "inventorytask.h"
#include "graphtask.h"
#include "textures/items.h"
#include "textures/blockselection.h"
#include "textures/inventory.h"
#include "textures/icons.h"
#include "deathtask.h"
#include "humanentity.h"
#include "chickenentity.h"
#include "creeperentity.h"
#include "grounddrops.h"
WorldTask world_task;
extern unsigned char font_dat[];
static unsigned int measureTextWidth(const char *str)
{
unsigned int w = 0;
while(*str)
w += font_dat[17 + static_cast<unsigned char>(*str++)];
return w;
}
constexpr GLFix WorldTask::player_width, WorldTask::player_height, WorldTask::eye_pos;
static BLOCK_WDATA inventoryDropItem(const BLOCK_WDATA block)
{
if(getBLOCK(block) == BLOCK_STONE)
return BLOCK_COBBLESTONE;
if(getBLOCK(block) == BLOCK_GRASS)
return BLOCK_DIRT;
if(getBLOCK(block) == BLOCK_LEAVES)
return BLOCK_AIR;
// Fast water dropping regular water
if(getBLOCK(block) == BLOCK_WATER_FAST)
return getBLOCKWDATA(BLOCK_WATER, RANGE_WATER);
if(global_block_renderer.isOriented(block))
return getBLOCK(block);
return getBLOCKWDATA(getBLOCK(block), getBLOCKDATA(block));
}
static int heldPickaxeTier(const BLOCK_WDATA held)
{
if(getBLOCK(held) != BLOCK_ITEM)
return 0;
switch(static_cast<ItemTexture>(getBLOCKDATA(held)))
{
case ItemTexture::WOODEN_PICKAXE:
return 1;
case ItemTexture::STONE_PICKAXE:
return 2;
case ItemTexture::IRON_PICKAXE:
return 3;
case ItemTexture::DIAMOND_PICKAXE:
return 4;
case ItemTexture::GOLDEN_PICKAXE:
return 5;
default:
return 0;
}
}
static int requiredPickaxeTierForDrop(const BLOCK block)
{
switch(block)
{
case BLOCK_STONE:
case BLOCK_COBBLESTONE:
case BLOCK_COAL_ORE:
case BLOCK_FURNACE:
case BLOCK_NETHERRACK:
return 1; // Wooden+
case BLOCK_IRON_ORE:
case BLOCK_IRON:
return 2; // Stone+
case BLOCK_GOLD_ORE:
case BLOCK_GOLD:
case BLOCK_DIAMOND_ORE:
case BLOCK_DIAMOND:
case BLOCK_REDSTONE_ORE:
return 3; // Iron+
default:
return 0;
}
}
static bool isPickaxeMinedBlock(const BLOCK block)
{
return requiredPickaxeTierForDrop(block) > 0;
}
/** Left click / KEY_7 primary: punch nearest mob in crosshair if closer than block hit (MC melee). */
static bool tryMeleeMob()
{
GLFix yr = world_task.yr;
yr.normaliseAngle();
GLFix xr = world_task.xr;
xr.normaliseAngle();
GLFix dx = GLFix(fast_sin(yr)) * GLFix(fast_cos(xr));
GLFix dy = -GLFix(fast_sin(xr));
GLFix dz = GLFix(fast_cos(yr)) * GLFix(fast_cos(xr));
const GLFix eye_y = world_task.y + WorldTask::eye_pos;
HumanEntity *hit_h = nullptr;
GLFix h_dist = GLFix::maxValue();
for(auto &h : human_entities)
{
if(!h.isAliveMob())
continue;
GLFix dist;
if(h.aabb.intersectsRay(world_task.x, eye_y, world_task.z, dx, dy, dz, dist) == AABB::NONE)
continue;
if(dist < GLFix(0))
continue;
if(dist < h_dist)
{
h_dist = dist;
hit_h = &h;
}
}
ChickenEntity *hit_c = nullptr;
GLFix c_dist = GLFix::maxValue();
for(auto &c : chicken_entities)
{
if(!c.isAliveMob())
continue;
GLFix dist;
if(c.aabb.intersectsRay(world_task.x, eye_y, world_task.z, dx, dy, dz, dist) == AABB::NONE)
continue;
if(dist < GLFix(0))
continue;
if(dist < c_dist)
{
c_dist = dist;
hit_c = &c;
}
}
CreeperEntity *hit_cr = nullptr;
GLFix cr_dist = GLFix::maxValue();
for(auto &cr : creeper_entities)
{
if(!cr.isAliveMob())
continue;
GLFix dist;
if(cr.aabb.intersectsRay(world_task.x, eye_y, world_task.z, dx, dy, dz, dist) == AABB::NONE)
continue;
if(dist < GLFix(0))
continue;
if(dist < cr_dist)
{
cr_dist = dist;
hit_cr = &cr;
}
}
if(hit_h == nullptr && hit_c == nullptr && hit_cr == nullptr)
return false;
GLFix best_dist = GLFix::maxValue();
int pick = -1; // 0 human, 1 chicken, 2 creeper
if(hit_h != nullptr && h_dist < best_dist)
{
best_dist = h_dist;
pick = 0;
}
if(hit_c != nullptr && c_dist < best_dist)
{
best_dist = c_dist;
pick = 1;
}
if(hit_cr != nullptr && cr_dist < best_dist)
{
best_dist = cr_dist;
pick = 2;
}
if(pick < 0)
return false;
VECTOR3 hit_block;
AABB::SIDE side = AABB::NONE;
GLFix block_dist;
const bool got_block = world.intersectsRay(world_task.x, eye_y, world_task.z, dx, dy, dz, hit_block, side, block_dist, false);
if(got_block && side != AABB::NONE && block_dist <= best_dist)
return false;
if(pick == 1)
hit_c->applyMeleeDamage(2, yr);
else if(pick == 2)
hit_cr->applyMeleeDamage(2, yr);
else
hit_h->applyMeleeDamage(2, yr);
return true;
}
void WorldTask::hurtPlayer(unsigned int dmg, const char *msg)
{
if(dmg == 0)
return;
if(dmg >= hearts)
hearts = 0;
else
hearts -= dmg;
if(hearts == 0)
{
death_task.makeCurrent();
return;
}
if(msg && msg[0])
setMessage(msg);
}
void WorldTask::makeCurrent()
{
Task::background_saved = false;
Task::makeCurrent();
}
//Invert pixel at (x|y) relative to the center of the screen
void WorldTask::crosshairPixel(int x, int y)
{
int pos = SCREEN_WIDTH/2 + x + (SCREEN_HEIGHT/2 + y)*SCREEN_WIDTH;
screen->bitmap[pos] = ~screen->bitmap[pos];
}
void WorldTask::getForward(GLFix *x, GLFix *z)
{
*x = GLFix(fast_sin(yr)) * speed();
*z = GLFix(fast_cos(yr)) * speed();
}
void WorldTask::getRight(GLFix *x, GLFix *z)
{
*x = GLFix(fast_sin((yr + 90).normaliseAngle())) * speed();
*z = GLFix(fast_cos((yr + 90).normaliseAngle())) * speed();
}
GLFix WorldTask::speed()
{
GLFix base = 10 * settings_task.getValue(SettingsTask::SPEED) + 10;
if(keyPressed(KEY_NSPIRE_CTRL)) // Sprint
return base * 2;
if(keyPressed(KEY_NSPIRE_SHIFT)) // Sneak
return base / 2;
if(speed_multiplier_held) // 10x speed
return base * 10;
return base;
}
void WorldTask::logic(GLFix dt)
{
const bool graph_mode = world.worldType() == World::WorldType::Graph;
#ifndef _TINSPIRE
const Uint8 *desktop_keys = SDL_GetKeyState(nullptr);
const bool desktop_t_held = desktop_keys[SDLK_t] != 0;
const bool desktop_g_held = desktop_keys[SDLK_g] != 0;
const bool desktop_j_held = desktop_keys[SDLK_j] != 0;
const bool desktop_x_held = desktop_keys[SDLK_x] != 0;
const bool desktop_z_held = desktop_keys[SDLK_z] != 0;
const bool desktop_v_held = desktop_keys[SDLK_v] != 0;
speed_multiplier_held = desktop_v_held;
#else
const bool desktop_t_held = false;
const bool desktop_g_held = false;
const bool desktop_j_held = false;
const bool desktop_x_held = false;
const bool desktop_z_held = false;
speed_multiplier_held = false;
#endif
GLFix dx = 0, dz = 0;
if(keyPressed(KEY_NSPIRE_8)) //Forward
{
GLFix dx1, dz1;
getForward(&dx1, &dz1);
dx += dx1;
dz += dz1;
}
else if(keyPressed(KEY_NSPIRE_2)) //Backward
{
GLFix dx1, dz1;
getForward(&dx1, &dz1);
dx -= dx1;
dz -= dz1;
}
if(keyPressed(KEY_NSPIRE_4)) //Left
{
GLFix dx1, dz1;
getRight(&dx1, &dz1);
dx -= dx1;
dz -= dz1;
}
else if(keyPressed(KEY_NSPIRE_6)) //Right
{
GLFix dx1, dz1;
getRight(&dx1, &dz1);
dx += dx1;
dz += dz1;
}
dx *= dt;
dz *= dt;
if(graph_mode)
{
x += dx;
z += dz;
GLFix dy = 0;
if(keyPressed(KEY_NSPIRE_5))
dy += speed() * dt;
if(keyPressed(KEY_NSPIRE_CTRL))
dy -= speed() * dt;
y += dy;
if(y < GLFix(2 * BLOCK_SIZE))
y = GLFix(2 * BLOCK_SIZE);
vy = 0;
can_jump = true;
in_water = false;
fall_distance = 0;
safe_spawn_pending = false;
}
else if(!world.intersect(aabb))
{
AABB aabb_moved = aabb;
aabb_moved.low_x += dx;
aabb_moved.high_x += dx;
if(!world.intersect(aabb_moved))
{
x += dx;
aabb = aabb_moved;
}
aabb_moved = aabb;
aabb_moved.low_z += dz;
aabb_moved.high_z += dz;
if(!world.intersect(aabb_moved))
{
z += dz;
aabb = aabb_moved;
}
const GLFix vy_before = vy;
aabb_moved = aabb;
aabb_moved.low_y += vy * dt;
aabb_moved.high_y += vy * dt;
can_jump = world.intersect(aabb_moved);
const bool landed_from_fall = (vy_before < GLFix(0)) && can_jump && fall_distance > GLFix(0);
if(!can_jump)
{
y += vy * dt;
aabb = aabb_moved;
// While falling (downward velocity) and we actually move, accumulate fall distance.
if(vy_before < GLFix(0))
fall_distance += (-vy_before) * dt;
}
else if(vy > GLFix(0))
{
can_jump = false;
vy = 0;
}
else
vy = 0;
vy -= GLFix(5) * dt;
in_water = getBLOCK(world.getBlock((x / BLOCK_SIZE).floor(), ((y + eye_pos) / BLOCK_SIZE).floor(), (z / BLOCK_SIZE).floor())) == BLOCK_WATER
|| getBLOCK(world.getBlock((x / BLOCK_SIZE).floor(), ((y + eye_pos) / BLOCK_SIZE).floor(), (z / BLOCK_SIZE).floor())) == BLOCK_WATER_FAST;
if(landed_from_fall)
{
// Apply fall damage only when not in water, and never during the initial safe spawn.
if(!safe_spawn_pending && !in_water)
{
const int fall_blocks = fall_distance.toInteger<int>() / BLOCK_SIZE;
const int dmg = fall_blocks > 3 ? (fall_blocks - 3) : 0;
if(dmg > 0)
{
if(static_cast<unsigned int>(dmg) >= hearts)
hearts = 0;
else
hearts -= static_cast<unsigned int>(dmg);
if(hearts == 0)
{
// Switch to death screen; the player can respawn from there.
death_task.makeCurrent();
return;
}
else
{
setMessage("Ouch!");
}
}
}
// Whether we took damage or not, we've impacted.
fall_distance = 0;
}
if(in_water)
can_jump = true;
}
if(!graph_mode && keyPressed(KEY_NSPIRE_5) && can_jump) //Jump
{
vy = 50;
can_jump = false;
}
#ifndef _TINSPIRE
int rel_x = 0, rel_y = 0;
SDL_GetRelativeMouseState(&rel_x, &rel_y);
if(rel_x != 0 || rel_y != 0)
{
yr += GLFix(rel_x) / 3;
xr += GLFix(rel_y) / 3;
}
#endif
if(has_touchpad)
{
touchpad_report_t touchpad;
touchpad_scan(&touchpad);
if(touchpad.pressed)
{
switch(touchpad.arrow)
{
case TPAD_ARROW_DOWN:
xr += speed()/2 * dt;
break;
case TPAD_ARROW_UP:
xr -= speed()/2 * dt;
break;
case TPAD_ARROW_LEFT:
yr -= speed()/2 * dt;
break;
case TPAD_ARROW_RIGHT:
yr += speed()/2 * dt;
break;
case TPAD_ARROW_RIGHTDOWN:
xr += speed()/2 * dt;
yr += speed()/2 * dt;
break;
case TPAD_ARROW_UPRIGHT:
xr -= speed()/2 * dt;
yr += speed()/2 * dt;
break;
case TPAD_ARROW_DOWNLEFT:
xr += speed()/2 * dt;
yr -= speed()/2 * dt;
break;
case TPAD_ARROW_LEFTUP:
xr -= speed()/2 * dt;
yr -= speed()/2 * dt;
break;
}
}
else if(tp_had_contact && touchpad.contact)
{
yr += (touchpad.x - tp_last_x) / 17;
xr -= (touchpad.y - tp_last_y) / 17;
}
tp_had_contact = touchpad.contact;
tp_last_x = touchpad.x;
tp_last_y = touchpad.y;
}
else
{
if(keyPressed(KEY_NSPIRE_UP))
xr -= speed()/3 * dt;
else if(keyPressed(KEY_NSPIRE_DOWN))
xr += speed()/3 * dt;
if(keyPressed(KEY_NSPIRE_LEFT))
yr -= speed()/3 * dt;
else if(keyPressed(KEY_NSPIRE_RIGHT))
yr += speed()/3 * dt;
}
//Normalisation required for rotation with nglRotate
yr.normaliseAngle();
xr.normaliseAngle();
//xr and yr are normalised, so we can't test for negative values
if(xr > GLFix(180))
if(xr <= GLFix(270))
xr = 269;
if(xr < GLFix(180))
if(xr >= GLFix(90))
xr = 89;
//Do test only on every second frame, it's expensive
if(do_test)
{
GLFix dx = fast_sin(yr)*fast_cos(xr), dy = -fast_sin(xr), dz = fast_cos(yr)*fast_cos(xr);
GLFix dist;
if(!world.intersectsRay(x, y + eye_pos, z, dx, dy, dz, selection_pos, selection_side, dist, in_water))
selection_side = AABB::NONE;
else
selection_pos_abs = {x + dx * dist, y + eye_pos + dy * dist, z + dz * dist};
}
world.setPosition(x, y, z);
if(safe_spawn_pending)
{
VECTOR3 hit_pos = {-1, -1, -1};
AABB::SIDE hit_side = AABB::NONE;
GLFix dist;
constexpr GLFix dx = GLFix(0);
constexpr GLFix dz = GLFix(0);
constexpr GLFix dy = GLFix(-1);
// Raycast straight down from the player's eye.
if(world.intersectsRay(x, y + eye_pos, z, dx, dy, dz, hit_pos, hit_side, dist, false))
{
const int hit_block_y = hit_pos.y.toInteger<int>();
// Place player 1 block above the first solid block hit.
y = GLFix(hit_block_y + 1) * BLOCK_SIZE + GLFix::minStep();
}
// Reset velocities and fall tracking after teleporting.
vy = 0;
fall_distance = 0;
safe_spawn_pending = false;
}
do_test = !do_test;
if(!keyPressed(KEY_NSPIRE_9))
{
mining_progress = 0;
mining_tick_accum = 0;
}
if(key_held_down)
{
key_held_down = keyPressed(KEY_NSPIRE_ESC) || keyPressed(KEY_NSPIRE_7) || keyPressed(KEY_NSPIRE_1) || keyPressed(KEY_NSPIRE_3) || keyPressed(KEY_NSPIRE_PERIOD) || keyPressed(KEY_NSPIRE_MINUS) || keyPressed(KEY_NSPIRE_PLUS) || keyPressed(KEY_NSPIRE_MENU) || keyPressed(KEY_NSPIRE_A) || desktop_t_held;
key_held_down = key_held_down || desktop_g_held || desktop_j_held || desktop_x_held || desktop_z_held;
}
else if(keyPressed(KEY_NSPIRE_ESC) || keyPressed(KEY_NSPIRE_MENU))
{
menu_task.makeCurrent();
key_held_down = true;
return;
}
else if(keyPressed(KEY_NSPIRE_7)) //Put block down
{
key_held_down = true;
// Melee first; do not return from logic() so mob updates still run this tick (avoids stale state / render glitches).
if(!tryMeleeMob())
{
if(selection_side == AABB::NONE)
return;
if(world.intersect(aabb))
return;
if(world.blockAction(selection_pos.x, selection_pos.y, selection_pos.z))
return;
BLOCK_WDATA current_block = world.getBlock(selection_pos.x, selection_pos.y, selection_pos.z),
block_to_place = current_inventory.currentSlot();
if(getBLOCK(block_to_place) == BLOCK_AIR)
return;
if(getBLOCK(block_to_place) == BLOCK_ITEM)
return;
// When placing fluid onto a non-full fluid block of the same type, "fill" it
if(current_block != block_to_place
&& ((getBLOCK(current_block) == BLOCK_WATER && getBLOCK(block_to_place) == BLOCK_WATER)
|| (getBLOCK(current_block) == BLOCK_LAVA && getBLOCK(block_to_place) == BLOCK_LAVA)))
{
world.changeBlock(selection_pos.x, selection_pos.y, selection_pos.z, block_to_place);
current_inventory.removeFromCurrentSlot();
return;
}
VECTOR3 pos = selection_pos;
switch(selection_side)
{
case AABB::BACK:
++pos.z;
break;
case AABB::FRONT:
--pos.z;
break;
case AABB::LEFT:
--pos.x;
break;
case AABB::RIGHT:
++pos.x;
break;
case AABB::BOTTOM:
--pos.y;
break;
case AABB::TOP:
++pos.y;
break;
default:
puts("This can't normally happen #1");
break;
}
current_block = world.getBlock(pos.x, pos.y, pos.z);
//Only set the block if there's air
if(current_block == BLOCK_AIR || (in_water && getBLOCK(current_block) == BLOCK_WATER))
{
bool placed = false;
if(!global_block_renderer.isOriented(block_to_place))
{
world.changeBlock(pos.x, pos.y, pos.z, block_to_place);
placed = true;
}
else
{
AABB::SIDE side = selection_side;
//If the block is not fully oriented and has been placed on top or bottom of another block, determine the orientation by yr
if(!global_block_renderer.isFullyOriented(block_to_place) && (side == AABB::TOP || side == AABB::BOTTOM))
side = yr < GLFix(45) ? AABB::FRONT : yr < GLFix(135) ? AABB::LEFT : yr < GLFix(225) ? AABB::BACK : yr < GLFix(315) ? AABB::RIGHT : AABB::FRONT;
world.changeBlock(pos.x, pos.y, pos.z, getBLOCKWDATA(block_to_place, side)); //AABB::SIDE is compatible to BLOCK_SIDE
placed = true;
}
//If the player is stuck now, it's because of the block change, so remove it again
if(placed && world.intersect(aabb))
{
world.changeBlock(pos.x, pos.y, pos.z, current_block);
placed = false;
}
if(placed)
{
if(getBLOCK(block_to_place) == BLOCK_FURNACE)
inventory_task.ensureFurnaceTile(pos.x, pos.y, pos.z);
current_inventory.removeFromCurrentSlot();
}
}
}
}
else if(keyPressed(KEY_NSPIRE_9)) //Remove block
{
BLOCK_WDATA b = world.getBlock(selection_pos.x, selection_pos.y, selection_pos.z);
if(selection_side != AABB::NONE && getBLOCK(b) != BLOCK_BEDROCK && getBLOCK(b) != BLOCK_AIR)
{
const BLOCK b_type = getBLOCK(b);
const int pickaxe_tier = heldPickaxeTier(current_inventory.currentSlot());
if (mining_pos.x != selection_pos.x || mining_pos.y != selection_pos.y || mining_pos.z != selection_pos.z) {
mining_pos = selection_pos;
mining_progress = 0;
mining_tick_accum = 0;
mining_duration = 30; // Default
if (b_type == BLOCK_DIRT || b_type == BLOCK_SAND || b_type == BLOCK_LEAVES || b_type == BLOCK_GRASS) mining_duration = 10;
else if (b_type == BLOCK_STONE || b_type == BLOCK_COBBLESTONE || b_type == BLOCK_IRON_ORE || b_type == BLOCK_COAL_ORE || b_type == BLOCK_FURNACE) mining_duration = 60;
else if (b_type == BLOCK_WOOD || b_type == BLOCK_PLANKS_NORMAL || b_type == BLOCK_CRAFTING_TABLE) mining_duration = 30;
else if (b_type == BLOCK_GLASS) mining_duration = 15;
else if (b_type == BLOCK_IRON || b_type == BLOCK_GOLD || b_type == BLOCK_DIAMOND) mining_duration = 100;
if(isPickaxeMinedBlock(b_type))
{
if(pickaxe_tier == 0)
mining_duration *= 3;
else if(pickaxe_tier == 1)
mining_duration = mining_duration * 100 / 100;
else if(pickaxe_tier == 2)
mining_duration = mining_duration * 70 / 100;
else if(pickaxe_tier == 3)
mining_duration = mining_duration * 50 / 100;
else if(pickaxe_tier == 4)
mining_duration = mining_duration * 40 / 100;
else
mining_duration = mining_duration * 35 / 100;
if(mining_duration < 3)
mining_duration = 3;
}
}
mining_tick_accum += dt;
while(mining_tick_accum >= GLFix(1))
{
mining_tick_accum -= GLFix(1);
mining_progress++;
if (mining_progress % 10 == 0) {
world.spawnDestructionParticles(selection_pos.x, selection_pos.y, selection_pos.z);
}
if (mining_progress >= mining_duration) {
world.spawnDestructionParticles(selection_pos.x, selection_pos.y, selection_pos.z);
const int required_pickaxe_tier = requiredPickaxeTierForDrop(b_type);
const bool can_harvest = required_pickaxe_tier == 0 || pickaxe_tier >= required_pickaxe_tier;
if(can_harvest)
{
const BLOCK_WDATA drop_stack = inventoryDropItem(b);
if(getBLOCK(drop_stack) != BLOCK_AIR)
{
const GLFix sx = selection_pos.x * GLFix(BLOCK_SIZE) + GLFix(BLOCK_SIZE / 2);
const GLFix sy = selection_pos.y * GLFix(BLOCK_SIZE) + GLFix(BLOCK_SIZE) + GLFix::minStep();
const GLFix sz = selection_pos.z * GLFix(BLOCK_SIZE) + GLFix(BLOCK_SIZE / 2);
spawnWorldDrop(sx, sy, sz, drop_stack, 1u);
}
}
if(b_type == BLOCK_FURNACE)
inventory_task.removeFurnaceTile(selection_pos.x, selection_pos.y, selection_pos.z);
world.changeBlock(selection_pos.x, selection_pos.y, selection_pos.z, BLOCK_AIR);
mining_progress = 0;
mining_tick_accum = 0;
break;
}
}
}
else
{
mining_progress = 0;
mining_tick_accum = 0;
}
}
else if(keyPressed(KEY_NSPIRE_1)) //Switch inventory slot
{
current_inventory.previousSlot();
key_held_down = true;
}
else if(keyPressed(KEY_NSPIRE_3))
{
current_inventory.nextSlot();
key_held_down = true;
}
else if(keyPressed(KEY_NSPIRE_PERIOD)) //Open list of blocks (or take screenshot with Ctrl + .)
{
if(keyPressed(KEY_NSPIRE_CTRL))
{
//Find a filename that doesn't exist
char buf[45];
unsigned int i;
for(i = 0; i <= 999; ++i)
{
snprintf(buf, sizeof(buf), "/documents/ndless/screenshot_%d.ppm.tns", i);
struct stat stat_buf;
if(stat(buf, &stat_buf) != 0)
break;
}
if(i > 999 || !saveTextureToFile(*screen, buf))
setMessage("Screenshot failed!");
else
{
snprintf(message, sizeof(message), "Screenshot taken (%d)!", i);
message_timeout = 20;
}
}
else
{
draw_inventory = false;
render();
draw_inventory = true;
block_list_task.makeCurrent();
}
key_held_down = true;
}
else if(graph_mode && desktop_z_held)
{
const int old_zoom = world.graphZoomPercent();
if(world.setGraphZoomPercent(old_zoom - 10))
{
world.clear();
char msg[32];
snprintf(msg, sizeof(msg), "Graph zoom: %d%%", world.graphZoomPercent());
setMessage(msg);
}
else
setMessage("Graph zoom min: 20%");
key_held_down = true;
}
else if(graph_mode && desktop_x_held)
{
const int old_zoom = world.graphZoomPercent();
if(world.setGraphZoomPercent(old_zoom + 10))
{
world.clear();
char msg[32];
snprintf(msg, sizeof(msg), "Graph zoom: %d%%", world.graphZoomPercent());
setMessage(msg);
}
else
setMessage("Graph zoom max: 800%");
key_held_down = true;
}
else if(keyPressed(KEY_NSPIRE_MINUS)) //Decrease max view distance
{
int fov = world.fieldOfView() - 1;
world.setFieldOfView(fov < 1 ? 1 : fov);
key_held_down = true;
}
else if(keyPressed(KEY_NSPIRE_PLUS)) //Increase max view distance
{
world.setFieldOfView(world.fieldOfView() + 1);
key_held_down = true;
}
// Handled above with ESC
else if(graph_mode && desktop_g_held)
{
graph_task.makeCurrent();
key_held_down = true;
}
else if(graph_mode && desktop_j_held)
{
world.setGraphUnbounded(!world.graphUnbounded());
world.clear();
setMessage(world.graphUnbounded() ? "Graph bounds: infinite" : "Graph bounds: [-30,30]");
key_held_down = true;
}
else if(keyPressed(KEY_NSPIRE_A))
{
inventory_task.makeCurrent();
key_held_down = true;
}
#ifndef _TINSPIRE
else
{
const Uint8 *keys = SDL_GetKeyState(nullptr);
if(keys[SDLK_t] != 0)
{
inventory_task.makeCurrent();
key_held_down = true;
}
}
#endif
// Discrete tick-based entities: advance ~dt worth of old 1-tick steps (carry fractional remainder).
sim_tick_accum += dt;
unsigned sim_steps = 0;
while(!graph_mode && sim_tick_accum >= GLFix(1) && sim_steps < 8)
{
sim_tick_accum -= GLFix(1);
inventory_task.tickFurnaces(world);
updateHumanEntities();
updateChickenEntities();
updateCreeperEntities();
updateGroundDrops();
++sim_steps;
}
}
void WorldTask::render()
{
const bool graph_mode = world.worldType() == World::WorldType::Graph;
aabb = {x - player_width/2, y, z - player_width/2, x + player_width/2, y + player_height, z + player_width/2};
//printf("X: %f Y: %f Z: %f XR: %d YR: %d\n", x.toFloat(), y.toFloat(), z.toFloat(), xr.toInt(), yr.toInt());
glColor3f(0.75f, 0.85f, 1.0f); //c0d8ff background
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
//Inverted rotation of the world
nglRotateX((GLFix(359) - xr).normaliseAngle());
nglRotateY((GLFix(359) - yr).normaliseAngle());
//Inverted translation of the world
glTranslatef(-x, -y - eye_pos, -z);
glBindTexture(terrain_current);
world.render();
// Render entities only in normal worlds.
if(!graph_mode)
{
renderHumanEntities();
renderChickenEntities();
renderCreeperEntities();
renderGroundDrops();
}
// Re-bind terrain texture for the rest of the world rendering
glBindTexture(terrain_current);
// Draw selection / breaking indication.
if(settings_task.getValue(SettingsTask::BLOCK_INDICATOR))
{
TextureAtlasEntry tex;
const bool show_breaking_overlay = mining_progress > 0 && mining_duration > 0 &&
selection_pos.x == mining_pos.x && selection_pos.y == mining_pos.y && selection_pos.z == mining_pos.z;
if(show_breaking_overlay)
{
glBindTexture(terrain_current);
static constexpr unsigned int breaking_frames = 10; // (0,15) to (9,15)
unsigned int breaking_frame = (static_cast<unsigned int>(mining_progress) * breaking_frames) / static_cast<unsigned int>(mining_duration);
if(breaking_frame >= breaking_frames)
breaking_frame = breaking_frames - 1;
tex = terrain_atlas[breaking_frame][15].current;
}
else
{
glBindTexture(&blockselection);
//Do a quick animation
const unsigned int blockselection_frame_width = blockselection.width / blockselection_frames;
tex = textureArea(0, 0, blockselection_frame_width, blockselection.height);
tex.left += blockselection_frame_width * blockselection_frame;
tex.right += blockselection_frame_width * blockselection_frame;
//Only increment the frame nr each 5 frames
if(++blockselection_frame_fraction == 5)
{
blockselection_frame_fraction = 0;
if(++blockselection_frame == blockselection_frames)
blockselection_frame = 0;
}
}
const GLFix indicator_x = selection_pos.x * BLOCK_SIZE, indicator_y = selection_pos.y * BLOCK_SIZE, indicator_z = selection_pos.z * BLOCK_SIZE;
const GLFix selection_offset = 3; //Needed to prevent Z-fighting
glPushMatrix();
glTranslatef(indicator_x, indicator_y, indicator_z);
glBegin(GL_QUADS);
if(show_breaking_overlay)
{
const GLFix block_size_fix = GLFix(BLOCK_SIZE);
const GLFix minus_offset = GLFix(0) - selection_offset;
const GLFix plus_offset = block_size_fix + selection_offset;
// Front
nglAddVertex({0, 0, minus_offset, tex.left, tex.bottom, TEXTURE_TRANSPARENT | TEXTURE_DRAW_BACKFACE});
nglAddVertex({0, block_size_fix, minus_offset, tex.left, tex.top, TEXTURE_TRANSPARENT | TEXTURE_DRAW_BACKFACE});
nglAddVertex({block_size_fix, block_size_fix, minus_offset, tex.right, tex.top, TEXTURE_TRANSPARENT | TEXTURE_DRAW_BACKFACE});