From 81b11f1582c4eef3dd90795cbbb6730c41fc0f51 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Mon, 6 May 2024 03:50:16 +0300 Subject: [PATCH 64/64] Lua: Add edit.create_trade_route() method See osdn #46550 Signed-off-by: Marko Lindqvist --- common/traderoutes.c | 3 +- server/scripting/api_server_edit.c | 39 ++++++++++++++++++++++++ server/scripting/api_server_edit.h | 2 ++ server/scripting/tolua_server.pkg | 3 ++ server/unithand.c | 49 ++++++++++++++++++------------ server/unithand.h | 3 ++ 6 files changed, 78 insertions(+), 21 deletions(-) diff --git a/common/traderoutes.c b/common/traderoutes.c index 883aa1dc8f..e2676b8102 100644 --- a/common/traderoutes.c +++ b/common/traderoutes.c @@ -277,8 +277,7 @@ int city_trade_removable(const struct city *pcity, int priority, /*********************************************************************//** Returns TRUE iff the two cities can establish a trade route. We look at the distance and ownership of the cities as well as their existing - trade routes. Should only be called if you already know that - can_cities_trade(). + trade routes. *************************************************************************/ bool can_establish_trade_route(const struct city *pc1, const struct city *pc2, int priority) diff --git a/server/scripting/api_server_edit.c b/server/scripting/api_server_edit.c index 5362974cc1..f96f507ad5 100644 --- a/server/scripting/api_server_edit.c +++ b/server/scripting/api_server_edit.c @@ -1202,3 +1202,42 @@ void api_edit_player_give_bulbs(lua_State *L, Player *pplayer, int amount, } } } + +/**********************************************************************//** + Create a trade route between two cities. +**************************************************************************/ +bool api_edit_create_trade_route(lua_State *L, City *from, City *to) +{ + struct player *pplayer, *partner_player; + + LUASCRIPT_CHECK_STATE(L, FALSE); + LUASCRIPT_CHECK_ARG_NIL(L, from, 2, City, FALSE); + LUASCRIPT_CHECK_ARG_NIL(L, to, 3, City, FALSE); + + /* Priority zero -> never replace old routes. */ + if (!can_establish_trade_route(from, to, 0)) { + return FALSE; + } + + create_trade_route(from, to, goods_from_city_to_unit(from, NULL)); + + /* Refresh the cities. */ + city_refresh(from); + city_refresh(to); + + pplayer = city_owner(from); + partner_player = city_owner(to); + + send_city_info(pplayer, from); + send_city_info(partner_player, to); + + /* Notify each player about the other's cities. */ + if (pplayer != partner_player && game.info.reveal_trade_partner) { + map_show_tile(partner_player, city_tile(from)); + send_city_info(partner_player, from); + map_show_tile(pplayer, city_tile(to)); + send_city_info(pplayer, to); + } + + return TRUE; +} diff --git a/server/scripting/api_server_edit.h b/server/scripting/api_server_edit.h index c35212b1d4..a62362297d 100644 --- a/server/scripting/api_server_edit.h +++ b/server/scripting/api_server_edit.h @@ -117,4 +117,6 @@ void api_edit_player_add_history(lua_State *L, Player *pplayer, int amount); void api_edit_player_give_bulbs(lua_State *L, Player *pplayer, int amount, Tech_Type *tech); +bool api_edit_create_trade_route(lua_State *L, City *from, City *to); + #endif /* API_SERVER_EDIT_H */ diff --git a/server/scripting/tolua_server.pkg b/server/scripting/tolua_server.pkg index f5e37b8859..b33ce88bb1 100644 --- a/server/scripting/tolua_server.pkg +++ b/server/scripting/tolua_server.pkg @@ -240,6 +240,9 @@ module edit { void api_edit_player_give_bulbs @ give_bulbs(lua_State *L, Player *self, int amount, Tech_Type *tech = NULL); + + bool api_edit_create_trade_route + @ create_trade_route(lua_State *L, City *from, City *to); } /* Luadata module. */ diff --git a/server/unithand.c b/server/unithand.c index 4e9f569242..20518cb4c7 100644 --- a/server/unithand.c +++ b/server/unithand.c @@ -6196,7 +6196,6 @@ static bool do_unit_establish_trade(struct player *pplayer, } if (can_establish) { - struct trade_route *proute_from, *proute_to; struct city_list *cities_out_of_home; struct city_list *cities_out_of_dest; struct player *partner_player; @@ -6248,24 +6247,7 @@ static bool do_unit_establish_trade(struct player *pplayer, free(pback); } trade_route_list_iterate_end; - /* Actually create the new trade route */ - proute_from = fc_malloc(sizeof(struct trade_route)); - proute_from->partner = pcity_dest->id; - proute_from->goods = goods; - - proute_to = fc_malloc(sizeof(struct trade_route)); - proute_to->partner = pcity_homecity->id; - proute_to->goods = goods; - - if (goods_has_flag(goods, GF_BIDIRECTIONAL)) { - proute_from->dir = RDIR_BIDIRECTIONAL; - proute_to->dir = RDIR_BIDIRECTIONAL; - } else { - proute_from->dir = RDIR_FROM; - proute_to->dir = RDIR_TO; - } - trade_route_list_append(pcity_homecity->routes, proute_from); - trade_route_list_append(pcity_dest->routes, proute_to); + create_trade_route(pcity_homecity, pcity_dest, goods); /* Refresh the cities. */ city_refresh(pcity_homecity); @@ -6336,6 +6318,35 @@ static bool do_unit_establish_trade(struct player *pplayer, return TRUE; } +/**********************************************************************//** + Create a trade route between cities. No checks - callers should have + made sure that the route can be added already. +**************************************************************************/ +void create_trade_route(struct city *from, struct city *to, + struct goods_type *goods) +{ + struct trade_route *proute_from, *proute_to; + + /* Actually create the new trade route */ + proute_from = fc_malloc(sizeof(struct trade_route)); + proute_from->partner = to->id; + proute_from->goods = goods; + + proute_to = fc_malloc(sizeof(struct trade_route)); + proute_to->partner = from->id; + proute_to->goods = goods; + + if (goods_has_flag(goods, GF_BIDIRECTIONAL)) { + proute_from->dir = RDIR_BIDIRECTIONAL; + proute_to->dir = RDIR_BIDIRECTIONAL; + } else { + proute_from->dir = RDIR_FROM; + proute_to->dir = RDIR_TO; + } + trade_route_list_append(from->routes, proute_from); + trade_route_list_append(to->routes, proute_to); +} + /**********************************************************************//** Change various unit server side client state. diff --git a/server/unithand.h b/server/unithand.h index 60396547e2..53a051f915 100644 --- a/server/unithand.h +++ b/server/unithand.h @@ -66,4 +66,7 @@ bool unit_server_side_agent_set(struct player *pplayer, struct unit *punit, enum server_side_agent agent); +void create_trade_route(struct city *from, struct city *to, + struct goods_type *goods); + #endif /* FC__UNITHAND_H */ -- 2.43.0