-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathMothership.ts
More file actions
140 lines (110 loc) · 5.71 KB
/
Mothership.ts
File metadata and controls
140 lines (110 loc) · 5.71 KB
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
/*
DiepCustom - custom tank game server that shares diep.io's WebSocket protocol
Copyright (C) 2022 ABCxFF (github.com/ABCxFF)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>
*/
import { ClientInputs } from "../../Client";
import { tps } from "../../config";
import { Color, Tank, Stat, ColorsHexCode, ClientBound, TeamFlags } from "../../Const/Enums";
import GameServer from "../../Game";
import { ArenaState } from "../../Native/Arena";
import { CameraEntity } from "../../Native/Camera";
import { AI, AIState, Inputs } from "../AI";
import Live from "../Live";
import TankBody from "../Tank/TankBody";
import { TeamEntity } from "./TeamEntity";
const POSSESSION_TIMER = tps * 60 * 5;
/**
* Mothership Entity
*/
export default class Mothership extends TankBody {
/** The AI that controls how the Mothership aims. */
public ai: AI;
/** If the mothership's AI ever gets possessed, this is the tick that the possession started. */
public possessionStartTick: number = -1;
public constructor(game: GameServer) {
const inputs = new Inputs();
const camera = new CameraEntity(game);
super(game, camera, inputs);
this.relationsData.values.team = game.arena;
this.styleData.values.color = Color.Neutral;
this.ai = new AI(this, true);
this.ai.inputs = inputs;
this.ai.viewRange = 2000;
camera.cameraData.values.player = this;
this.setTank(Tank.Mothership);
camera.setLevel(140);
this.nameData.values.name = "Mothership"
this.scoreReward = 0;
camera.cameraData.values.player = this;
for (let i = Stat.MovementSpeed; i < Stat.HealthRegen; ++i) camera.setStat(i as Stat, 7);
camera.setStat(Stat.HealthRegen, 1);
this.healthData.values.health = this.healthData.values.maxHealth = 7000;
}
public onDeath(killer: Live): void {
if ((this.game.arena.state >= ArenaState.OVER)) return; // Do not send a defeat notification if the game has been won already
const team = this.relationsData.values.team;
const teamIsATeam = TeamEntity.isTeam(team);
const killerTeam = killer.relationsData.values.team;
const killerTeamIsATeam = TeamEntity.isTeam(killerTeam);
// UNCOMMENT TO ALLOW SOLO KILLS
// if (!killerTeamIsATeam) return;
this.game.broadcast()
.u8(ClientBound.Notification)
// If mothership has a team name, use it, otherwise just say has destroyed a mothership
.stringNT(`${killerTeamIsATeam ? (killerTeam.teamName || "a mysterious group") : (killer.nameData?.values.name || "an unnamed tank")} has destroyed ${teamIsATeam ? (team.teamName || "a mysterious group") + "'s" : "a"} Mothership!`)
.u32(killerTeamIsATeam ? ColorsHexCode[killerTeam.teamData.values.teamColor] : 0x000000)
.float(-1)
.stringNT("").send();
}
public delete(): void {
// No more mothership arrow - seems like in old diep this wasn't the case - we should probably keep though
if (this.relationsData.values.team?.teamData) this.relationsData.values.team.teamData.flags &= ~TeamFlags.hasMothership;
super.delete();
}
public tick(tick: number) {
if (!this.barrels.length) return super.tick(tick)
this.inputs = this.ai.inputs;
if (this.ai.state === AIState.idle) {
const angle = this.positionData.values.angle + this.ai.passiveRotation;
const mag = Math.sqrt((this.inputs.mouse.x - this.positionData.values.x) ** 2 + (this.inputs.mouse.y - this.positionData.values.y) ** 2);
this.inputs.mouse.set({
x: this.positionData.values.x + Math.cos(angle) * mag,
y: this.positionData.values.y + Math.sin(angle) * mag
});
} else if (this.ai.state === AIState.possessed && this.possessionStartTick === -1) {
this.possessionStartTick = tick;
}
if (this.possessionStartTick !== -1 && this.ai.state !== AIState.possessed) {
this.possessionStartTick = -1;
}
// after 10 minutes, kick out the person possessing
if (this.possessionStartTick !== -1) {
if (this.possessionStartTick !== -1 && this.ai.state !== AIState.possessed) {
this.possessionStartTick = -1;
} else if (this.inputs instanceof ClientInputs) {
if (tick - this.possessionStartTick >= POSSESSION_TIMER) {
this.inputs.deleted = true;
} else if (tick - this.possessionStartTick === Math.floor(POSSESSION_TIMER - 10 * tps)) {
this.inputs.client.notify("You only have 10 seconds left in control of the Mothership", ColorsHexCode[this.styleData.values.color], 5_000, "");
}
}
}
const team = this.relationsData.values.team;
if (team?.teamData) {
team.teamData.mothershipX = this.positionData.values.x;
team.teamData.mothershipY = this.positionData.values.y;
team.teamData.flags |= TeamFlags.hasMothership;
}
super.tick(tick);
}
}