-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgora.html
More file actions
1661 lines (1503 loc) · 54.7 KB
/
Agora.html
File metadata and controls
1661 lines (1503 loc) · 54.7 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
(function() {
var ua = navigator.userAgent;
var isPhone = /iPhone|iPad|iPod|Android/i.test(ua) || window.innerWidth < 768;
if (isPhone) {
var meta = document.querySelector('meta[name="viewport"]');
if (meta) {
var targetWidth = 780;
var scale = window.innerWidth / targetWidth;
meta.setAttribute('content', 'width=' + targetWidth + ', initial-scale=' + scale + ', maximum-scale=3.0, user-scalable=yes');
}
}
})();
</script>
<meta name="theme-color" content="#0c1016">
<title>Agora — Mediterranean Trade Strategy Game | Board Gaming Hub</title>
<meta name="description" content="Build a Mediterranean trading empire in Agora — a free in-browser strategy game of routes, harbors, and rival merchants. Single HTML file, no signup, no downloads.">
<link rel="canonical" href="https://boardgaminghub.com/Agora.html">
<meta property="og:type" content="website">
<meta property="og:url" content="https://boardgaminghub.com/Agora.html">
<meta property="og:title" content="Agora — Mediterranean Trade Strategy">
<meta property="og:description" content="Free in-browser strategy game of Mediterranean trade routes and rival merchants.">
<meta property="og:image" content="https://boardgaminghub.com/og-image.png">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://boardgaminghub.com/og-image.png">
<script type="application/ld+json">
{ "@context": "https://schema.org", "@type": "VideoGame", "name": "Agora",
"description": "Strategy game of Mediterranean trade — build harbors, secure routes, outmaneuver rival merchants.",
"url": "https://boardgaminghub.com/Agora.html", "genre": "Strategy",
"applicationCategory": "Game", "operatingSystem": "Any",
"offers": { "@type": "Offer", "price": "0", "priceCurrency": "USD" } }
</script>
<link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>🏛️</text></svg>">
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Georgia', serif;
background: #1A1A2E;
color: #D8D0C0;
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
min-height: 100vh;
}
h1 {
font-size: 1.8em;
color: #D4A860;
margin-bottom: 2px;
text-shadow: 1px 1px 3px rgba(0,0,0,0.5);
letter-spacing: 0.15em;
}
.subtitle {
font-size: 0.8em;
color: #7A8A9A;
margin-bottom: 12px;
font-style: italic;
}
.mode-selector {
display: flex;
gap: 0;
margin-bottom: 12px;
border-radius: 6px;
overflow: hidden;
border: 2px solid #D4A860;
}
.mode-btn {
padding: 6px 18px;
font-size: 0.95em;
font-family: 'Georgia', serif;
cursor: pointer;
border: none;
background: #1E3A5F;
color: #7A8A9A;
transition: background 0.2s, color 0.2s;
}
.mode-btn:not(:last-child) { border-right: 1px solid #D4A860; }
.mode-btn.active {
background: #D4A860;
color: #1A1A2E;
cursor: default;
font-weight: bold;
}
.mode-btn:hover:not(.active) { background: #2A4A6F; color: #D8D0C0; }
.game-info { text-align: center; margin-bottom: 8px; }
#turn-indicator { font-size: 1.15em; font-weight: bold; margin-bottom: 4px; }
.dice-container {
display: flex;
gap: 8px;
justify-content: center;
margin: 6px 0;
min-height: 36px;
}
.die {
width: 36px;
height: 36px;
background: #2A1A10;
border-radius: 6px;
display: flex;
align-items: center;
justify-content: center;
font-size: 1.3em;
color: #D4A860;
font-weight: bold;
box-shadow: 1px 1px 3px rgba(0,0,0,0.3);
border: 2px solid #D4A860;
}
#dice-total { font-size: 1em; margin-bottom: 2px; color: #D4A860; }
#message {
font-size: 0.95em;
color: #D4A860;
min-height: 1.4em;
margin-bottom: 6px;
max-width: 600px;
}
.buttons { margin-bottom: 12px; }
button {
padding: 7px 18px;
font-size: 0.95em;
font-family: 'Georgia', serif;
cursor: pointer;
margin: 0 4px;
border: 2px solid #D4A860;
border-radius: 6px;
background: #1E3A5F;
color: #D8D0C0;
transition: background 0.2s;
}
button:hover:not(:disabled) { background: #2A4A6F; }
button:disabled { opacity: 0.4; cursor: not-allowed; }
.main-area {
display: flex;
align-items: flex-start;
gap: 20px;
flex-wrap: wrap;
justify-content: center;
}
/* ---- Hex Board ---- */
.board-wrap {
position: relative;
width: 460px;
height: 420px;
}
.board-svg {
width: 460px;
height: 420px;
}
/* ---- Market Panel ---- */
.market-panel {
background: #2A1A10;
border: 2px solid #D4A860;
border-radius: 8px;
padding: 14px;
min-width: 260px;
max-width: 280px;
}
.market-panel h2 {
font-size: 1.1em;
color: #D4A860;
text-align: center;
margin-bottom: 10px;
letter-spacing: 0.1em;
}
.market-row {
display: flex;
align-items: center;
margin-bottom: 7px;
gap: 6px;
}
.market-icon {
width: 18px;
height: 18px;
border-radius: 50%;
flex-shrink: 0;
}
.market-name {
width: 48px;
font-size: 0.8em;
flex-shrink: 0;
}
.market-bar-wrap {
flex: 1;
height: 16px;
background: #1A1A2E;
border-radius: 3px;
position: relative;
overflow: hidden;
}
.market-bar {
height: 100%;
border-radius: 3px;
transition: width 0.3s;
}
.market-price {
width: 22px;
text-align: right;
font-size: 0.85em;
font-weight: bold;
color: #D4A860;
flex-shrink: 0;
}
.market-btns {
display: flex;
gap: 3px;
flex-shrink: 0;
}
.market-btns button {
padding: 2px 7px;
font-size: 0.7em;
margin: 0;
min-width: 30px;
}
.player-info {
border-top: 1px solid #D4A860;
margin-top: 10px;
padding-top: 10px;
}
.player-info h3 {
font-size: 0.95em;
margin-bottom: 6px;
color: #D4A860;
}
.inv-row {
display: flex;
align-items: center;
gap: 6px;
margin-bottom: 3px;
font-size: 0.82em;
}
.inv-icon {
width: 12px;
height: 12px;
border-radius: 50%;
flex-shrink: 0;
}
.coins-display {
font-size: 1.1em;
font-weight: bold;
color: #D4A860;
margin-top: 6px;
text-align: center;
}
.build-section {
border-top: 1px solid #D4A860;
margin-top: 10px;
padding-top: 10px;
}
.build-section h3 {
font-size: 0.95em;
margin-bottom: 6px;
color: #D4A860;
}
.build-section button {
display: block;
width: 100%;
margin: 4px 0;
font-size: 0.82em;
padding: 5px 10px;
}
.build-hint {
font-size: 0.72em;
color: #7A8A9A;
margin-top: 2px;
}
/* ---- Rules ---- */
.rules {
max-width: 740px;
margin-top: 18px;
padding: 14px 18px;
font-size: 0.82em;
background: rgba(30,58,95,0.3);
border-radius: 8px;
border: 1px solid rgba(212,168,96,0.2);
line-height: 1.5;
}
.rules h3 {
color: #D4A860;
margin-bottom: 6px;
font-size: 1em;
}
.rules ul { padding-left: 18px; }
.rules li { margin-bottom: 4px; }
.phase-indicator {
font-size: 0.85em;
color: #7A8A9A;
margin-bottom: 4px;
}
.legend {
display: flex;
gap: 10px;
justify-content: center;
flex-wrap: wrap;
margin-top: 6px;
font-size: 0.75em;
color: #7A8A9A;
}
.legend span {
display: flex;
align-items: center;
gap: 3px;
}
.legend-swatch {
width: 10px;
height: 10px;
border-radius: 2px;
display: inline-block;
}
/* Mobile game nav */
</style>
<script type="application/ld+json">{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Board Gaming Hub","item":"https://boardgaminghub.com/"},{"@type":"ListItem","position":2,"name":"Board Games","item":"https://boardgaminghub.com/#board"},{"@type":"ListItem","position":3,"name":"Agora","item":"https://boardgaminghub.com/Agora.html"}]}</script>
<script type="application/ld+json">{"@context":"https://schema.org","@type":"FAQPage","mainEntity":[{"@type":"Question","name":"How long is a typical game?","acceptedAnswer":{"@type":"Answer","text":"15–25 minutes for a 4-city session. Larger maps run 30–45 minutes."}},{"@type":"Question","name":"How do I gain influence?","acceptedAnswer":{"@type":"Answer","text":"Trade between cities you control, deploy fleets to neighbors, and chain consecutive turns through allied ports. Influence rewards consistency more than single big plays."}},{"@type":"Question","name":"Is Agora based on a real game?","acceptedAnswer":{"@type":"Answer","text":"No — original design. The Mediterranean trade flavor draws on Catan and Concordia but the mechanics are independent."}},{"@type":"Question","name":"How do I win?","acceptedAnswer":{"@type":"Answer","text":"Control the most weighted-influence cities when the trade-deck exhausts. Some cities count more than others, so prioritize the high-value ones."}}]}</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2835365593874464"
crossorigin="anonymous"></script>
</head>
<body>
<style id="siteNav-css">
#siteNav { position: sticky; top: 0; z-index: 99999; background: rgba(16,20,28,0.96); border-bottom: 1px solid #2a3540; font-family: Georgia, "Times New Roman", serif; box-shadow: 0 2px 10px rgba(0,0,0,0.6); color: #d8d0c0; }
#siteNav * { box-sizing: border-box; }
#siteNav .nav-bar { display: flex; justify-content: space-between; align-items: center; padding: 8px 16px; max-width: 1400px; margin: 0 auto; gap: 12px; }
#siteNav .nav-home { color: #d8d0c0; text-decoration: none; font-size: 0.82em; letter-spacing: 2px; padding: 5px 12px; border: 1px solid #3a5060; border-radius: 3px; white-space: nowrap; }
#siteNav .nav-home:hover { background: #1e2838; color: #f0e0b0; }
#siteNav .nav-title { color: #f0d89c; font-size: 0.95em; letter-spacing: 3px; font-weight: bold; text-align: center; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#siteNav .nav-toggle { background: none; border: 1px solid #3a5060; color: #d8d0c0; font-size: 0.85em; letter-spacing: 2px; padding: 5px 12px; border-radius: 3px; cursor: pointer; font-family: inherit; white-space: nowrap; }
#siteNav .nav-toggle:hover { background: #1e2838; color: #f0e0b0; }
#siteNav .nav-menu { display: none; background: #0f141c; border-top: 1px solid #2a4050; padding: 14px 16px; max-width: 1400px; margin: 0 auto; }
#siteNav .nav-menu.open { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
#siteNav .nav-cat { min-width: 0; }
#siteNav .nav-cat-label { color: #8098a8; font-size: 0.72em; letter-spacing: 4px; padding: 4px 0 8px 0; border-bottom: 1px solid #2a3540; margin-bottom: 6px; }
#siteNav .nav-cat a { display: block; padding: 6px 10px; color: #a8b0c0; text-decoration: none; font-size: 0.88em; border-radius: 3px; }
#siteNav .nav-cat a:hover { background: #1e2838; color: #f0e0b0; }
#siteNav .nav-cat a.current { color: #f0d89c; font-weight: bold; background: #1e2838; }
@media (max-width: 640px) {
#siteNav .nav-menu.open { grid-template-columns: 1fr; gap: 14px; }
#siteNav .nav-title { font-size: 0.82em; letter-spacing: 2px; }
#siteNav .nav-home,
#siteNav .nav-toggle { font-size: 0.74em; padding: 4px 8px; }
}
</style>
<div id="siteNav">
<div class="nav-bar">
<a href="index.html" class="nav-home">HOME</a>
<span class="nav-title">Agora</span>
<button class="nav-toggle" onclick="document.querySelector('#siteNav .nav-menu').classList.toggle('open')">GAMES ☰</button>
</div>
<div class="nav-menu">
<div class="nav-cat">
<div class="nav-cat-label">BOARD GAMES</div>
<a href="Agora.html" class="current">Agora</a>
<a href="Aresia.html">Aresia</a>
<a href="Backgammon.html">Backgammon</a>
<a href="Bisque.html">Bisque</a>
<a href="Chess.html">Chess</a>
<a href="Convergence.html">Convergence</a>
<a href="Go.html">Go</a>
<a href="Mancala.html">Mancala</a>
<a href="Odyssey.html">Odyssey</a>
<a href="Othello.html">Othello</a>
<a href="PenteGrammai.html">Pente Grammai</a>
<a href="Senet.html">Senet</a>
<a href="Tidelands.html">Tidelands</a>
<a href="Ur.html">Ur</a>
</div>
<div class="nav-cat">
<div class="nav-cat-label">SIMULATIONS</div>
<a href="Apoapsis.html">Apoapsis</a>
<a href="BiosphereBlue.html">Biosphere Blue</a>
<a href="BonnevilleSpillwayOperator.html">Bonneville Spillway</a>
<a href="Cliffwalkers.html">Cliffwalkers</a>
<a href="Doctrine.html">Doctrine</a>
<a href="EclipsePredictor.html">Eclipse Predictor</a>
<a href="Floodline.html">Floodline</a>
<a href="Metropolis2K.html">Metropolis 2K</a>
<a href="Tower.html">Tower</a>
</div>
</div>
</div>
<h1>AGORA</h1>
<div class="subtitle">The Mediterranean Trade</div>
<div class="mode-selector">
<button class="mode-btn active" id="mode-1p" onclick="setMode(true)">1 Player</button>
<button class="mode-btn" id="mode-2p" onclick="setMode(false)">2 Players</button>
</div>
<div class="game-info">
<div id="turn-indicator"></div>
<div class="phase-indicator" id="phase-indicator"></div>
<div class="dice-container" id="dice-container"></div>
<div id="dice-total"></div>
<div id="message"></div>
</div>
<div class="buttons">
<button id="roll-btn" onclick="doRoll()">Roll Dice</button>
<button id="end-turn-btn" onclick="endTurn()" disabled>End Turn</button>
<button onclick="resetGame()">New Game</button>
</div>
<div class="main-area">
<div class="board-wrap">
<svg id="board-svg" class="board-svg" viewBox="0 0 460 420"></svg>
</div>
<div class="market-panel" id="market-panel"></div>
</div>
<div class="legend" id="legend"></div>
<div class="rules">
<h3>Rules</h3>
<ul>
<li>Two merchants compete to accumulate <b>30 coins</b> through trade on the Mediterranean.</li>
<li><b>Roll phase:</b> Roll 2d6. Hexes matching the total produce their resource for all adjacent trade posts.</li>
<li>A roll of <b>7</b> triggers a market crash — the most expensive resource drops 3 price levels.</li>
<li><b>Trade phase:</b> Buy and sell resources at the Agora (center market). Prices move with supply and demand.</li>
<li><b>Build phase:</b> Build trade posts at cities (1 Grain + 1 Marble) or trade routes between cities (1 Oil + 1 Silk).</li>
<li>You must have a trade route connected to the <b>Agora</b> (center) to buy or sell.</li>
<li>Click a <b>city</b> on the map to build a trade post there. Click a <b>route</b> (edge) to build a trade route.</li>
<li>First to <b>30 coins</b> wins! Trade posts and routes count as 2 coins each at game end.</li>
</ul>
</div>
<script>
// =============================================
// Constants & Resources
// =============================================
var RESOURCES = ['Grain', 'Wine', 'Oil', 'Marble', 'Spice', 'Silk'];
var RES_COLORS = {
Grain: '#E8C840',
Wine: '#9050A0',
Oil: '#70A040',
Marble: '#A0A0A8',
Spice: '#40B0A0',
Silk: '#C07050'
};
var TERRAIN_TYPES = [
{ name: 'Fields', resource: 'Grain', fill: '#C8A848', stroke: '#A08030' },
{ name: 'Vineyards', resource: 'Wine', fill: '#7A5090', stroke: '#5A3070' },
{ name: 'Groves', resource: 'Oil', fill: '#5A8A40', stroke: '#3A6A20' },
{ name: 'Quarries', resource: 'Marble', fill: '#808898', stroke: '#606878' },
{ name: 'Oasis', resource: 'Spice', fill: '#308878', stroke: '#206858' },
{ name: 'Pastures', resource: 'Silk', fill: '#90B870', stroke: '#70A050' }
];
var PLAYER_COLORS = ['#E8C840', '#D04030'];
var PLAYER_NAMES = ['Gold', 'Red'];
var WIN_COINS = 30;
// Hex layout: 3-4-5-4-3 arrangement (19 hexes)
var HEX_ROWS = [3, 4, 5, 4, 3];
var HEX_W = 66;
var HEX_H = 58;
var BOARD_OX = 230;
var BOARD_OY = 38;
// =============================================
// Game State
// =============================================
var hexes = []; // {row, col, cx, cy, terrain, number, resource}
var cities = []; // {id, x, y, hexIds[], owner: 0/1/-1} owner -1 = unclaimed
var edges = []; // {id, cityA, cityB, owner: -1/0/1}
var prices = {}; // resource -> 1..8
var players = []; // [{coins, resources:{}, posts:[], routes:[]}]
var playerTurn = 0;
var phase = 'roll'; // roll, trade, build
var gameOver = false;
var aiMode = true;
var aiTimeout = null;
var diceA = 0;
var diceB = 0;
var svgEl = document.getElementById('board-svg');
var rollBtn = document.getElementById('roll-btn');
var endTurnBtn = document.getElementById('end-turn-btn');
// =============================================
// Utilities
// =============================================
function setMessage(msg) {
document.getElementById('message').textContent = msg;
}
function shuffleArray(arr) {
for (var i = arr.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var t = arr[i]; arr[i] = arr[j]; arr[j] = t;
}
return arr;
}
function hexCenter(row, col) {
var rowLen = HEX_ROWS[row];
var maxLen = 5;
var offsetX = (maxLen - rowLen) * (HEX_W * 0.75) / 2;
var cx = BOARD_OX + offsetX + col * HEX_W * 0.75 - (rowLen - 1) * HEX_W * 0.375;
var cy = BOARD_OY + row * HEX_H * 0.88;
return { x: cx, y: cy };
}
function hexCorners(cx, cy, r) {
var corners = [];
for (var i = 0; i < 6; i++) {
var angle = Math.PI / 180 * (60 * i - 30);
corners.push({
x: cx + r * Math.cos(angle),
y: cy + r * Math.sin(angle)
});
}
return corners;
}
function dist(a, b) {
var dx = a.x - b.x;
var dy = a.y - b.y;
return Math.sqrt(dx * dx + dy * dy);
}
function pointsEqual(a, b) {
return Math.abs(a.x - b.x) < 3 && Math.abs(a.y - b.y) < 3;
}
// =============================================
// Board Generation
// =============================================
function generateHexLayout() {
hexes = [];
var id = 0;
for (var row = 0; row < HEX_ROWS.length; row++) {
for (var col = 0; col < HEX_ROWS[row]; col++) {
var c = hexCenter(row, col);
hexes.push({
id: id,
row: row,
col: col,
cx: c.x,
cy: c.y,
terrain: null,
number: 0,
resource: ''
});
id++;
}
}
}
function assignTerrain() {
// Distribute terrains: 3-4 of each type across 19 hexes
var terrainPool = [];
for (var i = 0; i < 6; i++) {
var count = (i < 5) ? 3 : 4; // 3+3+3+3+3+4 = 19
for (var j = 0; j < count; j++) {
terrainPool.push(i);
}
}
shuffleArray(terrainPool);
// Assign center hex as a special type but still a resource hex
for (var i = 0; i < hexes.length; i++) {
hexes[i].terrain = TERRAIN_TYPES[terrainPool[i]];
hexes[i].resource = hexes[i].terrain.resource;
}
}
function assignNumbers() {
// Numbers 2-12 excluding 7, distributed across hexes
// Standard distribution like Catan: 2,3,3,4,4,5,5,6,6,8,8,9,9,10,10,11,11,12
var numPool = [2, 3, 3, 4, 4, 5, 5, 6, 6, 8, 8, 9, 9, 10, 10, 11, 11, 12];
// We have 19 hexes, need 19 numbers. Add one more:
numPool.push(5); // extra 5 to make 19
// Shuffle and assign, but ensure no two 6s or 8s are adjacent
var valid = false;
var attempts = 0;
while (!valid && attempts < 200) {
attempts++;
shuffleArray(numPool);
valid = true;
for (var i = 0; i < hexes.length; i++) {
hexes[i].number = numPool[i];
}
// Check adjacency constraint for 6 and 8
for (var i = 0; i < hexes.length && valid; i++) {
if (hexes[i].number === 6 || hexes[i].number === 8) {
var adj = getAdjacentHexes(i);
for (var j = 0; j < adj.length; j++) {
if (hexes[adj[j]].number === 6 || hexes[adj[j]].number === 8) {
valid = false;
break;
}
}
}
}
}
}
function getAdjacentHexes(hexId) {
var h = hexes[hexId];
var adj = [];
for (var i = 0; i < hexes.length; i++) {
if (i === hexId) continue;
if (dist(h, { x: hexes[i].cx, y: hexes[i].cy }) < HEX_W * 0.95) {
adj.push(i);
}
}
return adj;
}
// =============================================
// Cities & Edges (vertices and edges of hexes)
// =============================================
function generateCitiesAndEdges() {
cities = [];
edges = [];
var R = HEX_H / 2 + 2;
var allCorners = [];
// Collect all hex corners
for (var i = 0; i < hexes.length; i++) {
var corners = hexCorners(hexes[i].cx, hexes[i].cy, R);
for (var c = 0; c < corners.length; c++) {
allCorners.push({ x: corners[c].x, y: corners[c].y, hexId: i, cornerIdx: c });
}
}
// Merge nearby corners into unique vertices (cities)
var vertices = [];
for (var i = 0; i < allCorners.length; i++) {
var found = false;
for (var j = 0; j < vertices.length; j++) {
if (dist(allCorners[i], vertices[j]) < 5) {
vertices[j].hexIds.push(allCorners[i].hexId);
found = true;
break;
}
}
if (!found) {
vertices.push({
x: allCorners[i].x,
y: allCorners[i].y,
hexIds: [allCorners[i].hexId]
});
}
}
// Filter to only vertices at intersection of 2+ hexes (interesting for gameplay)
// Also keep some outer vertices for variety
// We want exactly 6 cities for the game, placed at strategic positions
// Strategy: pick vertices adjacent to 3 hexes first, then 2-hex vertices spread around
var threeHex = [];
var twoHex = [];
for (var i = 0; i < vertices.length; i++) {
// Deduplicate hexIds
var unique = [];
for (var j = 0; j < vertices[i].hexIds.length; j++) {
if (unique.indexOf(vertices[i].hexIds[j]) === -1) unique.push(vertices[i].hexIds[j]);
}
vertices[i].hexIds = unique;
if (unique.length >= 3) threeHex.push(vertices[i]);
else if (unique.length === 2) twoHex.push(vertices[i]);
}
// Pick 6 cities: prefer 3-hex vertices, spread out
var candidates = threeHex.concat(twoHex);
var chosen = [];
// Greedily select spread-out cities
shuffleArray(candidates);
for (var i = 0; i < candidates.length && chosen.length < 12; i++) {
var tooClose = false;
for (var j = 0; j < chosen.length; j++) {
if (dist(candidates[i], chosen[j]) < HEX_W * 0.7) {
tooClose = true;
break;
}
}
if (!tooClose) {
chosen.push(candidates[i]);
}
}
// If we have less than 6, relax constraint
if (chosen.length < 6) {
for (var i = 0; i < candidates.length && chosen.length < 6; i++) {
var already = false;
for (var j = 0; j < chosen.length; j++) {
if (pointsEqual(candidates[i], chosen[j])) { already = true; break; }
}
if (!already) chosen.push(candidates[i]);
}
}
// Trim to first 10 (or however many we got)
chosen = chosen.slice(0, 10);
var cityNames = ['Carthage', 'Athens', 'Alexandria', 'Rome', 'Tyre', 'Massalia',
'Syracuse', 'Cyrene', 'Byzantium', 'Petra'];
for (var i = 0; i < chosen.length; i++) {
cities.push({
id: i,
x: chosen[i].x,
y: chosen[i].y,
hexIds: chosen[i].hexIds,
name: cityNames[i] || ('City ' + i),
owner: -1 // -1 = unclaimed
});
}
// Generate edges between nearby cities (sharing a hex edge neighborhood)
for (var i = 0; i < cities.length; i++) {
for (var j = i + 1; j < cities.length; j++) {
var d = dist(cities[i], cities[j]);
if (d < HEX_W * 1.1 && d > 5) {
edges.push({
id: edges.length,
cityA: i,
cityB: j,
owner: -1
});
}
}
}
// If too few edges, increase threshold
if (edges.length < 8) {
edges = [];
for (var i = 0; i < cities.length; i++) {
for (var j = i + 1; j < cities.length; j++) {
var d = dist(cities[i], cities[j]);
if (d < HEX_W * 1.5 && d > 5) {
edges.push({
id: edges.length,
cityA: i,
cityB: j,
owner: -1
});
}
}
}
}
}
// =============================================
// Check Agora Connection
// =============================================
function getCenterHexId() {
// Center hex is at row 2, col 2 (middle of the 5-wide row)
var idx = 0;
for (var r = 0; r < HEX_ROWS.length; r++) {
for (var c = 0; c < HEX_ROWS[r]; c++) {
if (r === 2 && c === 2) return idx;
idx++;
}
}
return 9;
}
function cityTouchesCenter(cityId) {
var centerId = getCenterHexId();
var c = cities[cityId];
for (var i = 0; i < c.hexIds.length; i++) {
if (c.hexIds[i] === centerId) return true;
}
return false;
}
function playerConnectedToAgora(p) {
// BFS: starting from any city the player owns that touches the center hex,
// or from any city connected via the player's routes to such a city
var visited = {};
var queue = [];
// Find all cities owned by player
for (var i = 0; i < cities.length; i++) {
if (cities[i].owner === p) {
queue.push(i);
visited[i] = true;
}
}
// BFS via player's routes
while (queue.length > 0) {
var cur = queue.shift();
if (cityTouchesCenter(cur)) return true;
for (var i = 0; i < edges.length; i++) {
if (edges[i].owner === p) {
var other = -1;
if (edges[i].cityA === cur) other = edges[i].cityB;
else if (edges[i].cityB === cur) other = edges[i].cityA;
if (other >= 0 && !visited[other]) {
visited[other] = true;
queue.push(other);
}
}
}
}
return false;
}
// =============================================
// Mode
// =============================================
function setMode(isAi) {
if (aiMode === isAi) return;
aiMode = isAi;
document.getElementById('mode-1p').classList.toggle('active', isAi);
document.getElementById('mode-2p').classList.toggle('active', !isAi);
resetGame();
}
// =============================================
// Price / Market
// =============================================
function initPrices() {
prices = {};
for (var i = 0; i < RESOURCES.length; i++) {
prices[RESOURCES[i]] = 4;
}
}
function adjustPrice(resource, delta) {
prices[resource] = Math.max(1, Math.min(8, prices[resource] + delta));
}
function marketCrash() {
// Most expensive resource drops by 3
var maxP = 0;
var maxR = RESOURCES[0];
for (var i = 0; i < RESOURCES.length; i++) {
if (prices[RESOURCES[i]] > maxP) {
maxP = prices[RESOURCES[i]];
maxR = RESOURCES[i];
}
}
adjustPrice(maxR, -3);
return maxR;
}
// =============================================
// Init / Reset
// =============================================
function initPlayers() {
players = [];
for (var i = 0; i < 2; i++) {
var res = {};
for (var j = 0; j < RESOURCES.length; j++) {
res[RESOURCES[j]] = 0;
}
players.push({
coins: 0,
resources: res,
posts: [],
routes: []
});
}
// Give each player starting resources to build their first post + route
players[0].resources.Grain = 2;
players[0].resources.Marble = 2;
players[0].resources.Oil = 1;
players[0].resources.Silk = 1;
players[1].resources.Grain = 2;
players[1].resources.Marble = 2;
players[1].resources.Oil = 1;
players[1].resources.Silk = 1;
}
function resetGame() {
if (aiTimeout) { clearTimeout(aiTimeout); aiTimeout = null; }
gameOver = false;
playerTurn = 0;
phase = 'roll';
diceA = 0;
diceB = 0;
generateHexLayout();
assignTerrain();
assignNumbers();
generateCitiesAndEdges();
initPrices();
initPlayers();
render();
renderMarket();
setMessage('Gold player begins. Roll the dice!');
updateButtons();
}
// =============================================
// Dice Roll
// =============================================
function doRoll() {
if (phase !== 'roll' || gameOver) return;
diceA = Math.floor(Math.random() * 6) + 1;
diceB = Math.floor(Math.random() * 6) + 1;
var total = diceA + diceB;
var crashMsg = '';
if (total === 7) {
var crashed = marketCrash();
crashMsg = ' Market crash! ' + crashed + ' price drops!';
} else {
// Produce resources
produceResources(total);
}
phase = 'trade';
setMessage('Rolled ' + total + '.' + crashMsg + ' Trade or build, then end turn.');
updateButtons();
render();
renderMarket();
if (aiMode && playerTurn === 1) {
scheduleAi();
}
}
function produceResources(total) {
for (var h = 0; h < hexes.length; h++) {
if (hexes[h].number === total) {
// Find all cities adjacent to this hex
for (var c = 0; c < cities.length; c++) {
if (cities[c].owner >= 0) {
for (var k = 0; k < cities[c].hexIds.length; k++) {
if (cities[c].hexIds[k] === h) {
var p = cities[c].owner;
players[p].resources[hexes[h].resource]++;
}
}
}
}
}
}
}
// =============================================
// Trade Actions
// =============================================
function sellResource(res) {
if (phase !== 'trade' || gameOver) return;
var p = players[playerTurn];
if (p.resources[res] <= 0) return;
if (!playerConnectedToAgora(playerTurn)) {
setMessage('You need a route connected to the Agora to trade!');
return;
}
var earned = prices[res];
p.resources[res]--;
p.coins += earned;
adjustPrice(res, -1);
setMessage(PLAYER_NAMES[playerTurn] + ' sold ' + res + ' for ' + earned + ' coins.');
renderMarket();
render();
checkWin();
}
function buyResource(res) {
if (phase !== 'trade' || gameOver) return;
var p = players[playerTurn];