Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
Merge branch 'dev' of github.com:Panxuc/EDC25_Team into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
DuGangfeng committed Dec 15, 2023
2 parents fd5090a + 75bce3c commit f221df9
Show file tree
Hide file tree
Showing 6 changed files with 466 additions and 40 deletions.
153 changes: 153 additions & 0 deletions contest/arduino/asyncapi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# Slave API 0.1.0 documentation

## Table of Contents

In a packet, the first 5 bytes are the header and the later bytes are the data.

| Byte | Description |
| -------- | ---------------------------------------------------- |
| 0 | Always 0x55 |
| 1 | Always 0xAA |
| 2 to 3 | Short. The number of bytes of the data (N) |
| 4 | Byte. The checksum of the data |
| 5 to N+4 | The data of the packet. N is the length of the data. |

* [Operations](#operations)
* [PUB main](#pub-main-operation)
* [SUB main](#sub-main-operation)

## Operations

### PUB `main` Operation

#### Message `<anonymous-message-1>`

##### Payload

| Name | Type | Description | Value | Constraints | Notes |
| ---------- | ------- | ---------------------------------------------------------------------- | ----------------------- | --------------- | ----------------------------------------- |
| (root) | object | - | - | - | **additional properties are NOT allowed** |
| actionType | integer | The action type. 0: ATTACK 1: PLACE_BLOCK 2: TRADE | allowed (`0`, `1`, `2`) | format (`int8`) | - |
| param | integer | The parameter. Chunk id for ATTACK and PLACE_BLOCK. Item id for TRADE. | - | format (`int8`) | - |

> Examples of payload _(generated)_
```json
{
"actionType": 0,
"param": 0
}
```

### SUB `main` Operation

#### Message `<anonymous-message-2>`

##### Payload

| Name | Type | Description | Value | Constraints | Notes |
| ---------------------------- | -------------- | ---------------------------------------------------------- | ----- | ---------------- | ----------------------------------------- |
| (root) | object | - | - | - | **additional properties are NOT allowed** |
| 0gameStage | integer | The game stage 0: READY 1: RUNNING 2: BATTLING 3: FINISHED | - | format (`int8`) | - |
| 1elapsedTicks | integer | The elapsed ticks. | - | format (`int32`) | - |
| 5heightOfChunks | array<integer> | - | - | 64 items | - |
| heightOfChunks (single item) | integer | The height of chunks. | - | format (`int8`) | - |
| 69hasBed | boolean | Whether the player has bed. | - | - | - |
| 70hasBedOpponent | boolean | Whether the opponent has bed. | - | - | - |
| 71positionX | number | The x coordinate. | - | format (`float`) | - |
| 75positionY | number | The y coordinate. | - | format (`float`) | - |
| 79positionOpponentX | number | The x coordinate of the opponent. | - | format (`float`) | - |
| 83positionOpponentY | number | The y coordinate of the opponent. | - | format (`float`) | - |
| 87agility | integer | The agility point. | - | format (`int8`) | - |
| 88health | integer | The health point. | - | format (`int8`) | - |
| 89maxHealth | integer | The max health point. | - | format (`int8`) | - |
| 90strength | integer | The strength point. | - | format (`int8`) | - |
| 91emeraldCount | integer | The emerald count. | - | format (`int8`) | - |
| 92woolCount | integer | The wool count. | - | format (`int8`) | - |

> Examples of payload _(generated)_
```json
{
"gameStage": 0,
"elapsedTicks": 0,
"heightOfChunks": [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
"hasBed": true,
"hasBedOpponent": true,
"positionX": 0,
"positionY": 0,
"positionOpponentX": 0,
"positionOpponentY": 0,
"agility": 0,
"health": 0,
"maxHealth": 0,
"strength": 0,
"emeraldCount": 0,
"woolCount": 0
}
```
95 changes: 79 additions & 16 deletions contest/arduino/include/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,41 @@
#include <queue>
#include <vector>

#define DEFAULT_SPEED 255

enum GameStage { READY, RUNNING, BATTLING, FINISHED };

enum Item {
AGILITY_BOOST,
HEALTH_BOOST,
STRENGTH_BOOST,
WOOL,
POTION_OF_HEALING
};

struct Position {
float_t x;
float_t y;
Position() {}
Position(float_t x, float_t y) : x(x), y(y) {}
Position(const Position &position) : x(position.x), y(position.y) {}
Position &operator=(const Position &position) {
x = position.x + 0.5;
y = position.y + 0.5;
return *this;
}
Position operator+(const Position &position) const {
return Position(x + position.x, y + position.y);
}
Position operator-(const Position &position) const {
return Position(x - position.x, y - position.y);
}
bool operator==(const Position &position) const {
return x == position.x && y == position.y;
}
bool operator!=(const Position &position) const {
return x != position.x || y != position.y;
}
};

struct Grid {
Expand Down Expand Up @@ -42,7 +72,13 @@ struct Grid {
return Grid(x - grid.x, y - grid.y);
}
bool operator==(const Grid &grid) const { return x == grid.x && y == grid.y; }
bool operator==(const Position &position) const {
return x == (int8_t)position.x && y == (int8_t)position.y;
}
bool operator!=(const Grid &grid) const { return x != grid.x || y != grid.y; }
bool operator!=(const Position &position) const {
return x != (int8_t)position.x || y != (int8_t)position.y;
}
};

struct PlayerInfo {
Expand All @@ -67,6 +103,8 @@ struct MapInfo {
std::vector<Position> diamondMine; // 钻石矿石
};

enum PlayerState { IDLE, COLLECTING, ATTACKING, FLEEING };

class Player {
public:
Player(); // 构造函数
Expand All @@ -79,35 +117,60 @@ class Player {

void attack(uint8_t chunk); // 攻击
void placeBlock(uint8_t chunk); // 放置方块
void trade(uint8_t item); // 交易
void trade(Item item); // 交易

std::vector<Grid> AStar(Grid src, Grid dst); // A*寻路
std::vector<Grid> BFS(Grid src, Grid dst); // BFS寻路

double calculateAngle(Position src, Position dst); // 计算角度
double calculateAngle(Position src, Grid dst); // 计算角度
int8_t calculateDistance(Position src, Position dst); // 计算距离
int8_t calculateDistance(Position src, Grid dst); // 计算距离

std::vector<Grid> AStar(Grid src, Grid dst);
std::vector<Grid> BFS(Grid src, Grid dst);
void turnLeft(double angle, int speed);
void turnRight(double angle, int speed);
void turnLeft(double angle, int speed); // 左转
void turnRight(double angle, int speed); // 右转
void faceTo(Grid dst, int speed); // 面向目标
void moveTo(Grid dst, int speed); // 移动到目标

PlayerInfo getPlayerInfo(void); // 获取玩家信息
MapInfo getMapInfo(void); // 获取地图信息
PlayerInfo getPlayerInfo(void); // 获取玩家信息
MapInfo getMapInfo(void); // 获取地图信息
PlayerState getPlayerState(void); // 获取玩家状态
double_t getDirectionFix(void); // 获取方向修正
Grid getHome(void); // 获取家的位置
int32_t getLastUpdateTicks(void); // 获取上次更新的tick数
int32_t getLastAttackTicks(void); // 获取上次攻击的tick数
int8_t getDesiredEmeraldCount(void); // 获取期望的绿宝石数量
double_t getAttackCooldown(void); // 获取攻击冷却

void setPlayerInfo(PlayerInfo playerInfo); // 设置玩家信息
void setMapInfo(MapInfo mapInfo); // 设置地图信息
void setPlayerState(PlayerState playerState); // 设置玩家状态
void setDirectionFix(double_t directionFix); // 设置方向修正
void setHome(Grid home); // 设置家的位置
void setCanMVK210(CanMVK210 *canmvk210); // 设置CanMV K210
void setJY62(JY62 *jy62); // 设置IMU
void setPID(PID *pid); // 设置PID
void setTB6612FNG(TB6612FNG *tb6612fng); // 设置电机驱动
void setZigbee(Zigbee *zigbee); // 设置Zigbee
void setLastUpdateTicks(int32_t lastUpdateTicks); // 设置上次更新的tick数
void
setDesiredEmeraldCount(int8_t desiredEmeraldCount); // 设置期望的绿宝石数量

private:
PlayerInfo _playerInfo; // 玩家信息
MapInfo _mapInfo; // 地图信息
double_t _directionFix; // 方向修正
CanMVK210 *_canmvk210; // CanMV K210
JY62 *_jy62; // IMU
PID *_pid; // PID
TB6612FNG *_tb6612fng; // 电机驱动
Zigbee *_zigbee; // Zigbee
int32_t _lastUpdateTicks; // 上次更新的tick数
PlayerInfo _playerInfo; // 玩家信息
MapInfo _mapInfo; // 地图信息
PlayerState _playerState; // 玩家状态
double_t _directionFix; // 方向修正
Grid _home; // 家的位置
CanMVK210 *_canmvk210; // CanMV K210
JY62 *_jy62; // IMU
PID *_pid; // PID
TB6612FNG *_tb6612fng; // 电机驱动
Zigbee *_zigbee; // Zigbee
int32_t _lastUpdateTicks; // 上次更新的tick数
int32_t _lastAttackTicks; // 上次攻击的tick数
int8_t _desiredEmeraldCount; // 期望的绿宝石数量
double_t _attackCooldown; // 攻击冷却
};

#endif // PLAYER_H
Loading

0 comments on commit f221df9

Please sign in to comment.