Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

converta código php em c++ <?php /** * @fileoverview Action - Loader for the Gravity…

  1. User

    converta código php em c++

  2. Assistant

    Sure! Below is the C++ version of your PHP Action class code:

    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    #include <stdexcept>
    #include <cstring>
    #include <cmath>
    
    class Action {
    private:
        std::ifstream fp;
        double version;
        std::vector<std::string> sounds;
    
    public:
        struct Layer {
            int x, y;
            int index;
            int is_mirror;
            std::vector<double> scale;
            std::vector<double> color;
            double angle;
            int spr_type;
            int width;
            int height;
        };
    
        struct Animation {
            std::vector<Layer> layers;
            int sound;
            // Position data can be added here if needed
        };
    
        struct ActionData {
            std::vector<Animation> animations;
            double delay;
        };
    
        std::vector<ActionData> actions;
    
        // Constructor
        Action(const std::string& filename = "") {
            if (!filename.empty())
                open(filename);
        }
    
        // Open a Action file
        void open(const std::string& filename) {
            fp.open(filename, std::ios::binary);
            if (!fp.is_open())
                throw std::runtime_error("ACT::open() - Can't find or read file '" + filename + "'.");
    
            // Get file size
            fp.seekg(0, std::ios::end);
            std::streampos size = fp.tellg();
            if (size < 0x08) {
                fp.close();
                throw std::runtime_error("ACT::open() - Incorrect file size, shouldn't be an ACT file.");
            }
            fp.seekg(0, std::ios::beg);
    
            // Read header
            char head[2];
            unsigned char ver1, ver2;
            fp.read(head, 2);
            fp.read(reinterpret_cast<char*>(&ver1), 1);
            fp.read(reinterpret_cast<char*>(&ver2), 1);
    
            if (std::strncmp(head, "AC", 2) != 0) {
                fp.close();
                throw std::runtime_error("ACT::load() - Incorrect act header, should be 'AC'");
            }
    
            version = ver1 / 10.0 + ver2;
        }
    
        // Adding exclusively for the chargen to speed up the process.
        Animation getAnimation(int action, int& animation, int doridori = -1) {
            // Skip header
            fp.seekg(0x4, std::ios::beg);
    
            // Pre-calculate layer size
            int layer_size;
            if (version < 2.0)
                layer_size = 16;
            else if (version < 2.4)
                layer_size = 32;
            else if (std::abs(version - 2.4) < 1e-6)
                layer_size = 36;
            else
                layer_size = 44;
    
            // Read actions
            uint16_t action_count;
            fp.read(reinterpret_cast<char*>(&action_count), 2);
            fp.seekg(10, std::ios::cur); // Skip next 10 bytes
    
            action %= action_count;
    
            for (int i = 0; i < action_count; ++i) {
                // Read animation count
                uint32_t animation_count;
                fp.read(reinterpret_cast<char*>(&animation_count), 4);
    
                if (i == action) {
                    // Doridori feature
                    if (doridori > -1) {
                        animation = (animation_count / 3) * doridori;
                    }
    
                    // Cap
                    if (animation > static_cast<int>(animation_count - 1))
                        animation = animation_count - 1;
                    if (animation < 0)
                        animation = 0;
                }
    
                for (uint32_t j = 0; j < animation_count; ++j) {
                    // Skip 32 bytes
                    fp.seekg(32, std::ios::cur);
    
                    // End.
                    if (action == i && animation == static_cast<int>(j))
                        return readLayers();
    
                    // Read layers
                    uint32_t layer_count;
                    fp.read(reinterpret_cast<char*>(&layer_count), 4);
                    fp.seekg(layer_size * layer_count, std::ios::cur);
    
                    if (version >= 2.0)
                        fp.seekg(4, std::ios::cur);
    
                    if (version >= 2.3) {
                        uint32_t pos_count;
                        fp.read(reinterpret_cast<char*>(&pos_count), 4);
                        fp.seekg(pos_count * 16, std::ios::cur);
                    }
                }
            }
    
            throw std::runtime_error("Action not found?");
        }
    
        // Load an Action file
        void load() {
            readActions();
    
            if (version >= 2.1) {
                // Sound
                uint32_t count;
                fp.read(reinterpret_cast<char*>(&count), 4);
                sounds.resize(count);
                for (uint32_t i = 0; i < count; ++i) {
                    char buffer[40];
                    fp.read(buffer, 40);
                    sounds[i] = std::string(buffer, 40);
                }
    
                // Delay
                if (version >= 2.2) {
                    for (auto& action : actions) {
                        float delay;
                        fp.read(reinterpret_cast<char*>(&delay), 4);
                        action.delay = delay * 25;
                    }
                }
            }
        }
    
    private:
        // Load ACT actions
        void readActions() {
            uint16_t count;
            fp.read(reinterpret_cast<char*>(&count), 2);
            fp.seekg(10, std::ios::cur); // Skip next 10 bytes
    
            actions.resize(count);
            for (uint16_t i = 0; i < count; ++i) {
                actions[i].animations = readAnimations();
                actions[i].delay = 150;
            }
        }
    
        // Load ACT animations
        std::vector<Animation> readAnimations() {
            uint32_t count;
            fp.read(reinterpret_cast<char*>(&count), 4);
            std::vector<Animation> animations(count);
    
            for (uint32_t i = 0; i < count; ++i) {
                fp.seekg(32, std::ios::cur);
                animations[i] = readLayers();
            }
    
            return animations;
        }
    
        // Load ACT layers
        Animation readLayers() {
            Animation animation;
            uint32_t count;
            fp.read(reinterpret_cast<char*>(&count), 4);
    
            for (uint32_t i = 0; i < count; ++i) {
                Layer layer;
                // Read base layer data
                uint32_t x, y;
                int32_t index;
                uint32_t is_mirror;
    
                fp.read(reinterpret_cast<char*>(&x), 4);
                fp.read(reinterpret_cast<char*>(&y), 4);
                fp.read(reinterpret_cast<char*>(&index), 4);
                fp.read(reinterpret_cast<char*>(&is_mirror), 4);
    
                layer.x = static_cast<int>(x);
                layer.y = static_cast<int>(y);
                layer.index = index;
                layer.is_mirror = static_cast<int>(is_mirror);
                layer.scale = {1.0, 1.0};
                layer.color = {1.0, 1.0, 1.0, 1.0};
                layer.angle = 0;
                layer.spr_type = 0;
                layer.width = 0;
                layer.height = 0;
    
                // Read additional data based on version
                if (version >= 2.0 && version < 2.4) {
                    unsigned char color[4];
                    float scale1;
                    uint32_t angle;
                    uint32_t spr_type;
    
                    fp.read(reinterpret_cast<char*>(color), 4);
                    fp.read(reinterpret_cast<char*>(&scale1), 4);
                    fp.read(reinterpret_cast<char*>(&angle), 4);
                    fp.read(reinterpret_cast<char*>(&spr_type), 4);
    
                    layer.color[0] = color[0] / 255.0;
                    layer.color[1] = color[1] / 255.0;
                    layer.color[2] = color[2] / 255.0;
                    layer.color[3] = color[3] / 255.0;
    
                    layer.scale[0] = scale1;
                    layer.scale[1] = scale1;
    
                    layer.angle = static_cast<double>(angle);
                    layer.spr_type = static_cast<int>(spr_type);
                } else if (std::abs(version - 2.4) < 1e-6) {
                    unsigned char color[4];
                    float scale1, scale2;
                    uint32_t angle;
                    uint32_t spr_type;
    
                    fp.read(reinterpret_cast<char*>(color), 4);
                    fp.read(reinterpret_cast<char*>(&scale1), 4);
                    fp.read(reinterpret_cast<char*>(&scale2), 4);
                    fp.read(reinterpret_cast<char*>(&angle), 4);
                    fp.read(reinterpret_cast<char*>(&spr_type), 4);
    
                    layer.color[0] = color[0] / 255.0;
                    layer.color[1] = color[1] / 255.0;
                    layer.color[2] = color[2] / 255.0;
                    layer.color[3] = color[3] / 255.0;
    
                    layer.scale[0] = scale1;
                    layer.scale[1] = scale2;
    
                    layer.angle = static_cast<double>(angle);
                    layer.spr_type = static_cast<int>(spr_type);
                } else if (version > 2.4) {
                    unsigned char color[4];
                    float scale1, scale2;
                    uint32_t angle;
                    uint32_t spr_type;
                    uint32_t width;
                    uint32_t height;
    
                    fp.read(reinterpret_cast<char*>(color), 4);
                    fp.read(reinterpret_cast<char*>(&scale1), 4);
                    fp.read(reinterpret_cast<char*>(&scale2), 4);
                    fp.read(reinterpret_cast<char*>(&angle), 4);
                    fp.read(reinterpret_cast<char*>(&spr_type), 4);
                    fp.read(reinterpret_cast<char*>(&width), 4);
                    fp.read(reinterpret_cast<char*>(&height), 4);
    
                    layer.color[0] = color[0] / 255.0;
                    layer.color[1] = color[1] / 255.0;
                    layer.color[2] = color[2] / 255.0;
                    layer.color[3] = color[3] / 255.0;
    
                    layer.scale[0] = scale1;
                    layer.scale[1] = scale2;
    
                    layer.angle = static_cast<double>(angle);
                    layer.spr_type = static_cast<int>(spr_type);
                    layer.width = static_cast<int>(width);
                    layer.height = static_cast<int>(height);
                }
    
                animation.layers.push_back(layer);
            }
    
            // Read sound
            if (version >= 2.0) {
                uint32_t sound;
                fp.read(reinterpret_cast<char*>(&sound), 4);
                animation.sound = static_cast<int>(sound);
            } else {
                animation.sound = -1;
            }
    
            // Read position data if version >= 2.3
            if (version >= 2.3) {
                uint32_t pos_count;
                fp.read(reinterpret_cast<char*>(&pos_count), 4);
                fp.seekg(pos_count * 16, std::ios::cur); // Skipping pos data for simplicity
            }
    
            return animation;
        }
    
    public:
        // Compile an ACT file
        std::string compile() {
            std::string result;
    
            // Header
            char ver1 = static_cast<char>(std::fmod(version * 10, 10));
            char ver2 = static_cast<char>(std::floor(version * 10) / 10);
            result.append("AC", 2);
            result.push_back(ver1);
            result.push_back(ver2);
    
            // Action count
            uint16_t action_count = static_cast<uint16_t>(actions.size());
            result.append(reinterpret_cast<char*>(&action_count), 2);
            result.append(10, '\0'); // Padding with 10 null bytes
    
            // Compile each action
            for (const auto& action : actions) {
                // Number of animations
                uint32_t animation_count = static_cast<uint32_t>(action.animations.size());
                result.append(reinterpret_cast<char*>(&animation_count), 4);
    
                // Compile animations
                for (const auto& animation : action.animations) {
                    // 32 unknown bytes + layers count
                    result.append(32, '\0');
                    uint32_t layer_count = static_cast<uint32_t>(animation.layers.size());
                    result.append(reinterpret_cast<char*>(&layer_count), 4);
    
                    // Compile layers
                    for (const auto& layer : animation.layers) {
                        result.append(reinterpret_cast<const char*>(&layer.x), 4);
                        result.append(reinterpret_cast<const char*>(&layer.y), 4);
                        result.append(reinterpret_cast<const char*>(&layer.index), 4);
                        result.append(reinterpret_cast<const char*>(&layer.is_mirror), 4);
    
                        if (version >= 2.0) {
                            unsigned char color[4] = {
                                static_cast<unsigned char>(layer.color[0] * 255),
                                static_cast<unsigned char>(layer.color[1] * 255),
                                static_cast<unsigned char>(layer.color[2] * 255),
                                static_cast<unsigned char>(layer.color[3] * 255)
                            };
                            result.append(reinterpret_cast<char*>(color), 4);
    
                            if (version < 2.4) {
                                float scale = static_cast<float>(layer.scale[0]);
                                result.append(reinterpret_cast<char*>(&scale), 4);
                            } else {
                                float scale1 = static_cast<float>(layer.scale[0]);
                                float scale2 = static_cast<float>(layer.scale[1]);
                                result.append(reinterpret_cast<char*>(&scale1), 4);
                                result.append(reinterpret_cast<char*>(&scale2), 4);
                            }
    
                            uint32_t angle = static_cast<uint32_t>(layer.angle);
                            uint32_t spr_type = static_cast<uint32_t>(layer.spr_type);
                            result.append(reinterpret_cast<char*>(&angle), 4);
                            result.append(reinterpret_cast<char*>(&spr_type), 4);
    
                            if (version > 2.4) {
                                uint32_t width = static_cast<uint32_t>(layer.width);
                                uint32_t height = static_cast<uint32_t>(layer.height);
                                result.append(reinterpret_cast<char*>(&width), 4);
                                result.append(reinterpret_cast<char*>(&height), 4);
                            }
                        }
                    }
    
                    // Animation sound
                    if (version >= 2.0) {
                        uint32_t sound = static_cast<uint32_t>(animation.sound);
                        result.append(reinterpret_cast<char*>(&sound), 4);
                    }
    
                    // Animation imf (head pos)
                    if (version >= 2.3) {
                        uint32_t pos_count = 0; // Assuming no position data for simplicity
                        result.append(reinterpret_cast<char*>(&pos_count), 4);
                        // If position data exists, it can be appended here
                    }
                }
            }
    
            if (version >= 2.1) {
                // Compile sounds
                uint32_t sound_count = static_cast<uint32_t>(sounds.size());
                result.append(reinterpret_cast<char*>(&sound_count), 4);
                for (const auto& sound : sounds) {
                    char buffer[40] = {0};
                    std::memcpy(buffer, sound.c_str(), std::min(sound.length(), size_t(40)));
                    result.append(buffer, 40);
                }
    
                // Compile delay
                if (version >= 2.2) {
                    for (const auto& action : actions) {
                        float delay = static_cast<float>(action.delay) / 25.0f;
                        result.append(reinterpret_cast<char*>(&delay), 4);
                    }
                }
            }
    
            return result;
        }
    };
    

    Explanation:

    • Class Structure: The C++ Action class mimics the PHP class structure, with private members and public methods.

    • File Handling: Uses std::ifstream to handle file operations, reading binary data as needed.

    • Data Parsing: Reads and parses binary data from the file, carefully handling byte order and data types.

    • Version Handling: Checks the version to determine how to parse different sections of the file, similar to the PHP code.

    • Layer and Animation Structures: Defines Layer, Animation, and ActionData structs to store data, replicating the associative arrays and objects from the PHP code.

    • Error Handling: Throws exceptions (std::runtime_error) when errors occur, similar to PHP's Exception.

    • Data Conversion: Converts data from binary to appropriate types and scales values as needed (e.g., colors are divided by 255).

    • Compile Method: Constructs a binary string (std::string result) with the compiled ACT data, handling byte order and data types correctly.

    Note: This code assumes that you will handle the specifics of reading and writing binary data carefully, ensuring that endianness and data sizes match between PHP and C++. Adjustments might be necessary based on the exact specifications of the ACT file format and how data is stored.