1 #include "ClientController.hpp"
2 #include "DirectInput.hpp"
4 #include "InteractiveManipulator.hpp"
5 #include "Interface.hpp"
7 #include "PlayerController.hpp"
9 #include "../app/Assets.hpp"
10 #include "../app/Config.hpp"
11 #include "../app/Environment.hpp"
12 #include "../app/FrameCounter.hpp"
13 #include "../app/init.hpp"
14 #include "../audio/Audio.hpp"
15 #include "../audio/SoundBank.hpp"
16 #include "../geometry/distance.hpp"
17 #include "../graphics/Font.hpp"
18 #include "../graphics/Viewport.hpp"
19 #include "../io/TokenStreamReader.hpp"
20 #include "../model/bounds.hpp"
21 #include "../net/CongestionControl.hpp"
22 #include "../world/BlockLookup.hpp"
23 #include "../world/World.hpp"
24 #include "../world/WorldManipulator.hpp"
31 #include <glm/gtc/matrix_transform.hpp>
32 #include <glm/gtx/projection.hpp>
33 #include <glm/gtx/rotate_vector.hpp>
34 #include <glm/gtx/io.hpp>
39 PlayerController::PlayerController(World &world, Player &player)
46 player.GetEntity().SetController(*this);
47 player.GetEntity().GetSteering().SetAcceleration(5.0f);
50 PlayerController::~PlayerController() {
51 if (&player.GetEntity().GetController() == this) {
52 player.GetEntity().UnsetController();
56 void PlayerController::SetMovement(const glm::vec3 &m) noexcept {
57 if (glm::dot(m, m) > 1.0f) {
58 move_dir = glm::normalize(m);
65 void PlayerController::TurnHead(float dp, float dy) noexcept {
66 player.GetEntity().TurnHead(dp, dy);
69 float PlayerController::GetPitch() const noexcept {
70 return player.GetEntity().Pitch();
73 float PlayerController::GetYaw() const noexcept {
74 return player.GetEntity().Yaw();
77 void PlayerController::SelectInventory(int i) noexcept {
78 player.SetInventorySlot(i);
81 int PlayerController::InventorySlot() const noexcept {
82 return player.GetInventorySlot();
85 void PlayerController::Invalidate() noexcept {
89 void PlayerController::UpdatePlayer() noexcept {
91 Ray aim = player.Aim();
92 Entity &entity = player.GetEntity();
93 if (!world.Intersection(aim, entity.ChunkCoords(), aim_world)) {
94 aim_world = WorldCollision();
96 if (!world.Intersection(aim, entity, aim_entity)) {
97 aim_entity = EntityCollision();
99 if (aim_world && aim_entity) {
100 // got both, pick the closest one
101 if (aim_world.depth < aim_entity.depth) {
102 aim_entity = EntityCollision();
104 aim_world = WorldCollision();
107 Steering &steering = entity.GetSteering();
108 if (!iszero(move_dir)) {
109 // scale input by max velocity, apply yaw, and transform to world space
110 steering.SetTargetVelocity(glm::vec3(
111 glm::vec4(glm::rotateY(move_dir * entity.MaxVelocity(), entity.Yaw()), 0.0f)
112 * glm::transpose(entity.Transform())
114 steering.Enable(Steering::TARGET_VELOCITY);
115 steering.Disable(Steering::HALT);
117 // target velocity of 0 is the same as halt
118 steering.Enable(Steering::HALT);
119 steering.Disable(Steering::TARGET_VELOCITY);
126 DirectInput::DirectInput(World &world, Player &player, WorldManipulator &manip)
127 : PlayerController(world, player)
130 , remove_timer(0.25f) {
134 void DirectInput::Update(Entity &, float dt) {
135 Invalidate(); // world has changed in the meantime
138 remove_timer.Update(dt);
139 if (remove_timer.Hit()) {
143 place_timer.Update(dt);
144 if (place_timer.Hit()) {
149 void DirectInput::StartPrimaryAction() {
150 if (!remove_timer.Running()) {
152 remove_timer.Start();
156 void DirectInput::StopPrimaryAction() {
160 void DirectInput::StartSecondaryAction() {
161 if (!place_timer.Running()) {
167 void DirectInput::StopSecondaryAction() {
171 void DirectInput::StartTertiaryAction() {
175 void DirectInput::StopTertiaryAction() {
179 void DirectInput::PickBlock() {
181 if (!BlockFocus()) return;
182 SelectInventory(BlockFocus().GetBlock().type - 1);
185 void DirectInput::PlaceBlock() {
186 // update block focus
188 // do nothing if not looking at any block
189 if (!BlockFocus()) return;
191 // determine block adjacent to the face the player is looking at
192 BlockLookup next_block(BlockFocus().chunk, BlockFocus().BlockPos(), Block::NormalFace(BlockFocus().normal));
193 // abort if it's unavailable
198 // "can replace" check
199 // this prevents players from replacing solid blocks e.g. by looking through slabs
200 // simple for now, should be expanded to include things like
201 // entities in the way or replacable blocks like water and stuff
202 if (next_block.GetBlock().type != 0) {
206 Block new_block(InventorySlot() + 1);
209 // align with player's up
210 const glm::vec3 player_up(GetPlayer().GetEntity().Up());
211 new_block.SetFace(Block::NormalFace(player_up));
212 // to align with player's local up/down look (like stairs in minecraft), just invert
213 // it if pitch is positive
214 // or, align with focus normal (like logs in minecraft)
216 // determine block's turn (local rotation about up axis)
217 // when aligned with player's up (first mode, and currently the only one implemented)
218 // project the player's view forward onto his entity's XZ plane and
219 // use the closest cardinal direction it's pointing in
220 const glm::vec3 view_forward(-GetPlayer().GetEntity().ViewTransform(GetPlayer().GetEntity().ChunkCoords())[2]);
221 // if view is straight up or down, this will be a null vector (NaN after normalization)
222 // in that case maybe the model forward should be used?
223 // the current implementation implicitly falls back to TURN_NONE which is -Z
224 const glm::vec3 local_forward(glm::normalize(view_forward - glm::proj(view_forward, player_up)));
225 // FIXME: I suspect this only works when player_up is positive Y
226 if (local_forward.x > 0.707f) {
227 new_block.SetTurn(Block::TURN_RIGHT);
228 } else if (local_forward.z > 0.707f) {
229 new_block.SetTurn(Block::TURN_AROUND);
230 } else if (local_forward.x < -0.707f) {
231 new_block.SetTurn(Block::TURN_LEFT);
233 // for mode two ("minecraft stairs") it should work the same, but I haven't properly
234 // thought that through (well, that's also true about the whole face/turn thing, but oh well)
235 // mode three I have absoloutely no clue. that placement would be appropriate for pipe-like
236 // blocks, where turn shouldn't make a difference, but what if it does?
238 manip.SetBlock(next_block.GetChunk(), next_block.GetBlockIndex(), new_block);
242 void DirectInput::RemoveBlock() {
244 if (!BlockFocus()) return;
245 manip.SetBlock(BlockFocus().GetChunk(), BlockFocus().block, Block(0));
250 HUD::HUD(Environment &env, Config &config, const Player &player)
256 , outline_transform(1.0f)
257 , outline_visible(false)
261 , block_transform(1.0f)
263 , block_visible(false)
277 , messages(env.assets.small_ui_font)
282 const float ls = env.assets.small_ui_font.LineSkip();
285 block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f));
286 block_transform = glm::scale(block_transform, glm::vec3(50.0f));
287 block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f));
288 block_transform = glm::rotate(block_transform, 0.35f, glm::vec3(0.0f, 1.0f, 0.0f));
289 block_label.Position(
290 glm::vec3(50.0f, 85.0f, 0.0f),
294 block_label.Foreground(glm::vec4(1.0f));
295 block_label.Background(glm::vec4(0.5f));
298 counter_text.Position(glm::vec3(-25.0f, 25.0f, 0.0f), Gravity::NORTH_EAST);
299 counter_text.Foreground(glm::vec4(1.0f));
300 counter_text.Background(glm::vec4(0.5f));
301 position_text.Position(glm::vec3(-25.0f, 25.0f + ls, 0.0f), Gravity::NORTH_EAST);
302 position_text.Foreground(glm::vec4(1.0f));
303 position_text.Background(glm::vec4(0.5f));
304 orientation_text.Position(glm::vec3(-25.0f, 25.0f + 2 * ls, 0.0f), Gravity::NORTH_EAST);
305 orientation_text.Foreground(glm::vec4(1.0f));
306 orientation_text.Background(glm::vec4(0.5f));
307 block_text.Position(glm::vec3(-25.0f, 25.0f + 4 * ls, 0.0f), Gravity::NORTH_EAST);
308 block_text.Foreground(glm::vec4(1.0f));
309 block_text.Background(glm::vec4(0.5f));
310 block_text.Set(env.assets.small_ui_font, "Block: none");
311 entity_text.Position(glm::vec3(-25.0f, 25.0f + 4 * ls, 0.0f), Gravity::NORTH_EAST);
312 entity_text.Foreground(glm::vec4(1.0f));
313 entity_text.Background(glm::vec4(0.5f));
314 entity_text.Set(env.assets.small_ui_font, "Entity: none");
317 bandwidth_text.Position(glm::vec3(-25.0f, 25.0f + 6 * ls, 0.0f), Gravity::NORTH_EAST);
318 bandwidth_text.Foreground(glm::vec4(1.0f));
319 bandwidth_text.Background(glm::vec4(0.5f));
320 bandwidth_text.Set(env.assets.small_ui_font, "TX: 0.0KB/s RX: 0.0KB/s");
321 rtt_text.Position(glm::vec3(-25.0f, 25.0f + 7 * ls, 0.0f), Gravity::NORTH_EAST);
322 rtt_text.Foreground(glm::vec4(1.0f));
323 rtt_text.Background(glm::vec4(0.5f));
324 rtt_text.Set(env.assets.small_ui_font, "RTT: unavailable");
325 packet_loss_text.Position(glm::vec3(-25.0f, 25.0f + 8 * ls, 0.0f), Gravity::NORTH_EAST);
326 packet_loss_text.Foreground(glm::vec4(1.0f));
327 packet_loss_text.Background(glm::vec4(0.5f));
328 packet_loss_text.Set(env.assets.small_ui_font, "Packet loss: 0.0%");
331 messages.Position(glm::vec3(25.0f, -25.0f - 2 * ls, 0.0f), Gravity::SOUTH_WEST);
332 messages.Foreground(glm::vec4(1.0f));
333 messages.Background(glm::vec4(0.5f));
336 PrimitiveMesh::Buffer buf;
337 buf.vertices = std::vector<glm::vec3>({
338 { -10.0f, 0.0f, 0.0f }, { 10.0f, 0.0f, 0.0f },
339 { 0.0f, -10.0f, 0.0f }, { 0.0f, 10.0f, 0.0f },
341 buf.indices = std::vector<PrimitiveMesh::Index>({
344 buf.colors.resize(4, { 255, 255, 255, 255 });
345 crosshair.Update(buf);
350 PrimitiveMesh::Buffer outl_buf;
354 void HUD::FocusBlock(const Chunk &chunk, int index) {
355 const Block &block = chunk.BlockAt(index);
356 const BlockType &type = chunk.Type(index);
358 type.OutlinePrimitiveMesh(outl_buf);
359 outline.Update(outl_buf);
360 outline_transform = chunk.Transform(player.GetEntity().ChunkCoords());
361 outline_transform *= chunk.ToTransform(Chunk::ToPos(index), index);
362 outline_transform *= glm::scale(glm::vec3(1.005f));
363 outline_visible = true;
368 << ", face: " << block.GetFace()
369 << ", turn: " << block.GetTurn();
370 block_text.Set(env.assets.small_ui_font, s.str());
376 void HUD::FocusEntity(const Entity &entity) {
379 s << "Entity: " << entity.Name();
380 entity_text.Set(env.assets.small_ui_font, s.str());
386 void HUD::FocusNone() {
387 outline_visible = false;
392 void HUD::DisplayNone() {
393 block_visible = false;
396 void HUD::Display(const BlockType &type) {
398 type.FillEntityMesh(block_buf);
399 block.Update(block_buf);
401 block_label.Set(env.assets.small_ui_font, type.label);
403 block_visible = type.visible;
407 void HUD::UpdateDebug() {
413 void HUD::UpdateCounter() {
415 s << std::setprecision(3) <<
416 "avg: " << env.counter.Average().running << "ms, "
417 "peak: " << env.counter.Peak().running << "ms";
418 std::string text = s.str();
419 counter_text.Set(env.assets.small_ui_font, text);
422 void HUD::UpdatePosition() {
424 s << std::setprecision(3) << "pos: " << player.GetEntity().AbsolutePosition();
425 position_text.Set(env.assets.small_ui_font, s.str());
428 void HUD::UpdateOrientation() {
430 s << std::setprecision(3) << "pitch: " << glm::degrees(player.GetEntity().Pitch())
431 << ", yaw: " << glm::degrees(player.GetEntity().Yaw());
432 orientation_text.Set(env.assets.small_ui_font, s.str());
435 void HUD::PostMessage(const char *msg) {
436 messages.PushLine(msg);
439 std::cout << msg << std::endl;
443 void HUD::UpdateNetStats(const CongestionControl &stat) {
444 if (!config.video.debug) return;
447 s << std::fixed << std::setprecision(1)
448 << "TX: " << stat.Upstream()
449 << "KB/s, RX: " << stat.Downstream() << "KB/s";
450 bandwidth_text.Set(env.assets.small_ui_font, s.str());
453 s << "RTT: " << stat.RoundTripTime() << "ms";
454 rtt_text.Set(env.assets.small_ui_font, s.str());
457 s << "Packet loss: " << (stat.PacketLoss() * 100.0f) << "%";
458 packet_loss_text.Set(env.assets.small_ui_font, s.str());
464 void HUD::Update(int dt) {
465 msg_timer.Update(dt);
466 if (msg_timer.HitOnce()) {
470 if (config.video.debug) {
471 if (env.counter.Changed()) {
479 void HUD::Render(Viewport &viewport) noexcept {
481 if (outline_visible && config.video.world) {
482 PlainColor &outline_prog = viewport.WorldColorProgram();
483 outline_prog.SetM(outline_transform);
487 // clear depth buffer so everything renders above the world
488 viewport.ClearDepth();
490 if (config.video.hud) {
493 DirectionalLighting &world_prog = viewport.HUDProgram();
494 world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f });
495 world_prog.SetLightColor({ 1.0f, 1.0f, 1.0f });
496 world_prog.SetAmbientColor({ 0.1f, 0.1f, 0.1f });
497 // disable distance fog
498 world_prog.SetFogDensity(0.0f);
500 viewport.DisableBlending();
501 world_prog.SetM(block_transform);
503 block_label.Render(viewport);
507 if (msg_keep || msg_timer.Running()) {
508 messages.Render(viewport);
512 PlainColor &outline_prog = viewport.HUDColorProgram();
513 viewport.EnableInvertBlending();
514 viewport.SetCursor(glm::vec3(0.0f), Gravity::CENTER);
515 outline_prog.SetM(viewport.Cursor());
516 crosshair.DrawLines();
520 if (config.video.debug) {
521 counter_text.Render(viewport);
522 position_text.Render(viewport);
523 orientation_text.Render(viewport);
525 block_text.Render(viewport);
526 } else if (show_entity) {
527 entity_text.Render(viewport);
530 bandwidth_text.Render(viewport);
531 rtt_text.Render(viewport);
532 packet_loss_text.Render(viewport);
538 InteractiveManipulator::InteractiveManipulator(Audio &audio, const SoundBank &sounds, Entity &player)
545 void InteractiveManipulator::SetBlock(Chunk &chunk, int index, const Block &block) {
546 const BlockType &old_type = chunk.Type(index);
547 chunk.SetBlock(index, block);
548 const BlockType &new_type = chunk.Type(index);
549 glm::vec3 coords = chunk.ToSceneCoords(player.ChunkCoords(), Chunk::ToCoords(index));
550 if (new_type.id == 0) {
551 if (old_type.remove_sound >= 0) {
552 audio.Play(sounds[old_type.remove_sound], coords);
555 if (new_type.place_sound >= 0) {
556 audio.Play(sounds[new_type.place_sound], coords);
562 Interface::Interface(
564 const Keymap &keymap,
565 PlayerController &pc,
566 ClientController &cc)
578 void Interface::Lock() {
584 void Interface::Unlock() {
588 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
589 if (!config.input.keyboard) return;
591 Keymap::Action action = keymap.Lookup(event);
593 case Keymap::MOVE_FORWARD:
597 case Keymap::MOVE_BACKWARD:
601 case Keymap::MOVE_LEFT:
605 case Keymap::MOVE_RIGHT:
609 case Keymap::MOVE_UP:
613 case Keymap::MOVE_DOWN:
618 case Keymap::PRIMARY:
619 player_ctrl.StartPrimaryAction();
621 case Keymap::SECONDARY:
622 player_ctrl.StartSecondaryAction();
624 case Keymap::TERTIARY:
625 player_ctrl.StartTertiaryAction();
628 case Keymap::INV_NEXT:
631 case Keymap::INV_PREVIOUS:
644 InvAbs(action - Keymap::INV_1);
651 case Keymap::TOGGLE_AUDIO:
652 client_ctrl.SetAudio(!config.audio.enabled);
654 case Keymap::TOGGLE_VIDEO:
655 client_ctrl.SetVideo(!config.video.world);
657 case Keymap::TOGGLE_HUD:
658 client_ctrl.SetHUD(!config.video.hud);
660 case Keymap::TOGGLE_DEBUG:
661 client_ctrl.SetDebug(!config.video.debug);
663 case Keymap::CAMERA_NEXT:
664 client_ctrl.NextCamera();
672 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
673 if (!config.input.keyboard) return;
675 switch (keymap.Lookup(event)) {
676 case Keymap::MOVE_FORWARD:
680 case Keymap::MOVE_BACKWARD:
684 case Keymap::MOVE_LEFT:
688 case Keymap::MOVE_RIGHT:
692 case Keymap::MOVE_UP:
696 case Keymap::MOVE_DOWN:
701 case Keymap::PRIMARY:
702 player_ctrl.StopPrimaryAction();
704 case Keymap::SECONDARY:
705 player_ctrl.StopSecondaryAction();
707 case Keymap::TERTIARY:
708 player_ctrl.StopTertiaryAction();
716 void Interface::Handle(const SDL_MouseMotionEvent &event) {
717 if (locked || !config.input.mouse) return;
718 player_ctrl.TurnHead(
719 event.yrel * config.input.pitch_sensitivity,
720 event.xrel * config.input.yaw_sensitivity);
723 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
724 if (!config.input.mouse) return;
726 switch (event.button) {
727 case SDL_BUTTON_LEFT:
728 player_ctrl.StartPrimaryAction();
730 case SDL_BUTTON_RIGHT:
731 player_ctrl.StartSecondaryAction();
733 case SDL_BUTTON_MIDDLE:
734 player_ctrl.StartTertiaryAction();
739 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
740 if (!config.input.mouse) return;
742 switch (event.button) {
743 case SDL_BUTTON_LEFT:
744 player_ctrl.StopPrimaryAction();
746 case SDL_BUTTON_RIGHT:
747 player_ctrl.StopSecondaryAction();
749 case SDL_BUTTON_MIDDLE:
750 player_ctrl.StopTertiaryAction();
756 void Interface::Handle(const SDL_MouseWheelEvent &event) {
757 if (!config.input.mouse) return;
761 } else if (event.y > 0) {
766 void Interface::UpdateMovement() {
767 player_ctrl.SetMovement(glm::vec3(fwd - rev));
770 void Interface::InvAbs(int s) {
771 int slot = s % num_slots;
775 player_ctrl.SelectInventory(slot);
778 void Interface::InvRel(int delta) {
779 InvAbs(player_ctrl.GetPlayer().GetInventorySlot() + delta);
788 void Keymap::Map(SDL_Scancode scancode, Action action) {
789 if (scancode > MAX_SCANCODE) {
790 throw std::runtime_error("refusing to map scancode: too damn high");
792 codemap[scancode] = action;
795 Keymap::Action Keymap::Lookup(SDL_Scancode scancode) const {
796 if (scancode < NUM_SCANCODES) {
797 return codemap[scancode];
804 void Keymap::LoadDefault() {
805 Map(SDL_SCANCODE_UP, MOVE_FORWARD);
806 Map(SDL_SCANCODE_W, MOVE_FORWARD);
807 Map(SDL_SCANCODE_DOWN, MOVE_BACKWARD);
808 Map(SDL_SCANCODE_S, MOVE_BACKWARD);
809 Map(SDL_SCANCODE_LEFT, MOVE_LEFT);
810 Map(SDL_SCANCODE_A, MOVE_LEFT);
811 Map(SDL_SCANCODE_RIGHT, MOVE_RIGHT);
812 Map(SDL_SCANCODE_D, MOVE_RIGHT);
813 Map(SDL_SCANCODE_SPACE, MOVE_UP);
814 Map(SDL_SCANCODE_RSHIFT, MOVE_UP);
815 Map(SDL_SCANCODE_LSHIFT, MOVE_DOWN);
816 Map(SDL_SCANCODE_LCTRL, MOVE_DOWN);
817 Map(SDL_SCANCODE_RCTRL, MOVE_DOWN);
819 Map(SDL_SCANCODE_TAB, INV_NEXT);
820 Map(SDL_SCANCODE_RIGHTBRACKET, INV_NEXT);
821 Map(SDL_SCANCODE_LEFTBRACKET, INV_PREVIOUS);
822 Map(SDL_SCANCODE_1, INV_1);
823 Map(SDL_SCANCODE_2, INV_2);
824 Map(SDL_SCANCODE_3, INV_3);
825 Map(SDL_SCANCODE_4, INV_4);
826 Map(SDL_SCANCODE_5, INV_5);
827 Map(SDL_SCANCODE_6, INV_6);
828 Map(SDL_SCANCODE_7, INV_7);
829 Map(SDL_SCANCODE_8, INV_8);
830 Map(SDL_SCANCODE_9, INV_9);
831 Map(SDL_SCANCODE_0, INV_10);
833 Map(SDL_SCANCODE_INSERT, SECONDARY);
834 Map(SDL_SCANCODE_MENU, TERTIARY);
835 Map(SDL_SCANCODE_DELETE, PRIMARY);
836 Map(SDL_SCANCODE_BACKSPACE, PRIMARY);
838 Map(SDL_SCANCODE_F1, TOGGLE_HUD);
839 Map(SDL_SCANCODE_F2, TOGGLE_VIDEO);
840 Map(SDL_SCANCODE_F3, TOGGLE_DEBUG);
841 Map(SDL_SCANCODE_F4, TOGGLE_AUDIO);
842 Map(SDL_SCANCODE_F5, CAMERA_NEXT);
844 Map(SDL_SCANCODE_ESCAPE, EXIT);
848 void Keymap::Load(std::istream &is) {
849 TokenStreamReader in(is);
850 std::string key_name;
851 std::string action_name;
854 while (in.HasMore()) {
855 if (in.Peek().type == Token::STRING) {
856 in.ReadString(key_name);
857 key = SDL_GetScancodeFromName(key_name.c_str());
859 key = SDL_Scancode(in.GetInt());
861 in.Skip(Token::EQUALS);
862 in.ReadIdentifier(action_name);
863 action = StringToAction(action_name);
864 if (in.HasMore() && in.Peek().type == Token::SEMICOLON) {
865 in.Skip(Token::SEMICOLON);
871 void Keymap::Save(std::ostream &out) {
872 for (unsigned int i = 0; i < NUM_SCANCODES; ++i) {
873 if (codemap[i] == NONE) continue;
875 const char *str = SDL_GetScancodeName(SDL_Scancode(i));
891 out << " = " << ActionToString(codemap[i]) << std::endl;;
898 std::map<std::string, Keymap::Action> action_map = {
899 { "none", Keymap::NONE },
900 { "move_forward", Keymap::MOVE_FORWARD },
901 { "move_backward", Keymap::MOVE_BACKWARD },
902 { "move_left", Keymap::MOVE_LEFT },
903 { "move_right", Keymap::MOVE_RIGHT },
904 { "move_up", Keymap::MOVE_UP },
905 { "move_down", Keymap::MOVE_DOWN },
907 { "primary", Keymap::PRIMARY },
908 { "secondary", Keymap::SECONDARY },
909 { "tertiary", Keymap::TERTIARY },
911 { "inventory_next", Keymap::INV_NEXT },
912 { "inventory_prev", Keymap::INV_PREVIOUS },
913 { "inventory_1", Keymap::INV_1 },
914 { "inventory_2", Keymap::INV_2 },
915 { "inventory_3", Keymap::INV_3 },
916 { "inventory_4", Keymap::INV_4 },
917 { "inventory_5", Keymap::INV_5 },
918 { "inventory_6", Keymap::INV_6 },
919 { "inventory_7", Keymap::INV_7 },
920 { "inventory_8", Keymap::INV_8 },
921 { "inventory_9", Keymap::INV_9 },
922 { "inventory_10", Keymap::INV_10 },
924 { "toggle_audio", Keymap::TOGGLE_AUDIO },
925 { "toggle_video", Keymap::TOGGLE_VIDEO },
926 { "toggle_hud", Keymap::TOGGLE_HUD },
927 { "toggle_debug", Keymap::TOGGLE_DEBUG },
928 { "camera_next", Keymap::CAMERA_NEXT },
930 { "exit", Keymap::EXIT },
935 const char *Keymap::ActionToString(Action action) {
936 for (const auto &entry : action_map) {
937 if (action == entry.second) {
938 return entry.first.c_str();
944 Keymap::Action Keymap::StringToAction(const std::string &str) {
945 auto entry = action_map.find(str);
946 if (entry != action_map.end()) {
947 return entry->second;
949 std::cerr << "unknown action \"" << str << '"' << std::endl;