From 3540ba0724fca60f18713cccaeecd147fee8078f Mon Sep 17 00:00:00 2001 From: Allanis Date: Sat, 1 Feb 2014 22:09:49 +0000 Subject: [PATCH] [Add] Gave AI customizable bribe messages. --- scripts/ai/collective.lua | 5 +++ scripts/ai/draktharr.lua | 5 +++ scripts/ai/empire.lua | 14 +++++++ scripts/ai/pirate.lua | 19 ++++++++- scripts/ai/trader.lua | 4 +- src/ai.c | 1 + src/comm.c | 81 ++++++++++++++++++++++++++++++--------- 7 files changed, 107 insertions(+), 22 deletions(-) diff --git a/scripts/ai/collective.lua b/scripts/ai/collective.lua index dde8eb7..0feaf84 100644 --- a/scripts/ai/collective.lua +++ b/scripts/ai/collective.lua @@ -5,3 +5,8 @@ control_rate = 0.5 -- Lower control rate. aggressive = true land_planet = false +function create() + mem.bribe_no = "No response." + attack_choose() +end + diff --git a/scripts/ai/draktharr.lua b/scripts/ai/draktharr.lua index 6e9a201..ef4d102 100644 --- a/scripts/ai/draktharr.lua +++ b/scripts/ai/draktharr.lua @@ -7,6 +7,11 @@ aggressive = true function create() ai.setcredits(rnd.int(1000, ai.shipprice()/200)) attack_choose() + if rnd.int() > 0.4 then + mem.bribe_no = "\"I shall especially enjoy your death.\"" + else + mem.bribe_no = "\"You shall not buy my honor!\"" + end end function taunt(target, offense) diff --git a/scripts/ai/empire.lua b/scripts/ai/empire.lua index 8cd115a..5778c97 100644 --- a/scripts/ai/empire.lua +++ b/scripts/ai/empire.lua @@ -10,6 +10,20 @@ function create() if rnd.int(0,2)==0 then ai.broadcast("The Empire is watching") end + + if rnd.int() > 0.7 then + mem.bribe = math.sqrt(ai.shipmass())*(500. * rnd.int() + 1750.) + mem.bribe_prompt = string.format("\"For some %d credits I could forget \ + about seeing you.\"", mem.bribe) + mem.bribe_paid = "\"Now, scram, before I change my mind.\"" + else + if rnd.int() > 0.5 then + mem.bribe_no = "\"You won't buy your way out of this one.\"" + else + mem.bribe_no = "\"The Empire likes to make examples out of scum like you.\"" + end + end + attack_choose() end diff --git a/scripts/ai/pirate.lua b/scripts/ai/pirate.lua index e840efd..bf19721 100644 --- a/scripts/ai/pirate.lua +++ b/scripts/ai/pirate.lua @@ -7,9 +7,24 @@ armour_run = 80 armour_return = 100 function create() - ai.setcredits(ai.shipprice()/1000, ai.shipprice()/100) - mem.bribe = math.sqrt(ai.shipmass()) * (300. * rnd.int() + 850.) attack_choose() + ai.setcredits(ai.shipprice()/1000, ai.shipprice()/100) + + -- Deal with bribeability. + if rnd.int() < 0.05 then + mem.bribe_no = = "\"You won't be able to slide out of this one!\"" + else + mem.bribe = math.sqrt(ai.shipmass()) * (300. * rnd.int() + 850.) + if rnd.int() > 0.5 then + mem.bribe_prompt = string.format("\"It'll cost you %d credits for me to ifnore your pile \ + of rubbish.\"", mem.bribe) + mem.bribe_paid = "\"You're lucky I'm so kind.\"" + else + mem.bribe_prompt = string.format("\"I'm in a good mood so I'll let you go for \ + %d credits.\"", mem.bribe) + mem.bribe_paid = "\"Life doesn't get easier then this.\"" + end + end end function taunt(target, offense) diff --git a/scripts/ai/trader.lua b/scripts/ai/trader.lua index f2b82cd..df1848f 100644 --- a/scripts/ai/trader.lua +++ b/scripts/ai/trader.lua @@ -4,7 +4,7 @@ function sos() msg = { "Mayday! We are under attack!", "Requesting assistance. We are under attack!", - "Merchant vessle here under attack! HELP!" + "Merchant vessel here under attack! HELP!" } ai.broadcast(msg[rnd.int(1, #msg)]) end @@ -12,6 +12,8 @@ end function create() ai.setcredits(rnd.int(100, ai.shipprice()/50)) + mem.bribe_no = "\"The space Traders do not negotiate with criminals.\"" + -- Some stuff has more than others. num = rnd.int(12) if num < 5 then diff --git a/src/ai.c b/src/ai.c index 067df6d..1cc0a2c 100644 --- a/src/ai.c +++ b/src/ai.c @@ -443,6 +443,7 @@ static int ai_loadProfile(char* filename) { /* Open basic Lua stuff. */ llua_loadBasic(L); + llua_load(L, luaopen_string); /* Open string. */ /* Constants. */ lua_regnumber(L, "player", PLAYER_ID); /* Player id. */ diff --git a/src/comm.c b/src/comm.c index fa1d321..76319ad 100644 --- a/src/comm.c +++ b/src/comm.c @@ -20,8 +20,9 @@ static Pilot* comm_pilot = NULL; /**< Pilot currently talking to. */ /* Static. */ -static void comm_bribe(unsigned int wid, char* str); +static void comm_bribe(unsigned int wid, char* unused); static unsigned int comm_getBribeAmount(void); +static char* comm_getBribeString(char* str); /* Extern. */ void ai_setPilot(Pilot* p); @@ -115,21 +116,35 @@ int comm_open(unsigned int pilot) { * @param wid ID of window calling the function. * @param str Unused. */ -static void comm_bribe(unsigned int wid, char* str) { - (void)str; +static void comm_bribe(unsigned int wid, char* unused) { + (void)unused; int answer; int price; + char* str; + + /* Set up for the comm_get* functions. */ + ai_setPilot(comm_pilot); price = comm_getBribeAmount(); /* Unbribeable. */ if(price == 0) { - dialogue_msg("Bribe Pilot", "\"Money won't save your hide now!\""); + str = comm_getBribeString("bribe_no"); + if(str == NULL) + dialogue_msg("Bribe Pilot", "\"Money won't save your hide now!\""); + else + dialogue_msg("Bribe Pilot", "%s", str); return; } - answer = dialogue_YesNo("Bribe Pilot", "\"I'm gonna need at least %d credits \ - to not leave you as a hunk of floating debris.\"\n\npay %d Credits?", price, price); + /* Bribe message. */ + str = comm_getBribeString("bribe_prompt"); + if(str == NULL) { + answer = dialogue_YesNo("Bribe Pilot", "\"I'm gonna need at least %d credits to not leave \ + you as a hunk of floating debris.\"\n\nPay %d credits?", price, price); + } + else + answer = dialogue_YesNo("Bribe Pilot", "%s\n\npay %d credits?", str, price); /* Said no. */ if(answer == 0) { @@ -142,7 +157,11 @@ static void comm_bribe(unsigned int wid, char* str) { dialogue_msg("Bribe Pilot", "You don't have enough credits for the bribary."); } else { player->credits -= price; - dialogue_msg("Bribe Pilot", "\"Pleasure to do business with you.\""); + str = comm_getBribeString("bribe_paid"); + if(str == NULL) + dialogue_msg("Bribe Pilot", "\"Pleasure to do business with you.\""); + else + dialogue_msg("Bribe Pilot", "%s", str); pilot_setFlag(comm_pilot, PILOT_BRIBED); /* Reopen window. */ window_destroy(wid); @@ -158,27 +177,51 @@ static void comm_bribe(unsigned int wid, char* str) { */ static unsigned int comm_getBribeAmount(void) { lua_State* L; - double bribe; + unsigned int bribe; /* Set up the state. */ - ai_setPilot(comm_pilot); L = comm_pilot->ai->L; + lua_getglobal(L, "mem"); /* Get the bribe amount. */ - lua_getglobal(L, "mem"); lua_getfield(L, -1, "bribe"); /* If not number consider unbribeable. */ - if(!lua_isnumber(L, -1)) { - lua_pop(L, 2); - return 0; - } - - /* Get amount. */ - bribe = (unsigned int) lua_tonumber(L, -1); - - /* Clean up. */ + if(!lua_isnumber(L, -1)) + bribe = 0; + else + bribe = (unsigned int)lua_tonumber(L, -1); lua_pop(L, 2); return bribe; } +/** + * @brief Get a string from the pilots memory. + * + * Valid targets are: + * -- bribe_no : Unbribe message. + * -- bribe_prompt : Bribe prompt. + * -- bribe_paid: paid message. + * + * @param str String to get. + * @return String matching str. + */ +static char* comm_getBribeString(char* str) { + lua_State* L; + char* ret; + + /* Get memory table. */ + L = comm_pilot->ai->L; + lua_getglobal(L, "mem"); + + /* Get str message. */ + lua_getfield(L, -1, str); + if(!lua_isstring(L, -1)) + ret = NULL; + else + ret = (char*) lua_tostring(L, -1); + lua_pop(L, 2); + + return ret; +} +