1/*****************************************************************************
2 Freeciv - Copyright (C) 2005 - The Freeciv Project
3   This program is free software; you can redistribute it and/or modify
4   it under the terms of the GNU General Public License as published by
5   the Free Software Foundation; either version 2, or (at your option)
6   any later version.
7
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   GNU General Public License for more details.
12*****************************************************************************/
13
14/*****************************************************************************
15  ADVERTISEMENT: do not attempt to change the name of the API functions.
16  They may be in use in Lua scripts in savefiles, so once released, the
17  name and signature cannot change shape even in new major versions of
18  Freeciv, until the relevant save format version can no longer be loaded.
19  If you really like to change a function name, be sure to keep also the
20  old one running.
21*****************************************************************************/
22
23$#ifdef HAVE_CONFIG_H
24$#include <fc_config.h>
25$#endif
26
27/* common/scriptcore */
28$#include "luascript_types.h"
29
30/* server */
31$#include "commands.h"
32$#include "console.h"
33
34/* server/scripting */
35$#include "api_server_edit.h"
36$#include "api_server_base.h"
37$#include "api_server_notify.h"
38$#include "api_server_game_methods.h"
39$#include "script_server.h"
40
41/* Server module. */
42module server {
43  bool api_server_save
44    @ save (lua_State *L, const char *filename);
45  bool api_server_was_started
46    @ started (lua_State *L);
47  int api_server_player_civilization_score
48    @ civilization_score (lua_State *L, Player *pplayer);
49  bool api_play_music
50    @ play_music (lua_State *L, Player *pplayer, const char *tag);
51
52  module setting {
53    const char *api_server_setting_get
54      @ get (lua_State *L, const char *setting_name);
55  }
56}
57
58/* Notify module. */
59module notify {
60  void api_notify_embassies_msg
61    @ embassies_msg (lua_State *L, Player *pplayer, Tile *ptile, int event,
62                     const char *message);
63  void api_notify_research_msg
64    @ research_msg (lua_State *L, Player *pplayer, bool include_plr,
65                    int event, const char *message);
66  void api_notify_research_embassies_msg
67    @ research_embassies_msg (lua_State *L, Player *pplayer, int event,
68                              const char *message);
69  void api_notify_event_msg
70    @ event_msg (lua_State *L, Player *pplayer, Tile *ptile, int event,
71                 const char *message);
72}
73
74$[
75-- Notify module implementation.
76
77function notify.all(...)
78  local arg = table.pack(...);
79  notify.event_msg(nil, nil, E.SCRIPT, string.format(table.unpack(arg)))
80end
81
82function notify.player(player, ...)
83  local arg = table.pack(...);
84  notify.event_msg(player, nil, E.SCRIPT, string.format(table.unpack(arg)))
85end
86
87function notify.event(player, tile, event, ...)
88  local arg = table.pack(...);
89  notify.event_msg(player, tile, event, string.format(table.unpack(arg)))
90end
91
92function notify.embassies(player, ptile, event, ...)
93  local arg = table.pack(...);
94  notify.embassies_msg(player, ptile, event, string.format(table.unpack(arg)))
95end
96
97function notify.research(player, selfmsg, event, ...)
98  local arg = table.pack(...);
99  notify.research_msg(player, selfmsg, event, string.format(table.unpack(arg)))
100end
101
102function notify.research_embassies(player, event, ...)
103  local arg = table.pack(...);
104  notify.research_embassies_msg(player, event, string.format(table.unpack(arg)))
105end
106$]
107
108/* Edit module. */
109module edit {
110  Unit *api_edit_create_unit
111    @ create_unit (lua_State *L, Player *pplayer, Tile *ptile,
112                   Unit_Type *ptype, int veteran_level, City *homecity,
113                   int moves_left);
114  Unit *api_edit_create_unit_full
115    @ create_unit_full (lua_State *L, Player *pplayer, Tile *ptile,
116                        Unit_Type *ptype, int veteran_level, City *homecity,
117                        int moves_left, int hp_left, Unit *ptransport);
118  bool api_edit_unit_teleport
119    @ unit_teleport(lua_State *L, Unit *self, Tile *dest);
120  void api_edit_unit_kill
121    @ unit_kill(lua_State *L, Unit *self, const char *reason,
122                Player *killer);
123  void api_edit_create_city
124    @ create_city (lua_State *L, Player *pplayer, Tile *ptile,
125                   const char *name);
126  void api_edit_create_extra
127    @ create_extra (lua_State *L, Tile *ptile, const char *name);
128  void api_edit_create_base
129    @ create_base (lua_State *L, Tile *ptile, const char *name,
130                   Player *pplayer);
131  /* Deprecated; use edit.create_extra() */
132  void api_edit_create_road
133    @ create_road (lua_State *L, Tile *ptile, const char *name);
134  void api_edit_remove_extra
135    @ remove_extra (lua_State *L, Tile *ptile, const char *name);
136  void api_edit_tile_set_label
137    @ tile_set_label (lua_State *L, Tile *ptile, const char *label);
138  Player *api_edit_create_player
139    @ create_player(lua_State *L, const char *username, Nation_Type *nation,
140                    const char *ai);
141  void api_edit_change_gold
142    @ change_gold (lua_State *L, Player *pplayer, int amount);
143  /* cost:
144   *     0 or above - The exact cost % to apply
145   *    -1          - Apply freecost
146   *    -2          - Apply conquercost
147   *    -3          - Apply diplbulbcost */
148  Tech_Type *api_edit_give_technology
149    @ give_tech (lua_State *L, Player *pplayer, Tech_Type *ptech,
150                 int cost, bool notify, const char *reason);
151  bool api_edit_trait_mod_set
152    @ trait_mod (lua_State *L, Player *pplayer, const char *tname,
153                 const int mod);
154  bool api_edit_unleash_barbarians
155    @ unleash_barbarians (lua_State *L, Tile *ptile);
156  void api_edit_place_partisans
157    @ place_partisans (lua_State *L, Tile *ptile, Player *pplayer, int count,
158                       int sq_radius);
159  enum climate_change_type {
160    CLIMATE_CHANGE_GLOBAL_WARMING @ GLOBAL_WARMING,
161    CLIMATE_CHANGE_NUCLEAR_WINTER @ NUCLEAR_WINTER
162  };
163  void api_edit_climate_change
164    @ climate_change (lua_State *L, enum climate_change_type type,
165                      int effect);
166  Player *api_edit_civil_war
167    @ civil_war(lua_State *L, Player *pplayer, int probability);
168  void api_edit_unit_turn
169    @ unit_turn(lua_State *L, Unit *punit, Direction dir);
170  void api_edit_player_victory
171    @ player_victory (lua_State *L, Player *self);
172  bool api_edit_unit_move
173    @ unit_move(lua_State *L, Unit *self, Tile *moveto, int movecost);
174
175  void api_edit_city_add_history
176    @ add_city_history(lua_State *L, City *self, int amount);
177  void api_edit_player_add_history
178    @ add_player_history(lua_State *L, Player *self, int amount);
179}
180
181$[
182-- Compatibility functions
183-- These top-level functions were exposed prior to Freeciv 2.4. Since then
184-- we prefer use of edit.*. Do not add new functions to this section.
185function create_unit(player, tile, utype, veteran_level, homecity, moves_left)
186  log.deprecation_warning("create_unit()", "edit.create_unit()",
187                          "2.4");
188  return edit.create_unit(player, tile, utype, veteran_level, homecity,
189                          moves_left)
190end
191
192function create_unit_full(player, tile, utype, veteran_level, homecity,
193                          moves_left, hp_left, transport)
194  log.deprecation_warning("create_unit_full()", "edit.create_unit_full()",
195                          "2.4");
196  return edit.create_unit_full(player, tile, utype, veteran_level, homecity,
197                               moves_left, hp_left, transport)
198end
199
200function create_city(player, tile, name)
201  log.deprecation_warning("create_city()", "edit.create_city()",
202                          "2.4");
203  edit.create_city (player, tile, name)
204end
205
206-- Deprecated; use edit.create_base()
207function create_base(tile, name, player)
208  -- Out-of-namespace since 2.4
209  log.deprecation_warning("create_base()", "edit.create_base()",
210                          "2.4");
211  edit.create_base(tile, name, player)
212end
213
214function create_player(username, nation)
215  log.deprecation_warning("create_player()", "edit.create_player()",
216                          "2.4");
217  return edit.create_player(username, nation, nil)
218end
219
220function change_gold(pplayer, amount)
221  log.deprecation_warning("change_gold()", "edit.change_gold()",
222                          "2.4");
223  edit.change_gold(pplayer, amount)
224end
225
226-- Deprecated; use edit.give_tech()
227function give_technology(player, tech, reason)
228  log.deprecation_warning("give_technology()", "edit.give_tech()",
229                          "2.4");
230  return edit.give_tech(player, tech, -1, false, reason)
231end
232
233-- Deprecated; use edit.give_tech()
234function edit.give_technology(player, tech, reason)
235  log.deprecation_warning("edit.give_technology()", "edit.give_tech()",
236                          "2.6");
237  return edit.give_tech(player, tech, -1, false, reason)
238end
239
240function trait_mod(player, trait, mod)
241  log.deprecation_warning("trait_mod()", "edit.trait_mod()",
242                          "2.4");
243  return edit.trait_mod(player, trait, mod)
244end
245
246function unleash_barbarians(tile)
247  log.deprecation_warning("unleash_barbarians()", "edit.unleash_barbarians()",
248                          "2.4");
249  return edit.unleash_barbarians(tile)
250end
251
252function place_partisans(tile, player, count, sq_radius)
253  log.deprecation_warning("place_partisans()", "edit.place_partisans()",
254                          "2.4");
255  edit.place_partisans(tile, player, count, sq_radius)
256end
257
258-- Server functions for Player module
259function Player:create_unit(tile, utype, veteran_level, homecity, moves_left)
260  return edit.create_unit(self, tile, utype, veteran_level, homecity,
261                          moves_left)
262end
263
264function Player:create_unit_full(tile, utype, veteran_level, homecity,
265                                 moves_left, hp_left, ptransport)
266  return edit.create_unit_full(self, tile, utype, veteran_level, homecity,
267                               moves_left, hp_left, ptransport)
268end
269
270function Player:civilization_score()
271  return server.civilization_score(self)
272end
273
274function Player:create_city(tile, name)
275  edit.create_city(self, tile, name)
276end
277
278function Player:change_gold(amount)
279  edit.change_gold(self, amount)
280end
281
282function Player:give_tech(tech, cost, notify, reason)
283  return edit.give_tech(self, tech, cost, notify, reason)
284end
285
286-- Deprecated; use Player:give_tech()
287function Player:give_technology(tech, reason)
288  log.deprecation_warning("Player:give_technology()", "Player:give_tech()",
289                          "2.6");
290  return edit.give_tech(self, tech, -1, false, reason)
291end
292
293function Player:trait_mod(trait, mod)
294  return edit.trait_mod(self, trait, mod)
295end
296
297function Player:civil_war(probability)
298  return edit.civil_war(self, probability)
299end
300
301function Player:victory()
302  edit.player_victory(self)
303end
304
305function Player:add_history(amount)
306  edit.add_player_history(self, amount)
307end
308
309-- Server functions for City module
310function City:add_history(amount)
311  edit.add_city_history(self, amount)
312end
313
314-- Server functions for Unit module
315function Unit:teleport(dest)
316  return edit.unit_teleport(self, dest)
317end
318
319function Unit:turn(direction)
320  edit.unit_turn(self, direction)
321end
322
323function Unit:kill(reason, killer)
324  edit.unit_kill(self, reason, killer)
325end
326
327function Unit:move(moveto, movecost)
328  return edit.unit_move(self, moveto, movecost)
329end
330
331-- Server functions for Tile module
332function Tile:create_extra(name)
333  edit.create_extra(self, name)
334end
335
336function Tile:create_base(name, player)
337  edit.create_base(self, name, player)
338end
339
340-- Deprecated; use Tile:create_extra()
341function Tile:create_road(name)
342  log.deprecation_warning("Tile:create_road()", "Tile:create_extra()",
343                          "2.6");
344  edit.create_road(self, name)
345end
346
347function Tile:remove_extra(name)
348  edit.remove_extra(self, name)
349end
350
351function Tile:unleash_barbarians()
352  return edit.unleash_barbarians(self)
353end
354
355function Tile:place_partisans(player, count, sq_radius)
356  edit.place_partisans(self, player, count, sq_radius)
357end
358
359function Tile:set_label(label)
360  edit.tile_set_label(self, label)
361end
362
363$]
364
365/* Additions to common Player module. */
366module Player {
367  int api_methods_player_trait
368    @ trait (lua_State *L, Player *pplayer, const char *tname);
369  int api_methods_player_trait_base
370    @ trait_base (lua_State *L, Player *pplayer, const char *tname);
371  int api_methods_player_trait_current_mod
372    @ trait_current_mod (lua_State *L, Player *pplayer, const char *tname);
373}
374
375/* Additions to common Nation_Type module. */
376module Nation_Type {
377  int api_methods_nation_trait_min
378    @ trait_min (lua_State *L, Nation_Type *pnation, const char *tname);
379  int api_methods_nation_trait_max
380    @ trait_max (lua_State *L, Nation_Type *pnation, const char *tname);
381  int api_methods_nation_trait_default
382    @ trait_default (lua_State *L, Nation_Type *pnation,
383                     const char *tname);
384}
385