自作のイベントスクリプトです。
このままでもコピペして使えますが、イベント発生条件をちゃんと自分で設定して使ったほうが良いでしょう。
Luaスクリプト
参考
- Lua reference manual (Freeciv公式wiki)
- Lua 5.2 リファレンスマニュアル (Lua言語のマニュアル)
- お気楽 Lua プログラミング超入門 (タイトルは超入門となっていますが内容はプログラミング方面寄りです)
注意: FreecivではLua関数がすべて使用できるわけではありません。
クライアント動作スクリプト
クライアント上のLuaコンソールにおいて使用するスクリプトコマンドです。
クライアントユーザーが把握できないデータを表示することはできません。また、各種の変更も (サーバーデータを操作することが不可能なため) 行うことができません。
Freecivのバージョンを表示
Freecivクライアントのバージョンを表示します。
chat.msg("%s", fc_version())
なお、次のサーバーコマンドを入力すると接続しているサーバーのバージョンが得られる。(注: lua コマンドの実行には Admin 権限が必要)
/lua cmd log.normal("%s", fc_version())
すべての Lua 変数を表示
使用可能なすべての Lua 変数 (function と table も含む) を表示する。
listenv()
内蔵の Lua のバージョンを表示
chat.msg("%s", _VERSION)
サーバー動作スクリプト
Freecivのサーバー上で動作するスクリプトです。
サーバーが把握するデータを操作したり、編集モードでやれるような多種の変更をすることができます。
今日の格言を表示する
ワールドマップが生成されたときに、プレイヤー民族にソビエトが含まれているとメッセージを表示する。
乱数、および配列を使用した基本例。
function wise_saying_callback() local wit_table = { "'If the opposition disarms, well and good. If it refuses to disarm, we shall disarm it ourselves.' - Joseph Stalin", "'Death solves all problems - no man, no problem.' - Joseph Stalin", "'In the Soviet army it takes more courage to retreat than advance.' - Joseph Stalin" } local number = random(1, #wit_table)
for player in players_iterate() do if player.nation:rule_name() == "Soviet" then notify.all("Today's wit (No. %d)\n %s", number, wit_table[number]) end end end
signal.connect("map_generated", "wise_saying_callback")
ユニットの被撃破でお金が入る
不思議Palaceを持つ国家においてユニットが撃破されると、被害国にユニットのコストに応じたお金が入る。
function get_gold_when_killed_callback(unit, loser, reason) local wonder_type = find.building_type("Palace") if loser:has_wonder(wonder_type) and reason == "killed" then local gold = math.floor( unit.utype:build_shield_cost() ) edit.change_gold(loser, gold) notify.event(unit.owner, unit.tile, E.HUT_GOLD, _("You get %d gold."), gold) end end
signal.connect("unit_lost", "get_gold_when_killed_callback")
建物を建てるとお金が手に入る
建物Palaceを建て終えると、すべての都市サイズの合計に応じたお金が入る。
function get_gold_that_equal_to_city_size_callback(building_type, city)
if building_type ~= find.building_type("Palace") then return false end
local gold = 0 local city_size = 0 for target_city in city.owner:cities_iterate() do city_size = city_size + target_city.size end gold = city_size * 10 edit.change_gold(city.owner, gold) notify.event(city.owner, city.tile, E.HUT_GOLD, _("You get %d gold."), gold) end
signal.connect("building_built", "get_gold_that_equal_to_city_size_callback")
ユニットを自動入手
不思議のPalaceが建っている都市で、5ターンごとに所属都市なしの新兵ユニットWarriorsが自動出現する。
function get_volunteers_callback(turn, year)
if turn % 5 ~= 0 then return false end
local wonder_type = find.building_type("Palace")
for player in players_iterate() do if player:has_wonder(wonder_type) then for city in player:cities_iterate() do if city:has_building(wonder_type) then
local unit_type = find.unit_type("Warriors")
edit.create_unit(player, city.tile, unit_type, 0, nil, -1) end end end end end
signal.connect("turn_started", "get_volunteers_callback")
技術開発でユニットを入手
不思議Palaceを持つ国が任意の科学技術を入手すると、維持無料ユニットが全都市に置かれる。
function get_phalanx_series_callback(tech_type, player, source)
if not (source == "researched" or source == "traded" or source == "stolen") then return false end
if not (player:has_wonder(find.building_type("Palace"))) then return false end
local units_name = "" if tech_type:rule_name() == "Bronze Working" then units_name = "Phalanx" elseif tech_type:rule_name() == "Gunpowder" then units_name = "Musketeers" elseif tech_type:rule_name() == "Conscription" then units_name = "Riflemen" elseif tech_type:rule_name() == "Tactics" then units_name = "Alpine Troops" elseif tech_type:rule_name() == "Labor Union" then units_name = "Mech. Inf." else units_name = "Warriors" end
for city in player:cities_iterate() do edit.create_unit(city.owner, city.tile, find.unit_type(units_name), 1, nil, -1) end end
signal.connect("tech_researched", "get_phalanx_series_callback")
都市占領で内戦が発生
都市を占領した時、占領後の都市に建物City Wallsが残っていると占領者の国家で10%の確率で内戦が発生する。
function civiliwar_via_curse2_callback(city, loser, winner) if winner.nation:rule_name() == "Barbarian" or winner.nation:rule_name() == "Pirate" then return false end
if city:has_building(find.building_type("City Walls")) then
local probability = 10 if edit.civil_war(winner, probability) ~= nil then notify.event(nil, city.tile, E.CIVIL_WAR, _("Civilwar has happend at " .. city.name .. " !")) end end end
signal.connect("city_lost", "civiliwar_via_curse2_callback")