-
Notifications
You must be signed in to change notification settings - Fork 2
/
function_host.cpp
4610 lines (4016 loc) · 140 KB
/
function_host.cpp
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 "function_host.h"
#include "function_both.h" /* LOGadd etc */
#include <math.h> /* fabs etc. */
#pragma warning(disable: 4018 4244)
void function_host_init(void) { /* this groups all the initialisation stuff that was floating around the .h file */
//sockets_accept sets newsocket to new sockets id and waits till user sets
//newsocket=INVALID_SOCKET before accepting new sockets
//shared global variables
newsocket=INVALID_SOCKET;
newsocket_ip=0;
//temp local variables (transferred to globals after successful connection)
tnewsocket=INVALID_SOCKET;
tnewsocket_ip=0;
hsockets_accept=NULL;
idsockets_accept=NULL;
AUTOPICKUPfirst=0;
AUTOPICKUPnextfree=0;
housestorageadd=0;housestoragerestore=0; //flags used when saving/restoring house items in file
//house creation tool 1.0 variables
patchx=0;
patchy=0; //base offset for adding objects/changing basetiles
housenumber=65535; //house currently being created (1-?, 0 RESERVED, 65535=non-specific)
basehousenumber=20;
//OBJadd_allow=FALSE;
OBJmove_allow=FALSE;
OBJlist_last=0;
//entry values
WPF_NEXTTO=0;
WPF_OBJECT=NULL;
objsave_last=-1;
objsave_node_last=-1;
hrevive_infiniteloopexit=NULL;
idrevive_infiniteloopexit=NULL;
stealing_txt=NULL;
stealing_MESSAGE=TRUE;
OBJtmp=NULL;
OBJtmp2=NULL;
OBJtmp3=NULL;
OBJaddtocontainer_containermore=NULL;
}
void AUTOPICKUPadd(object* partymember, object *obj){//adds an item to the autopickup list
static npc *tnpc;
tnpc=(npc*)partymember->more;
AUTOPICKUPobject[AUTOPICKUPnextfree]=obj;
AUTOPICKUPett[AUTOPICKUPnextfree]=ett;
AUTOPICKUPplayer[AUTOPICKUPnextfree]=tnpc->player;
AUTOPICKUPpartymember[AUTOPICKUPnextfree]=partymember;
AUTOPICKUPflags[AUTOPICKUPnextfree]=0;
if (obj->info&32768){//remove mark if present and flag correctly
obj->info^=32768;
AUTOPICKUPflags[AUTOPICKUPnextfree]|=1;
}
AUTOPICKUPnextfree++;
if (AUTOPICKUPnextfree==AUTOPICKUPfirst){
AUTOPICKUPfirst++;
}
AUTOPICKUPadd_selfmanagement:
if (AUTOPICKUPfirst!=AUTOPICKUPnextfree){
if (AUTOPICKUPett[AUTOPICKUPfirst]<(ett-1024.0f)){
AUTOPICKUPfirst++; goto AUTOPICKUPadd_selfmanagement;
}
}
}//AUTOPICKUPadd
object *AUTOPICKUPcheck(player* tplayer, object *obj){
//returns an object pointer to the npc to return the item to or NULL
static short i,i2;
AUTOPICKUPcheck_selfmanagement:
if (AUTOPICKUPfirst!=AUTOPICKUPnextfree){
if (AUTOPICKUPett[AUTOPICKUPfirst]<(ett-1024.0f)){
AUTOPICKUPfirst++; goto AUTOPICKUPcheck_selfmanagement;
}
}
i=AUTOPICKUPfirst;
AUTOPICKUPcheck_next:
if (i!=AUTOPICKUPnextfree){
if (AUTOPICKUPobject[i]==obj){
if (AUTOPICKUPplayer[i]==tplayer){
for (i2=0;i2<=7;i2++){
if (AUTOPICKUPpartymember[i]==tplayer->party[i2]){
AUTOPICKUPobject[i]=NULL;//set object as invalid
AUTOPICKUPett[i]=ett-1024.0f-1.0f;//set ett so object can easily be cleared
if (AUTOPICKUPflags[i]&1){
obj->info|=32768;
}
return AUTOPICKUPpartymember[i];
}
}//i2
return NULL;
}
}
i++; goto AUTOPICKUPcheck_next;
}
return NULL;
}//AUTOPICKUPcheck
DWORD WINAPI sockets_accept(LPVOID null_value){
static long x;
sockets_accept_next:
static sockaddr_in accept_addr;
ZeroMemory(&accept_addr,sizeof(accept_addr));
x=sizeof(accept_addr);
tnewsocket=accept(u6osocket,(sockaddr*)&accept_addr,(int*)&x);
tnewsocket_ip=NULL;
if (accept_addr.sin_family==AF_INET){
tnewsocket_ip=accept_addr.sin_addr.S_un.S_addr;
}
//ban by IP?
static file *tfh;
static txt *t=txtnew(),*t2=txtnew();
tfh=open2("banip.txt",OF_READWRITE|OF_SHARE_COMPAT);
if (tfh->h!=HFILE_ERROR){
banip_nextip:
if (seek(tfh)<lof(tfh)){
txtfilein(t,tfh);
if (t->l){
txtset(t2,t);
x=(long)txtnum(t);
banip_nextbyte1: txtright(t,t->l-1); if (t->d2[0]!=46) goto banip_nextbyte1; txtright(t,t->l-1);
x+=((long)txtnum(t)<<8);
banip_nextbyte2: txtright(t,t->l-1); if (t->d2[0]!=46) goto banip_nextbyte2; txtright(t,t->l-1);
x+=((long)txtnum(t)<<16);
banip_nextbyte3: txtright(t,t->l-1); if (t->d2[0]!=46) goto banip_nextbyte3; txtright(t,t->l-1);
x+=((long)txtnum(t)<<24);
if (tnewsocket_ip==x){
x=tnewsocket;
txtset(t,"BannedIP_Blocked:"); txtadd(t,t2); LOGadd(t);//log
shutdown(x,SD_RECEIVE|SD_SEND);
SleepEx(4096,NULL);
closesocket(x);
goto sockets_accept_next;
}//tnewsocket_ip==x
}//t->l
goto banip_nextip;
}
close(tfh);
}//!=HFILE_ERROR
x=1; setsockopt(tnewsocket,IPPROTO_TCP,TCP_NODELAY,(char*)&x,4);
x=65536; setsockopt(tnewsocket,SOL_SOCKET,SO_RCVBUF,(char*)&x,4);
x=65536; setsockopt(tnewsocket,SOL_SOCKET,SO_SNDBUF,(char*)&x,4);
x=1; ioctlsocket(tnewsocket,FIONBIO,(unsigned long*)&x);
//WSAAsyncSelect(newsocket, hWnd, WM_USER + 100, FD_READ | FD_WRITE | FD_CONNECT);
/*
timeval tv_r;
tv_r.tv_sec = 0;
tv_r.tv_usec = 500;
fd_set socketset;
FD_ZERO(&socketset);
FD_SET(newsocket, &socketset);
select(FD_SETSIZE, &socketset, NULL, NULL, &tv_r);
*/
//x=1; ioctlsocket(newsocket, FIONBIO, (u_long FAR*) &x);
//x=0; setsockopt(newsocket,SOL_SOCKET,SO_DONTLINGER,(char*)&x,4);
send(tnewsocket,(char*)&U6O_SIGNATURE,4,0);
newsocket_ip=tnewsocket_ip;
newsocket=tnewsocket;
newsocket_wait:
if (newsocket!=INVALID_SOCKET){
SleepEx(15,FALSE);
goto newsocket_wait;
}
goto sockets_accept_next;
ExitThread(0);
return 0;
}
/*
//volatile links (vlnk)
*/
void VLNKnew(void *lnks,void *lnk,unsigned long off){
if (vlnkb_free_last==-1){
vlnkb_off[++vlnkb_last]=off;
vlnkb_lnk[vlnkb_last]=lnk;
vlnkb_lnks[vlnkb_last]=lnks;
}else{
vlnkb_off[vlnkb_free[vlnkb_free_last]]=off;
vlnkb_lnk[vlnkb_free[vlnkb_free_last]]=lnk;
vlnkb_lnks[vlnkb_free[vlnkb_free_last--]]=lnks;
}
}
void VLNKremove(void *lnk){ //remove any vlnk to lnk from other object
//if (lnk==NULL) MessageBox(NULL,"NULL/removed vlnk","Ultima 6 Online",MB_OK);
static long i;
static unsigned long *lp;
for (i=0;i<=vlnkb_last;i++){
if (vlnkb_lnk[i]==lnk){
lp=(unsigned long*)vlnkb_off[i];
lp[0]=NULL; //NULL vlnk
vlnkb_lnk[i]=NULL; vlnkb_lnks[i]=NULL; vlnkb_off[i]=NULL; //remove vlnk
vlnkb_free[++vlnkb_free_last]=i;
}//=
}//i
}
void VLNKsremove(void *lnks){ //remove all vlnk using offset within lnks
static long i;
for (i=0;i<=vlnkb_last;i++){
if (vlnkb_lnks[i]==lnks){
vlnkb_lnk[i]=NULL; vlnkb_lnks[i]=NULL; vlnkb_off[i]=NULL; //remove vlnk
vlnkb_free[++vlnkb_free_last]=i;
}//=
}//i
}
object *OBJnew(){
if (objb_free_last==-1){
//if (objb_last>28039) MessageBox(NULL,"OBJnew","Ultima 6 Online",MB_OK);
//objb_last+=10000;
ZeroMemory(&objb[++objb_last],sizeof(object));
return &objb[objb_last];
}else{
ZeroMemory(&objb[objb_free[objb_free_last]],sizeof(object));
return &objb[objb_free[objb_free_last--]];
}
return NULL; //OBJnew failed
}
void OBJrelease(object* obj){
int i=0;
//all objects attached to primary object must also be released
objb_free[++objb_free_last]=((unsigned long)obj-(unsigned long)&objb)/sizeof(object);
//for debugging "double release object bug"
/*for(i=0;i<objb_free_last-1;i++) {
if(objb_free[objb_free_last]==objb_free[i]) {
i=objb_free_last;
}
}*/
//clear object data
ZeroMemory(obj,sizeof(object));
}
/* free player struct */
void free(player* plr) {
static unsigned int i;
for (i=0;i<=9;i++) {
VLNKremove(plr->ktar[i]); VLNKsremove(plr->ktar[i]);
OBJremove(plr->ktar[i]); OBJrelease(plr->ktar[i]);
}
VLNKremove(plr->talk_target); VLNKsremove(plr->talk_target);
OBJremove(plr->talk_target); OBJrelease(plr->talk_target);
free(plr->password);
free(plr->name);
free(plr->newpassword);
if (plr->npcname) { /* this should be always set when the player logs in, but I'll add this here just in case */
free(plr->npcname);
}
free((void*)plr);
}
/* free creature items and creature struct */
void free(creature* crt) {
static unsigned int x5=0,y6;
static object *myobj;
//free inventory items
myobj=(object*)crt->items; //includes the bag!
if (myobj) x5=OBJlist(myobj);
OBJlist_last=NULL;
for(y6=0;y6<x5;y6++){
OBJremove(OBJlist_list[y6]); OBJrelease(OBJlist_list[y6]);
}
free((void*)crt);
}
/* this should properly clean everything from npc struct and after that free the struct,except the horse, because when removing npc it should be always dismounted*/
void free(npc* tnpc) {
static unsigned int x5,x2,y6;
static object *myobj;
//free inventory items
x5=0;
for (x2=0;x2<=8;x2++){
if (x2==0) myobj=tnpc->helm;
if (x2==1) myobj=tnpc->wep_right;
if (x2==2) myobj=tnpc->wep_left;
if (x2==3) myobj=tnpc->armour;
if (x2==4) myobj=tnpc->boots;
if (x2==5) myobj=tnpc->neck;
if (x2==6) myobj=tnpc->ring_right;
if (x2==7) myobj=tnpc->ring_left;
if (x2==8) myobj=(object*)tnpc->items; //includes the bag!
if (myobj) x5=OBJlist(myobj);
}
OBJlist_last=NULL;
for(y6=0;y6<x5;y6++){
if ((OBJlist_list[y6]->type&1023)==OBJ_HORSE_PAPERS) { /* if item being relesed is horse paper, remove the horse also */
myobj=(object*)((creature*)((object*)OBJlist_list[y6]->more)->more)->more;
OBJremove(myobj); OBJrelease(myobj);
free((creature*)((object*)OBJlist_list[y6]->more)->more);
OBJremove((object*)OBJlist_list[y6]->more); OBJrelease((object*)OBJlist_list[y6]->more);
}
else if ((OBJlist_list[y6]->type&1023)==OBJ_SHIP_DEED) { /* remove the ship */
myobj=(object*)OBJlist_list[y6]->more;
if ((myobj->type&1023)==OBJ_SHIP){ //ship
static mlobj *mmyobj;
if (myobj->info&2){ /* If on board the ship */
tnpc=(npc*)myobj->more;
mmyobj=(mlobj*)tnpc->player->craft_con;
}else{
mmyobj=(mlobj*)myobj->more;
}
for (x2=0;x2<=4;x2++){
OBJremove(mmyobj->obj[x2]); OBJrelease(mmyobj->obj[x2]);
}
}
if (!(myobj->info&2) && myobj->type!=0 && myobj->x!=0&& myobj->y!=0) { //remove the ship main object / skiff only if no one is abroad and the skiff is not already removed
OBJremove(myobj); OBJrelease(myobj);
}
}
OBJremove(OBJlist_list[y6]); OBJrelease(OBJlist_list[y6]);
}
free(tnpc->name);
free((void*)tnpc);
}
void OBJcheckflags(unsigned long x,unsigned long y){
//bt[][] unsigned short
//512 current basetiles exist ->expandable to 1024 using 10 bits
//6 bit flags reamain
//1=land passabe
//2=air passable
//4=sea passable
//8=bolt passable
//16=NPC passable (unlocked doors/containing items that can be picked up)
//32=view OK (can be seen past)
OBJcheckflags_flags=btflags[bt[y][x]&1023]; //reset using basetile flags
OBJcheckflags_flags+=32; //look ok
if (((bt[y][x]&1023)>=140)&&((bt[y][x]&1023)<188)) OBJcheckflags_flags&=(255-32); //look NOT ok wall
if (((bt[y][x]&1023)>=240)&&((bt[y][x]&1023)<252)) OBJcheckflags_flags&=(255-32); //look NOT ok cave
if (((bt[y][x]&1023)>=192)&&((bt[y][x]&1023)<208)) OBJcheckflags_flags&=(255-32); //look NOT ok win
OBJtmp=od[y][x];
OBJcf1: if (OBJtmp!=NULL){ //object exists
OBJcheckflags_td=objpassflags[(OBJtmp->type>>10)+sprlnk[OBJtmp->type&1023]];
if ((OBJcheckflags_td&1)==0) {OBJcheckflags_flags=OBJcheckflags_flags&(255-1-2);} //remove bottom bit from byte value
//*if item can be picked up include the NPCpassable flag else remove it (todo)
//skiff and ship can go on top of chests and probably some other items too needs fixing.
if (OBJcheckflags_td&2) {OBJcheckflags_flags=OBJcheckflags_flags|1|2;
if (OBJcheckflags_flags&4) OBJcheckflags_flags-=4;
} //override
if (OBJcheckflags_td&4) {
OBJcheckflags_flags=OBJcheckflags_flags&(255-8);
//if a bolt can't pass through it may affect our vision too
//is it a door?
if (((OBJtmp->type&1023)>=297)&&((OBJtmp->type&1023)<=300)) OBJcheckflags_flags&=(255-32);
if ((OBJtmp->type&1023)==334){
if ((OBJtmp->type&1024)==0) OBJcheckflags_flags&=(255-32); //secret door
}
if ((OBJtmp->type&1023)==213) OBJcheckflags_flags&=(255-32); //mousehole
} //remove bolt passable flag
//make back of horse passable
//if ((OBJtmp->type&1023)==431){
//if ((OBJtmp->type>>10)>=8){
//OBJcheckflags_flags|=16;
//}
//}
if (OBJcheckflags_td&8){ //NPC passable
OBJcheckflags_flags|=16;
}
if (OBJtmp->info&(4+2)){ //crt or npc
if (OBJcheckflags_flags&4){ //sea passable
OBJcheckflags_flags-=4;
}
if (OBJcheckflags_flags&2){ //air passable
OBJcheckflags_flags-=2;
}
}
OBJtmp=(object*)OBJtmp->next; goto OBJcf1;
}
bt[y][x]=(bt[y][x]&1023)+OBJcheckflags_flags*1024;
}
unsigned char OBJadd(unsigned long x,unsigned long y,object* obj){
if (od[y][x]==NULL){
od[y][x]=obj;
obj->prev=NULL;
obj->next=NULL;
obj->x=x;
obj->y=y;
}else{
OBJtmp=od[y][x];
OBJadd_j1:
//if ((objfloatflags[(OBJtmp->type>>10)+sprlnk[OBJtmp->type&1023]])&&((objfloatflags[(obj->type>>10)+sprlnk[obj->type&1023]] )==0)){
if (objfloatflags[(OBJtmp->type>>10)+sprlnk[OBJtmp->type&1023]]){
OBJtmp2=(object*)OBJtmp->prev;
if (OBJtmp2!=NULL){
OBJtmp2->next=obj;
obj->prev=OBJtmp2;
}else{
od[y][x]=obj;
obj->prev=NULL;
}
obj->next=OBJtmp;
OBJtmp->prev=obj;
obj->x=x;
obj->y=y;
goto OBJaddflt1;
}
if (OBJtmp->next!=NULL){
OBJtmp=(object*)OBJtmp->next;
goto OBJadd_j1;
}
OBJtmp->next=obj;
obj->prev=OBJtmp;
obj->next=NULL;
obj->x=x;
obj->y=y;
}
OBJaddflt1:
OBJcheckflags(x,y);
return 0; //success
}
void OBJaddtocontainer(object *container,object* objecttoadd){
if (obji[sprlnk[objecttoadd->type&1023]].weight==NULL){
if (U6O_DEBUG){
static txt *errortext=txtnew();
MessageBox(NULL,"ERROR CORRECTION: NULL WEIGHT ITEM TO PUT INTO CONTAINER PLACED ABOVE CONTAINER!","Ultima 6 Online",MB_OK);
txtnumint(errortext,container->x); txtadd(errortext,"<-X CONTAINER LOCATION"); MessageBox(NULL,errortext->d,"Ultima 6 Online",MB_OK);
txtnumint(errortext,container->y); txtadd(errortext,"<-Y CONTAINER LOCATION"); MessageBox(NULL,errortext->d,"Ultima 6 Online",MB_OK);
}
OBJadd(container->x,container->y,objecttoadd);
return;
}//weight==0
OBJaddtocontainer_containermore=(object*)container->more;
container->more=objecttoadd;
objecttoadd->next=OBJaddtocontainer_containermore;
objecttoadd->prev=container;
if (OBJaddtocontainer_containermore) OBJaddtocontainer_containermore->prev=objecttoadd;
}
void BTset(long x,long y,unsigned short i){
static unsigned short p;
x+=patchx; y+=patchy;
bt[y][x]=i;
if (NEThost){
OBJcheckflags(x,y);
if (housenumber!=65535){
if ((housex1[housenumber]==0)||(housex1[housenumber]>x)) housex1[housenumber]=x;
if ((housex2[housenumber]==0)||(housex2[housenumber]<x)) housex2[housenumber]=x;
if ((housey1[housenumber]==0)||(housey1[housenumber]>y)) housey1[housenumber]=y;
if ((housey2[housenumber]==0)||(housey2[housenumber]<y)) housey2[housenumber]=y;
for (p=0;p<housepnext[housenumber];p++){
if ((x==housepx[housenumber][p])&&(y==housepy[housenumber][p])) return;
}//p
p=housepnext[housenumber]; housepx[housenumber][p]=x; housepy[housenumber][p]=y;
housepnext[housenumber]++;
}//housenumber!=65535
}//NEThost
}
/* luteijn: away with thee!
void encrypt(txt *t4){
static txt *t2=txtnew();
static long x,x2,x3,x4,i3;
//ENCRYPT3.0
x4=rnd*65536; txtset(t2,"????"); t2->df[0]=(float)x4; //code offset
x3=0; //prev UNENCRYPTED value
for(i3=2;i3<t4->l;i3++){ //skip version (first 2 bytes)
x2=t4->d2[i3]; //=original value
x2=x2^0xFF; //bitwise NOT
x2+=x3; //add prev UNENCRYPTED value
if (x2>=256) x2-=256; //"loop" byte value
x2+=encryptcode[x4]; x4=(x4+1)&65535;
if (x2>=256) x2-=256; //"loop" byte value
x3=t4->d2[i3]; //set prev UNENCRYPTED value
t4->d2[i3]=x2; //set ENCRYPTED value
}
txtadd(t4,t2); //add encrypt code starting offset (float)
}
*/
void decrypt(txt *t4){
static txt *t5=txtnew();
static long x,x2,x3,x4,i3;
static float f;
//DECRYPT3.0
txtset(t5,"????");
t5->d2[0]=t4->d2[t4->l-4]; t5->d2[1]=t4->d2[t4->l-3]; t5->d2[2]=t4->d2[t4->l-2]; t5->d2[3]=t4->d2[t4->l-1];
f=t5->df[0]; x4=f;
x3=0; //prev UNENCRYPTED value
for(i3=2;i3<(t4->l-4);i3++){ //skip version (first 2 bytes)
x2=t4->d2[i3]; //=encrypted value
x2-=encryptcode[x4]; x4=(x4+1)&65535;
if (x2<0) x2+=256; //"loop" byte value
x2-=x3; //- prev UNENCRYPTED value
if (x2<0) x2+=256; //"loop" byte value
x2=x2^0xFF; //bitwise NOT
x3=x2; //set prev UNENCRYPTED value
t4->d2[i3]=x2; //set UNENCRYPTED value
}//i3
}
void addu6monsterdropitems(object *crtobj){
static creature *crt;
static unsigned char wep[32];
static unsigned char arm[32];
static unsigned short inv[32]; //changed to short, to allow objects after 255 being added to drops
static unsigned char chest[32];
static long x1,x2,x3,x5,x8,y8,x7,y7;
static unsigned short type;
static unsigned char wepn,armn,invn,chestn;
type=crtobj->type&1023;
crt=(creature*)crtobj->more;
wepn=0; armn=0; invn=0;
if (type==384){//BEGGAR/PEASANT
wep[wepn++]=38;
wep[wepn++]=34;
arm[armn++]=185;
inv[invn++]=OBJ_TMAP;
}
if (type==428){//COW
inv[invn++]=129;
}
if (type==424){//CYCLOPS
wep[wepn++]=44;
wep[wepn++]=33;
wep[wepn++]=34;
arm[armn++]=20;
arm[armn++]=3;
arm[armn++]=13;
inv[invn++]=98;
inv[invn++]=OBJ_TMAP;
}
if (type==350){//DEER
inv[invn++]=129;
}
if (type==411){//DRAGON
inv[invn++]=98;
inv[invn++]=88;
inv[invn++]=88;
inv[invn++]=OBJ_TMAP;
}
if (type==369){//DRAKE
inv[invn++]=88;
inv[invn++]=88;
}
if (type==387){//WOMAN/DRESS-WEARER
wep[wepn++]=38;
arm[armn++]=185;
inv[invn++]=88;
inv[invn++]=OBJ_TMAP;
}
if (type==385){//FARMER
wep[wepn++]=105;
wep[wepn++]=101;
arm[armn++]=17;
inv[invn++]=88;
inv[invn++]=OBJ_TMAP;
}
if (type==376){//FIGHTER
wep[wepn++]=43;
wep[wepn++]=42;
arm[armn++]=21;
arm[armn++]=10;
arm[armn++]=3;
arm[armn++]=22;
inv[invn++]=88;
inv[invn++]=88;
inv[invn++]=OBJ_TMAP;
}
if (type==362){//WINGED gargoyle
wep[wepn++]=46;
wep[wepn++]=41;
wep[wepn++]=47;
wep[wepn++]=49;
arm[armn++]=18;
arm[armn++]=1;
inv[invn++]=58;
inv[invn++]=129;
inv[invn++]=57;
inv[invn++]=OBJ_TMAP;
}
if (type==363){//gargoyle (WINGLESS)
wep[wepn++]=34;
wep[wepn++]=49;
wep[wepn++]=37;
arm[armn++]=1;
arm[armn++]=9;
arm[armn++]=18;
arm[armn++]=20;
}
if (type==382){//GUARD
wep[wepn++]=42;
wep[wepn++]=47;
arm[armn++]=22;
arm[armn++]=10;
arm[armn++]=3;
inv[invn++]=88;
inv[invn++]=OBJ_TMAP;
}
if (type==370){//HEADLESS
wep[wepn++]=34;
wep[wepn++]=36;
arm[armn++]=17;
arm[armn++]=9;
inv[invn++]=88;
inv[invn++]=OBJ_TMAP;
}
if (type==383){//JESTER
wep[wepn++]=38;
wep[wepn++]=33;
arm[armn++]=17;
inv[invn++]=88;
inv[invn++]=OBJ_TMAP;
}
if (type==386){//LUTE-PLAYER/MUSCIAN
wep[wepn++]=41;
wep[wepn++]=33;
wep[wepn++]=43;
arm[armn++]=19;
arm[armn++]=1;
arm[armn++]=9;
inv[invn++]=88;
inv[invn++]=158;
inv[invn++]=OBJ_TMAP;
}
if (type==378){//MAGE
wep[wepn++]=38;
arm[armn++]=17;
arm[armn++]=1;
inv[invn++]=58;
inv[invn++]=98;
inv[invn++]=88;
inv[invn++]=OBJ_TMAP;
}
if (type==380){//MERCHANT
wep[wepn++]=38;
wep[wepn++]=43;
arm[armn++]=17;
arm[armn++]=1;
inv[invn++]=88;
inv[invn++]=OBJ_TMAP;
}
if (type==372){//MONGBAT
wep[wepn++]=38;
wep[wepn++]=43;
wep[wepn++]=36;
wep[wepn++]=37;
arm[armn++]=9;
arm[armn++]=17;
arm[armn++]=18;
inv[invn++]=98;
inv[invn++]=88;
inv[invn++]=OBJ_TMAP;
}
if (type==347){//REAPER
inv[invn++]=58;
inv[invn++]=88;
}
if (type==348){//SHEEP
inv[invn++]=129;
}
if (type==368){//SKELETON
wep[wepn++]=43;
wep[wepn++]=36;
wep[wepn++]=37;
wep[wepn++]=41;
wep[wepn++]=38;
arm[armn++]=9;
arm[armn++]=1;
inv[invn++]=88;
inv[invn++]=OBJ_TMAP;
}
if (type==361){//SPIDER
inv[invn++]=129;
}
if (type==377){//SWASHBUCKLER
wep[wepn++]=43;
wep[wepn++]=41;
wep[wepn++]=33;
arm[armn++]=19;
arm[armn++]=1;
arm[armn++]=9;
inv[invn++]=88;
inv[invn++]=88;
inv[invn++]=OBJ_TMAP;
}
if (type==371){//TROLL
wep[wepn++]=37;
wep[wepn++]=36;
wep[wepn++]=43;
wep[wepn++]=41;
wep[wepn++]=34;
arm[armn++]=9;
arm[armn++]=18;
arm[armn++]=1;
arm[armn++]=19;
inv[invn++]=98;
inv[invn++]=88;
inv[invn++]=88;
inv[invn++]=OBJ_TMAP;
}
if (type==379){//VILLAGER
wep[wepn++]=43;
wep[wepn++]=33;
arm[armn++]=17;
inv[invn++]=88;
inv[invn++]=OBJ_TMAP;
}
/*
if (type==){//
wep[wepn++]=0;
arm[armn++]=0;
inv[invn++]=0;
}
[GREMLIN] (CHECK THIS DATA BEFORE IMPLEMENTING)
198264 0 nothing
198265 0 nothing
198266 128 bread
198267 99 backpack
198268 0 nothing
198269 0 nothing
198270 0 nothing
198271 0 nothing
198272 0 nothing
[MIMIC BOX] (NOT IMPLEMENTED)
198349 0 nothing
198350 0 nothing
198351 98 chest
198352 98 chest
198353 0 nothing
198354 0 nothing
*/
static object *invbag,*invobj;
static long i,i2,n,item,chance,chanceresult,addmany,chest_restorei2;
invbag=crt->items;
for (i=0;i<=2;i++){
i2=0;
if (i==0) n=wepn; if (i==1) n=armn; if (i==2) n=invn;
whilei2:
while (i2<n){
if (i==0) item=wep[i2]; if (i==1) item=arm[i2]; if (i==2) item=inv[i2]; if (i==3) item=chest[i2];
//add given item to crt's inventory
chance=4;
if (i2>=1) chance=8;
if (i2>=3) chance=16;
if (i==3){
chance=2;
if (i2>=1) chance=2;
if (i2>=3) chance=16;
}
if (item==58) chance=16;//spell (actually a random potion) RARE
if (item==OBJ_TMAP) chance=128;//for debugging set to 2, will set to 128 or something like that later.
chanceresult=rnd*chance;
if (chanceresult==0){
addmany=0;
if (item==36){chanceresult=rnd*5; chanceresult++; addmany=chanceresult;}//spear
if (item==37){chanceresult=rnd*5; chanceresult++; addmany=chanceresult;}//axe
if (item==38){chanceresult=rnd*5; chanceresult++; addmany=chanceresult;}//dagger
addmany_loop:
invobj=OBJnew(); invobj->type=item;
if (item==88){
//gold modification
//2/3 times it's in the first half of the range
//1/3 times it's in the top half of the range
chanceresult=rnd*3;
//gold(quantity 10-60)
invobj->more2=unsigned long(rnd*26)+10;
if (chanceresult==2) invobj->more2=unsigned long(rnd*26)+35;
//CHEST ITEM: gold (quantity 10-100)
if (i==3){
invobj->more2=unsigned long(rnd*46)+10;
if (chanceresult==2) invobj->more2=unsigned long(rnd*46)+55;
}
//old code
//invobj->more2=unsigned long(rnd*51)+10;//gold (quantity 10-60)
//if (i==3) invobj->more2=unsigned long(rnd*91)+10;//CHEST ITEM: gold (quantity 10-100)
}//item==88
if (item==58){//spell (actually a random potion)
chanceresult=rnd*8;
invobj->type=275+chanceresult*1024;
}
if (item==90){//torch(es)
//CHEST ITEM
invobj->more2=unsigned long(rnd*3)+1;
}
if (item==77){//gem
//CHEST ITEM
invobj->more2=1;
}
if (item==OBJ_TMAP) {
x7=randomchestlocation(true);
y7=x7>>10;
x7=x7&1023;
x1=rnd*7;
x2=rnd*7;
while (((bt[y7+(x2-3)*2][x7+(x1-3)*2]&1023)>=8)&&((bt[y7+(x2-3)*2][x7+(x1-3)*2]&1023)<16)) { //check that the map marker is not in the sea.
x1=rnd*7;
x2=rnd*7;
}
x3=rnd*7;
x5=rnd*7;
invobj->more2=x7+1024*y7+1024*1024*x1+1024*1024*8*x2+1024*1024*8*8*x3+1024*1024*8*8*8*x5;
}
if (item==129){
if ((i==3)||(type==362)){//CHEST ITEM/WINGED GARG SPECIAL ITEM: random food drop
chanceresult=rnd*3;
if (chanceresult!=2){
if (chanceresult==1) invobj->type=128;//bread
invobj->more2=unsigned long(rnd*5)+1;
}else{
invobj->type=133;//ham
invobj->more2=0;
}
}else{
invobj->more2=unsigned long(rnd*3)+1;
}
}
//WHAT ABOUT THE CURRENT OBJEST IN THE BAGS PREV POINTER!!!!
invobj->info|=112; invobj->next=invbag->more; invobj->prev=invbag; invbag->more=invobj; if (invobj->next) ((object*)invobj->next)->prev=invobj;
// t222
ENHANCEnewn(invobj,8,4);
if (item==42){//xbow
chanceresult=rnd*25; chanceresult++;
invobj=OBJnew(); invobj->type=56; invobj->more2=chanceresult;
invobj->info|=112; invobj->next=invbag->more; invobj->prev=invbag; invbag->more=invobj; if (invobj->next) ((object*)invobj->next)->prev=invobj;
}
if (item==41){//bow
chanceresult=rnd*25; chanceresult++;
invobj=OBJnew(); invobj->type=55; invobj->more2=chanceresult;
invobj->info|=112; invobj->next=invbag->more; invobj->prev=invbag; invbag->more=invobj; if (invobj->next) ((object*)invobj->next)->prev=invobj;
}
//chest?
if (item==98){
invobj->type=98+1024;
//IDEA: add lock or magical lock ;) depending on INT of crt
chestn=0;
chest[chestn++]=88;//gold
chest[chestn++]=90;//torch
chest[chestn++]=129;//meat
chest[chestn++]=77;//gem
chest_restorei2=i2+1;
n=chestn;
i=3;
i2=0;
invbag=invobj;
goto whilei2;
}
if (addmany){addmany--; goto addmany_loop;}
}//chanceresult==0
i2++;
}
if (i==3){
invbag=crt->items;
n=invn;
i=2;
i2=chest_restorei2;
goto whilei2;
}
}//i
return;
}
/*
MOVERNEW: FLAGS
flags can be set globally, in which case they need to be reset when finished
or locally by being passed as flags
local flags and OR-ed with global flags
*/
//unsigned long MOVERNEW_GLOBALFLAGS=0;
//unsigned char MOVERNEW_ERROR;//valid when movernew(...) returns 0
//1=FAILED (object is not a mover)
//2=IGNORED (object is not the primary part of a mover)
unsigned char movernew(unsigned short type,unsigned short x,unsigned short y,unsigned long flags){
static long x2,y2,x3,y3,z,z2,z3;
static object *myobj3,*myobj;
static creature *crt,*crt2;
static unsigned char allegiance;
static object *invbag;
static object *invobj;
flags|=MOVERNEW_GLOBALFLAGS;
MOVERNEW_ERROR=0;
if (!tclass_mover[type]){
MOVERNEW_ERROR=1;
if (obji[sprlnk[type&1023]].v4){
MOVERNEW_ERROR=2;
}
}