source: tags/1.2.0/AI Template/Project/Empire.cs

Last change on this file was 191, checked in by chronos, 4 years ago
  • Added: AI Template directory with manual for development of custom AI from original game.
File size: 3.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using CevoAILib;
4using CevoAILib.Diplomacy;
5
6namespace AI
7{
8 sealed class Empire : AEmpire
9 {
10 Random random = new Random();
11
12 public Empire(int nationID, IntPtr serverPtr, IntPtr dataPtr, bool isNewGame)
13 : base(nationID, serverPtr, dataPtr, isNewGame)
14 {
15 }
16
17 protected override void NewGame()
18 {
19 }
20
21 protected override void Resume()
22 {
23 }
24
25 protected override void OnTurn()
26 {
27 // check if research complete
28 if (HadEvent__Turn(EmpireEvent.ResearchComplete))
29 {
30 if (random.Next(20) == 0)
31 { // start military research
32 Blueprint.SetDomain__Turn(ModelDomain.Ground);
33 Blueprint.SetProperty__Turn(ModelProperty.Weapons, 3);
34 Blueprint.SetProperty__Turn(ModelProperty.Armor, 1);
35 Blueprint.SetProperty__Turn(ModelProperty.Mobility, Blueprint.Stage.MaximumWeight - Blueprint.Weight);
36 SetResearch__Turn(Advance.MilitaryResearch);
37 }
38 else
39 { // set random research
40 Advance newResearch = Advance.None;
41 int count = 0;
42 for (Advance testResearch = Advance.FirstCommon; testResearch <= Advance.LastFuture; testResearch++)
43 {
44 if (CanSetResearch__Turn(testResearch))
45 {
46 count++;
47 if (random.Next(count) == 0)
48 newResearch = testResearch;
49 }
50 }
51 if (newResearch != Advance.None)
52 SetResearch__Turn(newResearch);
53 }
54 }
55
56 // check cities
57 foreach (City city in Cities)
58 {
59 if (city.Size < 4)
60 city.OptimizeExploitedLocations__Turn(ResourceWeights.MaxGrowth);
61 else
62 city.OptimizeExploitedLocations__Turn(ResourceWeights.HurryProduction);
63 }
64
65 // move units
66 foreach (Unit unit in Units)
67 {
68 OtherLocation[] neighborLocations = unit.Location.Neighbors;
69 if (neighborLocations.Length > 0)
70 unit.MoveTo__Turn(neighborLocations[random.Next(neighborLocations.Length)].Location); // move unit to random adjacent location
71 }
72 }
73
74 protected override void OnStealAdvance(Advance[] selection)
75 {
76 StealAdvance__Turn(selection[random.Next(selection.Length)]);
77 }
78
79 protected override void OnChanceToNegotiate(Phase situation, Nation Opponent, ref bool wantNegotiation, ref bool cancelTreatyIfRejected)
80 {
81 if (situation == Phase.BeginOfTurn) // start negotiation?
82 {
83 if (random.Next(30) == 0)
84 wantNegotiation = true;
85 }
86 if (situation == Phase.ForeignTurn) // accept contact for negotiation?
87 {
88 if (random.Next(2) == 0)
89 wantNegotiation = true;
90 }
91 }
92
93 protected override void OnNegotiate(Negotiation negotiation)
94 {
95 if (negotiation.History.Count == 0 && RelationTo(negotiation.Opponent) < Relation.Alliance)
96 negotiation.SetOurNextStatement(new SuggestTrade(new ITradeItem[] { new ChangeRelation(RelationTo(negotiation.Opponent) + 1) }, null)); // suggest better treaty
97 if (negotiation.History.Count > 0 && negotiation.History[0].OpponentResponse is SuggestTrade)
98 {
99 SuggestTrade trade = negotiation.History[0].OpponentResponse as SuggestTrade;
100 if ((trade.Offers.Length == 1 && trade.Wants.Length == 0 && trade.Offers[0] is ChangeRelation &&
101 ((ChangeRelation)trade.Offers[0]).NewRelation > RelationTo(negotiation.Opponent) ||
102 (trade.Offers.Length == 0 && trade.Wants.Length == 1 && trade.Wants[0] is ChangeRelation &&
103 ((ChangeRelation)trade.Wants[0]).NewRelation > RelationTo(negotiation.Opponent))))
104 negotiation.SetOurNextStatement(new AcceptTrade()); // accept better treaty
105 }
106 }
107
108 protected override void OnForeignMove(IUnitInfo unit, Location destination)
109 {
110 }
111
112 protected override void OnBeforeForeignAttack(IUnitInfo attacker, Location target, BattleOutcome outcome)
113 {
114 }
115
116 protected override void OnAfterForeignAttack()
117 {
118 }
119
120 protected override void OnBeforeForeignCapture(Nation nation, ICity city)
121 {
122 }
123
124 protected override void OnAfterForeignCapture()
125 {
126 }
127 }
128}
Note: See TracBrowser for help on using the repository browser.