File: api.pkg

package info (click to toggle)
freeciv 2.3.2-1%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 123,684 kB
  • sloc: ansic: 300,981; sh: 11,763; makefile: 4,671; python: 1,717; xml: 172
file content (669 lines) | stat: -rw-r--r-- 19,720 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/**********************************************************************
 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.
***********************************************************************/

/****************************************************************************
  ADVERTISEMENT: do not attempt to change the name of the API functions.
  They *must* be compatible between the minor Freeciv versions within the
  same major version. If you really like to change a function name, be sure
  to keep also the old one running.
****************************************************************************/

$#ifdef HAVE_CONFIG_H
$#include <config.h>
$#endif

$#include "api_types.h"
$#include "api_actions.h"
$#include "api_effects.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 {
  const char *name;
  Nation_Type *nation;
  bool ai_controlled;
};

module Player {
  module properties {
    int api_methods_player_number
      @ id (Player *self);
  }

  int api_methods_player_num_cities
    @ num_cities (Player *self);
  int api_methods_player_num_units
    @ num_units (Player *self);
  bool api_methods_player_has_wonder
    @ has_wonder (Player *self, Building_Type *building);
  void api_methods_player_victory
    @ victory (Player *self);
  int api_methods_player_civilization_score
    @ civilization_score (Player *self);
  int api_methods_player_gold
    @ gold (Player *self);
  bool api_methods_player_knows_tech
    @ knows_tech (Player *self, Tech_Type *ptech);
}

module methods_private {
  module Player {
    Unit_List_Link *api_methods_private_player_unit_list_head
      @ unit_list_head (Player *self);
    City_List_Link *api_methods_private_player_city_list_head
      @ city_list_head (Player *self);
  }
}

struct City {
  const char *name;
  Player *owner;
  Player *original;
  Tile *tile;
  int size;

  const int id;
};

module City {
  bool api_methods_city_has_building
    @ has_building(City *self, Building_Type *building);
  int api_methods_city_map_sq_radius
    @ map_sq_radius(City *self);
}


struct Unit {
  Unit_Type *utype;
  Player *owner;

  /* This used to be @ homecity_id, but it does not work with toluaxx. */
  int homecity;
  Tile *tile;

  const int id;
};

module Unit {
  bool api_methods_unit_city_can_be_built_here
    @ is_on_possible_city_tile (Unit *self);
}

struct Tile {
  const int nat_x;
  const int nat_y;

  Terrain *terrain;

  const int index @ id;
};

module Tile {
  City *api_methods_tile_city
    @ city (Tile *self);
  bool api_methods_tile_city_exists_within_max_city_map
    @ city_exists_within_max_city_map (Tile *self, bool center);
  int api_methods_tile_num_units
    @ num_units (Tile *self);
  int api_methods_tile_sq_distance
    @ sq_distance (Tile *self, Tile *other);
}

module methods_private {
  module Tile {
    int api_methods_private_tile_next_outward_index
      @ next_outward_index (Tile *pcenter, int index, int max_dist);
    Tile *api_methods_private_tile_for_outward_index
      @ tile_for_outward_index (Tile *pcenter, int index);
    Unit_List_Link *api_methods_private_tile_unit_list_head
      @ unit_list_head (Tile *self);
  }
}

struct Government {
  const int item_number @ id;
};

module Government {
  const char *api_methods_government_rule_name
    @ rule_name (Government *self);
  const char *api_methods_government_name_translation
    @ name_translation (Government *self);
}

struct Nation_Type {
  const int item_number @ id;
};

module Nation_Type {
  const char *api_methods_nation_type_rule_name
    @ rule_name (Nation_Type *self);
  const char *api_methods_nation_type_name_translation
    @ name_translation (Nation_Type *self);
  const char *api_methods_nation_type_plural_translation
    @ plural_translation (Nation_Type *self);
}

struct Building_Type {
  int build_cost;

  const int item_number @ id;
};

module Building_Type {
  bool api_methods_building_type_is_wonder
    @ is_wonder (Building_Type *self);
  bool api_methods_building_type_is_great_wonder
    @ is_great_wonder (Building_Type *self);
  bool api_methods_building_type_is_small_wonder
    @ is_small_wonder (Building_Type *self);
  bool api_methods_building_type_is_improvement
    @ is_improvement (Building_Type *self);
  const char *api_methods_building_type_rule_name
    @ rule_name (Building_Type *self);
  const char *api_methods_building_type_name_translation
    @ name_translation (Building_Type *self);
}

struct Unit_Type {
  int build_cost;

  const int item_number @ id;
};

module Unit_Type {
  bool api_methods_unit_type_has_flag
    @ has_flag (Unit_Type *self, const char *flag);
  bool api_methods_unit_type_has_role
    @ has_role (Unit_Type *self, const char *role);
  const char *api_methods_unit_type_rule_name
    @ rule_name (Unit_Type *self);
  const char *api_methods_unit_type_name_translation
    @ name_translation (Unit_Type *self);
  bool api_methods_unit_type_can_exist_at_tile
    @ can_exist_at_tile(Unit_Type *self, Tile *ptile);
}

