diff --git a/tests/unit/test_door_wings.cpp b/tests/unit/test_door_wings.cpp index 290260f6a..9eff62441 100644 --- a/tests/unit/test_door_wings.cpp +++ b/tests/unit/test_door_wings.cpp @@ -2,6 +2,7 @@ #include "doctest.h" #include "vehicle/doorwings.h" +#include "utilities/parser.h" // the poses of the animated door wings (eu07/maszyna#512): a wing of the main model and its namesake in the low // poly interior get the same pose, worked out once from the door position of the mover @@ -52,3 +53,155 @@ TEST_CASE("door wings: a folding wing turns one way and its larger wing twice as CHECK(closed.larger == 0.f); CHECK(closed.step == 0.f); } + +// a wing with its own kind of animation (eu07/maszyna#70): animdoor: in the .mmd, for vehicles whose doors on one side +// aren't all alike, e.g. 111Ainw with folding doors at one end and wider ones at the other + +namespace { + +// the spec of the .fiz the entries start from: folding doors which turn by 80 degrees +door_wings::wing_spec const fizdoors{0, door_wings::fold_type, 80.f, 0.f}; + +// reads the entry after animdoor: from the text, starting from the spec of the .fiz +std::string read(std::string const &Entry, door_wings::wing_spec &Spec) { + Spec = fizdoors; + cParser parser(Entry, cParser::buffer_TEXT); + return door_wings::read_spec(parser, Spec); +} + +} // namespace + +TEST_CASE("door wings: the kinds of animation are named as DoorOpenMethod of the .fiz") { + CHECK(door_wings::type_from_name("Shift") == door_wings::shift_type); + CHECK(door_wings::type_from_name("Rotate") == door_wings::rotate_type); + CHECK(door_wings::type_from_name("Fold") == door_wings::fold_type); + CHECK(door_wings::type_from_name("Plug") == door_wings::plug_type); + // in any letter case + CHECK(door_wings::type_from_name("plug") == door_wings::plug_type); + CHECK(door_wings::type_from_name("SHIFT") == door_wings::shift_type); + // the same numbers as TMoverParameters::door_data::type + CHECK(door_wings::shift_type == 1); + CHECK(door_wings::rotate_type == 2); + CHECK(door_wings::fold_type == 3); + CHECK(door_wings::plug_type == 4); + CHECK_FALSE(door_wings::type_from_name("Roll").has_value()); + CHECK_FALSE(door_wings::type_from_name("").has_value()); +} + +TEST_CASE("door wings: an animdoor entry gives the wing its own kind, range and plug shift") { + door_wings::wing_spec spec; + CHECK(read("3 { type: Plug range: 0.9 rangeout: 0.06 } endmodels", spec).empty()); + CHECK(spec.number == 3); + CHECK(spec.type == door_wings::plug_type); + CHECK(spec.range == doctest::Approx(0.9f)); + CHECK(spec.range_out == doctest::Approx(0.06f)); +} + +TEST_CASE("door wings: the keys an animdoor entry leaves out keep the values of the .fiz") { + door_wings::wing_spec spec; + SUBCASE("only the kind") { + CHECK(read("2 { type: Rotate }", spec).empty()); + CHECK(spec.number == 2); + CHECK(spec.type == door_wings::rotate_type); + CHECK(spec.range == 80.f); + CHECK(spec.range_out == 0.f); + } + SUBCASE("only the range, keys in any letter case and order") { + CHECK(read("1 { Range: -95 }", spec).empty()); + CHECK(spec.type == door_wings::fold_type); + CHECK(spec.range == -95.f); + } + SUBCASE("nothing") { + CHECK(read("4 { }", spec).empty()); + CHECK(spec.number == 4); + CHECK(spec.type == fizdoors.type); + CHECK(spec.range == fizdoors.range); + } +} + +TEST_CASE("door wings: a faulty animdoor entry says what is wrong with it") { + door_wings::wing_spec spec; + CHECK(read("3 type: Shift range: 0.65", spec) == "no { after the wing number"); + CHECK(read("3 { type: Roll range: 0.65 }", spec) == "unknown door type \"Roll\""); + CHECK(read("3 { type: Shift range: 0,65 }", spec) == "bad number \"0,65\" for \"range:\""); + CHECK(read("3 { type: Shift range: wide }", spec) == "bad number \"wide\" for \"range:\""); + CHECK(read("3 { kind: Shift }", spec) == "no } after the keys, \"kind:\" is none of them"); + CHECK(read("3 { type: Shift range: }", spec) == "no value for \"range:\""); + CHECK(read("3 { type: Shift range: 0.65", spec) == "no } after the keys"); +} + +TEST_CASE("door wings: an animdoor entry without its } leaves the entries after it to the .mmd") { + // the first token that is neither a key nor the value of one ends the entry, and is read next by the caller + auto const next{[](std::string const &Text) { + door_wings::wing_spec spec{fizdoors}; + cParser parser(Text, cParser::buffer_TEXT); + auto const error{door_wings::read_spec(parser, spec)}; + std::vector rest; + std::string token; + do { + token.clear(); + parser.getTokens(1, false); + parser >> token; + if (false == token.empty()) { + rest.emplace_back(token); + } + } while (false == token.empty()); + return std::pair{error, rest}; + }}; + { + auto const [error, rest]{next("3 { type: Plug range: 0.9\nsounds: horn.wav\nlights: 1\nendmodels")}; + CHECK(error == "no } after the keys, \"sounds:\" is none of them"); + CHECK(rest == std::vector{"sounds:", "horn.wav", "lights:", "1", "endmodels"}); + } + { + auto const [error, rest]{next("3 { type: Plug range: endmodels")}; + CHECK(error == "no } after the keys, \"endmodels\" is none of them"); + CHECK(rest == std::vector{"endmodels"}); + } + { + // a whole entry leaves the rest as it is + auto const [error, rest]{next("3 { type: Plug range: 0.9 } sounds: horn.wav endmodels")}; + CHECK(error.empty()); + CHECK(rest == std::vector{"sounds:", "horn.wav", "endmodels"}); + } +} + +TEST_CASE("door wings: the parsed keys go into the spec, pairs of a key and its value") { + door_wings::wing_spec spec{fizdoors}; + CHECK(door_wings::parse_spec({"type:", "Shift", "range:", "0.65"}, spec).empty()); + CHECK(spec.type == door_wings::shift_type); + CHECK(spec.range == doctest::Approx(0.65f)); + CHECK(door_wings::parse_spec({"rangeout:"}, spec) == "no value for \"rangeout:\""); + CHECK(door_wings::parse_spec({}, spec).empty()); +} + +TEST_CASE("door wings: only the wings with an entry animate in their own way, the latest entry counts") { + std::vector specs; + CHECK(door_wings::find(specs, 1) == nullptr); + door_wings::store(specs, {3, door_wings::shift_type, 0.65f, 0.f}); + door_wings::store(specs, {4, door_wings::shift_type, 0.65f, 0.f}); + door_wings::store(specs, {3, door_wings::plug_type, 0.9f, 0.06f}); + CHECK(specs.size() == 2); + CHECK(door_wings::find(specs, 1) == nullptr); + CHECK(door_wings::find(specs, 2) == nullptr); + REQUIRE(door_wings::find(specs, 3) != nullptr); + CHECK(door_wings::find(specs, 3)->type == door_wings::plug_type); + CHECK(door_wings::find(specs, 3)->range == 0.9f); + REQUIRE(door_wings::find(specs, 4) != nullptr); + CHECK(door_wings::find(specs, 4)->type == door_wings::shift_type); +} + +TEST_CASE("door wings: a wing with its own range is as far along it as the door of its side") { + // folding doors of the .fiz turn by 80 degrees; the wide wing slides by 0.9 m + CHECK(door_wings::wing_position(0.f, 80.f, 0.9f) == 0.f); + CHECK(door_wings::wing_position(40.f, 80.f, 0.9f) == doctest::Approx(0.45f)); + CHECK(door_wings::wing_position(80.f, 80.f, 0.9f) == doctest::Approx(0.9f)); + // a wing turning the other way + CHECK(door_wings::wing_position(20.f, 80.f, -100.f) == doctest::Approx(-25.f)); + // no doors in the .fiz: the wing stays closed + CHECK(door_wings::wing_position(0.f, 0.f, 0.9f) == 0.f); + // the pose of the plug wing at the end of its range + auto const open{door_wings::plug(door_wings::wing_position(80.f, 80.f, 0.9f), 0.06f)}; + CHECK(open.x == doctest::Approx(0.06f)); + CHECK(open.z == doctest::Approx(0.87f)); +} diff --git a/vehicle/DynObj.cpp b/vehicle/DynObj.cpp index 64354d774..3780851e3 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -700,6 +700,62 @@ void TDynamicObject::UpdateDoorPlug(TAnim *pAnim) { } } +// a wing animated in its own way (animdoor: of the .mmd): as far along its own range as the door of its side is +void TDynamicObject::UpdateDoorWing( TAnim *pAnim, door_wings::wing_spec const &Spec ) { + + if( pAnim->smAnimated == nullptr ) { return; } + + auto const position { door_wings::wing_position( door_position( *MoverParameters, pAnim ), MoverParameters->Doors.range, Spec.range ) }; + for( auto *wing : wing_parts( pAnim ) ) { + if( wing == nullptr ) { continue; } + switch( Spec.type ) { + case door_wings::shift_type: { + wing->SetTranslate( door_wings::slide( position ) ); + break; + } + case door_wings::rotate_type: { + wing->SetRotate( float3( 1, 0, 0 ), position ); + break; + } + case door_wings::fold_type: { + fold_wing( wing, door_wings::fold( position ) ); + break; + } + case door_wings::plug_type: { + wing->SetTranslate( door_wings::plug( position, Spec.range_out ) ); + break; + } + default: { + break; + } + } + } +} + +void TDynamicObject::bind_door_animation( TAnim &Animation ) { + + if( auto const *spec { door_wings::find( m_doorwings, Animation.iNumber ) } ) { + Animation.yUpdate = [ this, spec = *spec ]( TAnim *pAnim ) { UpdateDoorWing( pAnim, spec ); }; + return; + } + switch( MoverParameters->Doors.type ) { // od razu zapinamy potrzebny typ animacji + case 1: + Animation.yUpdate = std::bind( &TDynamicObject::UpdateDoorTranslate, this, std::placeholders::_1 ); + break; + case 2: + Animation.yUpdate = std::bind( &TDynamicObject::UpdateDoorRotate, this, std::placeholders::_1 ); + break; + case 3: + Animation.yUpdate = std::bind( &TDynamicObject::UpdateDoorFold, this, std::placeholders::_1 ); + break; // obrót 3 kolejnych submodeli + case 4: + Animation.yUpdate = std::bind( &TDynamicObject::UpdateDoorPlug, this, std::placeholders::_1 ); + break; + default: + break; + } +} + void TDynamicObject::UpdatePant(TAnim *pAnim) { // animacja pantografu - 4 obracane ramiona, ślizg piąty float a, b, c; @@ -5882,24 +5938,8 @@ void TDynamicObject::LoadMMediaFile( std::string const &TypeName, std::string co if (pAnimations[i + j].smAnimated) { //++iAnimatedDoors; pAnimations[i + j].smAnimated->WillBeAnimated(); // wyłączenie optymalizacji transformu - switch( MoverParameters->Doors.type ) - { // od razu zapinamy potrzebny typ animacji - case 1: - pAnimations[ i + j ].yUpdate = std::bind( &TDynamicObject::UpdateDoorTranslate, this, std::placeholders::_1 ); - break; - case 2: - pAnimations[ i + j ].yUpdate = std::bind( &TDynamicObject::UpdateDoorRotate, this, std::placeholders::_1 ); - break; - case 3: - pAnimations[ i + j ].yUpdate = std::bind( &TDynamicObject::UpdateDoorFold, this, std::placeholders::_1 ); - break; // obrót 3 kolejnych submodeli - case 4: - pAnimations[ i + j ].yUpdate = std::bind( &TDynamicObject::UpdateDoorPlug, this, std::placeholders::_1 ); - break; - default: - break; - } pAnimations[i + j].iNumber = i + 1; // parzyste działają inaczej niż nieparzyste + bind_door_animation( pAnimations[ i + j ] ); // od razu zapinamy potrzebny typ animacji pAnimations[i + j].fMaxDist = 300 * 300; // drzwi to z daleka widać /* // NOTE: no longer used @@ -5910,6 +5950,34 @@ void TDynamicObject::LoadMMediaFile( std::string const &TypeName, std::string co } } + else if( token == "animdoor:" ) { + // own kind of animation of one door wing, in place of the one of the .fiz (eu07/maszyna#70): + // animdoor: { type: range: rangeout: } + // the keys left out keep the values of the .fiz. the wing still follows the door of its side + door_wings::wing_spec spec { + 0, + MoverParameters->Doors.type, + MoverParameters->Doors.range, + MoverParameters->Doors.range_out }; + auto error { door_wings::read_spec( parser, spec ) }; + if( error.empty() + && ( spec.number < 1 || spec.number > iAnimType[ ANIM_DOORS ] ) ) { + error = "no door wing " + std::to_string( spec.number ) + " among the door animations"; + } + if( false == error.empty() ) { + ErrorLog( "Bad model: " + asFileName + " - animdoor: " + error, logtype::model ); + } + else { + door_wings::store( m_doorwings, spec ); + // a wing animdoorprefix has found already gets the new kind of animation now; the ones it + // finds later get it then + auto const index { static_cast( std::accumulate( std::begin( iAnimType ), std::begin( iAnimType ) + ANIM_DOORS, 0 ) + spec.number - 1 ) }; + if( index < pAnimations.size() && pAnimations[ index ].yUpdate ) { + bind_door_animation( pAnimations[ index ] ); + } + } + } + else if( token == "animstepprefix:" ) { // animated doorstep submodel name prefix int i, j; diff --git a/vehicle/DynObj.h b/vehicle/DynObj.h index 7e2fb6e37..db427e34d 100644 --- a/vehicle/DynObj.h +++ b/vehicle/DynObj.h @@ -22,6 +22,7 @@ http://mozilla.org/MPL/2.0/. #include "audio/sound.h" #include "world/Spring.h" #include "vehicle/mirrors.h" +#include "vehicle/doorwings.h" #include "scripting/scriptscreenrules.h" #include @@ -305,6 +306,12 @@ private: void UpdateDoorRotate(TAnim *pAnim); // animacja drzwi - obrót void UpdateDoorFold(TAnim *pAnim); // animacja drzwi - składanie void UpdateDoorPlug(TAnim *pAnim); // animacja drzwi - odskokowo-przesuwne + // a wing with its own kind of animation, given in the .mmd (animdoor:), in place of the one of the .fiz + void UpdateDoorWing( TAnim *pAnim, door_wings::wing_spec const &Spec ); + // sets the update of a door wing animation, for its own kind if the .mmd gives one, otherwise for the .fiz + void bind_door_animation( TAnim &Animation ); + // door wings animated in their own way, from the .mmd (eu07/maszyna#70) + std::vector m_doorwings; void UpdatePant(TAnim *pAnim); // animacja pantografu void UpdatePlatformTranslate(TAnim *pAnim); // doorstep animation, shift void UpdatePlatformRotate(TAnim *pAnim); // doorstep animation, rotate diff --git a/vehicle/doorwings.h b/vehicle/doorwings.h index 07286aac8..db05b03fc 100644 --- a/vehicle/doorwings.h +++ b/vehicle/doorwings.h @@ -10,6 +10,13 @@ http://mozilla.org/MPL/2.0/. #pragma once #include +#include +#include +#include +#include +#include +#include +#include #include @@ -17,6 +24,12 @@ http://mozilla.org/MPL/2.0/. // in the main model and, when the low poly interior has a submodel of the same name, in the interior as well namespace door_wings { +// the kinds of wing animation, numbered as DoorOpenMethod of the .fiz (TMoverParameters::door_data::type) +inline constexpr int shift_type{1}; +inline constexpr int rotate_type{2}; +inline constexpr int fold_type{3}; +inline constexpr int plug_type{4}; + // whether the animation with the given flags (TAnim::iFlags, the kind of animation in bits 4-7) moves a door wing inline bool is_wing(int const Flags) { return (Flags & 0xF0) == 0x10; @@ -49,4 +62,145 @@ inline fold_angles fold(float const Position) { return {Position, -Position - Position, Position}; } +// the animation of one wing given in the .mmd, in place of the one the .fiz sets for all door wings of the vehicle +// (eu07/maszyna#70). the wing still follows the door of its side, over its own range: +// animdoor: { type: range: rangeout: } +// every key is optional; a key left out keeps the value of the .fiz +struct wing_spec { + int number{0}; // of the wing, as in animdoorprefix: door1 is 1 + int type{rotate_type}; + float range{0.f}; // shift or angle of the wing when the door is fully open + float range_out{0.f}; // how far a plug wing is pushed out +}; + +// the kind of wing animation with the given name of DoorOpenMethod, in any letter case +inline std::optional type_from_name(std::string_view const Name) { + std::string name{Name}; + std::transform(name.begin(), name.end(), name.begin(), [](unsigned char const Character) { return static_cast(std::tolower(Character)); }); + if (name == "shift") { + return shift_type; + } + if (name == "rotate") { + return rotate_type; + } + if (name == "fold") { + return fold_type; + } + if (name == "plug") { + return plug_type; + } + return std::nullopt; +} + +// the number written in the whole of the text, or nothing +inline std::optional number_from_text(std::string const &Text) { + if (Text.empty()) { + return std::nullopt; + } + char *end{nullptr}; + auto const value{std::strtof(Text.c_str(), &end)}; + if (end != Text.c_str() + Text.size() || false == std::isfinite(value)) { + return std::nullopt; + } + return value; +} + +// applies the keys and values of the block of an animdoor entry (the tokens between the braces) to the spec, which +// comes filled with the values of the .fiz. returns what is wrong with the block, or an empty text when nothing is; +// the keys before the fault are applied +inline std::string parse_spec(std::vector const &Tokens, wing_spec &Spec) { + for (std::size_t index = 0; index < Tokens.size(); index += 2) { + std::string key{Tokens[index]}; + std::transform(key.begin(), key.end(), key.begin(), [](unsigned char const Character) { return static_cast(std::tolower(Character)); }); + if (index + 1 >= Tokens.size()) { + return "no value for \"" + key + "\""; + } + auto const &value{Tokens[index + 1]}; + if (key == "type:") { + auto const type{type_from_name(value)}; + if (false == type.has_value()) { + return "unknown door type \"" + value + "\""; + } + Spec.type = *type; + } + else if (key == "range:" || key == "rangeout:") { + auto const number{number_from_text(value)}; + if (false == number.has_value()) { + return "bad number \"" + value + "\" for \"" + key + "\""; + } + (key == "range:" ? Spec.range : Spec.range_out) = *number; + } + else { + return "unknown key \"" + key + "\""; + } + } + return {}; +} + +// whether the token is one of the keys of an animdoor block, in any letter case +inline bool is_spec_key(std::string const &Token) { + std::string key{Token}; + std::transform(key.begin(), key.end(), key.begin(), [](unsigned char const Character) { return static_cast(std::tolower(Character)); }); + return key == "type:" || key == "range:" || key == "rangeout:"; +} + +// reads an animdoor entry after its key: the wing number and the block in braces, into the spec filled with the values +// of the .fiz. returns what is wrong with the entry, or an empty text when nothing is. the parser is a cParser. a block +// without its } ends at the first token that is neither a key nor the value right after one (or at endmodels): that +// token is left for the caller, so the entries after a faulty one are read as ever +template +std::string read_spec(Parser_ &Parser, wing_spec &Spec) { + Parser.getTokens(1, false); + Parser >> Spec.number; + std::string token; + Parser.getTokens(1, false); + Parser >> token; + if (token != "{") { + return "no { after the wing number"; + } + std::vector block; + auto value{false}; // the token is the value of the key before it + while (true) { + token.clear(); + Parser.getTokens(1, false); + Parser >> token; + if (token.empty()) { + return "no } after the keys"; + } + if (token == "}") { + break; + } + if (token == "endmodels" || (false == value && false == is_spec_key(token))) { + Parser.injectString(token); + return "no } after the keys, \"" + token + "\" is none of them"; + } + value = false == value; + block.emplace_back(token); + } + return parse_spec(block, Spec); +} + +// the spec given for the wing with the given number, or null when the wing animates as the .fiz says +inline wing_spec const *find(std::vector const &Specs, int const Number) { + auto const lookup{std::find_if(Specs.begin(), Specs.end(), [Number](wing_spec const &Spec) { return Spec.number == Number; })}; + return lookup != Specs.end() ? &(*lookup) : nullptr; +} + +// keeps the spec of a wing; a later entry for the same wing replaces the earlier one +inline void store(std::vector &Specs, wing_spec const &Spec) { + auto lookup{std::find_if(Specs.begin(), Specs.end(), [&Spec](wing_spec const &Stored) { return Stored.number == Spec.number; })}; + if (lookup != Specs.end()) { + *lookup = Spec; + } + else { + Specs.emplace_back(Spec); + } +} + +// the shift or angle of a wing which opens over Wingrange, while the door of its side opens over Doorrange: the wing +// is as far along its range as the door is along the door range +inline float wing_position(float const Doorposition, float const Doorrange, float const Wingrange) { + return Doorrange != 0.f ? Doorposition / Doorrange * Wingrange : 0.f; +} + } // namespace door_wings