30#include "absl/strings/str_cat.h"
31#include "absl/strings/str_split.h"
51using nanommo::cinema::Shot;
61 std::optional<bool> cel;
87float Rate(
float speed) {
return std::clamp(std::sqrt(speed), 0.4f, 1.0f); }
89void WriteWave(
const std::string& path,
const std::vector<float>& left,
90 const std::vector<float>& right) {
91 std::ofstream file(path, std::ios::binary);
92 const auto u32 = [&](uint32_t v) {
93 file.write(
reinterpret_cast<char*
>(&v), 4);
95 const auto u16 = [&](uint16_t v) {
96 file.write(
reinterpret_cast<char*
>(&v), 2);
98 const uint32_t bytes =
static_cast<uint32_t
>(left.size() * 4);
99 file.write(
"RIFF", 4);
101 file.write(
"WAVEfmt ", 8);
104 file.write(
"data", 4);
106 for (
size_t i = 0; i < left.size(); ++i) {
107 for (
const float s : {left[i], right[i]}) {
108 u16(
static_cast<uint16_t
>(
109 static_cast<int16_t
>(std::clamp(s, -1.0f, 1.0f) * 32767.0f)));
116void Mix(
const std::vector<Sound>& sounds,
float seconds,
117 std::vector<float> left, std::vector<float> right,
118 const std::string& path) {
119 const size_t length =
static_cast<size_t>(seconds *
nanommo::kRate);
120 left.resize(length, 0.0f);
121 right.resize(length, 0.0f);
122 for (
const Sound& sound : sounds) {
123 const float rate = Rate(sound.speed);
124 const float gain = sound.level * sound.heard.volume;
125 const float to_left = gain * std::min(1.0f, 1.0f - sound.heard.pan);
126 const float to_right = gain * std::min(1.0f, 1.0f + sound.heard.pan);
127 const size_t start =
static_cast<size_t>(sound.at *
nanommo::kRate);
128 for (
size_t i = 0; start + i < length; ++i) {
129 const float from =
static_cast<float>(i) * rate;
130 const size_t a =
static_cast<size_t>(from);
131 if (a + 1 >= sound.samples.size()) {
134 const float s = Lerp(sound.samples[a], sound.samples[a + 1],
135 from -
static_cast<float>(a));
136 left[start + i] += s * to_left;
137 right[start + i] += s * to_right;
143 WriteWave(path, left, right);
148void AddMusic(
const Shot& shot, std::vector<float>& left,
149 std::vector<float>& right) {
151 if (!IsWaveValid(wave)) {
157 float* samples = LoadWaveSamples(wave);
158 const size_t length =
static_cast<size_t>(shot.seconds() *
nanommo::kRate);
159 left.resize(length, 0.0f);
160 right.resize(length, 0.0f);
161 const size_t start =
static_cast<size_t>(shot.music_from() *
nanommo::kRate);
164 for (
size_t i = 0; i < length && start + i < wave.frameCount; ++i) {
165 const float fade = std::min({1.0f,
static_cast<float>(i) / in,
166 static_cast<float>(length - i) / out});
167 const float gain = shot.music() * fade;
168 left[i] += gain * samples[2 * (start + i)];
169 right[i] += gain * samples[2 * (start + i) + 1];
171 UnloadWaveSamples(samples);
176 const Options& options) {
177 const auto started = std::chrono::steady_clock::now();
181 const bool cel = options.cel.value_or(
false);
183 renderer.set_cel_shading(cel);
184 renderer.set_terrain(terrain, land);
189 if (shot.sky().fog_near() > 0.0f) {
190 sky.
fog_near = shot.sky().fog_near();
192 if (shot.sky().fog_far() > 0.0f) {
193 sky.
fog_far = shot.sky().fog_far();
195 if (shot.sky().has_sun()) {
196 const auto& sun = shot.sky().sun();
197 sky.
sun = {sun.x(), sun.y(), sun.z()};
199 if (shot.sky().light() > 0.0f) {
201 c *= shot.sky().light();
204 c *= shot.sky().light();
207 if (shot.sky().has_cloudiness()) {
214 const bool skied = shot.sky().has_day() || !shot.sky().weather().empty();
215 nanommo::protocol::Weather weather = nanommo::protocol::WEATHER_CLEAR;
216 if (!shot.sky().weather().empty() &&
217 !nanommo::protocol::Weather_Parse(
"WEATHER_" + shot.sky().weather(),
219 std::cerr <<
"nanommo_cinema: no weather called " << shot.sky().weather()
223 const auto sky_at = [&](
float now) {
224 nanommo::protocol::Sky sky;
225 const float day = shot.
sky().has_day() ? shot.sky().day() : 0.5f;
226 sky.set_day(std::fmod(day + shot.sky().days() * now, 1.0f));
227 sky.set_weather(weather);
228 const float share = std::clamp(now / shot.seconds(), 0.0f, 1.0f);
230 shot.sky().has_strength_to()
231 ? Lerp(shot.sky().strength(), shot.sky().strength_to(), share)
232 : shot.sky().strength());
233 const bool gale = weather == nanommo::protocol::WEATHER_STORM ||
234 weather == nanommo::protocol::WEATHER_FIRE;
235 sky.set_wind_x(shot.sky().has_wind_x() ? shot.sky().wind_x()
238 sky.set_wind_z(shot.sky().has_wind_z() ? shot.sky().wind_z()
246 const uint32_t shot_seed =
247 static_cast<uint32_t
>(std::hash<std::string>()(shot.id()));
248 renderer.
weather().set_seed(shot_seed);
249 if (shot.sky().has_wetness()) {
250 renderer.weather().set_wetness(shot.sky().wetness());
254 std::vector<float> bed_left, bed_right;
258 const RenderTexture2D picture =
259 LoadRenderTexture(options.width, options.height);
261 const std::string name =
262 absl::StrCat(options.out,
"/", shot.id(), options.tall ?
"-tall" :
"");
263 FILE* video =
nullptr;
264 if (options.still < 0.0f) {
265 const std::string encode = absl::StrCat(
266 "ffmpeg -loglevel error -y -f rawvideo -pix_fmt rgba -s ",
267 options.width,
"x", options.height,
" -r ", options.fps,
268 " -i - -vf vflip -c:v libx264 -preset fast -crf 14 -pix_fmt yuv420p ",
272 video = _popen(encode.c_str(),
"wb");
274 video = popen(encode.c_str(),
"w");
276 if (video ==
nullptr) {
277 std::cerr <<
"nanommo_cinema: couldn't start ffmpeg\n";
286 std::vector<Sound> sounds;
287 std::vector<Pending> pending;
290 static_cast<int>(std::lround(shot.seconds() * options.fps));
294 :
static_cast<int>(std::lround(options.still * options.fps));
296 for (
int frame = 0; frame < frames && (still < 0 || frame <= still);
298 const float now = clock.film();
299 const float game = clock.game();
300 std::vector<nanommo::Figure> figures =
301 animator.Update(stage.At(game), moved);
303 figure.position.y = terrain.
Height(figure.position.x, figure.position.z);
305 renderer.set_figures(std::move(figures));
306 const Camera3D camera =
308 renderer.set_camera(camera);
310 renderer.set_sky(sky_at(now), showing);
312 renderer.Animate(moved);
316 const Vector3 right = Vector3Normalize(Vector3CrossProduct(
317 Vector3Subtract(camera.target, camera.position), camera.up));
319 std::map<uint64_t, nanommo::Gait> gaits;
320 std::map<uint64_t, Vector3> where;
322 if (
const auto gait = renderer.GaitOf(figure)) {
327 for (
const uint64_t who : footsteps.
Update(gaits)) {
328 const Vector3 at = where.at(who);
338 const auto skill = content.
skills.
by_id.find(figure.to_skill);
344 for (
const nanommo::skills::Fx& fx : skill->second.look().fx()) {
345 if (fx.at() == nanommo::skills::Fx::TARGET && figure.target == 0) {
348 pending.push_back({.game = game + fx.from_ms() / 1000.0f,
350 .at = figure.position});
354 for (
auto it = pending.begin(); it != pending.end();) {
355 if (it->game > game) {
359 sounds.push_back({.samples = std::move(it->samples),
364 it = pending.erase(it);
367 if (still < 0 || frame == still) {
368 post.
Begin(graphics, options.width, options.height);
372 Image image = LoadImageFromTexture(picture.texture);
373 if (video !=
nullptr) {
374 fwrite(image.data, 4,
375 static_cast<size_t>(options.width) * options.height, video);
377 ImageFlipVertical(&image);
378 ExportImage(image, absl::StrCat(name,
".png").c_str());
384 if (video !=
nullptr && skied) {
385 const Vector3 ears = camera.target;
386 for (
const nanommo::Bolt& bolt : renderer.weather().TakeBolts()) {
387 ambience.Thunder(Vector3Distance(bolt.at, ears),
390 bool by_water =
false;
391 for (
const auto& [dx, dz] : {std::pair{0.0f, 0.0f},
396 by_water = by_water || terrain.
Height(ears.x + dx, ears.z + dz) <
399 const size_t from = bed_left.size();
400 const size_t to =
static_cast<size_t>(
402 bed_left.resize(to, 0.0f);
403 bed_right.resize(to, 0.0f);
404 ambience.Fill(std::span(bed_left).subspan(from),
405 std::span(bed_right).subspan(from),
408 moved = clock.Advance(1.0f / options.fps);
410 UnloadRenderTexture(picture);
411 if (video !=
nullptr) {
417 if (shot.music() > 0.0f) {
418 AddMusic(shot, bed_left, bed_right);
420 Mix(sounds, shot.seconds(), std::move(bed_left), std::move(bed_right),
422 const std::string mux =
423 absl::StrCat(
"ffmpeg -loglevel error -y -i ", name,
".video.mp4 -i ",
424 name,
".wav -c:v copy -c:a aac -b:a 256k -shortest ", name,
425 ".mp4 && rm ", name,
".video.mp4");
426 if (std::system(mux.c_str()) != 0) {
427 std::cerr <<
"nanommo_cinema: couldn't add the sound to " << name <<
'\n';
432 std::chrono::duration<float>(std::chrono::steady_clock::now() - started)
434 std::cout << shot.id() <<
": " << frames <<
" frames, " << shot.seconds()
435 <<
" s of film, " << clock.game() - shot.from()
436 <<
" s of game, drawn in " << took <<
" s\n";
442int Heights(
const std::string& name,
bool shore) {
444 std::cerr <<
"nanommo_cinema: no land called " << name <<
'\n';
452 std::vector<Place> places;
453 const float edge = terrain.
half_size() - 4.0f;
454 for (
float x = -edge; x <= edge; x += 2.0f) {
455 for (
float z = -edge; z <= edge; z += 2.0f) {
456 const float height = terrain.
Height(x, z);
462 height > 0.4f && height < 2.5f &&
464 std::array<std::pair<float, float>, 4>{
465 {{6.0f, 0.0f}, {-6.0f, 0.0f}, {0.0f, 6.0f}, {0.0f, -6.0f}}},
467 return terrain.
Height(x + d.first, z + d.second) < -0.3f;
469 if (!shore || by_sea) {
470 places.push_back({x, z, height});
474 std::ranges::sort(places, [](
const Place& a,
const Place& b) {
475 return a.height > b.height;
477 std::vector<Place> kept;
478 for (
const Place& place : places) {
479 if (std::ranges::none_of(kept, [&](
const Place& k) {
480 return std::hypot(k.x - place.x, k.z - place.z) < 25.0f;
482 kept.push_back(place);
484 if (kept.size() == 12) {
488 for (
const Place& place : kept) {
489 std::cout << name <<
" x " << place.x <<
" z " << place.z <<
" height "
490 << place.height <<
'\n';
497int Scout(
const std::string& name,
float radius) {
499 std::cerr <<
"nanommo_cinema: no land called " << name <<
'\n';
507 std::vector<Place> places;
508 const float edge = terrain.
half_size() - radius - 4.0f;
509 for (
float x = -edge; x <= edge; x += 2.0f) {
510 for (
float z = -edge; z <= edge; z += 2.0f) {
511 float low = 1e9f, high = -1e9f;
513 for (
float dx = -radius; dx <= radius && open; dx += 1.0f) {
514 for (
float dz = -radius; dz <= radius && open; dz += 1.0f) {
515 if (dx * dx + dz * dz > radius * radius) {
518 open = terrain.
Walkable(x + dx, z + dz) &&
519 terrain.
Height(x + dx, z + dz) > 0.3f;
520 low = std::min(low, terrain.
Height(x + dx, z + dz));
521 high = std::max(high, terrain.
Height(x + dx, z + dz));
525 places.push_back({x, z, high - low});
529 std::ranges::sort(places, {}, &Place::rise);
531 std::vector<Place> kept;
532 for (
const Place& place : places) {
533 if (std::ranges::none_of(kept, [&](
const Place& k) {
534 return std::hypot(k.x - place.x, k.z - place.z) < 2.0f * radius;
536 kept.push_back(place);
538 if (kept.size() == 12) {
542 for (
const Place& place : kept) {
543 std::cout << name <<
" x " << place.x <<
" z " << place.z <<
" rise "
544 << place.rise <<
" height " << terrain.
Height(place.x, place.z)
552int main(
int argc,
char** argv) {
553 if (argc == 4 && std::string(argv[1]) ==
"scout") {
554 return Scout(argv[2], std::strtof(argv[3],
nullptr));
557 (std::string(argv[1]) ==
"peaks" || std::string(argv[1]) ==
"shores")) {
558 return Heights(argv[2], std::string(argv[1]) ==
"shores");
562 <<
"usage: nanommo_cinema <film.txtpb> <out dir> [key=value ...]\n"
563 " nanommo_cinema scout <land> <radius>\n";
567 options.out = argv[2];
568 std::set<std::string> wanted;
570 for (
int i = 3; i < argc; ++i) {
571 const std::vector<std::string> pair = absl::StrSplit(argv[i],
'=');
572 if (pair.size() != 2) {
573 std::cerr <<
"nanommo_cinema: not key=value: " << argv[i] <<
'\n';
576 const std::string& key = pair[0];
577 const std::string& value = pair[1];
578 if (key ==
"shots") {
579 for (
const auto&
id : absl::StrSplit(value,
',')) {
582 }
else if (key ==
"size") {
583 sized = std::sscanf(value.c_str(),
"%dx%d", &options.width,
584 &options.height) == 2;
585 }
else if (key ==
"fps") {
586 options.fps = std::strtof(value.c_str(),
nullptr);
587 }
else if (key ==
"tall") {
588 options.tall = value ==
"1";
589 }
else if (key ==
"cel") {
590 options.cel = value ==
"1";
591 }
else if (key ==
"still") {
592 options.still = std::strtof(value.c_str(),
nullptr);
594 std::cerr <<
"nanommo_cinema: unknown key " << key <<
'\n';
598 if (options.tall && !sized) {
599 options.width = 1080;
600 options.height = 1920;
603 const absl::StatusOr<nanommo::cinema::Film> film =
606 std::cerr <<
"nanommo_cinema: " << film.status().message() <<
'\n';
610 options.cel = film->cel_shading();
614 SetTraceLogLevel(LOG_WARNING);
615 SetConfigFlags(FLAG_WINDOW_HIDDEN);
616 InitWindow(320, 180,
"nanoMMO cinema");
617 if (!IsWindowReady()) {
623 std::cerr <<
"nanommo_cinema: " << content.status().message() <<
'\n';
627 for (
const Shot& shot : film->shots()) {
628 if (wanted.empty() || wanted.contains(shot.id())) {
629 failed += Shoot(shot, *content, options);
633 return failed == 0 ? 0 : 1;
std::vector< Figure > Update(const std::vector< Figure > &figures)
void DrawOnto(const RenderTexture2D &into)
void Begin(const GraphicsSettings &graphics)
float half_size() const
From the middle to each side of the grid.
static constexpr float kSeaLevel
float Height(float x, float z) const
On the triangles between the grid's corners, as the client draws them.
bool Walkable(float x, float z) const
Camera3D CameraAt(const Shot &shot, float seconds, bool tall, const Terrain &terrain)
Vector3 Place(const Point &point, const Terrain &terrain)
float SpeedAt(const Shot &shot, float seconds)
How fast the game runs at that second of the shot.
int LandOf(const Shot &shot)
The land a shot is in, by its place in kLands.
Terrain TerrainOf(int land)
The land's terrain, as the server builds it for the first world.
absl::StatusOr< Film > ReadFilm(const std::string &path)
The shots in a text file, as the content files are written.
std::vector< float > Samples
One channel of sound, at kRate.
Samples FxSound(const skills::Fx &fx, uint32_t seed)
The sound of one of a skill's fx, from when it starts.
Ground GroundAt(const Terrain &terrain, int land, float x, float z)
What's underfoot there: the water we wade in, or the land's surface.
int LandNamed(std::string_view name)
Its place in the list, or the first land's for a name that isn't there.
std::vector< Cube > GateCubes(const Terrain &terrain, int land)
void UseRecordings(Ambience &ambience)
uint64_t Who(const Figure &figure)
void Mix(Samples &mix, const Samples &sound, float at, float gain)
Samples Step(Ground ground, uint32_t seed)
std::string ContentFolder()
void Limit(Samples &samples)
Heard Hear(Vector3 source, Vector3 ears, Vector3 right)
Hearing HearingOf(int land, const protocol::Sky &sky, bool by_water, bool weather)
std::string MusicFolder()
absl::StatusOr< Content > ReadContent(const std::string &folder)
Reads each file in the folder, or says which file is wrong, and where.
bool IsLand(std::string_view name)
The sky and the light, which the shaders read each frame.
std::array< float, 3 > ambient
What a face gets even when it's turned away from the sun.
std::array< float, 3 > sunlight
What the sun adds to a face that's turned to it.
std::array< float, 3 > sky
The background, which the fog fades into.
std::array< float, 3 > sun
Towards the sun.
Where a bolt of lightning struck, for its thunder to come from.
What turns on and off: the time of day, and the weather.
std::map< uint32_t, skills::Skill > by_id