-
Notifications
You must be signed in to change notification settings - Fork 0
/
fish.cpp
144 lines (120 loc) · 2.99 KB
/
fish.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
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
#include <ctime>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/cstdint.hpp>
#include <QDebug>
#include "fish.h"
Fish::Fish(QObject* parent)
: QObject(parent),
isAlive_(true),
dieStepsLeft_(-1) // fish is alive
{
boost::posix_time::ptime myEpoch(boost::gregorian::date(1970,1,1));
boost::posix_time::ptime microTime
= boost::posix_time::microsec_clock::local_time();
boost::posix_time::time_duration myTimeFromEpoch = microTime - myEpoch;
boost::int64_t microseconds = myTimeFromEpoch.ticks();
rng_.seed(static_cast<boost::int64_t>(microseconds));
qDebug() << QString("Fish created!");
}
QPoint Fish::getNewDestination()
{
int x;
int y;
if (dieStepsLeft_ != -1)
{
if (dieStepsLeft_ > 0)
--dieStepsLeft_;
QPoint toReturn = chooseNextDrowingDestination();
lastMoves_.append(toReturn);
return toReturn;
}
else
{
x = getRandomNumber(startLimit_.x(),endLimit_.x());
y = getRandomNumber(startLimit_.y(),endLimit_.y());
}
qDebug() << QString("New destination: " + QString::number(x) + "," + QString::number(y));
QPoint toReturn(x,y);
lastMoves_.append(toReturn);
return toReturn;
}
bool Fish::isAlive() const
{
return isAlive_;
}
bool Fish::isDying() const
{
return dieStepsLeft_ > 0;
}
void Fish::die(unsigned int steps)
{
dieStepsLeft_ = static_cast<int>(steps);
emit dyingStateChanged();
}
QPoint Fish::getStartLimit() const
{
return startLimit_;
}
void Fish::setStartLimit(QPoint startLimit)
{
startLimit_ = startLimit;
emit limitChanged();
}
QPoint Fish::getEndLimit() const
{
return endLimit_;
}
void Fish::setEndLimit(QPoint endLimit)
{
endLimit_ = endLimit;
emit limitChanged();
}
int Fish::getIdentifier() const
{
return identifier_;
}
void Fish::setIdentifier(int identifier)
{
identifier_ = identifier;
emit identifierChanged();
qDebug() << QString("Set new identifier = " + QString::number(identifier));
}
QString Fish::getName() const
{
return name_;
}
void Fish::setName(const QString& name)
{
name_ = name;
emit nameChanged();
}
int Fish::getRandomNumber(int begin, int end) const
{
if (begin < 0)
begin = 0;
boost::random::uniform_int_distribution<> random_distribution(begin,end);
return random_distribution(rng_);
}
QPoint Fish::chooseNextDrowingDestination() const
{
if (lastMoves_.isEmpty())
return QPoint(endLimit_.x(),endLimit_.y());
QList<QPoint>::const_iterator iter = lastMoves_.end();
QPoint lastMove = *(--iter);
QPoint prevMove(lastMove);
if (lastMoves_.count() > 1)
prevMove = *(--iter);
if (dieStepsLeft_ == 0)
return QPoint(lastMove.x(),endLimit_.y());
int stepX = prevMove.x() - lastMove.x();
int deltaX = stepX/dieStepsLeft_;
int stepY = endLimit_.y() - lastMove.y();
int deltaY = stepY/dieStepsLeft_;
int newX = abs(lastMove.x() + deltaX);
if (newX > endLimit_.x())
newX = endLimit_.x();
int newY = abs(lastMove.y() + deltaY);
if (newY > endLimit_.y())
newY = endLimit_.y();
return QPoint(newX, newY);
}