Tips

「Tips」の編集履歴(バックアップ)一覧に戻る

Tips - (2018/09/14 (金) 13:33:10) のソース

*** 地面のアイテムを取得する
map_stackクラスとitem_stack_iteratorクラスを使う。
以下は隣接する床から人間の死体(human_flesh)を探すコード。
#highlight(lua){{
function get_item_on_floor(p)
    for delta_x = -1, 1 do
        for delta_y = -1, 1 do
            local tpoint = tripoint(p.x + delta_x, p.y + delta_y, p.z)
            local stack = map:i_at(tpoint)
            local iter = stack:cppbegin()
            while iter ~= stack:cppend() do
                local tmp = iter:elem()
                if tmp:is_corpse() then
                    if tmp:get_mtype():get_meat_itype() == "human_flesh" then
                        return tmp, tpoint
                    end
                end
                iter:inc()
            end
        end
    end
    return nil, nil
end
}}

*** 弾薬タイプをstring型で取得する
以下のようにammotypeクラスのstr関数を使う。
これに限らず`string_id = "hogehoge"`のように定義されているクラスはstr関数でstring型の実体を取得することができる。
#highlight(lua){{
local at = it:ammo_type()
local at_string = at:str()
}}

*** 足下の地形を取得する
名前の取得は以下のようにternameで可能。
#highlight(lua){{
local terrain_name = map:tername(player:pos())
}}
idをstring型で取得するときは以下のようにterでter_idを取得し、そこからget_terrain_typeでter_tクラスを生成する必要がある。(ter_idの実体はint型のため)
#highlight(lua){{
local terrain_int_id = map:ter(player:pos()):to_i()
local terrain = game.get_terrain_type(terrain_int_id)
local terrain_str_id = terrain.id:str()
}}

*** 容器の容量を取得する
get_remaining_capacity_for_liquidを使う。この関数は液体のアイテムが容器にあといくつ入るかを返す関数である。
同じ容積でも入れる液体によって返ってくる値は違うので注意すること。
(C++側にはget_container_capacityという関数があってそっちを使えば早いのだが、lua側には提供されてない)
#highlight(lua){{
local tmp_container = item("bottle_plastic", 1)
local tmp_liquid = item("water", 1)
local capacity_in_250ml = tmp_container:get_remaining_capacity_for_liquid(tmp_liquid, true)
}}