-
Notifications
You must be signed in to change notification settings - Fork 4
/
atlantis.c
5041 lines (4254 loc) · 133 KB
/
atlantis.c
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
/*
* Atlantis v1.0 13 September 1993
* Copyright 1993 by Russell Wallace
*
* This program may be freely used, modified and distributed. It may not be
* sold or used commercially without prior written permission from the author.
*/
#include "atlantis.h"
#include "faction.h"
#include "keywords.h"
#include "region.h"
#include "building.h"
#include "ship.h"
#include "unit.h"
#include "battle.h"
#include "settings.h"
#include "spells.h"
#include "skills.h"
#include "items.h"
#include "parser.h"
#include "report.h"
#include "combat.h"
#include "game.h"
#include "json.h"
#include "rtl.h"
#include "bool.h"
#include "rtl.h"
#include <storage.h>
#include <binarystore.h>
#include <textstore.h>
#include <stream.h>
#include <quicklist.h>
#include <filestream.h>
#include <mtrand.h>
#include <cJSON.h>
int ignore_password = 0;
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <assert.h>
#include <time.h>
#include <stddef.h>
#include <limits.h>
#define VER_NOHEADER 0 // no version header
#define VER_HEADER 1 // has a version header
#define VER_UNIT_NEXT 2 // next-pointer chained units
#define VER_SHIPLEFT_FIX 3 // before this version, ship->left was invalid
#define VER_STACKS 4 // storing the stack
#define VER_FEATURES 5 // store feature flags
#define VER_TERRAINS 6 // terrains as names, not ids
#define VER_SHIPTYPE 7 // store ship->type as string, not enum
#define VER_REGIONUID 8 // regions have a uid and we store the graph
#define VER_CURRENT VER_REGIONUID
#define HAS_STACKS 0x01
#define HAS_FACTION_MONEY 0x02
static void (*store_init)(struct storage *, stream*) = binstore_init;
static void (*store_done)(struct storage *) = binstore_done;
typedef struct order {
struct order *next;
struct unit *unit;
int qty;
} order;
char buf2[256];
const char *keywords[] = {
"accept",
"address",
"admit",
"ally",
"attack",
"behind",
"board",
"build",
"building",
"cast",
"clipper",
"combat",
"demolish",
"display",
"end",
"enter",
"entertain",
"faction",
"find",
"form",
"galleon",
"give",
"guard",
"leave",
"longboat",
"move",
"name",
"password",
"pay",
"produce",
"promote",
"quit",
"recruit",
"research",
"reshow",
"sail",
"ship",
"sink",
"stack",
"study",
"tax",
"teach",
"transfer",
"unit",
"unstack",
"work",
};
const char *regionnames[] = {
"Aberaeron",
"Aberdaron",
"Aberdovey",
"Abernethy",
"Abersoch",
"Abrantes",
"Adrano",
"AeBrey",
"Aghleam",
"Akbou",
"Aldan",
"Alfaro",
"Alghero",
"Almeria",
"Altnaharra",
"Ancroft",
"Anshun",
"Anstruther",
"Antor",
"Arbroath",
"Arcila",
"Ardfert",
"Ardvale",
"Arezzo",
"Ariano",
"Arlon",
"Avanos",
"Aveiro",
"Badalona",
"Baechahoela",
"Ballindine",
"Balta",
"Banlar",
"Barika",
"Bastak",
"Bayonne",
"Bejaia",
"Benlech",
"Beragh",
"Bergland",
"Berneray",
"Berriedale",
"Binhai",
"Birde",
"Bocholt",
"Bogmadie",
"Braga",
"Brechlin",
"Brodick",
"Burscough",
"Calpio",
"Canna",
"Capperwe",
"Caprera",
"Carahue",
"Carbost",
"Carnforth",
"Carrigaline",
"Caserta",
"Catrianchi",
"Clatter",
"Coilaco",
"Corinth",
"Corofin",
"Corran",
"Corwen",
"Crail",
"Cremona",
"Crieff",
"Cromarty",
"Cumbraes",
"Daingean",
"Darm",
"Decca",
"Derron",
"Derwent",
"Deveron",
"Dezhou",
"Doedbygd",
"Doramed",
"Dornoch",
"Drammes",
"Dremmer",
"Drense",
"Drimnin",
"Drumcollogher",
"Drummore",
"Dryck",
"Drymen",
"Dunbeath",
"Duncansby",
"Dunfanaghy",
"Dunkeld",
"Dunmanus",
"Dunster",
"Durness",
"Duucshire",
"Elgomaar",
"Ellesmere",
"Ellon",
"Enfar",
"Erisort",
"Eskerfan",
"Ettrick",
"Fanders",
"Farafra",
"Ferbane",
"Fetlar",
"Flock",
"Florina",
"Formby",
"Frainberg",
"Galloway",
"Ganzhou",
"Geal Charn",
"Gerr",
"Gifford",
"Girvan",
"Glenagallagh",
"Glenanane",
"Glin",
"Glomera",
"Glormandia",
"Gluggby",
"Gnackstein",
"Gnoelhaala",
"Golconda",
"Gourock",
"Graevbygd",
"Grandola",
"Gresberg",
"Gresir",
"Greverre",
"Griminish",
"Grisbygd",
"Groddland",
"Grue",
"Gurkacre",
"Haikou",
"Halkirk",
"Handan",
"Hasmerr",
"Helmsdale",
"Helmsley",
"Helsicke",
"Helvete",
"Hoersalsveg",
"Hullevala",
"Ickellund",
"Inber",
"Inverie",
"Jaca",
"Jahrom",
"Jeormel",
"Jervbygd",
"Jining",
"Jotel",
"Kaddervar",
"Karand",
"Karothea",
"Kashmar",
"Keswick",
"Kielder",
"Killorglin",
"Kinbrace",
"Kintore",
"Kirriemuir",
"Klen",
"Knesekt",
"Kobbe",
"Komarken",
"Kovel",
"Krod",
"Kursk",
"Lagos",
"Lamlash",
"Langholm",
"Larache",
"Larkanth",
"Larmet",
"Lautaro",
"Leighlin",
"Lervir",
"Leven",
"Licata",
"Limavady",
"Lingen",
"Lintan",
"Liscannor",
"Locarno",
"Lochalsh",
"Lochcarron",
"Lochinver",
"Lochmaben",
"Lom",
"Lorthalm",
"Louer",
"Lurkabo",
"Luthiir",
"Lybster",
"Lynton",
"Mallaig",
"Mataro",
"Melfi",
"Melvaig",
"Menter",
"Methven",
"Moffat",
"Monamolin",
"Monzon",
"Morella",
"Morgel",
"Mortenford",
"Mullaghcarn",
"Mulle",
"Murom",
"Nairn",
"Navenby",
"Nephin Beg",
"Niskby",
"Nolle",
"Nork",
"Olenek",
"Oloron",
"Oranmore",
"Ormgryte",
"Orrebygd",
"Palmi",
"Panyu",
"Partry",
"Pauer",
"Penhalolen",
"Perkel",
"Perski",
"Planken",
"Plattland",
"Pleagne",
"Pogelveir",
"Porthcawl",
"Portimao",
"Potenza",
"Praestbygd",
"Preetsome",
"Presu",
"Prettstern",
"Rantlu",
"Rappbygd",
"Rath Luire",
"Rethel",
"Riggenthorpe",
"Rochfort",
"Roddendor",
"Roin",
"Roptille",
"Roter",
"Rueve",
"Sagunto",
"Saklebille",
"Salen",
"Sandwick",
"Sarab",
"Sarkanvale",
"Scandamia",
"Scarinish",
"Scourie",
"Serov",
"Shanyin",
"Siegen",
"Sinan",
"Sines",
"Skim",
"Skokholm",
"Skomer",
"Skottskog",
"Sledmere",
"Sorisdale",
"Spakker",
"Stackforth",
"Staklesse",
"Stinchar",
"Stoer",
"Strichen",
"Stroma",
"Stugslett",
"Suide",
"Tabuk",
"Tarraspan",
"Tetuan",
"Thurso",
"Tiemcen",
"Tiksi",
"Tolsta",
"Toppola",
"Torridon",
"Trapani",
"Tromeforth",
"Tudela",
"Turia",
"Uxelberg",
"Vaila",
"Valga",
"Verguin",
"Vernlund",
"Victoria",
"Waimer",
"Wett",
"Xontormia",
"Yakleks",
"Yuci",
"Zaalsehuur",
"Zamora",
"Zapulla",
};
skill_t itemskill[] = {
SK_MINING,
SK_LUMBERJACK,
SK_QUARRYING,
SK_HORSE_TRAINING,
SK_WEAPONSMITH,
SK_WEAPONSMITH,
SK_WEAPONSMITH,
SK_ARMORER,
SK_ARMORER,
};
item_t rawmaterial[] = {
0,
0,
0,
0,
I_IRON,
I_WOOD,
I_WOOD,
I_IRON,
I_IRON,
};
int spelllevel[] = {
4,
2,
1,
1,
2,
3,
2,
2,
1,
5,
4,
3,
3,
3,
3,
4,
3,
4,
3,
2,
4,
3,
5,
3,
};
char iscombatspell[] = {
1,
1,
0,
1,
1,
1,
0,
1,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
1,
0,
};
const char *spelldata[] = {
"This spell creates a black whirlwind of energy which destroys all life, "
"leaving frozen corpses with faces twisted into expressions of horror. Cast "
"in battle, it kills from 2 to 1250 enemies.",
"This spell creates an aura of fear which causes enemy troops in battle to "
"panic. Each time it is cast, it demoralizes between 2 and 100 troops for "
"the duration of the battle. Demoralized troops are at a -1 to their "
"effective skill.",
"This ritual spell causes pestilence to contaminate the water supply of the "
"region in which it is cast. It causes from 2 to 50 peasants to die from "
"drinking the contaminated water. Any units which end the month in the "
"affected region will know about the deaths, however only units which have "
"Observation skill higher than the caster's Stealth skill will know who "
"was responsible. The spell costs $50 to cast.",
"This spell, cast in battle, creates a flash of light which dazzles from 2 "
"to 50 enemy troops. Dazzled troops are at a -1 to their effective skill "
"for their next attack.",
"This spell enables the caster to hurl balls of fire. Each time it is cast "
"in battle, it will incinerate between 2 and 50 enemy troops.",
"This spell disrupts the metabolism of living organisms, sending an "
"invisible wave of death across a battlefield. Each time it is cast, it "
"will kill between 2 and 250 enemy troops.",
"This spell enables the caster to attempt to heal the injured after a "
"battle. It is used automatically, and does not require use of either the "
"COMBAT or CAST command. If one's side wins a battle, a number of "
"casualties on one's side, between 2 and 50, will be healed. (If this "
"results in all the casualties on the winning side being healed, the winner "
"is still eligible for combat experience.)",
"This spell boosts the morale of one's troops in battle. Each time it is "
"cast, it cancels the effect of the Cause Fear spell on a number of one's "
"own troops ranging from 2 to 100.",
"This spell enables the caster to throw bolts of lightning to strike down "
"enemies in battle. It kills from 2 to 10 enemies.",
"This spell allows one to create an Amulet of Darkness. This amulet allows "
"its possessor to cast the Black Wind spell in combat, without having to "
"know the spell; the only requirement is that the user must have the Magic "
"skill at 1 or higher. The Black Wind spell creates a black whirlwind of "
"energy which destroys all life. Cast in battle, it kills from 2 to 1250 "
"people. The amulet costs $1000 to make.",
"This spell allows one to create an Amulet of Death. This amulet allows its "
"possessor to cast the Hand of Death spell in combat, without having to "
"know the spell; the only requirement is that the user must have the Magic "
"skill at 1 or higher. The Hand of Death spell disrupts the metabolism of "
"living organisms, sending an invisible wave of death across a battlefield. "
"Each time it is cast, it will kill between 2 and 250 enemy troops. The "
"amulet costs $800 to make.",
"This spell allows one to create an Amulet of Healing. This amulet allows "
"its possessor to attempt to heal the injured after a battle. It is used "
"automatically, and does not require the use of either the COMBAT or CAST "
"command; the only requirement is that the user must have the Magic skill "
"at 1 or higher. If the user's side wins a battle, a number of casualties "
"on that side, between 2 and 50, will be healed. (If this results in all "
"the casualties on the winning side being healed, the winner is still "
"eligible for combat experience.) The amulet costs $600 to make.",
"This spell allows one to create an Amulet of True Seeing. This allows its "
"possessor to see units which are hidden by Rings of Invisibility. (It has "
"no effect against units which are hidden by the Stealth skill.) The amulet "
"costs $600 to make.",
"This spell allows one to create a Cloak of Invulnerability. This cloak "
"protects its wearer from injury in battle; any attack with a normal weapon "
"which hits the wearer has a 99.99% chance of being deflected. This benefit "
"is gained instead of, rather than as well as, the protection of any armor "
"worn; and the cloak confers no protection against magical attacks. The "
"cloak costs $600 to make.",
"This spell allows one to create a Ring of Invisibility. This ring renders "
"its wearer invisible to all units not in the same faction, regardless of "
"Observation skill. For a unit of many people to remain invisible, it must "
"possess a Ring of Invisibility for each person. The ring costs $600 to "
"make.",
"This spell allows one to create a Ring of Power. This ring doubles the "
"effectiveness of any spell the wearer casts in combat, or any magic item "
"the wearer uses in combat. The ring costs $800 to make.",
"This spell allows one to create a Runesword. This is a black sword with "
"magical runes etched along the blade. To use it, one must have both the "
"Sword and Magic skills at 1 or higher. It confers a bonus of 2 to the "
"user's Sword skill in battle, and also projects an aura of power that has "
"a 50% chance of cancelling any Fear spells cast by an enemy magician. The "
"sword costs $600 to make.",
"This spell allows one to create a Shieldstone. This is a small black "
"stone, engraved with magical runes, that creates an invisible shield of "
"energy that deflects hostile magic in battle. The stone is used "
"automatically, and does not require the use of either the COMBAT or CAST "
"commands; the only requirement is that the user must have the Magic skill "
"at 1 or higher. Each round of combat, it adds one layer to the shielding "
"around one's own side. When a hostile magician casts a spell, provided "
"there is at least one layer of shielding present, there is a 50% chance of "
"the spell being deflected. If the spell is deflected, nothing happens. If "
"it is not, then it has full effect, and one layer of shielding is removed. "
"The stone costs $800 to make.",
"This spell allows one to create a Staff of Fire. This staff allows its "
"possessor to cast the Fireball spell in combat, without having to know the "
"spell; the only requirement is that the user must have the Magic skill at "
"1 or higher. The Fireball spell enables the caster to hurl balls of fire. "
"Each time it is cast in battle, it will incinerate between 2 and 50 enemy "
"troops. The staff costs $600 to make.",
"This spell allows one to create a Staff of Lightning. This staff allows "
"its possessor to cast the Lightning Bolt spell in combat, without having "
"to know the spell; the only requirement is that the user must have the "
"Magic skill at 1 or higher. The Lightning Bolt spell enables the caster to "
"throw bolts of lightning to strike down enemies. It kills from 2 to 10 "
"enemies. The staff costs $400 to make.",
"This spell allows one to create a Wand of Teleportation. This wand allows "
"its possessor to cast the Teleport spell, without having to know the "
"spell; the only requirement is that the user must have the Magic skill at "
"1 or higher. The Teleport spell allows the caster to move himself and "
"others across vast distances without traversing the intervening space. The "
"command to use it is CAST TELEPORT target-unit unit-no ... The target unit "
"is a unit in the region to which the teleport is to occur. If the target "
"unit is not in your faction, it must be in a faction which has issued an "
"ADMIT command for you that month. After the target unit comes a list of "
"one or more units to be teleported into the target unit's region (this may "
"optionally include the caster). Any units to be teleported, not in your "
"faction, must be in a faction which has issued an ACCEPT command for you "
"that month. The total weight of all units to be teleported (including "
"people, equipment and horses) must not exceed 10000. If the target unit is "
"in a building or on a ship, the teleported units will emerge there, "
"regardless of who owns the building or ship. The caster spends the month "
"preparing the spell and the teleport occurs at the end of the month, so "
"any other units to be transported can spend the month doing something "
"else. The wand costs $800 to make, and $50 to use.",
"This spell creates an invisible shield of energy that deflects hostile "
"magic. Each round that it is cast in battle, it adds one layer to the "
"shielding around one's own side. When a hostile magician casts a spell, "
"provided there is at least one layer of shielding present, there is a 50% "
"chance of the spell being deflected. If the spell is deflected, nothing "
"happens. If it is not, then it has full effect, and one layer of shielding "
"is removed.",
"This spell allows the caster to incinerate whole armies with fire brighter "
"than the sun. Each round it is cast, it kills from 2 to 6250 enemies.",
"This spell allows the caster to move himself and others across vast "
"distances without traversing the intervening space. The command to use it "
"is CAST TELEPORT target-unit unit-no ... The target unit is a unit in the "
"region to which the teleport is to occur. If the target unit is not in "
"your faction, it must be in a faction which has issued an ADMIT command "
"for you that month. After the target unit comes a list of one or more "
"units to be teleported into the target unit's region (this may optionally "
"include the caster). Any units to be teleported, not in your faction, must "
"be in a faction which has issued an ACCEPT command for you that month. The "
"total weight of all units to be teleported (including people, equipment "
"and horses) must not exceed 10000. If the target unit is in a building or "
"on a ship, the teleported units will emerge there, regardless of who owns "
"the building or ship. The caster spends the month preparing the spell and "
"the teleport occurs at the end of the month, so any other units to be "
"transported can spend the month doing something else. The spell costs $50 "
"to cast.",
};
typedef struct strlist {
struct strlist *next;
char s[1];
} strlist;
static void freestrlist(strlist * slist) {
while (slist) {
strlist * sl = slist;
slist = sl->next;
free(sl);
}
}
int atoip(const char *s)
{
int n;
n = atoi(s);
return (n < 0) ? 0 : n;
}
void nstrcpy(char *to, const char *from, size_t n)
{
n--;
do
if ((*to++ = *from++) == 0)
return;
while (--n);
*to = 0;
}
void rnd_seed(unsigned long x)
{
init_genrand(x);
}
static void removelist(quicklist **qlp, void *data) {
ql_iter qli;
for (qli=qli_init(qlp);qli_more(qli);) {
void *x = qli_get(qli);
if (x==data) {
qli_delete(&qli);
break;
}
qli_next(&qli);
}
}
strlist *makestrlist(char *s)
{
strlist *S;
S = (strlist *)malloc(sizeof(strlist) + strlen(s));
if (S) {
S->next = 0;
strcpy(S->s, s);
}
return S;
}
void addevent(faction * f, const char *s)
{
ql_push(&f->events, _strdup(s));
}
void addmessage(faction * f, const char *s)
{
ql_push(&f->messages, _strdup(s));
}
void addstrlist(strlist ** SP, char *s)
{
while (*SP) SP = &(*SP)->next;
*SP = makestrlist(s);
}
/* directions: N S E W M Y */
static struct { int x, y; } offset[MAXDIRECTIONS] = { { 0, -1 }, { 0, 1 }, { -1, 0 }, { 1, 0 }, { -1, -1 }, { 1, 1 } };
void transform(int *x, int *y, int direction)
{
assert(direction>=0 && direction<MAXDIRECTIONS);
assert(x || !"invalid reference to X coordinate");
assert(y || !"invalid reference to Y coordinate");
*x += offset[direction].x;
*y += offset[direction].y;
if (config.width && config.height) {
if (*x<0) *x += config.width;
if (*y<0) *y += config.height;
if (*x >= config.width) *x -= config.width;
if (*y >= config.height) *y -= config.height;
}
}
int effskill(const unit * u, int i)
{
int n, j, result;
n = 0;
if (u->number) {
n = u->skills[i] / u->number;
}
j = 30;
result = 0;
while (j <= n) {
n -= j;
j += 30;
result++;
}
return result;
}
int cansee(const faction * f, region * r, const unit * ux)
{
int n, o;
int cansee;
unit *u;
if (ux->faction == f) {
return 2;
}
cansee = 0;
if (ux->guard || ux->building || ux->ship)
cansee = 1;
n = effskill(ux, SK_STEALTH);
for (u = r->units;u;u=u->next) {
if (u->faction != f)
continue;
if (ux->items[I_RING_OF_INVISIBILITY] &&
ux->items[I_RING_OF_INVISIBILITY] == ux->number &&
!u->items[I_AMULET_OF_TRUE_SEEING])
continue;
o = effskill(u, SK_OBSERVATION);
if (o > n)
return 2;
if (o >= n)
cansee = 1;
}
return cansee;
}
faction *findfaction(int n)
{
ql_iter fli;
for (fli = qli_init(&factions); qli_more(fli);) {
faction *f = (faction *)qli_next(&fli);
if (f->no == n) return f;
}
return 0;
}
faction *getfaction(void)
{
return findfaction(atoi(igetstr(0)));
}
building *findbuilding(int n)
{
ql_iter rli;
for (rli = qli_init(®ions); qli_more(rli);) {
region *r = (region *)qli_next(&rli);
ql_iter qli;
for (qli=qli_init(&r->buildings);qli_more(qli);) {
building *b = (building *)qli_next(&qli);
if (b->no == n)
return b;
}
}
return 0;
}
building *getbuilding(region * r)
{
int n;
ql_iter qli;
n = atoi(igetstr(0));
for (qli=qli_init(&r->buildings);qli_more(qli);) {
building *b = (building *)qli_next(&qli);
if (b->no == n) {
return b;
}
}
return 0;
}
ship *findship(int n)
{
ql_iter rli;
for (rli = qli_init(®ions); qli_more(rli);) {
region *r = (region *)qli_next(&rli);
ql_iter sli;
for (sli = qli_init(&r->ships); qli_more(sli);) {
ship *sh = (ship *)qli_next(&sli);
if (sh->no == n)
return sh;
}
}
return 0;
}
ship *getship(region * r)
{
int n;
ql_iter sli;
n = atoi(igetstr(0));
for (sli = qli_init(&r->ships); qli_more(sli);) {
ship *sh = (ship *)qli_next(&sli);
if (sh->no == n) {
return sh;
}
}
return 0;
}
unit *findunitg(int n)
{
ql_iter rli;
for (rli = qli_init(®ions); qli_more(rli);) {
region *r = (region *)qli_next(&rli);
unit *u;
for (u = r->units;u;u=u->next) {
if (u->no == n) {
return u;
}
}
}
return 0;
}
faction * addplayer(region * r, const char * email, int no)
{
faction * f;
unit * u;
int i;
char msg[1024];
char name[NAMESIZE];
if (no==0) ++no;
while (findfaction(no)) ++no;
f = create_faction(no);
f->lastorders = turn;
sprintf(name, "Faction %d", f->no);
faction_setname(f, name);
faction_setaddr(f, email);
for (i=0;i!=4;++i) {
sprintf(buf2+i*4, "%4x", (unsigned int)genrand_int32());
}
buf2[16] = 0;
faction_setpassword(f, buf2);
sprintf(msg, "Your password is '%s'.", buf2);
addmessage(f, msg);
while (findunitg(nextunitid)) ++nextunitid;
u = create_unit(f, nextunitid++);
region_addunit(r, u, 0);
strcpy(u->lastorder, "work");
u->combatspell = -1;
u->number = config.startmen ? config.startmen : 1;
u->money = config.startmoney ? config.startmoney : STARTMONEY;
u->isnew = true;
f->origin_x = r->x;
f->origin_y = r->y;
return f;
}
void addplayers(region *r, stream *strm)
{
int no = 0;
char buf[1024];
for (;;) {
faction *f;
if (strm->api->readln(strm->handle, buf, sizeof(buf))!=0) {
break;
}
f = addplayer(r, buf, no);
no = f->no+1;
}
}
void connecttothis(region * r, int x, int y, int from, int to)
{
region *r2;