struct Tech_Type {
  const int item_number @ id;
};

module Tech_Type {
  const char *api_methods_tech_type_rule_name
    @ rule_name (Tech_Type *self);
  const char *api_methods_tech_type_name_translation
    @ name_translation (Tech_Type *self);
}

struct Terrain {
  const int item_number @ id;
};

module Terrain {
  const char *api_methods_terrain_rule_name
    @ rule_name (Terrain *self);
  const char *api_methods_terrain_name_translation
    @ name_translation (Terrain *self);
}

struct Unit_List_Link {
};

module Unit_List_Link {
  Unit *api_methods_unit_list_link_data
    @ data (Unit_List_Link *self);
  Unit_List_Link *api_methods_unit_list_next_link
    @ next (Unit_List_Link *self);
}

struct City_List_Link {
};

module City_List_Link {
  City *api_methods_city_list_link_data
    @ data (City_List_Link *self);
  City_List_Link *api_methods_city_list_next_link
    @ next (City_List_Link *self);
}

struct Nonexistent {
};

$[

-- Building_Type methods.
function Building_Type:build_shield_cost()
  return self.build_cost
end

-- Player methods
function Player:is_human()
  return not self.ai_controlled
end

----------------------------------------------------------------------
--  Player and Tile: cities_iterate and units_iterate methods
----------------------------------------------------------------------
do
  local private = methods_private

  -- Iterate over the values of 'array' in order:
  -- array[1], array[2], array[3], etc.
  local function value_iterator(array)
    local i = 0
    local function iterator()
      i = i + 1
      return array[i]
    end
    return iterator
  end

  -- use a copy of the list for safe iteration
  local function safe_iterate_list(link)
    local objs = {}
    while link do
      objs[#objs + 1] = link:data()
      link = link:next()
    end
    return value_iterator(objs)
  end

  -- Safe iteration over all units that belong to Player
  function Player:units_iterate()
    return safe_iterate_list(private.Player.unit_list_head(self))
  end

  -- Safe iteration over all cities that belong to Player
  function Player:cities_iterate()
    return safe_iterate_list(private.Player.city_list_head(self))
  end

  -- Safe iteration over the units on Tile
  function Tile:units_iterate()
    return safe_iterate_list(private.Tile.unit_list_head(self))
  end
end

----------------------------------------------------------------------
--  Tile
--  Deprecated. New one is Tile:city_exists_within_max_city_map().
----------------------------------------------------------------------
function Tile:city_exists_within_city_radius(center)
  return self:city_exists_within_max_city_map(center)
end

----------------------------------------------------------------------
--  Tile: square_iterate, circle_iterate
----------------------------------------------------------------------
do
  local next_outward_index = methods_private.Tile.next_outward_index
  local tile_for_outward_index = methods_private.Tile.tile_for_outward_index

  -- iterate over tiles at distance 'radius'
  function Tile:square_iterate(radius)
    local index = -1
    local function iterator()
      index = next_outward_index(self, index, radius)
      if index < 0 then
        return nil
      else
        return tile_for_outward_index(self, index)
      end
    end
    return iterator
  end

  -- iterate over tiles at squared distance 'sq_radius'
  function Tile:circle_iterate(sq_radius)
    local cr_radius = math.floor(math.sqrt(sq_radius))
    local sq_iter = self:square_iterate(cr_radius)
    local function iterator()
      local tile = nil
      repeat
        tile = sq_iter()
      until not tile or self:sq_distance(tile) <= sq_radius
      return tile
    end
    return iterator
  end
end  -- end do-block (Tile methods)

-- City methods.
function City:exists()
  return true
end

-- Unit methods.
function Unit:exists()
  return true
end

function Unit:get_homecity()
  return find.city(self.owner, self.homecity)
end

-- Unit_Type methods.
function Unit_Type:build_shield_cost()
  return self.build_cost
end

-- Nonexistent methods.
function Nonexistent:exists()
  return false
end

-- Hide all private methods
methods_private = nil
$]

$[
----------------------------------------------------------------------
--  API Types Special Methods
----------------------------------------------------------------------
do
  local api_types = {
    "Player",
    "Unit",
    "City",
    "Tile",
    "Government",
    "Nation_Type",
    "Building_Type",
    "Unit_Type",
    "Tech_Type",
    "Terrain",
    "Unit_List_Link",
    "City_List_Link",
    "Nonexistent",
  }

  local function id_eq (o1, o2)
    return o1.id == o2.id and (o1.id ~= nil)
  end

  -- define string representation for tostring
  local function string_rep(self)
    local id = self.id
    local name = self.rule_name and self:rule_name() or self.name
    if name and id then
      return string.format('<%s #%d %s>', tolua.type(self), id, name)
    elseif id then
      return string.format('<%s #%d>', tolua.type(self), id)
    else
      return string.format('<%s>', tolua.type(self))
    end
  end

  for index, typename in ipairs(api_types) do
    local api_type = _G[typename]

    api_type[".eq"] = id_eq
    api_type.__tostring = string_rep

    -- Object field resolution
    -- 1) Check properties defined in our API
    --    (Properties are fields that call an accessor to get their value)
    -- 2) Delegate to tolua's __index if name is without _ or . prefix
    --    (metamethods and tolua fields give access to unprotected C
    --     functions in a pointer-unsafe way).
    -- otherwise, return nil
    local api_type_index = api_type.__index
    local properties = api_type.properties
    -- Prevent tampering with the notion of equality
    local rawequal = rawequal
    local string_sub = string.sub

    local function field_getter(self, field)
      local getter = properties and properties[field]
      if getter then
        return getter(self)
      else
        local pfx = string_sub(field, 1, 1)
        if rawequal(pfx, '.') or rawequal(pfx, '_') then
          return nil
        else
          return api_type_index(self, field)
        end
      end
    end
    api_type.__index = field_getter

    -- Delete '.set' table to disallow direct writing of struct fields
    api_type[".set"] = nil
    -- Hide the metatable and hide the class from global namespace
    api_type.__metatable = false
    _G[typename] = nil
  end
  -- End (API Types Special Methods)
