Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Jochen Bauer
spn-relayserver
Commits
123a8afb
Commit
123a8afb
authored
Apr 11, 2018
by
Hubert Denkmair
Browse files
track bots
parent
9569a7ed
Changes
2
Hide whitespace changes
Inline
Side-by-side
relayserver/TcpProtocol.cpp
View file @
123a8afb
...
...
@@ -69,22 +69,108 @@ void TcpProtocol::OnMessageReceived(std::vector<char> &data)
break
;
case
0x01
:
std
::
cerr
<<
"world update"
<<
std
::
endl
;
{
if
(
arr
.
size
<
4
)
{
return
;
}
auto
&
bots
=
arr
.
ptr
[
2
].
via
.
array
;
auto
&
foods
=
arr
.
ptr
[
3
].
via
.
array
;
for
(
auto
&
item
:
bots
)
{
auto
&
bot_arr
=
item
.
via
.
array
;
auto
bot
=
Bot
{
bot_arr
.
ptr
[
0
].
via
.
u64
,
// guid
bot_arr
.
ptr
[
1
].
via
.
str
.
ptr
,
// name
bot_arr
.
ptr
[
2
].
via
.
f64
,
// segment_radius
};
bot
.
segments
.
reserve
(
100
);
for
(
auto
&
segment_item
:
bot_arr
.
ptr
[
3
].
via
.
array
)
{
auto
&
segment_arr
=
segment_item
.
via
.
array
;
if
(
segment_arr
.
size
<
2
)
{
continue
;
}
bot
.
segments
.
push_back
({
bot
.
guid
,
Vector2D
{
segment_arr
.
ptr
[
0
].
via
.
f64
,
segment_arr
.
ptr
[
1
].
via
.
f64
}
});
}
for
(
auto
&
color
:
bot_arr
.
ptr
[
4
].
via
.
array
)
{
bot
.
color
.
push_back
(
static_cast
<
uint32_t
>
(
color
.
via
.
u64
));
}
OnBotSpawnReceived
(
bot
);
}
for
(
auto
&
item
:
foods
)
{
auto
&
fi
=
item
.
via
.
array
;
if
(
fi
.
size
<
3
)
{
continue
;
}
OnFoodSpawnReceived
({
static_cast
<
guid_t
>
(
fi
.
ptr
[
0
].
via
.
i64
),
// guid
{
fi
.
ptr
[
1
].
via
.
f64
,
fi
.
ptr
[
2
].
via
.
f64
},
// position
fi
.
ptr
[
3
].
via
.
f64
// value
});
}
break
;
}
case
0x10
:
std
::
cerr
<<
"tick"
<<
std
::
endl
;
break
;
case
0x20
:
std
::
cerr
<<
"bot spawn"
<<
std
::
endl
;
{
if
(
arr
.
size
<
3
)
{
return
;
}
auto
&
bot_arr
=
arr
.
ptr
[
2
].
via
.
array
;
if
(
bot_arr
.
size
<
5
)
{
return
;
}
auto
bot
=
Bot
{
bot_arr
.
ptr
[
0
].
via
.
u64
,
// guid
bot_arr
.
ptr
[
1
].
via
.
str
.
ptr
,
// name
bot_arr
.
ptr
[
2
].
via
.
f64
,
// segment_radius
};
bot
.
segments
.
reserve
(
100
);
for
(
auto
&
segment_item
:
bot_arr
.
ptr
[
3
].
via
.
array
)
{
auto
&
segment_arr
=
segment_item
.
via
.
array
;
if
(
segment_arr
.
size
<
2
)
{
continue
;
}
bot
.
segments
.
push_back
({
bot
.
guid
,
Vector2D
{
segment_arr
.
ptr
[
0
].
via
.
f64
,
segment_arr
.
ptr
[
1
].
via
.
f64
}
});
}
for
(
auto
&
color
:
bot_arr
.
ptr
[
4
].
via
.
array
)
{
bot
.
color
.
push_back
(
static_cast
<
uint32_t
>
(
color
.
via
.
u64
));
}
OnBotSpawnReceived
(
bot
);
break
;
}
case
0x21
:
std
::
cerr
<<
"bot kill"
<<
std
::
endl
;
if
(
arr
.
size
<
4
)
{
return
;
}
OnBotKillReceived
(
arr
.
ptr
[
2
].
via
.
u64
,
arr
.
ptr
[
3
].
via
.
u64
);
break
;
case
0x22
:
std
::
cerr
<<
"bot move"
<<
std
::
endl
;
if
(
arr
.
size
<
3
)
{
return
;
}
for
(
auto
&
item
:
arr
.
ptr
[
2
].
via
.
array
)
{
auto
&
bi
=
item
.
via
.
array
;
if
(
bi
.
size
<
4
)
{
continue
;
}
guid_t
bot_id
=
bi
.
ptr
[
0
].
via
.
u64
;
std
::
vector
<
SnakeSegment
>
new_segments
;
for
(
auto
&
segment
:
bi
.
ptr
[
1
].
via
.
array
)
{
auto
&
seg_arr
=
segment
.
via
.
array
;
if
(
seg_arr
.
size
<
2
)
{
continue
;
}
new_segments
.
push_back
({
bot_id
,
Vector2D
{
seg_arr
.
ptr
[
0
].
via
.
f64
,
seg_arr
.
ptr
[
1
].
via
.
f64
}});
}
OnBotMoveReceived
(
bot_id
,
new_segments
,
bi
.
ptr
[
2
].
via
.
u64
,
bi
.
ptr
[
3
].
via
.
f64
);
}
break
;
case
0x30
:
...
...
@@ -127,6 +213,7 @@ void TcpProtocol::OnMessageReceived(std::vector<char> &data)
void
TcpProtocol
::
OnWorldInfoReceived
(
real_t
size_x
,
real_t
size_y
,
real_t
decay_rate
)
{
_segments
=
std
::
make_unique
<
SnakeSegmentMap
>
(
size_x
,
size_y
,
1000
);
_food
=
std
::
make_unique
<
FoodMap
>
(
size_x
,
size_y
,
1000
);
_foodDecayRate
=
decay_rate
;
}
...
...
@@ -139,10 +226,34 @@ void TcpProtocol::OnFoodSpawnReceived(const TcpProtocol::Food &food)
void
TcpProtocol
::
OnFoodConsumedReceived
(
guid_t
food_id
,
guid_t
bot_id
)
{
if
(
_food
==
nullptr
)
{
return
;
}
_food
->
erase_if
([
food_id
](
const
Food
&
food
)
{
return
food
.
guid
==
food_id
;
});
}
void
TcpProtocol
::
OnFoodDecayedReceived
(
guid_t
food_id
)
{
if
(
_food
==
nullptr
)
{
return
;
}
_food
->
erase_if
([
food_id
](
const
Food
&
food
)
{
return
food
.
guid
==
food_id
;
});
}
void
TcpProtocol
::
OnBotSpawnReceived
(
const
TcpProtocol
::
Bot
&
bot
)
{
_bots
.
push_back
(
bot
);
}
void
TcpProtocol
::
OnBotKillReceived
(
guid_t
killer_id
,
guid_t
victim_id
)
{
_bots
.
erase
(
std
::
remove_if
(
_bots
.
begin
(),
_bots
.
end
(),
[
victim_id
](
const
Bot
&
bot
)
{
return
bot
.
guid
==
victim_id
;
}));
}
void
TcpProtocol
::
OnBotMoveReceived
(
guid_t
bot_id
,
std
::
vector
<
TcpProtocol
::
SnakeSegment
>
&
new_segments
,
size_t
new_length
,
real_t
new_segment_radius
)
{
auto
it
=
std
::
find_if
(
_bots
.
begin
(),
_bots
.
end
(),
[
bot_id
](
const
Bot
&
bot
)
{
return
bot
.
guid
==
bot_id
;
});
if
(
it
==
_bots
.
end
())
{
return
;
}
auto
&
bot
=
*
it
;
bot
.
segments
.
insert
(
bot
.
segments
.
begin
(),
new_segments
.
begin
(),
new_segments
.
end
());
bot
.
segments
.
resize
(
new_length
);
bot
.
segment_radius
=
new_segment_radius
;
}
relayserver/TcpProtocol.h
View file @
123a8afb
...
...
@@ -30,6 +30,23 @@ class TcpProtocol
};
typedef
SpatialMap
<
Food
,
SPATIAL_MAP_TILES_X
,
SPATIAL_MAP_TILES_Y
>
FoodMap
;
struct
SnakeSegment
{
guid_t
bot_id
;
Vector2D
position
;
const
Vector2D
&
pos
()
const
{
return
position
;
}
};
typedef
SpatialMap
<
SnakeSegment
,
SPATIAL_MAP_TILES_X
,
SPATIAL_MAP_TILES_Y
>
SnakeSegmentMap
;
struct
Bot
{
guid_t
guid
;
std
::
string
name
;
real_t
segment_radius
;
std
::
vector
<
SnakeSegment
>
segments
;
std
::
vector
<
uint32_t
>
color
;
};
size_t
_awaitedSize
=
0
;
std
::
deque
<
char
>
_buf
;
MessageReceivedCallback
_messageReceivedCallback
;
...
...
@@ -37,10 +54,18 @@ class TcpProtocol
std
::
unique_ptr
<
FoodMap
>
_food
;
real_t
_foodDecayRate
=
0
;
std
::
vector
<
Bot
>
_bots
;
std
::
unique_ptr
<
SnakeSegmentMap
>
_segments
;
void
OnMessageReceived
(
std
::
vector
<
char
>&
data
);
void
OnWorldInfoReceived
(
real_t
size_x
,
real_t
size_y
,
real_t
decay_rate
);
void
OnFoodSpawnReceived
(
const
Food
&
food
);
void
OnFoodConsumedReceived
(
guid_t
food_id
,
guid_t
bot_id
);
void
OnFoodDecayedReceived
(
guid_t
food_id
);
void
OnBotSpawnReceived
(
const
Bot
&
bot
);
void
OnBotKillReceived
(
guid_t
killer_id
,
guid_t
victim_id
);
void
OnBotMoveReceived
(
guid_t
bot_id
,
std
::
vector
<
TcpProtocol
::
SnakeSegment
>
&
new_segments
,
size_t
new_length
,
real_t
new_segment_radius
);
};
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment