-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandomai.cpp
74 lines (59 loc) · 1.34 KB
/
randomai.cpp
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
//
// author: Kovacs Marton
// email: [email protected]
// license: whatever. note my name
//
// randomai.cpp
//
// sample ai which copies the builtin random ai
//
#include <fstream>
#include <string>
#include <sstream>
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <unistd.h>
#include "skeleton.hpp"
int main(int argc, char *argv[])
{
if (argc != 3)
{
std::cerr << "Usage: " << argv[0] << " <commands file> <data file>\n";
return 1;
}
std::ifstream in(argv[2]);
std::ofstream out(argv[1]);
if (!out || !in)
{
std::cerr << "Error with opening file\n";
return 2;
}
Skeleton ipc(in, out);
std::srand(std::time(0) + getpid());
const int changeDirectionInterval = 10;
const int changeTargetInterval = 3;
const int maxSpeed = 5;
Coordinate randum;
int ticker = 0;
std::string str;
while (true)
{
ipc.Begin();
if (!(ticker % changeDirectionInterval))
{
randum = Normalize(Coordinate(std::rand()%1000 - 500, std::rand()%1000 - 500), maxSpeed);
}
Coordinate speed = ipc.GetSpeed();
ipc.SetSpeed(speed + randum);
Skeleton::ShipList enemies = ipc.GetEnemies();
if (!enemies.empty())
{
std::pair<int, Coordinate> enemy = enemies[std::rand() % enemies.size()];
ipc.Shoot(enemy.second);
}
ipc.End();
++ticker;
}
}