18constexpr uint32_t kWorldSeed = 1;
20constexpr float kFov = 45.0f;
25float Smooth(
float s) {
return s * s * (3.0f - 2.0f * s); }
28float Eased(
float s,
float ease) {
29 s = std::clamp(s, 0.0f, 1.0f);
30 return s + ease * (Smooth(s) - s);
35Vector3 CatmullRom(Vector3 a, Vector3 b, Vector3 c, Vector3 d,
float u) {
36 const float u2 = u * u;
37 const float u3 = u2 * u;
38 const auto term = [&](
float pa,
float pb,
float pc,
float pd) {
39 return 0.5f * (2.0f * pb + (pc - pa) * u +
40 (2.0f * pa - 5.0f * pb + 4.0f * pc - pd) * u2 +
41 (3.0f * pb - pa - 3.0f * pc + pd) * u3);
43 return {term(a.x, b.x, c.x, d.x), term(a.y, b.y, c.y, d.y),
44 term(a.z, b.z, c.z, d.z)};
47float Radians(
float degrees) {
return degrees * DEG2RAD; }
49Camera3D Lens(Vector3 eye, Vector3 look,
float fov) {
50 return {.position = eye,
52 .up = {0.0f, 1.0f, 0.0f},
54 .projection = CAMERA_PERSPECTIVE};
57Camera3D Along(
const google::protobuf::RepeatedPtrField<Key>& keys,
58 float seconds,
float ease,
const Terrain& terrain) {
60 std::vector<Vector3> eyes, looks;
61 std::vector<float> fovs, times;
62 for (
const Key& key : keys) {
63 eyes.push_back(
Place(key.eye(), terrain));
64 looks.push_back(key.has_look() ?
Place(key.look(), terrain)
65 : looks.empty() ? Vector3Add(eyes.back(), {0, 0, 1})
67 fovs.push_back(key.fov() > 0.0f ? key.fov()
70 times.push_back(key.at());
72 const int n =
static_cast<int>(keys.size());
74 return Lens(eyes[0], looks[0], fovs[0]);
78 const float first = times.front();
79 const float span = std::max(times.back() - first, 1e-3f);
80 const float t = first + span *
Eased((seconds - first) / span, ease);
82 while (i < n - 2 && t > times[i + 1]) {
85 const float u = std::clamp(
86 (t - times[i]) / std::max(times[i + 1] - times[i], 1e-3f), 0.0f, 1.0f);
87 const auto curve = [&](
const std::vector<Vector3>& p) {
88 return CatmullRom(p[std::max(i - 1, 0)], p[i], p[i + 1],
89 p[std::min(i + 2, n - 1)], u);
91 return Lens(curve(eyes), curve(looks), Lerp(fovs[i], fovs[i + 1], u));
94Camera3D Round(
const Orbit& orbit,
float share,
const Terrain& terrain) {
95 const Vector3 center =
Place(orbit.center(), terrain);
96 const float angle = Radians(Lerp(orbit.from(), orbit.to(), share));
97 const Vector3 eye = {center.x + orbit.radius() * std::sin(angle),
98 center.y + orbit.up(),
99 center.z + orbit.radius() * std::cos(angle)};
100 return Lens(eye, center, orbit.fov() > 0.0f ? orbit.fov() : kFov);
108 protocol::Motion motion = protocol::MOTION_IDLE;
111Spot Walked(
const Point& start,
float yaw,
112 const google::protobuf::RepeatedPtrField<Leg>& legs,
float game) {
113 Spot spot{.at = {start.x(), start.z()}, .yaw = yaw};
117 for (
const Leg& leg : legs) {
118 const float off = std::max(leg.at(), free);
122 const Vector2 to = {leg.to().x(), leg.to().z()};
123 const Vector2 way = Vector2Subtract(to, spot.at);
124 const float distance = Vector2Length(way);
126 const float arrive = off + distance / speed;
127 if (distance > 1e-3f) {
128 spot.yaw = std::atan2(way.x, way.y);
131 spot.at = Vector2Lerp(spot.at, to, (game - off) / (arrive - off));
132 spot.motion = leg.run() ? protocol::MOTION_RUN : protocol::MOTION_WALK;
143absl::StatusOr<Film>
ReadFilm(
const std::string& path) {
144 absl::StatusOr<std::string> text =
ReadFile(path);
146 return text.status();
149 if (absl::Status parsed =
ParseText(*text, film); !parsed.ok()) {
150 return absl::InvalidArgumentError(
151 absl::StrCat(path,
": ", parsed.message()));
153 for (
const Shot& shot : film.shots()) {
154 if (!
IsLand(shot.land())) {
155 return absl::InvalidArgumentError(
156 absl::StrCat(shot.id(),
": no land called ", shot.land()));
158 if (shot.camera().empty() && !shot.has_orbit() && !shot.has_shoulder()) {
159 return absl::InvalidArgumentError(absl::StrCat(
160 shot.id(),
": no camera keys, no orbit and no shoulder"));
162 if (shot.has_shoulder() &&
163 std::ranges::none_of(shot.players(), [&](
const Player& p) {
164 return p.id() == shot.shoulder().player();
166 return absl::InvalidArgumentError(
167 absl::StrCat(shot.id(),
": no player ", shot.shoulder().player(),
168 " for the camera to look over"));
180float SpeedAt(
const Shot& shot,
float seconds) {
181 const auto& pace = shot.pace();
185 if (seconds <= pace[0].at()) {
186 return pace[0].speed();
188 for (
int i = 0; i + 1 < pace.size(); ++i) {
189 const Pace& from = pace[i];
190 const Pace& to = pace[i + 1];
191 if (seconds < to.at()) {
192 const float share = (seconds - from.at()) / (to.at() - from.at());
193 return Lerp(from.speed(), to.speed(), Smooth(share));
196 return pace[pace.size() - 1].speed();
201 constexpr int kSteps = 16;
202 const float step = seconds / kSteps;
204 for (
int i = 0; i < kSteps; ++i) {
205 moved += step *
SpeedAt(shot_, film_ + (i + 0.5f) * step);
215 return {point.x(), base + point.up(), point.z()};
221const Player* Watcher(
const Shot& shot) {
222 for (
const Player& player : shot.players()) {
223 if (player.id() == shot.shoulder().player()) {
230Camera3D OverShoulder(
const Shot& shot,
float share,
bool tall,
231 const Terrain& terrain) {
232 const Shoulder& shoulder = shot.shoulder();
233 const Player* watcher = Watcher(shot);
235 watcher !=
nullptr ?
Place(watcher->at(), terrain) : Vector3{};
236 const Vector3 look =
Place(shoulder.look(), terrain);
237 const Vector3 forward =
238 Vector3Normalize({look.x - feet.x, 0.0f, look.z - feet.z});
240 const Vector3 right = {-forward.z, 0.0f, forward.x};
241 const float back = Lerp(shoulder.back(), shoulder.back_end(), share);
242 const Vector3 eye = {feet.x - forward.x * back + right.x * shoulder.side(),
243 feet.y + shoulder.up(),
244 feet.z - forward.z * back + right.z * shoulder.side()};
245 const float fov = tall && shoulder.tall_fov() > 0.0f ? shoulder.tall_fov()
246 : shoulder.fov() > 0.0f ? shoulder.fov()
248 return Lens(eye, look, fov);
253Camera3D
CameraAt(
const Shot& shot,
float seconds,
bool tall,
255 const float share =
Eased(seconds / shot.seconds(), shot.ease());
256 if (shot.has_shoulder()) {
257 return OverShoulder(shot, share, tall, terrain);
259 if (tall && !shot.tall().empty()) {
260 return Along(shot.tall(), seconds, shot.ease(), terrain);
262 if (tall && shot.has_tall_orbit()) {
263 return Round(shot.tall_orbit(), share, terrain);
265 if (!shot.camera().empty()) {
266 return Along(shot.camera(), seconds, shot.ease(), terrain);
268 return Round(shot.orbit(), share, terrain);
272 : shot_(shot), content_(content) {
273 for (
const Monster& monster : shot.monsters()) {
274 foes_.push_back({.spec = monster,
275 .at = {monster.at().x(), monster.at().z()},
276 .yaw = Radians(monster.yaw()),
277 .hits = std::max(monster.hits(), 1u)});
281 constexpr float kGolden = 2.39996f;
282 for (
const Pack& pack : shot.packs()) {
283 for (uint32_t i = 0; i < pack.count(); ++i) {
284 const float out = pack.radius() * std::sqrt((i + 0.5f) / pack.count());
285 const float turn = kGolden * static_cast<float>(i);
287 monster.set_id(pack.first() + i);
288 monster.set_kind(pack.kind());
289 monster.mutable_at()->set_x(pack.center().x() + out * std::sin(turn));
290 monster.mutable_at()->set_z(pack.center().z() + out * std::cos(turn));
291 monster.set_face(pack.face());
292 monster.set_yaw(pack.yaw());
293 monster.set_charge(pack.charge());
295 monster.set_charge_at(pack.charge_at() + 0.04f * static_cast<float>(i));
296 monster.set_hits(pack.hits());
297 monster.set_level(pack.level());
298 if (pack.dies() > 0.0f) {
299 monster.set_dies(pack.dies() + 0.13f * (i % 7));
301 foes_.push_back({.spec = monster,
302 .at = {monster.at().x(), monster.at().z()},
303 .yaw = Radians(pack.yaw()),
304 .hits = std::max(monster.hits(), 1u)});
309Stage::Blow Stage::BlowOf(
const Player& player,
const Cast& cast)
const {
310 const auto skill = content_.skills.by_id.find(cast.skill());
311 if (skill == content_.skills.by_id.end()) {
314 const skills::Effect& effect = skill->second.effect();
315 if (effect.weapon()) {
316 const auto item = content_.items.by_id.find(player.weapon());
317 if (item == content_.items.by_id.end()) {
320 const items::Weapon& weapon = item->second.weapon();
321 return {.lasts = weapon.swing_ms() / 1000.0f,
322 .lands = weapon.hit_ms() / 1000.0f,
323 .reach = weapon.reach()};
325 return {.lasts = effect.cast_ms() / 1000.0f,
326 .lands = effect.hit_ms() / 1000.0f,
327 .reach = effect.reach(),
329 .push = effect.push(),
330 .arc = effect.arc()};
333Vector2 Stage::PlayerAt(
int player,
float game)
const {
334 const Player& who = shot_.players(player);
335 return Walked(who.at(), Radians(who.yaw()), who.legs(), game).at;
338void Stage::Land(
int player,
const Cast& cast,
float game) {
339 const Player& who = shot_.players(player);
340 const Blow blow = BlowOf(who, cast);
341 if (blow.reach <= 0.0f) {
344 const Vector2 from = PlayerAt(player, game);
345 const auto alive = [](
const Foe& foe) {
return !foe.fallen; };
346 std::vector<Foe*> hit;
349 float facing = Radians(who.yaw());
350 for (
const Foe& foe : foes_) {
351 if (foe.spec.id() == cast.target()) {
352 facing = std::atan2(foe.at.x - from.x, foe.at.y - from.y);
355 for (Foe& foe : foes_) {
356 const Vector2 way = Vector2Subtract(foe.at, from);
357 if (!alive(foe) || Vector2Length(way) > blow.reach) {
361 std::remainder(std::atan2(way.x, way.y) - facing, 2.0f * PI);
362 if (blow.arc <= 0.0f || std::abs(off) <= Radians(blow.arc) / 2) {
369 Foe* target =
nullptr;
370 Foe* nearest =
nullptr;
371 float best = blow.reach + 1.0f;
372 for (Foe& foe : foes_) {
376 const float d = Vector2Distance(foe.at, from);
377 if (foe.spec.id() == cast.target() && d <= blow.reach + 2.0f) {
385 if (Foe* struck = target !=
nullptr ? target : nearest) {
386 hit.push_back(struck);
389 for (Foe* foe : hit) {
390 foe->fallen = --foe->hits == 0;
391 if (blow.push > 0.0f) {
392 const Vector2 away = Vector2Normalize(Vector2Subtract(foe->at, from));
393 foe->pushed_from = foe->at;
394 foe->pushed_to = Vector2Add(foe->at, Vector2Scale(away, blow.push));
395 foe->pushed_at = game;
400void Stage::Step(
float from,
float to) {
402 for (
int p = 0; p < shot_.players_size(); ++p) {
403 const Player& who = shot_.players(p);
404 for (
const Cast& cast : who.casts()) {
405 const float lands = cast.at() + BlowOf(who, cast).lands;
406 if (lands > from && lands <= to) {
407 Land(p, cast, lands);
412 const float seconds = to - from;
413 for (Foe& foe : foes_) {
414 const Monster& spec = foe.spec;
415 if (spec.dies() > 0.0f && to >= spec.dies()) {
418 foe.
motion = protocol::MOTION_IDLE;
419 if (foe.pushed_at >= 0.0f && to < foe.pushed_at +
kPushTime) {
420 const float share = (to - foe.pushed_at) /
kPushTime;
421 foe.at = Vector2Lerp(foe.pushed_from, foe.pushed_to,
422 1.0f - (1.0f - share) * (1.0f - share));
428 if (!spec.legs().empty()) {
429 const Spot spot = Walked(spec.at(), Radians(spec.yaw()), spec.legs(), to);
430 foe.at = spot.at, foe.yaw = spot.yaw, foe.motion = spot.motion;
433 for (
int p = 0; p < shot_.players_size(); ++p) {
434 if (shot_.players(p).id() == spec.charge() && to >= spec.charge_at()) {
437 if (shot_.players(p).id() == spec.face() && spec.legs().empty()) {
438 const Vector2 at = PlayerAt(p, to);
439 foe.yaw = std::atan2(at.x - foe.at.x, at.y - foe.at.y);
443 const Vector2 at = PlayerAt(chased, to);
444 const Vector2 way = Vector2Subtract(at, foe.at);
445 const float distance = Vector2Length(way);
446 foe.yaw = std::atan2(way.x, way.y);
447 const auto kind = content_.monsters.kinds.find(spec.kind());
448 const float reach = kind != content_.monsters.kinds.end()
449 ? kind->second.fight().reach()
451 if (distance > reach) {
452 const float step = std::min(
kRunSpeed * seconds, distance - reach);
453 foe.at = Vector2Add(foe.at, Vector2Scale(way, step / distance));
454 foe.motion = protocol::MOTION_RUN;
456 foe.motion = protocol::MOTION_ATTACK;
461 constexpr float kApart = 0.75f;
462 for (
size_t i = 0; i < foes_.size(); ++i) {
463 for (
size_t j = i + 1; j < foes_.size(); ++j) {
466 if (a.fallen || b.fallen) {
469 const Vector2 way = Vector2Subtract(b.at, a.at);
470 const float distance = Vector2Length(way);
471 if (distance < kApart && distance > 1e-4f) {
473 Vector2Scale(way, 0.5f * (kApart - distance) / distance);
474 a.at = Vector2Subtract(a.at, half);
475 b.at = Vector2Add(b.at, half);
483 constexpr float kStep = 1.0f / 120.0f;
484 while (now_ < game) {
485 const float to = std::min(game, now_ + kStep);
490 protocol::World world;
491 const int land =
LandOf(shot_);
492 world.set_seed(kWorldSeed +
kLands[land].seed);
493 world.set_land(land);
494 for (
int i = 0; i < shot_.players_size(); ++i) {
495 const Player& who = shot_.players(i);
496 Spot spot = Walked(who.at(), Radians(who.
yaw()), who.legs(), game);
497 protocol::Player* player = world.add_players();
498 player->set_id(who.id());
499 player->set_name(who.
name());
500 const uint32_t level = who.level() > 0 ? who.level() : 10;
501 player->set_level(level);
503 player->set_max_health(100 + 20 * (level - 1));
504 player->set_health(player->max_health());
505 player->set_weapon(who.weapon());
506 for (
const uint32_t item : who.wearing()) {
507 player->add_wearing(item);
509 const auto facing = [&](uint32_t id) {
510 for (
const Foe& foe : foes_) {
511 if (foe.spec.id() ==
id) {
512 spot.yaw = std::atan2(foe.at.x - spot.at.x, foe.at.y - spot.at.y);
516 if (spot.motion == protocol::MOTION_IDLE && who.face() != 0) {
520 if (spot.motion == protocol::MOTION_IDLE && shot_.has_shoulder() &&
521 shot_.shoulder().player() == who.id()) {
522 const Point& look = shot_.shoulder().look();
523 spot.yaw = std::atan2(look.x() - spot.at.x, look.z() - spot.at.y);
527 for (
const Cast& cast : who.
casts()) {
528 if (cast.at() > game) {
532 if (game < cast.at() + BlowOf(who, cast).lasts) {
533 spot.motion = protocol::MOTION_SKILL;
534 player->set_skill(cast.skill());
535 player->set_target(cast.target());
536 facing(cast.target());
539 player->set_casts(casts);
540 player->set_x(spot.at.x);
541 player->set_z(spot.at.y);
542 player->set_yaw(spot.yaw);
543 player->set_motion(spot.motion);
545 if (shot_.players_size() > 0) {
546 world.set_you(shot_.players(0).id());
548 for (
const Foe& foe : foes_) {
549 protocol::Monster* monster = world.add_monsters();
550 monster->set_id(foe.spec.id());
551 monster->set_kind(foe.spec.kind());
552 monster->set_level(foe.spec.level() > 0 ? foe.spec.level() : 5);
553 const auto kind = content_.monsters.kinds.find(foe.spec.kind());
554 const uint32_t health =
555 kind != content_.monsters.kinds.end() ? kind->second.max_health() : 80;
556 monster->set_max_health(health);
557 monster->set_health(foe.fallen ? 0 : health);
558 monster->set_x(foe.at.x);
559 monster->set_z(foe.at.y);
560 monster->set_yaw(foe.yaw);
561 monster->set_motion(foe.fallen ? protocol::MOTION_DEAD : foe.motion);