end


----------------------------------------------------------------------
--  API Lockdown
----------------------------------------------------------------------

-- Override global 'tolua' module with a reduced version
tolua = {
  type=tolua.type,
}

$]

/* 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);
  Unit *api_find_transport_unit
    @ transport_unit (Player *pplayer, Unit_Type *ptype, Tile *ptile);
  Tile *api_find_tile @ tile (int nat_x, int nat_y);
  Tile *api_find_tile_by_index @ tile (int index);

  /* NOTE: For overloading to work correctly, the string function
     must be before the integer function for each case below
   */
  Government *api_find_government_by_name
    @ government (const char *name_orig);
  Government *api_find_government
    @ government (int government_id);
  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);
  Unit_Type *api_find_role_unit_type
    @ role_unit_type (const char *role_name, Player *pplayer);
  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);
  Nonexistent *api_find_nonexistent
    @ nonexistent ();
}

$[
-- 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 .. ')'
      elseif v.id then
        res = res .. '(' .. v.id .. ')'
      else
        res = res .. '()'
      end
      res = res .. '\n'
    end
  end

  return res
end
$]

$[
----------------------------------------------------------------------
-- Iteration constructs for game-global objects
----------------------------------------------------------------------
do
  -- iterate over the values returned by lookup
  -- until nil is returned:
  -- lookup(0), lookup(1), lookup(2), etc
  local function index_iterate(lookup)
    local index = -1
    local function iterator()
      index = index + 1
      return lookup(index)
    end
    return iterator
  end

  -- Iterate over all players of the game
  function players_iterate()
    return index_iterate(find.player)
  end

  -- Iterate over all tiles of the game
  function whole_map_iterate()
    return index_iterate(find.tile)
  end

  -- NOTE: Identical further definitions can be made for
  -- governments, tech_types, building_types etc
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 *untranslated);
const char *api_intl_N_ @ N_ (const char *untranslated);
const char *api_intl_Q_ @ Q_ (const char *untranslated);
const char *api_intl_PL_ @ PL_ (const char *singular, const char *plural,
                                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);
}

module E {
  /* Notify events module is exported by api_specenum */
}


$[
-- Notify module implementation.

function notify.all(...)
  notify.event_msg(nil, nil, E.SCRIPT, string.format(unpack(arg)))
end

function notify.player(player, ...)
  notify.event_msg(player, nil, E.SCRIPT, 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
$]

/* Effects module */
module effects {
  int api_effects_world_bonus @ world_bonus (const char *effect_type);
  int api_effects_player_bonus @ player_bonus (Player *pplayer,
                                               const char *effect_type);
  int api_effects_city_bonus @ city_bonus (City *pcity,
                                           const char *effect_type);
}

/* Utilities module. */
int api_utilities_random @ random (int min, int max);
void api_utilities_error_log @ error_log (const char *msg);
void api_utilities_debug_log @ debug_log (const char *msg);


/* Actions module. */
Unit *api_actions_create_unit @ create_unit (Player *pplayer, Tile *ptile,
                                             Unit_Type *ptype,
                                             int veteran_level,
                                             City *homecity, int moves_left);
Unit *api_actions_create_unit_full @ create_unit_full (Player *pplayer,
                                                       Tile *ptile,
                                                       Unit_Type *ptype,
                                                       int veteran_level,
                                                       City *homecity,
                                                       int moves_left,
                                                       int hp_left,
                                                       Unit *ptransport);
void api_actions_create_city @ create_city (Player *pplayer, Tile *ptile,
                                            const char *name);
void api_actions_create_base @ create_base (Tile *ptile, const char *name,
                                            Player *pplayer);
Player *api_actions_create_player @ create_player(const char *username,
                                                  Nation_Type *nation);
void api_actions_change_gold @ change_gold (Player *pplayer, int amount);
Tech_Type *api_actions_give_technology @ give_technology (Player *pplayer,
                                                          Tech_Type *ptech,
                                                          const char *reason);
bool api_actions_unleash_barbarians @ unleash_barbarians (Tile *ptile);
void api_actions_place_partisans @ place_partisans (Tile *ptile,
                                                    Player *pplayer,
                                                    int count,
                                                    int sq_radius);