File: ai_strategy.cpp

package info (click to toggle)
warmux 1%3A11.04.1%2Brepack2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 126,388 kB
  • sloc: cpp: 186,040; xml: 8,909; sh: 3,358; makefile: 1,052; ansic: 713
file content (136 lines) | stat: -rw-r--r-- 4,554 bytes parent folder | download | duplicates (4)
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
/******************************************************************************
 *  Warmux is a convivial mass murder game.
 *  Copyright (C) 2001-2011 Warmux Team.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 ******************************************************************************
 * An AI player for a team.
 *****************************************************************************/

#include "ai/ai_strategy.h"

const float RATING_EPSILON = 0.00001f;
const uint WATCH_MISSILE_TIME_IN_MS = 2000;

AIStrategy::AIStrategy(float rating):
rating(rating)
{
  // do nothing
}

AIStrategy::CompareResult AIStrategy::CompareRatingWith(AIStrategy * other) const
{
  if (this->GetRating() + RATING_EPSILON < other->GetRating())
    return AIStrategy::LOWER_RATING;

  if (this->GetRating() - LOWER_RATING > other->GetRating())
    return AIStrategy::HIGHER_RATING;

  return AIStrategy::SIMILAR_RATING;
}

DoNothingStrategy::DoNothingStrategy():
AIStrategy(-1.0f) // It's worse then skipping the turn (rating 0) as the human needs to wait.
{
  // do nothing
}

AICommand * DoNothingStrategy::CreateCommand() const
{
  return new DoNothingForeverCommand;
}

SkipTurnStrategy::SkipTurnStrategy():
AIStrategy(0.0f)
{
  // do nothing
}

AICommand * SkipTurnStrategy::CreateCommand() const
{
  CommandList * commands = new CommandList();
  commands->Add(new SelectWeaponCommand(Weapon::WEAPON_SKIP_TURN));
  commands->Add(new StartShootingCommand());
  commands->Add(new StopShootingCommand());
  return commands;
}

static CommandList * CreateSelectCommandList(const Character & character, Weapon::Weapon_type weapon,
                                             LRDirection  direction, float angle, int timeout = -1)
{
  CommandList * commands = new CommandList();
  commands->Add(new SelectCharacterCommand(&character));
  commands->Add(new DoNothingCommand(1000));
  commands->Add(new SelectWeaponCommand(weapon));
  commands->Add(new DoNothingCommand(100));
  if (timeout > 0) {
    commands->Add(new SetTimeoutCommand(timeout));
  }
  commands->Add(new DoNothingCommand(100));
  commands->Add(new SetDirectionCommand(direction));
  commands->Add(new SetWeaponAngleCommand(angle));
  commands->Add(new DoNothingCommand(200));
  return commands;
}

ShootWithGunStrategy::ShootWithGunStrategy(float rating, const Character & shooter,
                                           Weapon::Weapon_type weapon, LRDirection direction,
                                           float angle, int bullets)
  : AIStrategy(rating)
  , shooter(shooter)
  , weapon(weapon)
  , direction(direction)
  , angle(angle)
  , bullets(bullets)
{
  // do nothing
}

AICommand * ShootWithGunStrategy::CreateCommand() const
{
  CommandList * commands = CreateSelectCommandList(shooter, weapon, direction, angle);
  for (int i = 0; i < bullets; i++) {
    if (i != 0)
      commands->Add(new DoNothingCommand(1000));
    commands->Add(new StartShootingCommand());
    commands->Add(new StopShootingCommand());
  }
  commands->Add(new DoNothingCommand(WATCH_MISSILE_TIME_IN_MS));
  return commands;
}

LoadAndFireStrategy::LoadAndFireStrategy(float rating, const Character & shooter,
                                         Weapon::Weapon_type weapon, LRDirection direction,
                                         float angle, float strength, int timeout)
  : AIStrategy(rating)
  , shooter(shooter)
  , weapon(weapon)
  , direction(direction)
  , angle(angle)
  , strength(strength)
  , timeout(timeout)
{
  // do nothing
}

AICommand * LoadAndFireStrategy::CreateCommand() const
{
  CommandList * commands = CreateSelectCommandList(shooter, weapon, direction, angle, timeout);
  commands->Add(new StartShootingCommand());
  commands->Add(new WaitForStrengthCommand(strength));
  commands->Add(new StopShootingCommand());
  commands->Add(new DoNothingForeverCommand());
  return commands;
}