-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtrade.c
More file actions
1116 lines (1059 loc) · 30.3 KB
/
trade.c
File metadata and controls
1116 lines (1059 loc) · 30.3 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
/*conquer : Copyright (c) 1988 by Ed Barlow.
* I spent a long time writing this code & I hope that you respect this.
* I give permission to alter the code, but not to copy or redistribute
* it without my explicit permission. If you alter the code,
* please document changes and send me a copy, so all can have it.
* This code, to the best of my knowledge works well, but it is my first
* 'C' program and should be treated as such. I disclaim any
* responsibility for the codes actions (use at your own risk). I guess
* I am saying "Happy gaming", and am trying not to get sued in the process.
* Ed
*/
/*
* The following file "trade.c" was written by Adam Bryant who
* gives all rights to this code to Ed Barlow provided that this
* message remains intact.
*/
/* thanks adam -- Ed */
/* trade.c */
/*include files*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curses.h>
#include "header.h"
#include "data.h"
#ifdef TRADE
/* possible commodities */
#define TDGOLD 0
#define TDFOOD 1
#define TDMETAL 2
#define TDJEWL 3
#define TDLAND 4
#define TDARMY 5
#define TDSHIP 6
/* constants and indicators */
#define NUMPRODUCTS 7
#define MAXITM 30
#define SELL 0
#define BUY 1
#define NODEAL 2
#define NOSALE 3
#define TRADECOST(cost) (100-cost)/100 /* twenty percent cost is normal*/
extern short country;
char *commodities[NUMPRODUCTS] = { "Gold", "Food", "Metal", "Jewels",
"Land", "Soldiers", "Ships"};
#ifdef ADMIN
char *tradefail[NUMPRODUCTS] = { "lack of gold", "lack of food",
"lack of metal", "lack of jewels", "land not owned",
"unavailable or destroyed armies",
"unavailable or destoryed navies"};
#endif /* ADMIN */
#ifdef CONQUER
/* Use this when you wish to bid something */
char *buylist[NUMPRODUCTS] = { "Bid how much gold? ", "Bid how much food? ",
"Bid how much metal? ", "Bid how many jewels? ", "What X location? ",
"Bid what army? ", "Bid what navy? "};
void
trade()
{
FILE *tfile;
int count, done=FALSE, notopen=FALSE;
int buysell, holdint, holdint2, extint, inloop;
int type1[MAXITM], type2[MAXITM], deal[MAXITM], extra[MAXITM];
int natn[MAXITM], itemnum, getland(), gettrade(), checkland();
int tradable();
long lvar1[MAXITM], lvar2[MAXITM], holdlong, holdlong2, armyvalue();
void tradeerr(), setaside(), takeback();
clear();
while (done==FALSE) {
itemnum=0;
done=TRUE;
/* open trading file */
if ((tfile=fopen(tradefile,"r")) == NULL ) {
notopen=TRUE;
}
/* read in all of the data */
while (notopen==FALSE && !feof(tfile)) {
holdint = fscanf(tfile,"%d %d %d %d %ld %ld %d\n",
&deal[itemnum],&natn[itemnum],&type1[itemnum],
&type2[itemnum],&lvar1[itemnum],
&lvar2[itemnum],&extra[itemnum]);
if (holdint == 7) {
if (deal[itemnum]==NOSALE) {
/* remove item from sales list */
deal[type1[itemnum]]=NOSALE;
} else if (deal[itemnum]==SELL) {
itemnum++;
}
}
}
if (notopen==FALSE) fclose(tfile);
move(0,0);
clrtobot();
/* display header */
standout();
mvaddstr(0,27,"COMMODITIES EXCHANGE");
mvaddstr(1,20,"No Guarantees... All Trades Final");
standend();
count=3;
mvaddstr(count,0," Nation Item Price");
/* give some minor items for purchase */
mvprintw(++count,0,"%2d) %-20s",GETFOOD,"Merchants Guild");
mvprintw(count,30,"%9ld %s",GODFOOD,commodities[TDFOOD]);
mvprintw(count,50,"%9ld %s",GODPRICE,commodities[TDGOLD]);
mvprintw(++count,0,"%2d) %-20s",GETMETAL,"Merchants Guild");
mvprintw(count,30,"%9ld %s",GODMETAL,commodities[TDMETAL]);
mvprintw(count,50,"%9ld %s",GODPRICE,commodities[TDGOLD]);
mvprintw(++count,0,"%2d) %-20s",GETJEWL,"Merchants Guild");
mvprintw(count,30,"%9ld %s",GODJEWL,commodities[TDJEWL]);
mvprintw(count,50,"%9ld %s",GODPRICE,commodities[TDGOLD]);
/* go through list of commodities */
for (holdint=0;holdint<itemnum;holdint++) {
if ((deal[holdint]==SELL) && ((country==0) ||
(ntn[natn[holdint]].dstatus[country] != UNMET)
|| (natn[holdint] == country))) {
count++;
mvprintw(count,0,"%2d) %-20s",
holdint+1,
ntn[natn[holdint]].name);
if (type1[holdint]==TDLAND) {
holdlong = (long) tofood( &sct[(int)lvar1[holdint]][extra[holdint]],0);
mvprintw(count,30,"(food=%2ld) %s",
holdlong,
commodities[type1[holdint]]);
} else {
holdlong = lvar1[holdint];
mvprintw(count,30,"%9ld %s",
holdlong,
commodities[type1[holdint]]);
}
if (type2[holdint]==TDLAND) {
mvprintw(count,50,"(food=%2ld) %s",
lvar2[holdint],
commodities[type2[holdint]]);
} else {
mvprintw(count,50,"%9ld %s",
lvar2[holdint],
commodities[type2[holdint]]);
}
if (count>LINES-8) {
standout();
mvaddstr(LINES-2,30,"Hit Any Key to Continue");
standend();
refresh();
getch();
clear();
standout();
mvaddstr(0,27,"COMMODITIES EXCHANGE");
standend();
mvaddstr(2,0," Nation Item Minimum Price");
count=2;
}
}
}
standout();
count++;
count++;
mvaddstr(count++,0,"Do you wish to (B)uy, (S)ell, or (U)nsell?");
standend();
refresh();
inloop=TRUE;
while (inloop==TRUE) switch(getch()) {
case 'b':
case 'B':
buysell=BUY;
mvaddstr(count++,0,"What item number do you want to purchase? ");
refresh();
holdint = get_number();
if (holdint<0) break;
/* check for minor sales */
if (holdint==GETFOOD || holdint==GETMETAL || holdint==GETJEWL) {
/* strange flow but less control needed */
mvaddstr(count++,0,"Spend how much gold? ");
refresh();
holdlong = get_number();
curntn->tgold-=holdlong;
if (curntn->tgold<0L) {
curntn->tgold+=holdlong;
tradeerr("You do not have enough gold");
}
else switch (holdint) {
case GETFOOD:
curntn->tfood+= (long)(GODFOOD * ((double)holdlong / GODPRICE));
if ( (tfile = fopen(tradefile,"a+"))==NULL) {
tradeerr("Error opening file for trading");
abrt();
}
fprintf(tfile, "%d %d %d %d %ld %ld %d\n",BUY, country, GETFOOD, 0, curntn->tfood, 0, 0);
fclose(tfile);
break;
case GETMETAL:
curntn->metals+=(long)(GODMETAL * ((double)holdlong / GODPRICE));
break;
case GETJEWL:
curntn->jewels+=(long)(GODMETAL * ((double)holdlong / GODPRICE));
break;
}
inloop=FALSE;
done=FALSE;
break;
} else if (holdint<1 || holdint>itemnum
|| ntn[natn[holdint - 1]].dstatus[country]==UNMET) {
tradeerr("Invalid Item Number");
return;
}
holdint--;
if (deal[holdint]!=SELL) {
tradeerr("Sorry, that item is not on the market.");
return;
}
if (ntn[natn[holdint]].dstatus[country]==UNMET) {
tradeerr("That nation has not been met by you");
return;
}
if (ntn[natn[holdint]].dstatus[country]>HOSTILE) {
tradeerr("That nation is not doing business with you");
return;
}
/* obtain bid */
mvprintw(count++,0,"%s",buylist[type2[holdint]]);
refresh();
holdlong2 = 0L;
holdlong = (long) get_number();
if (holdlong< 0L) break;
/* check for valid bid */
switch(type2[holdint]) {
case TDGOLD:
if (holdlong < lvar2[holdint]) {
tradeerr("You underbid the minimum.");
buysell=NODEAL;
} else if (holdlong > curntn->tgold) {
tradeerr("Not Enough Gold");
buysell=NODEAL;
}
break;
case TDFOOD:
if (holdlong < lvar2[holdint]) {
tradeerr("You underbid the minimum.");
buysell=NODEAL;
} else if (holdlong > curntn->tfood) {
tradeerr("Not Enough Food");
buysell=NODEAL;
}
break;
case TDMETAL:
if (holdlong < lvar2[holdint]) {
tradeerr("You underbid the minimum.");
buysell=NODEAL;
} else if (holdlong > curntn->metals) {
tradeerr("Not Enough Iron");
buysell=NODEAL;
}
break;
case TDJEWL:
if (holdlong < lvar2[holdint]) {
tradeerr("You underbid the minimum.");
buysell=NODEAL;
} else if (holdlong > curntn->jewels) {
tradeerr("Not Enough Jewels");
buysell=NODEAL;
}
break;
case TDLAND:
mvaddstr(count++,0,"What Y position? ");
refresh();
holdlong2 = (long) get_number();
if (holdlong2 < 0L) break;
if (checkland(BUY,(int)(holdlong),(int)(holdlong2))==NODEAL) {
buysell=NODEAL;
} else if (tofood( &sct[(int)holdlong][(int)holdlong2],natn[holdint]) < lvar2[holdint]) {
tradeerr("You underbid the minimum");
buysell=NODEAL;
}
break;
case TDARMY:
if ((int)holdlong > MAXARM) {
tradeerr("Invalid Unit");
buysell=NODEAL;
} else if (tradable(country,(int)holdlong)==FALSE) {
tradeerr("That unit type is non-tradable.");
buysell=NODEAL;
} else if (armyvalue(country,(int)holdlong) < lvar2[holdint]) {
tradeerr("You underbid the minimum.");
buysell=NODEAL;
}
holdlong2=holdlong;
break;
case TDSHIP:
if ((int)holdlong >= MAXNAVY) {
tradeerr("Invalid Navy");
buysell=NODEAL;
} else if (flthold((int)holdlong) < (int)lvar2[holdint]) {
tradeerr("You underbid the minimum.");
buysell=NODEAL;
} else if((curntn->nvy[(int)holdlong].armynum!=MAXARM)||(curntn->nvy[(int)holdlong].people!=0)) {
tradeerr("Navy must be unloaded first.");
buysell=NODEAL;
}
holdlong2=holdlong;
break;
default:
tradeerr("Invalid Commodity");
buysell=NODEAL;
break;
}
if (buysell==BUY) {
if ( (tfile = fopen(tradefile,"a+"))==NULL) {
tradeerr("Error opening file for trading");
abrt();
}
setaside(country,type2[holdint],holdlong,(int)holdlong,FALSE);
fprintf(tfile, "%d %d %d %d %ld %ld %d\n",BUY, country, holdint, 0, holdlong, holdlong2, 0);
fclose(tfile);
}
return;
case 's':
case 'S':
/* sell an item */
/* only allow MAXITM on market */
if (itemnum>=MAXITM) {
standout();
if (itemnum==0) mvaddstr(count++,0,"Market Congested. Hit any key to continue");
else mvaddstr(count++,0,"Market Congested. (B)uy or any key to continue");
standend();
refresh();
break;
}
buysell=SELL;
holdint = gettrade("Selling",&count);
if (holdint==(-1)) {
tradeerr("Invalid Option");
return;
}
mvprintw(count++,0,"%s",buylist[holdint]);
refresh();
/* find out how much commodities */
holdlong = (long) get_number();
if (holdlong < 0) return;
extint = 0;
if (holdint< TDLAND && holdlong==0L)
return;
/* check for valid items */
switch(holdint) {
case TDGOLD:
if (holdlong > curntn->tgold) {
tradeerr("Not Enough Gold");
buysell=NODEAL;
}
break;
case TDFOOD:
if (holdlong > curntn->tfood) {
tradeerr("Not Enough Food");
buysell=NODEAL;
}
break;
case TDMETAL:
if (holdlong > curntn->metals) {
tradeerr("Not Enough Iron");
buysell=NODEAL;
}
break;
case TDJEWL:
if (holdlong > curntn->jewels) {
tradeerr("Not Enough Jewels");
buysell=NODEAL;
}
break;
case TDLAND:
mvaddstr(count++,0,"What Y position? ");
refresh();
extint = get_number();
if (extint < 0) {
buysell=NODEAL;
break;
}
buysell = checkland(SELL,(int)holdlong,extint);
break;
case TDARMY:
if (holdlong>=MAXARM || curntn->arm[(int)holdlong].sold <= 0) {
tradeerr("Invalid Army");
buysell=NODEAL;
} else if (tradable(country,(int)holdlong)==FALSE) {
tradeerr("That unit is non-tradable.");
buysell=NODEAL;
}
break;
case TDSHIP:
if (holdlong>=MAXNAVY || flthold((int)holdlong) <= 0) {
tradeerr("Invalid Navy");
buysell=NODEAL;
} else if((curntn->nvy[(int)holdlong].armynum!=MAXARM)||(curntn->nvy[(int)holdlong].people!=0)) {
tradeerr("Navy must be unloaded first.");
buysell=NODEAL;
}
break;
default:
tradeerr("Invalid Commodity");
buysell=NODEAL;
break;
}
/* invalid commodity */
if (buysell==NODEAL) return;
/* find out what they want in trade */
holdint2 = gettrade("In Trade For",&count);
if (holdint2==(-1)) {
tradeerr("Invalid Option");
return;
}
if (holdint2==TDLAND) {
holdlong2 = (long) getland(&count);
if (holdlong2==(-1L)) {
tradeerr("Invalid Vegetation");
return;
}
} else {
/* find out for what value */
mvprintw(count++,0,"Minimum Amount of %s? "
,commodities[holdint2]);
refresh();
holdlong2 = (long) get_number();
if (holdlong2 <= 0L) return;
}
/* make sure what was bid is unusable */
setaside(country,holdint,holdlong,(int)holdlong,FALSE);
/* set up output properly */
if (holdint==TDARMY) {
extint = (int) holdlong;
holdlong = armyvalue(country,(int)holdlong);
}
else if (holdint==TDSHIP) {
extint = (int)holdlong;
holdlong = (long)flthold(extint);
}
/* send it out */
if ( (tfile = fopen(tradefile,"a+"))==NULL) {
tradeerr("Error opening file for trading");
abrt();
}
fprintf(tfile, "%d %d %d %d %ld %ld %d\n", SELL, country, holdint, holdint2, holdlong, holdlong2, extint);
fclose(tfile);
inloop=FALSE;
done=FALSE;
break;
case 'u':
case 'U':
/* unsell an item */
if (itemnum==0) break;
mvaddstr(count++,0,"What item number to remove? ");
refresh();
holdint = get_number();
if (holdint < 0) return;
if (holdint==0 || holdint>itemnum) {
tradeerr("Invalid Item Number");
return;
}
holdint--;
#ifdef OGOD
/* allow god to remove commodities */
if (country!=0 && country!=natn[holdint])
#else
if (country != natn[holdint])
#endif /* OGOD */
{
tradeerr("That is not your item");
return;
}
if (deal[holdint]!=SELL) {
tradeerr("That item is not up for sale");
return;
}
/* remove it from market */
if ( (tfile = fopen(tradefile,"a+"))==NULL) {
tradeerr("Error opening file for trading");
abrt();
}
fprintf(tfile, "%d %d %d %d %ld %ld %d\n", NOSALE, natn[holdint], holdint, 0, 0L, 0L, 0);
fclose(tfile);
takeback(natn[holdint],type1[holdint],lvar1[holdint],extra[holdint],FALSE);
/*redraw the commodities board so removal is seen*/
inloop=FALSE;
done=FALSE;
break;
default:
/* return on no choice */
return;
}
}
}
void
tradeerr(mesg)
char *mesg;
{
clear_bottom(0);
standout();
mvaddstr(21,0,mesg);
standend();
mvaddstr(22,0,"Hit any key to continue");
refresh();
getch();
}
int
checkland(tradestat,xspot,yspot)
int tradestat,xspot,yspot;
{
int newstat=tradestat;
if (!ONMAP(xspot,yspot)) {
tradeerr("That is off the map");
newstat=NODEAL;
}
else if (sct[xspot][yspot].owner != country) {
tradeerr("You don't own it");
newstat=NODEAL;
}
else if (curntn->capx==xspot && curntn->capy==yspot) {
tradeerr("That is your capitol");
newstat=NODEAL;
}
else if (sct[xspot][yspot].designation == DTOWN) {
tradeerr("Towns may not be sold");
newstat=NODEAL;
}
else if (sct[xspot][yspot].designation == DCITY) {
tradeerr("Cities may not be sold");
newstat=NODEAL;
}
return(newstat);
}
/* get minimum foodvalue for land */
int
getland(count)
int *count;
{
int temp;
int i,j;
char entered;
mvprintw((*count)++,0,"MINIMUM VEGETATION: %c, %c, %c, %c, %c, %c, %c, %c, %c, %c, %c or %c: ",
VOLCANO,DESERT,TUNDRA,BARREN,LT_VEG,
GOOD,WOOD,FOREST,JUNGLE,SWAMP,ICE,NONE);
refresh();
entered=getch();
if(entered!=VOLCANO &&entered!=JUNGLE
&&entered!=DESERT &&entered!=TUNDRA
&&entered!=BARREN &&entered!=LT_VEG
&&entered!=NONE &&entered!=GOOD
&&entered!=WOOD &&entered!=FOREST
&&entered!=SWAMP &&entered!=ICE) temp=(-1);
else {
for(i=0;i<MAPX;i++)
for(j=0;j<MAPY;j++) if(sct[i][j].vegetation == entered) {
temp = tofood( &sct[i][j],country );
i=MAPX+1;
j=MAPY+1;
}
}
mvprintw((*count)++,0," JUST ENTERED %c so food value is %d", entered,temp);
refresh();
return(temp);
}
int
gettrade(saletype,count)
char *saletype;
int *count;
{
int hold=(-1);
mvprintw((*count)++,0,"%s: (G)old, (F)ood, (I)ron, (J)ewels, (L)and, (A)rmy, (S)hips?",saletype);
refresh();
switch(getch()) {
case 'g':
case 'G':
hold=TDGOLD;
break;
case 'f':
case 'F':
hold=TDFOOD;
break;
case 'i':
case 'I':
hold=TDMETAL;
break;
case 'j':
case 'J':
hold=TDJEWL;
break;
case 'l':
case 'L':
hold=TDLAND;
break;
case 'a':
case 'A':
hold=TDARMY;
break;
case 's':
case 'S':
hold=TDSHIP;
break;
default:
break;
}
return(hold);
}
#endif /* CONQUER */
/* set aside things that are up for bid */
void
setaside(cntry,item,longval,extint,isup)
int cntry,item,isup,extint;
long longval;
{
switch(item)
{
case TDGOLD:
if (isup==FALSE) ntn[cntry].tgold -= longval;
break;
case TDFOOD:
ntn[cntry].tfood -= longval;
break;
case TDMETAL:
if (isup==FALSE) ntn[cntry].metals -= longval;
break;
case TDJEWL:
if (isup==FALSE) ntn[cntry].jewels -= longval;
break;
case TDLAND:
break;
case TDARMY:
ntn[cntry].arm[extint].smove = 0;
ntn[cntry].arm[extint].stat = TRADED;
break;
case TDSHIP:
/* use commodity to hold indicator */
ntn[cntry].nvy[extint].smove = 0;
ntn[cntry].nvy[extint].commodity = TRADED;
break;
}
}
/* regain things that are up for bid */
void
takeback(cntry,item,longval,extint,isup)
int cntry,item,isup,extint;
long longval;
{
if (cntry == -1) return;
switch(item)
{
case TDGOLD:
if (isup==FALSE) ntn[cntry].tgold += longval;
break;
case TDFOOD:
ntn[cntry].tfood += longval;
break;
case TDMETAL:
if (isup==FALSE) ntn[cntry].metals += longval;
break;
case TDJEWL:
if (isup==FALSE) ntn[cntry].jewels += longval;
break;
case TDLAND:
break;
case TDARMY:
ntn[cntry].arm[extint].stat = DEFEND;
break;
case TDSHIP:
/* use commodity to hold indicator */
ntn[cntry].nvy[extint].commodity = 0;
break;
}
}
#ifdef ADMIN
/* give things that were purchased from cntry1 to cntry2 */
long
tradeit(cntry1,cntry2,item,longval,extra)
int cntry1,cntry2,item,extra;
long longval;
{
int unitnum=(-1),unitcount=0;
/* error for -1 returned */
long returnval=(-1);
switch(item)
{
case TDGOLD:
returnval = longval;
ntn[cntry2].tgold += longval * TRADECOST(20);
break;
case TDFOOD:
returnval = longval;
ntn[cntry2].tfood += longval * TRADECOST(20);
break;
case TDMETAL:
ntn[cntry2].metals += longval * TRADECOST(20);
returnval = longval;
break;
case TDJEWL:
ntn[cntry2].jewels += longval * TRADECOST(20);
returnval = longval;
break;
case TDLAND:
if (sct[(int)longval][extra].owner==cntry1) {
sct[(int)longval][extra].owner = cntry2;
returnval = longval;
}
break;
case TDARMY:
/* find army number for cntry2 */
/* give army to cntry2 */
if(ntn[cntry1].arm[extra].sold <= 0) return -1;
while(unitnum==(-1)&&unitcount<MAXARM) {
if (ntn[cntry2].arm[unitcount].sold<=0) {
/* give army to cntry2 */
ntn[cntry2].arm[unitcount].sold = ntn[cntry1].arm[extra].sold;
ntn[cntry2].arm[unitcount].unittyp = ntn[cntry1].arm[extra].unittyp;
ntn[cntry2].arm[unitcount].xloc = ntn[cntry2].capx;
ntn[cntry2].arm[unitcount].yloc = ntn[cntry2].capy;
ntn[cntry2].arm[unitcount].stat = DEFEND;
ntn[cntry2].arm[unitcount].smove = 0;
/* remove army from cntry1 */
ntn[cntry1].arm[extra].sold = 0;
ntn[cntry1].arm[extra].smove = 0;
ntn[cntry1].arm[extra].stat = DEFEND;
unitnum=unitcount;
}
unitcount++;
}
returnval=(long)unitnum;
break;
case TDSHIP:
/* give navy to cntry1 */
if(ntn[cntry1].nvy[extra].merchant==0
&& ntn[cntry1].nvy[extra].warships==0
&& ntn[cntry1].nvy[extra].galleys==0) return -1;
while(unitnum==(-1)&&unitcount<MAXARM){
if ((int)ntn[cntry2].nvy[unitcount].merchant+ntn[cntry2].nvy[unitcount].warships+ntn[cntry2].nvy[unitcount].galleys == 0) {
/* give navy to cntry2 */
ntn[cntry2].nvy[unitcount].warships = ntn[cntry1].nvy[extra].warships;
ntn[cntry2].nvy[unitcount].merchant = ntn[cntry1].nvy[extra].merchant;
ntn[cntry2].nvy[unitcount].galleys = ntn[cntry1].nvy[extra].galleys;
ntn[cntry2].nvy[unitcount].crew = ntn[cntry1].nvy[extra].crew;
ntn[cntry2].nvy[unitcount].xloc = ntn[cntry1].nvy[extra].xloc;
ntn[cntry2].nvy[unitcount].yloc = ntn[cntry1].nvy[extra].yloc;
ntn[cntry2].nvy[unitcount].commodity = 0;
ntn[cntry2].nvy[unitcount].smove = 0;
/* remove navy from cntry1 */
ntn[cntry1].nvy[extra].smove = 0;
ntn[cntry1].nvy[extra].merchant = 0;
ntn[cntry1].nvy[extra].warships = 0;
ntn[cntry1].nvy[extra].galleys = 0;
ntn[cntry1].nvy[extra].crew = 0;
ntn[cntry1].nvy[extra].commodity = 0;
unitnum=unitcount;
}
unitcount++;
}
returnval=(long)unitnum;
break;
}
return(returnval);
}
long
gettval(cntry1,cntry2,type,longval,extint)
int cntry1, cntry2, extint;
long longval;
{
int returnval=(-1);
long armyvalue();
switch(type) {
case TDGOLD:
case TDFOOD:
case TDMETAL:
case TDJEWL:
returnval=longval;
break;
case TDLAND:
if (cntry2 == sct[(int)longval][extint].owner)
returnval=(long)tofood( &sct[(int)longval][extint],cntry1);
break;
case TDARMY:
if (armyvalue(cntry2,extint)>0)
returnval=armyvalue(cntry2,extint);
break;
case TDSHIP:
curntn = &ntn[cntry2];
if (flthold(extint)>0)
returnval = (long)flthold(extint);
break;
}
return(returnval);
}
/* this function sends detailed message to players */
/* upon completion of a trade */
void
trademail(cntry1,cntry2,item1,item2,lvar1,lvar2,lvar3,lvar4)
int cntry1,cntry2,item1,item2;
long lvar1,lvar2,lvar3,lvar4;
{
FILE *fp[2];
int count;
char cname[2][NAMELTH+1],filename[2][FILELTH];
sprintf(filename[0],"%s%d",msgfile,cntry1);
sprintf(filename[1],"%s%d",msgfile,cntry2);
strcpy(cname[0],ntn[cntry1].name);
strcpy(cname[1],ntn[cntry2].name);
if ((fp[0]=fopen(filename[0],"a+"))==NULL) {
printf("error opening <%s>\n",filename[0]);
abrt();
}
if ((fp[1]=fopen(filename[1],"a+"))==NULL) {
printf("error opening <%s>\n",filename[1]);
abrt();
}
for (count=0;count<2;count++) {
fprintf(fp[count],"Message to %s from Conquer Commerce Commision\n",cname[count]);
fprintf(fp[count],"Dated: %s of Year %d\n",PSEASON(TURN),YEAR(TURN));
fprintf(fp[count],"\n");
fprintf(fp[count]," Trade transaction between %s and %s completed.\n",cname[1],cname[0]);
if (item1<=TDJEWL)
fprintf(fp[count]," Nation %s receives %ld %s\n",cname[1],lvar1,commodities[item1]);
else if (item1==TDLAND)
fprintf(fp[count]," Nation %s receives sector %ld, %ld\n",cname[1],lvar1,lvar2);
else if (item1==TDARMY)
fprintf(fp[count]," Nation %s receives army #%ld\n",cname[1],lvar1);
else if (item1==TDSHIP)
fprintf(fp[count]," Nation %s receives navy #%ld\n",cname[1],lvar1);
if (item2<=TDJEWL)
fprintf(fp[count]," Nation %s receives %ld %s\n",cname[0],lvar3,commodities[item2]);
else if (item2==TDLAND)
fprintf(fp[count]," Nation %s receives sector %ld, %ld\n",cname[0],lvar3,lvar4);
else if (item2==TDARMY)
fprintf(fp[count]," Nation %s receives army #%ld\n",cname[0],lvar3);
else if (item2==TDSHIP)
fprintf(fp[count]," Nation %s receives navy #%ld\n",cname[0],lvar3);
fprintf(fp[count],"END\n");
fclose(fp[count]);
}
}
#endif /* ADMIN */
#ifdef CONQUER
/* routine to determine whether or not an army type is tradable */
int
tradable(cntry,armynum)
int cntry,armynum;
{
int oldcntry=country,returnval=FALSE;
country=cntry;
if ( (ASTAT!=TRADED) && (ASTAT!=ONBOARD) && (ATYPE==A_MERCENARY
|| ATYPE==A_SIEGE || ATYPE==A_CATAPULT
|| ATYPE==A_ELEPHANT || ATYPE>=MINMONSTER) ) returnval=TRUE;
country=oldcntry;
return(returnval);
}
#endif /* CONQUER */
/* routine to determine commercial value of army */
long armyvalue(cntry,unit)
int cntry,unit;
{
long returnval;
returnval = ntn[cntry].arm[unit].sold*100 +
ntn[cntry].arm[unit].sold * unitattack[ntn[cntry].arm[unit].unittyp%UTYPE];
if (ntn[cntry].arm[unit].unittyp >= MINMONSTER) returnval+=ntn[cntry].arm[unit].sold*10;
returnval/=100;
return(returnval);
}
void
checktrade()
{
FILE *tfile;
int count, itemnum=0, natn[MAXITM];
int type1[MAXITM], type2[MAXITM], deal[MAXITM], extra[MAXITM];
long lvar1[MAXITM], lvar2[MAXITM];
void takeback();
void setaside();
/* initialize purchase list */
for (count=0; count<MAXITM; count++) {
deal[count]=(-1);
}
/* open trading file */
if ((tfile = fopen(tradefile,"r")) == NULL) {
/* no commodities - no transactions */
return;
}
/* read in all of the transactions */
while(!feof(tfile)) {
if (7 != fscanf(tfile,"%d %d %d %d %ld %ld %d\n",&deal[itemnum],
&natn[itemnum],&type1[itemnum],&type2[itemnum],&lvar1[itemnum],&lvar2[itemnum],&extra[itemnum])) break;
if (deal[itemnum]==NOSALE) {
if (natn[itemnum]==country)
takeback(country,type1[type1[itemnum]],lvar1[type1[itemnum]],extra[type1[itemnum]],TRUE);
} else if (deal[itemnum]==SELL) {
if (natn[itemnum]==country)
setaside(country,type1[itemnum],lvar1[itemnum],extra[itemnum],TRUE);
itemnum++;
} else if (deal[itemnum]==BUY) {
if (natn[itemnum]==country) {
if (type1[itemnum]==GETFOOD) ntn[country].tfood=lvar1[itemnum];
else setaside(country,type2[type1[itemnum]],lvar1[itemnum],(int)lvar1[itemnum],TRUE);
}
}
}
fclose(tfile);
}
#ifdef ADMIN
void
uptrade()
{
FILE *tfile;
int count, itemnum=0, natn[MAXITM];
int type1[MAXITM], type2[MAXITM], deal[MAXITM], extra[MAXITM];
extern FILE *fnews;
void trademail();
int whobuy[MAXITM];
long tradeit(), buy1[MAXITM], buy2[MAXITM];
long price[MAXITM], gettval(), longval1, longval2;
long lvar1[MAXITM], lvar2[MAXITM];
void takeback();
/* initialize purchase list */
for (count=0; count<MAXITM; count++) {
deal[count]=(-1);
whobuy[count]=(-1);
price[count]=(-1);
}
/* open trading file */
if ((tfile = fopen(tradefile,"r")) == NULL) {
/* no commodities - no transactions */
return;
}
/* read in all of the transactions */
while(!feof(tfile)) {
if (7 != fscanf(tfile,"%d %d %d %d %ld %ld %d\n",&deal[itemnum],
&natn[itemnum],&type1[itemnum],&type2[itemnum],&lvar1[itemnum],&lvar2[itemnum],&extra[itemnum])) break;
if (deal[itemnum]==NOSALE) {
/* remove item from sales list */
deal[type1[itemnum]]=NOSALE;
} else if (deal[itemnum]==SELL) {
itemnum++;
} else if (deal[itemnum]==BUY) {
if (type1[itemnum]==GETFOOD) /* just ignore food */;
else if (deal[type1[itemnum]]==SELL &&
(price[type1[itemnum]] <
gettval(natn[type1[itemnum]],natn[itemnum],
type2[type1[itemnum]],lvar1[itemnum],(int)lvar2[itemnum]))) {
deal[type1[itemnum]]=BUY;
/* highest bid so far */
price[type1[itemnum]]=gettval(natn[type1[itemnum]],natn[itemnum],type2[type1[itemnum]],lvar1[itemnum],(int)lvar2[itemnum]);
/* return bid to loser */
takeback(whobuy[type1[itemnum]],
type2[type1[itemnum]],
buy1[type1[itemnum]],(int)buy2[type1[itemnum]],FALSE);
/* record details of trade */
buy1[type1[itemnum]]=lvar1[itemnum];
buy2[type1[itemnum]]=lvar2[itemnum];
whobuy[type1[itemnum]]=natn[itemnum];
} else {
/* return bid */
takeback(natn[itemnum],
type2[type1[itemnum]],
lvar1[itemnum],(int)lvar2[itemnum],FALSE);
}
}
}
fclose(tfile);