/********************************************************************** Freeciv - Copyright (C) 2005 - The Freeciv Project 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 2, 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. ***********************************************************************/ $#ifdef HAVE_CONFIG_H $#include $#endif $#include "api_types.h" $#include "api_actions.h" $#include "api_find.h" $#include "api_intl.h" $#include "api_methods.h" $#include "api_notify.h" $#include "api_utilities.h" $#include "script.h" /* Classes. */ struct Player_ai { bool control; }; struct Player { const char *name; Nation_Type *nation; Player_ai ai; const int player_no @ id; }; struct City { const char *name; Player *owner; Tile *tile; const int id; }; struct Unit { Unit_Type *utype; Player *owner; int homecity @ homecity_id; Tile *tile; const int id; }; struct Tile { const int nat_x; const int nat_y; Terrain *terrain; const int index @ id; }; struct Government { const int index @ id; }; struct Nation_Type { const int index @ id; }; struct Building_Type { int build_cost; const int index @ id; }; struct Unit_Type { int build_cost; const int index @ id; }; struct Tech_Type { const int index @ id; }; struct Terrain { const int index @ id; }; /* Class methods. */ int api_methods_player_num_cities @ methods_player_num_cities (Player *pplayer); int api_methods_player_num_units @ methods_player_num_units (Player *pplayer); bool api_methods_unit_type_has_flag @ methods_unit_type_has_flag (Unit_Type *punit_type, const char *flag); bool api_methods_unit_type_has_role @ methods_unit_type_has_role (Unit_Type *punit_type, const char *role); bool api_methods_building_type_is_wonder @ methods_building_type_is_wonder (Building_Type *pbuilding); bool api_methods_building_type_is_great_wonder @ methods_building_type_is_great_wonder (Building_Type *pbuilding); bool api_methods_building_type_is_small_wonder @ methods_building_type_is_small_wonder (Building_Type *pbuilding); bool api_methods_building_type_is_improvement @ methods_building_type_is_improvement (Building_Type *pbuilding); $[ -- Player methods. function Player:is_human() return not self.ai.control end function Player:num_cities() return methods_player_num_cities(self) end function Player:num_units() return methods_player_num_units(self) end -- Unit methods. function Unit:homecity() return find.city(self.owner, self.homecity_id) end -- Building_Type methods. function Building_Type:build_shield_cost() return self.build_cost end function Building_Type:is_wonder() return methods_building_type_is_wonder(self) end function Building_Type:is_great_wonder() return methods_building_type_is_great_wonder(self) end function Building_Type:is_small_wonder() return methods_building_type_is_small_wonder(self) end function Building_Type:is_improvement() return methods_building_type_is_improvement(self) end -- Unit_Type methods. function Unit_Type:build_shield_cost() return self.build_cost end function Unit_Type:has_flag(flag) return methods_unit_type_has_flag(self, flag) end function Unit_Type:has_role(role) return methods_unit_type_has_role(self, role) end $] /* Object find module. */ module find { Player *api_find_player @ player (int player_id); City *api_find_city @ city (Player *pplayer, int city_id); Unit *api_find_unit @ unit (Player *pplayer, int unit_id); Tile *api_find_tile @ tile (int nat_x, int nat_y); Government *api_find_government @ government (int government_id); Government *api_find_government_by_name @ government (const char *name_orig); Nation_Type *api_find_nation_type_by_name @ nation_type (const char *name_orig); Nation_Type *api_find_nation_type @ nation_type (int nation_type_id); Building_Type *api_find_building_type_by_name @ building_type (const char *name_orig); Building_Type *api_find_building_type @ building_type (int building_type_id); Unit_Type *api_find_unit_type_by_name @ unit_type (const char *name_orig); Unit_Type *api_find_unit_type @ unit_type (int unit_type_id); Tech_Type *api_find_tech_type_by_name @ tech_type (const char *name_orig); Tech_Type *api_find_tech_type @ tech_type (int tech_type_id); Terrain *api_find_terrain_by_name @ terrain (const char *name_orig); Terrain *api_find_terrain @ terrain (int terrain_id); } $[ -- Dump the state of user scalar variables to a Lua code string. function _freeciv_state_dump() local res = '' for k, v in pairs(_G) do if k == '_VERSION' then -- ignore _VERSION variable. elseif type(v) == 'boolean' or type(v) == 'number' then local rvalue = tostring(v) res = res .. k .. '=' .. rvalue .. '\n' elseif type(v) == 'string' then local rvalue = string.format('%q', v) res = res .. k .. '=' .. rvalue .. '\n' elseif type(v) == 'userdata' then local method = string.lower(tolua.type(v)) res = res .. k .. '=find.' .. method if method == 'city' or method == 'unit' then res = res .. '(nil,' .. v.id .. ')' else res = res .. '(' .. v.id .. ')' end res = res .. '\n' end end return res end $] /* Signal module. */ module signal { void script_signal_connect @ connect(const char *signal_name, const char *callback_name); } /* Intl module. */ const char *api_intl__ @ _ (const char *string); const char *api_intl_N_ @ N_ (const char *string); const char *api_intl_Q_ @ Q_ (const char *string); const char *api_intl_PL_ @ PL_ (const char *string1, const char *string2, int n); /* Notify module. */ module notify { void api_notify_embassies_msg @ embassies_msg (Player *pplayer, Tile *ptile, int event, const char *message); void api_notify_event_msg @ event_msg (Player *pplayer, Tile *ptile, int event, const char *message); } /* Notify events module. */ module E { enum event_type { E_CITY_CANTBUILD @ CITY_CANTBUILD, E_CITY_LOST @ CITY_LOST, E_CITY_LOVE @ CITY_LOVE, E_CITY_DISORDER @ CITY_DISORDER, E_CITY_FAMINE @ CITY_FAMINE, E_CITY_FAMINE_FEARED @ CITY_FAMINE_FEARED, E_CITY_GROWTH @ CITY_GROWTH, E_CITY_MAY_SOON_GROW @ CITY_MAY_SOON_GROW, E_CITY_AQUEDUCT @ CITY_AQUEDUCT, E_CITY_AQ_BUILDING @ CITY_AQ_BUILDING, E_CITY_NORMAL @ CITY_NORMAL, E_CITY_NUKED @ CITY_NUKED, E_CITY_CMA_RELEASE @ CITY_CMA_RELEASE, E_CITY_GRAN_THROTTLE @ CITY_GRAN_THROTTLE, E_CITY_TRANSFER @ CITY_TRANSFER, E_CITY_BUILD @ CITY_BUILD, E_CITY_PRODUCTION_CHANGED @ CITY_PRODUCTION_CHANGED, E_WORKLIST @ WORKLIST, E_UPRISING @ UPRISING, E_CIVIL_WAR @ CIVIL_WAR, E_ANARCHY @ ANARCHY, E_FIRST_CONTACT @ FIRST_CONTACT, E_NEW_GOVERNMENT @ NEW_GOVERNMENT, E_LOW_ON_FUNDS @ LOW_ON_FUNDS, E_POLLUTION @ POLLUTION, E_REVOLT_DONE @ REVOLT_DONE, E_REVOLT_START @ REVOLT_START, E_SPACESHIP @ SPACESHIP, E_MY_DIPLOMAT_BRIBE @ MY_DIPLOMAT_BRIBE, E_DIPLOMATIC_INCIDENT @ DIPLOMATIC_INCIDENT, E_MY_DIPLOMAT_ESCAPE @ MY_DIPLOMAT_ESCAPE, E_MY_DIPLOMAT_EMBASSY @ MY_DIPLOMAT_EMBASSY, E_MY_DIPLOMAT_FAILED @ MY_DIPLOMAT_FAILED, E_MY_DIPLOMAT_INCITE @ MY_DIPLOMAT_INCITE, E_MY_DIPLOMAT_POISON @ MY_DIPLOMAT_POISON, E_MY_DIPLOMAT_SABOTAGE @ MY_DIPLOMAT_SABOTAGE, E_MY_DIPLOMAT_THEFT @ MY_DIPLOMAT_THEFT, E_ENEMY_DIPLOMAT_BRIBE @ ENEMY_DIPLOMAT_BRIBE, E_ENEMY_DIPLOMAT_EMBASSY @ ENEMY_DIPLOMAT_EMBASSY, E_ENEMY_DIPLOMAT_FAILED @ ENEMY_DIPLOMAT_FAILED, E_ENEMY_DIPLOMAT_INCITE @ ENEMY_DIPLOMAT_INCITE, E_ENEMY_DIPLOMAT_POISON @ ENEMY_DIPLOMAT_POISON, E_ENEMY_DIPLOMAT_SABOTAGE @ ENEMY_DIPLOMAT_SABOTAGE, E_ENEMY_DIPLOMAT_THEFT @ ENEMY_DIPLOMAT_THEFT, E_CARAVAN_ACTION @ CARAVAN_ACTION, E_TUTORIAL @ TUTORIAL, E_BROADCAST_REPORT @ BROADCAST_REPORT, E_GAME_END @ GAME_END, E_GAME_START @ GAME_START, E_MESSAGE_WALL @ MESSAGE_WALL, E_NATION_SELECTED @ NATION_SELECTED, E_DESTROYED @ DESTROYED, E_REPORT @ REPORT, E_TURN_BELL @ TURN_BELL, E_NEXT_YEAR @ NEXT_YEAR, E_GLOBAL_ECO @ GLOBAL_ECO, E_NUKE @ NUKE, E_HUT_BARB @ HUT_BARB, E_HUT_CITY @ HUT_CITY, E_HUT_GOLD @ HUT_GOLD, E_HUT_BARB_KILLED @ HUT_BARB_KILLED, E_HUT_MERC @ HUT_MERC, E_HUT_SETTLER @ HUT_SETTLER, E_HUT_TECH @ HUT_TECH, E_HUT_BARB_CITY_NEAR @ HUT_BARB_CITY_NEAR, E_IMP_BUY @ IMP_BUY, E_IMP_BUILD @ IMP_BUILD, E_IMP_AUCTIONED @ IMP_AUCTIONED, E_IMP_AUTO @ IMP_AUTO, E_IMP_SOLD @ IMP_SOLD, E_TECH_GAIN @ TECH_GAIN, E_TECH_LEARNED @ TECH_LEARNED, E_TREATY_ALLIANCE @ TREATY_ALLIANCE, E_TREATY_BROKEN @ TREATY_BROKEN, E_TREATY_CEASEFIRE @ TREATY_CEASEFIRE, E_TREATY_PEACE @ TREATY_PEACE, E_TREATY_SHARED_VISION @ TREATY_SHARED_VISION, E_UNIT_LOST_ATT @ UNIT_LOST_ATT, E_UNIT_WIN_ATT @ UNIT_WIN_ATT, E_UNIT_BUY @ UNIT_BUY, E_UNIT_BUILT @ UNIT_BUILT, E_UNIT_LOST @ UNIT_LOST, E_UNIT_WIN @ UNIT_WIN, E_UNIT_BECAME_VET @ UNIT_BECAME_VET, E_UNIT_UPGRADED @ UNIT_UPGRADED, E_UNIT_RELOCATED @ UNIT_RELOCATED, E_UNIT_ORDERS @ UNIT_ORDERS, E_WONDER_BUILD @ WONDER_BUILD, E_WONDER_OBSOLETE @ WONDER_OBSOLETE, E_WONDER_STARTED @ WONDER_STARTED, E_WONDER_STOPPED @ WONDER_STOPPED, E_WONDER_WILL_BE_BUILT @ WONDER_WILL_BE_BUILT, E_DIPLOMACY @ DIPLOMACY, E_TREATY_EMBASSY @ TREATY_EMBASSY, E_BAD_COMMAND @ BAD_COMMAND, E_SETTING @ SETTING, E_CHAT_MSG @ CHAT_MSG, E_CHAT_ERROR @ CHAT_ERROR, /* * Note: If you add a new event, make sure you make a similar change * to the events array in common/events.c using GEN_EV and to * data/stdsounds.soundspec. */ E_LAST @ LAST }; } $[ -- Notify module implementation. function notify.all(...) notify.event_msg(nil, nil, E.NOEVENT, string.format(unpack(arg))) end function notify.player(player, ...) notify.event_msg(player, nil, E.NOEVENT, string.format(unpack(arg))) end function notify.event(player, tile, event, ...) notify.event_msg(player, tile, event, string.format(unpack(arg))) end function notify.embassies(player, ptile, event, ...) notify.embassies_msg(player, ptile, event, string.format(unpack(arg))) end $] /* Utilities module. */ int api_utilities_random @ random (int min, int max); /* Actions module. */ Unit *api_actions_create_unit @ create_unit (Player *pplayer, Tile *ptile, Unit_Type *ptype, int veteran_level, City *homecity, int moves_left); void api_actions_create_city @ create_city (Player *pplayer, Tile *ptile, const char *name); void api_actions_change_gold @ change_gold (Player *pplayer, int amount); bool api_actions_give_technology @ give_technology (Player *pplayer, Tech_Type *ptech);