File: FDtux_lfuns.lua

package info (click to toggle)
freedroidrpg 1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 273,532 kB
  • sloc: ansic: 66,191; cpp: 2,033; sh: 766; makefile: 627; python: 322; xml: 94; perl: 87
file content (434 lines) | stat: -rw-r--r-- 12,642 bytes parent folder | download | duplicates (4)
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
--[[

  Copyright (c) 2013 Samuel Degrande

  This file is part of Freedroid

  Freedroid 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 of the License, or
  (at your option) any later version.

  Freedroid 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.

  You should have received a copy of the GNU General Public License
  along with Freedroid; see the file COPYING. If not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  MA  02111-1307  USA

]]--

--!
--! \file FDtux_lfuns.lua
--! \brief This file contains the Lua functions to add to FDtux (Lua binding of Tux).
--!

local function output_msg(msg)
	if (run_from_dialog()) then
		cli_says(msg, "NO_WAIT")
		npc_says("")
	else
		display_big_message(msg)
	end
end

--! \addtogroup FDtux
--!@{
-- start FDtux submodule

--! \fn number get_hp_ratio(self)
--!
--! \brief Returns Tux hp/max_hp ratio
--!
--! \param self [\p FDtux] FDtux instance
--!
--! \return Tux's HP ratio
--!
--! \bindtype lfun

function FDtux.get_hp_ratio(self)
	return self:get_hp() / self:get_max_hp()
end

--! \fn bool del_gold(self, amount)
--!
--! \brief Remove some amount of gold.
--!
--! If the \e amount of gold is lower than what Tux currently has, then remove
--! that amount of gold and return TRUE. Else, do nothing and return FALSE
--!
--! \param self   [\p FDtux]   FDtux instance
--! \param amount [\p integer] Amount of gold to remove
--!
--! \return TRUE if gold was actually removed, FALSE otherwise
--!
--! \bindtype lfun

function FDtux.del_gold(self, amount)
	if (amount <= self:get_gold()) then
		self:add_gold(-amount)
		return true
	else
		return false
	end
end

--! \fn bool del_points(self, points)
--!
--! \brief Remove some training points
--!
--! If the amount of \e points is lower than current training points, then remove
--! that amount of training points and return TRUE. Else, do nothing and return
--! FALSE
--!
--! \param self   [\p FDtux]   FDtux instance
--! \param points [\p integer] Amount of training points to remove
--!
--! \return TRUE if training points were actually removed, FALSE otherwise
--!
--! \bindtype lfun

function FDtux.del_points(self, points)
	if (self:get_training_points() >= points) then
		self:del_training_points(points)
		return true
	else
		return false
	end
end

--! \fn bool del_health(self, points)
--!
--! \brief Remove some health points
--!
--! If the amount of \e points is lower than current health points, then remove
--! that amount of health points and return TRUE. Else, do nothing and return
--! FALSE
--!
--! \param self   [\p FDtux]   FDtux instance
--! \param points [\p integer] Amount of health points to remove
--!
--! \return TRUE if health points were actually removed, FALSE otherwise
--!
--! \bindtype lfun

function FDtux.del_health(self, points)
	if (points < self:get_hp()) then
		self:hurt(points)
		return true
	else
		return false
	end
end

--! \fn bool can_train(self, gold, points)
--!
--! \brief Check if Tux has enough gold and training points
--!
--! Improving a program or a skill cost money, and cost training points.\n
--! This function can be used to check in one single call if Tux has enough
--! money and training points before to be trained.
--!
--! \param self   [\p FDtux]   FDtux instance
--! \param gold   [\p integer] Amount of gold to check
--! \param points [\p integer] Amount of points to check
--!
--! \return TRUE if Tux has enough gold AND enough training points
--!
--! \bindtype lfun

function FDtux.can_train(self, gold, points)
	if (self:get_gold() < gold) then
		return false
	end

	if (self:get_training_points() < points) then
		return false
	end

	return true
end

--! \fn bool train_skill(self, gold, points, skill)
--!
--! \brief Improve a skill by one level
--!
--! Improve the given \e skill, and remove \e gold and training \e points to Tux.\n
--! First check that Tux has enough \e gold and training \e points. If not, do not
--! improve the skill.
--!
--! \param self   [\p FDtux]   FDtux instance
--! \param gold   [\p integer] Amount of gold to remove
--! \param points [\p integer] Amount of points to remove
--! \param skill  [\p string]  Name of the skill to improve
--!
--! \return TRUE if the skill was improved
--!
--! \bindtype lfun

function FDtux.train_skill(self, gold, points, skill)
	if (not self:can_train(gold, points)) then
		return false
	end

	self:add_gold(-gold)
	self:del_training_points(points)
	self:improve_skill(skill)

	return true
end

--! \fn bool train_program(self, gold, points, program)
--!
--! \brief Improve a program by one level
--!
--! Improve the given \e program, and remove \e gold and training \e points to Tux.\n
--! First check that Tux has enough \e gold and training \e points. If not, do not
--! improve the skill.
--!
--! \param self    [\p FDtux]   FDtux instance
--! \param gold    [\p integer] Amount of gold to remove
--! \param points  [\p integer] Amount of points to remove
--! \param program [\p string]  Name of the program to improve
--!
--! \return TRUE if the program was improved
--!
--! \bindtype lfun

function FDtux.train_program(self, gold, points, program)
	if (not self:can_train(gold, points)) then
		return false
	end

	self:add_gold(-gold)
	self:del_training_points(points)
	self:improve_program(program)

	return true
end

--! \fn void add_quest(self, quest, text)
--!
--! \brief Assign a quest to the player, and fill the diary
--!
--! If the \e quest was already assigned, output an error and do nothing.
--! Else, assign the \e quest, add the \e text to the diary, play a sound and
--! notify in the console
--!
--! \param self  [\p FDtux]  FDtux instance
--! \param quest [\p string] Name of the quest to assign
--! \param text  [\p string] Text to add in the diary
--!
--! \bindtype lfun

function FDtux.add_quest(self, quest, text)
	if (not running_benchmark()) then
		if self:done_quest(quest) or
		   self:has_quest(quest) then
			print(FDutils.text.red("\n\tSEVERE ERROR"))
			print(FDutils.text.red("\tTried to assign already assigned quest!"))
			print(FDutils.text.red("\tWe will continue execution, quest is:"))
			print(FDutils.text.red(quest))
		end
	end
	self:assign_quest(quest, text)
	play_sound("effects/Mission_Status_Change_Sound_0.ogg")
	output_msg("   " .. S_"New Quest assigned: " .. D_(quest))
end

--! \fn void update_quest(self, quest, text)
--!
--! \brief Update the diary text associated to a quest
--!
--! If the \e quest was not yet assigned, output an error and do nothing.
--! Else, add the \e text to the diary, play a sound and notify in the console
--!
--! \param self  [\p FDtux]  FDtux instance
--! \param quest [\p string] Name of the quest to update
--! \param text  [\p string] Text to add in the diary
--!
--! \bindtype lfun

function FDtux.update_quest(self, quest, text)
	if (self:has_quest(quest)) then
		add_diary_entry(quest, text)
		play_sound("effects/Mission_Status_Change_Sound_0.ogg")
		output_msg("   " .. S_"Quest log updated: " .. D_(quest))
	else -- we don't have the quest, how so ?
		if (not running_benchmark()) then -- don't spam the validator
			print(FDutils.text.red("\n\tSEVERE ERROR"))
			print(FDutils.text.red("\tTried to update quest that was never assigned!"))
			print(FDutils.text.red("\tWe will continue execution, quest is:"))
			print(FDutils.text.red(quest))
		end
	end
end

--! \fn void end_quest(self, quest, text)
--!
--! \brief Complete a quest, and fill the diary text associated to the quest
--!
--! If the \e quest was not yet assigned, or is already completed, output an
--! error and do nothing. Else, complete the \e quest, add the \e text to the
--! diary, play a sound and notify in the console
--!
--! \param self  [\p FDtux]  FDtux instance
--! \param quest [\p string] Name of the quest to complete
--! \param text  [\p string] Text to add in the diary
--!
--! \bindtype lfun

function FDtux.end_quest(self, quest, text)
	if (not running_benchmark()) then -- don't spam the validator
		if (self:done_quest(quest)) then
				print(FDutils.text.red("\n\tERROR"))
				print(FDutils.text.red("\tTried to end already done quest!"))
				print(FDutils.text.red("\tWe will continue execution, quest is:"))
				print(FDutils.text.red(quest))
		elseif (not self:has_quest(quest)) then
				print(FDutils.text.red("\n\tSEVERE ERROR"))
				print(FDutils.text.red("\tTried to end never assigned quest!"))
				print(FDutils.text.red("\tWe will continue execution, quest is:"))
				print(FDutils.text.red(quest))
		end
	end

	self:complete_quest(quest, text)
	play_sound("effects/Mission_Status_Change_Sound_0.ogg")
	output_msg("   " .. S_"Quest completed: " .. D_(quest))
end

--! \fn integer count_item(self, item)
--!
--! \brief Return the number of a given item that Tux possesses
--!
--! Count the number of \e item those are in the inventory or equipped.
--!
--! \param self [\p FDtux]  FDtux instance
--! \param item [\p string] Name of the item to count
--!
--! \return Number of items
--!
--! \bindtype lfun

function FDtux.count_item(self, item)
	local number = self:count_item_backpack(item)
	if self:has_item_equipped(item) then
		return number + 1
	else
		return number
	end
end

--! \fn bool has_item(self, ...)
--!
--! \brief Check if all items of a list are in Tux's possession
--!
--! \param self [\p FDtux]  FDtux instance
--! \param ...  [\p string] Comma separated list of names of item to check
--!
--! \return TRUE if Tux has got all items, FALSE otherwise
--!
--! \bindtype lfun

function FDtux.has_item(self, ...)
	for i,item in ipairs({...}) do
		if (not (self:count_item(item) > 0)) then
			return false
		end
	end
	return true
end

--! \fn bool has_item_backpack(self, item)
--!
--! \brief Check if an item is in Tux's inventory (but not equipped)
--!
--! \param self [\p FDtux]  FDtux instance
--! \param item [\p string] Name of the item to check
--!
--! \return TRUE if the item is in the inventory, FALSE otherwise
--!
--! \bindtype lfun

function FDtux.has_item_backpack(self, item)
	return (self:count_item_backpack(item) > 0)
end

--! \fn bool del_item(self, item)
--!
--! \brief Remove an item from Tux's inventory (but not equipped)
--!
--! \param self [\p FDtux]  FDtux instance
--! \param item [\p string] Name of the item to remove
--!
--! \return TRUE if the item was removed, FALSE otherwise (the item is not in
--! the inventory or is equipped)
--!
--! \bindtype lfun

function FDtux.del_item(self, item)
	if (self:count_item_backpack(item) > 0) then
		self:del_item_backpack(item)
		return true
	else
		return false
	end
end

--! \fn void says(self, format, ...)
--!
--! \brief Display a formatted text in the chat log
--!
--! Output a text in the chat log and wait the user to click.\n
--! The text is displayed using the color/font associated to Tux words.\n
--! If the last argument is "NO WAIT", the user's click is not waited. This
--! optional argument is not used to create the formatted text.
--!
--! \param self   [\p FDtux]  FDtux instance
--! \param format [\p string] Format string (as used in string.format())
--! \param ...    [\p any]    Arguments expected by the format string to create the displayed text
--!
--! \bindtype lfun

function FDtux.says(self, format, ...)
	local text, no_wait = chat_says_format('\1- ' .. format .. '\n', ...)
	chat_says(text, no_wait)
end

--! \fn void says_random(self, ...)
--!
--! \brief Randomly choose one of the text arguments and display it in the chat log
--!
--! Output a text in the chat log and wait the user to click.\n
--! The text to display (using the color/font associated to Tux words) is randomly
--! chosen from one of the string arguments.\n
--! If the last argument is "NO WAIT", the user's click is not waited. This
--! optional argument is not used as a text to display.
--!
--! \param self   [\p FDtux]  FDtux instance
--! \param ...    [\p string] List of strings
--!
--! \bindtype lfun

function FDtux.says_random(self, ...)
	arg = {...}
	if (arg[#arg] == "NO_WAIT") then
		self:says(arg[math.random(#arg-1)],"NO_WAIT")
	else
		self:says(arg[math.random(#arg)])
	end
end

-- end FDtux submodule
--!@}

-- Override some methods (cfuns as well as lfuns) when used during dialog validation
function FDtux.__override_for_validator(FDtux)
	-- _says functions are not run by the validator, as they display
	-- text on screen and wait for clicks
	FDtux.says = function(self) end
end