s2s2lua 0.1.0

Transpile Stranded II .s2s scripts to Lua for OpenStranded
Documentation
// s2s2lua – transpile Stranded II .s2s scripts to Lua for OpenStranded
// Copyright (C) 2025  s2s2lua contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use super::*;

impl LuaGenerator {
    pub(super) fn emit_event_handler(&mut self, event: &str, body: &[Stmt]) -> S2sResult<()> {
        self.emit_indent();
        self.emit(&format!(
            "{}on_event(\"{}\", function()\n",
            self.options.command_prefix, event
        ));
        self.indent += 1;
        for stmt in body {
            self.emit_statement(stmt)?;
        }
        self.indent -= 1;
        self.emit_indent();
        self.emit("end)\n\n");
        Ok(())
    }

    pub(super) fn emit_if(
        &mut self,
        condition: &Expr,
        then_body: &[Stmt],
        else_ifs: &[ElseIfClause],
        else_body: &[Stmt],
    ) -> S2sResult<()> {
        self.emit_indent();
        self.emit(&format!("if {} then\n", self.expr_to_lua(condition)));
        self.indent += 1;
        for stmt in then_body {
            self.emit_statement(stmt)?;
        }
        self.indent -= 1;

        for elseif_clause in else_ifs {
            self.emit_indent();
            self.emit(&format!(
                "elseif {} then\n",
                self.expr_to_lua(&elseif_clause.condition)
            ));
            self.indent += 1;
            for stmt in &elseif_clause.body {
                self.emit_statement(stmt)?;
            }
            self.indent -= 1;
        }

        if !else_body.is_empty() {
            self.emit_indent();
            self.emit("else\n");
            self.indent += 1;
            for stmt in else_body {
                self.emit_statement(stmt)?;
            }
            self.indent -= 1;
        }

        self.emit_indent();
        self.emit("end\n");
        Ok(())
    }

    pub(super) fn emit_loop(&mut self, mode: &str, arg: &str, body: &[Stmt]) -> S2sResult<()> {
        if mode == "count" {
            self.emit_indent();
            self.emit(&format!(
                "{}loop(\"count\", {}, function(id)\n",
                self.options.command_prefix, arg
            ));
        } else {
            let class = match mode {
                "objects" => "object",
                "units" => "unit",
                "items" => "item",
                "infos" => "info",
                _ => mode,
            };
            self.emit_indent();
            self.emit(&format!(
                "{}loop({{class=\"{}\", type={}}}, function(id)\n",
                self.options.command_prefix, class, arg
            ));
        }

        self.indent += 1;
        let prev = std::mem::replace(&mut self.inside_loop, true);
        for stmt in body {
            self.emit_statement(stmt)?;
        }
        self.inside_loop = prev;
        self.indent -= 1;
        self.emit_indent();
        self.emit("end)\n");
        Ok(())
    }
}