Skip to content

Commit

Permalink
Manual weapon reloading (close #40)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuviman committed Dec 3, 2019
1 parent 260efa9 commit e62fff6
Show file tree
Hide file tree
Showing 70 changed files with 505 additions and 185 deletions.
1 change: 1 addition & 0 deletions clients/cpp/MyStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ UnitAction MyStrategy::getAction(const Unit &unit, const Game &game,
action.jumpDown = !action.jump;
action.aim = aim;
action.shoot = true;
action.reload = false;
action.swapWeapon = false;
action.plantMine = false;
return action;
Expand Down
2 changes: 1 addition & 1 deletion clients/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Runner {
myStrategy.getAction(unit, playerView->game, debug)));
}
}
PlayerMessageGame::ActionMessage(actions).writeTo(*outputStream);
PlayerMessageGame::ActionMessage(Versioned(actions)).writeTo(*outputStream);
outputStream->flush();
}
}
Expand Down
21 changes: 4 additions & 17 deletions clients/cpp/model/PlayerMessageGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,19 @@ std::string PlayerMessageGame::CustomDataMessage::toString() const {
}

PlayerMessageGame::ActionMessage::ActionMessage() { }
PlayerMessageGame::ActionMessage::ActionMessage(std::unordered_map<int, UnitAction> action) : action(action) { }
PlayerMessageGame::ActionMessage::ActionMessage(Versioned action) : action(action) { }
PlayerMessageGame::ActionMessage PlayerMessageGame::ActionMessage::readFrom(InputStream& stream) {
PlayerMessageGame::ActionMessage result;
size_t actionSize = stream.readInt();
result.action = std::unordered_map<int, UnitAction>();
result.action.reserve(actionSize);
for (size_t i = 0; i < actionSize; i++) {
int actionKey;
actionKey = stream.readInt();
UnitAction actionValue;
actionValue = UnitAction::readFrom(stream);
result.action.emplace(std::make_pair(actionKey, actionValue));
}
result.action = Versioned::readFrom(stream);
return result;
}
void PlayerMessageGame::ActionMessage::writeTo(OutputStream& stream) const {
stream.write(TAG);
stream.write((int)(action.size()));
for (const auto& actionEntry : action) {
stream.write(actionEntry.first);
actionEntry.second.writeTo(stream);
}
action.writeTo(stream);
}
std::string PlayerMessageGame::ActionMessage::toString() const {
return std::string("PlayerMessageGame::ActionMessage") + "(" +
"TODO" +
action.toString() +
")";
}
std::shared_ptr<PlayerMessageGame> PlayerMessageGame::readFrom(InputStream& stream) {
Expand Down
6 changes: 4 additions & 2 deletions clients/cpp/model/PlayerMessageGame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "TextAlignment.hpp"
#include <stdexcept>
#include "ColorFloat.hpp"
#include <stdexcept>
#include "Versioned.hpp"
#include <unordered_map>
#include <stdexcept>
#include "UnitAction.hpp"
Expand Down Expand Up @@ -64,9 +66,9 @@ class PlayerMessageGame::ActionMessage : public PlayerMessageGame {
public:
static const int TAG = 1;
public:
std::unordered_map<int, UnitAction> action;
Versioned action;
ActionMessage();
ActionMessage(std::unordered_map<int, UnitAction> action);
ActionMessage(Versioned action);
static ActionMessage readFrom(InputStream& stream);
void writeTo(OutputStream& stream) const;
std::string toString() const override;
Expand Down
5 changes: 4 additions & 1 deletion clients/cpp/model/UnitAction.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include "UnitAction.hpp"

UnitAction::UnitAction() { }
UnitAction::UnitAction(double velocity, bool jump, bool jumpDown, Vec2Double aim, bool shoot, bool swapWeapon, bool plantMine) : velocity(velocity), jump(jump), jumpDown(jumpDown), aim(aim), shoot(shoot), swapWeapon(swapWeapon), plantMine(plantMine) { }
UnitAction::UnitAction(double velocity, bool jump, bool jumpDown, Vec2Double aim, bool shoot, bool reload, bool swapWeapon, bool plantMine) : velocity(velocity), jump(jump), jumpDown(jumpDown), aim(aim), shoot(shoot), reload(reload), swapWeapon(swapWeapon), plantMine(plantMine) { }
UnitAction UnitAction::readFrom(InputStream& stream) {
UnitAction result;
result.velocity = stream.readDouble();
result.jump = stream.readBool();
result.jumpDown = stream.readBool();
result.aim = Vec2Double::readFrom(stream);
result.shoot = stream.readBool();
result.reload = stream.readBool();
result.swapWeapon = stream.readBool();
result.plantMine = stream.readBool();
return result;
Expand All @@ -19,6 +20,7 @@ void UnitAction::writeTo(OutputStream& stream) const {
stream.write(jumpDown);
aim.writeTo(stream);
stream.write(shoot);
stream.write(reload);
stream.write(swapWeapon);
stream.write(plantMine);
}
Expand All @@ -29,6 +31,7 @@ std::string UnitAction::toString() const {
(jumpDown ? "true" : "false") +
aim.toString() +
(shoot ? "true" : "false") +
(reload ? "true" : "false") +
(swapWeapon ? "true" : "false") +
(plantMine ? "true" : "false") +
")";
Expand Down
3 changes: 2 additions & 1 deletion clients/cpp/model/UnitAction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ class UnitAction {
bool jumpDown;
Vec2Double aim;
bool shoot;
bool reload;
bool swapWeapon;
bool plantMine;
UnitAction();
UnitAction(double velocity, bool jump, bool jumpDown, Vec2Double aim, bool shoot, bool swapWeapon, bool plantMine);
UnitAction(double velocity, bool jump, bool jumpDown, Vec2Double aim, bool shoot, bool reload, bool swapWeapon, bool plantMine);
static UnitAction readFrom(InputStream& stream);
void writeTo(OutputStream& stream) const;
std::string toString() const;
Expand Down
31 changes: 31 additions & 0 deletions clients/cpp/model/Versioned.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "Versioned.hpp"

Versioned::Versioned() { }
Versioned::Versioned(std::unordered_map<int, UnitAction> inner) : inner(inner) { }
Versioned Versioned::readFrom(InputStream& stream) {
Versioned result;
size_t innerSize = stream.readInt();
result.inner = std::unordered_map<int, UnitAction>();
result.inner.reserve(innerSize);
for (size_t i = 0; i < innerSize; i++) {
int innerKey;
innerKey = stream.readInt();
UnitAction innerValue;
innerValue = UnitAction::readFrom(stream);
result.inner.emplace(std::make_pair(innerKey, innerValue));
}
return result;
}
void Versioned::writeTo(OutputStream& stream) const {
stream.write(43981);
stream.write((int)(inner.size()));
for (const auto& innerEntry : inner) {
stream.write(innerEntry.first);
innerEntry.second.writeTo(stream);
}
}
std::string Versioned::toString() const {
return std::string("Versioned") + "(" +
"TODO" +
")";
}
22 changes: 22 additions & 0 deletions clients/cpp/model/Versioned.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef _MODEL_VERSIONED_HPP_
#define _MODEL_VERSIONED_HPP_

#include "../Stream.hpp"
#include <string>
#include <unordered_map>
#include <stdexcept>
#include "UnitAction.hpp"
#include <stdexcept>
#include "Vec2Double.hpp"

class Versioned {
public:
std::unordered_map<int, UnitAction> inner;
Versioned();
Versioned(std::unordered_map<int, UnitAction> inner);
static Versioned readFrom(InputStream& stream);
void writeTo(OutputStream& stream) const;
std::string toString() const;
};

#endif
24 changes: 4 additions & 20 deletions clients/csharp/Model/PlayerMessageGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,38 +41,22 @@ public override void WriteTo(System.IO.BinaryWriter writer)
public class ActionMessage : PlayerMessageGame
{
public const int TAG = 1;
public System.Collections.Generic.IDictionary<int, Model.UnitAction> Action { get; set; }
public Model.Versioned Action { get; set; }
public ActionMessage() {}
public ActionMessage(System.Collections.Generic.IDictionary<int, Model.UnitAction> action)
public ActionMessage(Model.Versioned action)
{
this.Action = action;
}
public static new ActionMessage ReadFrom(System.IO.BinaryReader reader)
{
var result = new ActionMessage();
int ActionSize = reader.ReadInt32();
result.Action = new System.Collections.Generic.Dictionary<int, Model.UnitAction>(ActionSize);
for (int i = 0; i < ActionSize; i++)
{
int ActionKey;
ActionKey = reader.ReadInt32();
Model.UnitAction ActionValue;
ActionValue = Model.UnitAction.ReadFrom(reader);
result.Action.Add(ActionKey, ActionValue);
}
result.Action = Model.Versioned.ReadFrom(reader);
return result;
}
public override void WriteTo(System.IO.BinaryWriter writer)
{
writer.Write(TAG);
writer.Write(Action.Count);
foreach (var ActionEntry in Action)
{
var ActionKey = ActionEntry.Key;
var ActionValue = ActionEntry.Value;
writer.Write(ActionKey);
ActionValue.WriteTo(writer);
}
Action.WriteTo(writer);
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion clients/csharp/Model/UnitAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ public struct UnitAction
public bool JumpDown { get; set; }
public Model.Vec2Double Aim { get; set; }
public bool Shoot { get; set; }
public bool Reload { get; set; }
public bool SwapWeapon { get; set; }
public bool PlantMine { get; set; }
public UnitAction(double velocity, bool jump, bool jumpDown, Model.Vec2Double aim, bool shoot, bool swapWeapon, bool plantMine)
public UnitAction(double velocity, bool jump, bool jumpDown, Model.Vec2Double aim, bool shoot, bool reload, bool swapWeapon, bool plantMine)
{
this.Velocity = velocity;
this.Jump = jump;
this.JumpDown = jumpDown;
this.Aim = aim;
this.Shoot = shoot;
this.Reload = reload;
this.SwapWeapon = swapWeapon;
this.PlantMine = plantMine;
}
Expand All @@ -27,6 +29,7 @@ public static UnitAction ReadFrom(System.IO.BinaryReader reader)
result.JumpDown = reader.ReadBoolean();
result.Aim = Model.Vec2Double.ReadFrom(reader);
result.Shoot = reader.ReadBoolean();
result.Reload = reader.ReadBoolean();
result.SwapWeapon = reader.ReadBoolean();
result.PlantMine = reader.ReadBoolean();
return result;
Expand All @@ -38,6 +41,7 @@ public void WriteTo(System.IO.BinaryWriter writer)
writer.Write(JumpDown);
Aim.WriteTo(writer);
writer.Write(Shoot);
writer.Write(Reload);
writer.Write(SwapWeapon);
writer.Write(PlantMine);
}
Expand Down
38 changes: 38 additions & 0 deletions clients/csharp/Model/Versioned.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace AiCup2019.Model
{
public struct Versioned
{
public System.Collections.Generic.IDictionary<int, Model.UnitAction> Inner { get; set; }
public Versioned(System.Collections.Generic.IDictionary<int, Model.UnitAction> inner)
{
this.Inner = inner;
}
public static Versioned ReadFrom(System.IO.BinaryReader reader)
{
var result = new Versioned();
int InnerSize = reader.ReadInt32();
result.Inner = new System.Collections.Generic.Dictionary<int, Model.UnitAction>(InnerSize);
for (int i = 0; i < InnerSize; i++)
{
int InnerKey;
InnerKey = reader.ReadInt32();
Model.UnitAction InnerValue;
InnerValue = Model.UnitAction.ReadFrom(reader);
result.Inner.Add(InnerKey, InnerValue);
}
return result;
}
public void WriteTo(System.IO.BinaryWriter writer)
{
writer.Write(43981);
writer.Write(Inner.Count);
foreach (var InnerEntry in Inner)
{
var InnerKey = InnerEntry.Key;
var InnerValue = InnerEntry.Value;
writer.Write(InnerKey);
InnerValue.WriteTo(writer);
}
}
}
}
1 change: 1 addition & 0 deletions clients/csharp/MyStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public UnitAction GetAction(Unit unit, Game game, Debug debug)
action.JumpDown = !jump;
action.Aim = aim;
action.Shoot = true;
action.Reload = false;
action.SwapWeapon = false;
action.PlantMine = false;
return action;
Expand Down
2 changes: 1 addition & 1 deletion clients/csharp/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void Run()
actions.Add(unit.Id, myStrategy.GetAction(unit, playerView.Game, debug));
}
}
new Model.PlayerMessageGame.ActionMessage(actions).WriteTo(writer);
new Model.PlayerMessageGame.ActionMessage(new Model.Versioned(actions)).WriteTo(writer);
writer.Flush();
}
}
Expand Down
2 changes: 1 addition & 1 deletion clients/dlang/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Runner {
actions[unit.id] = myStrategy.getAction(unit, playerView.game, debugger);
}
}
new PlayerMessageGame.ActionMessage(actions).writeTo(stream);
new PlayerMessageGame.ActionMessage(Versioned(actions)).writeTo(stream);
stream.flush();
}
}
Expand Down
1 change: 1 addition & 0 deletions clients/dlang/source/model/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public import text_alignment;
public import custom_data;
public import vec2_double;
public import unit_action;
public import versioned;
public import player_message_game;
public import weapon_type;
public import bullet_parameters;
Expand Down
20 changes: 4 additions & 16 deletions clients/dlang/source/model/player_message_game.d
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,19 @@ abstract class PlayerMessageGame {

static class ActionMessage : PlayerMessageGame {
static const int TAG = 1;
UnitAction[int] action;
Versioned action;
this() {}
this(UnitAction[int] action) {
this(Versioned action) {
this.action = action;
}
static ActionMessage readFrom(Stream reader) {
auto result = new ActionMessage();
int actionSize = reader.readInt();
result.action.clear();
for (int i = 0; i < actionSize; i++) {
int actionKey;
actionKey = reader.readInt();
UnitAction actionValue;
actionValue = UnitAction.readFrom(reader);
result.action[actionKey] = actionValue;
}
result.action = Versioned.readFrom(reader);
return result;
}
override void writeTo(Stream writer) const {
writer.write(TAG);
writer.write(cast(int)(action.length));
foreach (actionKey, actionValue; action) {
writer.write(actionKey);
actionValue.writeTo(writer);
}
action.writeTo(writer);
}
override string toString() const {
return "ActionMessage" ~ "(" ~
Expand Down
Loading

0 comments on commit e62fff6

Please sign in to comment.