Ir para conteúdo
Entre para seguir isso  
pejuge2

Jocker - Blackjack : NPC de Jogos

Recommended Posts

pejuge2    1
pejuge2
Autor: Pedro Silva Moreira - PeJuGe

Testado em: TFS 0.3.6.92pl1 Crying Damson

Distribuição: Autorizada, desde que cite o blog http://gcoders.info

Screen: A preguiça borrou a screen e não foi possível postá-la.

 

Depois de algum tempo sem postar scripts...

 

Bem, quem presta um pouco de atenção nos posts de NPC e te uma boa memória, deve se lembrar do Master of Games, que jogava par ou ímpar e jogo da velha, nada muito interessante.

 

O Jocker é um Master of Games aprimorado, entretanto, não utilizo os jogos anteriores. O sistema está bem mais fácil de edição para scripters quanto a adição de novos jogos.

 

Vantagens:

- Sistema de fácil adição de novos jogos.

- Código limpo.

- Padrão fácil de ser estabelecido.

- Possibilidade de uso das bibliotecas, com poucas edições para outros sistemas.

 

Para a Próxima Versão:

- Adição do jogo da forca. http://forums.otserv.com.br/showthread.php?82140-8.4-NPC-de-forca

 

Vamos lá:

XML: npc/jocker.xml

[spoiler=jocker.xml]

<?xml version="1.0" encoding="UTF-8"?>
<npc name="Jocker" nameDescription="Jocker of casino." script="data/npc/scripts/jocker.lua" walkinterval="1500" floorchange="0">
   <health now="150" max="150"/>
   <look type="132" head="150" body="80" legs="150" feet="76" addons="3" corpse="2212"/>
</npc>

 

 

XML Script: npc/scripts/jocker.lua

[spoiler=jocker.lua]

-- Loading special Jocker functions
dofile(getDataDir() .. 'npc/lib/jocker.lua')

local words =
{
start = { "hi" },
finish = { "bye" }
}

local mbet = 100

local messages =
{
start = "Hello, i'm a joker. ",
finish = "Good bye...",
games = "Choose your game: ",
bet = "Place your bet, the minumum is " .. mbet ..".",
play = "Lets play!",
win = "Congratulations, you won the game!",
draw = "Draw...",
lose = "Sorry, you lost. Try again later.",
random = { "Time to JOKE!", "Psiu", "Come on, place your bets!" }
}

local games =
{
{ name = "blackjack", func = blackjack, rules = "Rules:\nGet 21 points\nYou may get cards" }
}

for all = 1, #games do
messages.games = messages.games ..  '{' .. games[all].name .. '}'
if all > 1 then
	messages.games = messages.games .. ", "
end
end
messages.games = messages.games .. "."

local focus = {}

function onCreatureSay(cid, typ, msg)
if (getDistanceTo(cid) > 6) then
	return false;
end

if ( not getFocus(cid, focus) and getText(msg, words.start) ) then
	selfSay(messages.start, cid)
	addFocus(cid, focus)
elseif ( getFocus(cid, focus) ~= false and getText(msg, words.finish) ) then
	selfSay(messages.finish, cid)
	removeFocus(cid, focus)
elseif ( not getFocus(cid, focus) ) then
	return false
end

if focus[ getFocus(cid, focus) ].status == STATUS.reload then
	if msgcontains(msg, "yes") then
		focus[ getFocus(cid, focus) ].status = STATUS.continue
		doPlayerRemoveMoney(cid, focus[ getFocus(cid, focus) ].bet)
	elseif msgcontains(msg, "no") then
		focus[ getFocus(cid, focus) ].status = 0
	else
		selfSay("Answer: {yes}, {no}", cid)
		return true
	end
end

if focus[ getFocus(cid, focus) ].status == 0 then
	for all = 1, #games do
		if msgcontains(msg, games[all].name) then
			selfSay("Select the level of game {1...inf}. How great, better is your prize.", cid)
			focus[ getFocus(cid, focus) ].game = all
			focus[ getFocus(cid, focus) ].status = STATUS.level
			return true
		end
	end

	selfSay(messages.games, cid)
	return true
end

if focus[ getFocus(cid, focus) ].status == STATUS.level then
	focus[ getFocus(cid, focus) ].level = getNumber( msg, {1} )
	selfSay("Your level is: {" .. focus[ getFocus(cid, focus) ].level .. "}. " .. messages.bet, cid)
	focus[ getFocus(cid, focus) ].status = STATUS.bet
	return true
end

if focus[ getFocus(cid, focus) ].status == STATUS.bet then
	focus[ getFocus(cid, focus) ].bet = getNumber( msg, {mbet} )
	selfSay("Your bet is: {" .. focus[ getFocus(cid, focus) ].bet .. "}, wish continue? Answer: {yes}, {no}.", cid)
	focus[ getFocus(cid, focus) ].status = STATUS.checkbet
	return true
end

if focus[ getFocus(cid, focus) ].status == STATUS.checkbet then
	if msgcontains(msg, "no") then
		focus[ getFocus(cid, focus) ].status = STATUS.bet
		selfSay(messages.bet, cid)
		return true
	elseif msgcontains(msg, "yes") then
		focus[ getFocus(cid, focus) ].status = STATUS.continue
		selfSay(messages.play, cid)

		doPlayerRemoveMoney(cid, focus[ getFocus(cid, focus) ].bet)
	else
		selfSay("Answer: {yes}, {no}.", cid)
		return true
	end
end

if ( focus[ getFocus(cid, focus) ].game ~= 0 ) then
	focus[ getFocus(cid, focus) ].status = games[ focus[ getFocus(cid, focus) ].game ].func(focus[ getFocus(cid, focus) ], msg)
	local f_status = focus[ getFocus(cid, focus) ].status

	local prize = games[ focus[ getFocus(cid, focus) ].game ].func(focus[ getFocus(cid, focus) ], 0)
	doPlayerAddMoney(cid, prize)

	if f_status == STATUS.lose then
		selfSay(messages.lose, cid)
	elseif f_status == STATUS.draw then
		selfSay(messages.draw .. ". But you got " .. prize .. " of money.", cid)
	elseif f_status == STATUS.win then
		selfSay(messages.win .. ". You got " .. prize .. " of money!", cid)
		doSendMagicEffect(getCreaturePosition(cid), 13)
	end

	if f_status ~= STATUS.continue then
		focus[ getFocus(cid, focus) ].status = STATUS.reload
		focus[ getFocus(cid, focus) ].values = {}

		selfSay("Wanna continue with game/level/bet? Answer: {yes}, {no}.", cid)
	end
end

return true;
end

function onThink()
for all = 1, #focus do
	if( not isCreature(focus[all].id) ) then
		removeFocus(focus[all].id, focus)
	else
		distance = getDistanceTo(focus[all].id) or -1

		if( (distance > 6) or (distance == -1) ) then
			selfSay(messages.finish, focus[all].id)
			removeFocus(focus[all].id, focus)
		end
	end
end

local rand = math.random(1, 600)
if (rand <= #messages.random) then
	selfSay( messages.random[rand] )
end
end

 

 

Jocker lib: npc/lib/jocker.lua

[spoiler=jocker.lua]

-- Include the Advanced NPC System
dofile(getDataDir() .. 'npc/lib/npcsystem/npcsystem.lua')

-- Loading Blackjack
dofile(getDataDir() .. 'npc/lib/blackjack.lua')

STATUS =
{
level = 1,
lose = 2,
draw = 3,
win = 4,
continue = 5,
reload = 6,
bet = 7,
checkbet = 8
}

function getFocusData(cid)
return { id = cid, status = 0, game = 0, level = 0, bet = 0, values = {} }
end

function getFocus(cid, f_table)
for all = 1, #f_table do
	if(f_table[all].id == cid) then
		return all
	end
end
return false
end

function addFocus(cid, f_table)
if( not getFocus(cid, f_table) ) then
	table.insert(f_table, getFocusData(cid))
end
end

function removeFocus(cid, f_table)
for all = 1, #f_table do
	if (f_table[all].id == cid) then
		table.remove(f_table, i)
		break
	end
end
end

 

 

Blackjack lib: npc/lib/blackjack.lua

[spoiler=blackjack.lua]

dofile(getDataDir() .. 'npc/lib/npc.lua')

local function getValue(number)
while number > 13 do
	number = number - 13
end
return number
end

local function getSuit(number)
local ret = 1
while number > 13 do
	number = number - 13
	ret = ret + 1
end

return ret
end

local function getName(number)
local msg = ""
local tmp = getValue(number)

if (tmp == 1) then
	msg = "Ace"
elseif (tmp == 11) then
	msg = "Jack"
elseif (tmp == 12) then
	msg = "Queen"
elseif (tmp == 13) then
	msg = "King"
else msg = tmp
end

msg = msg .. " of "
tmp = getSuit(number)
if (tmp == 1) then
	msg = msg .. "Spade"
elseif (tmp == 2) then
	msg = msg .. "Heart"
elseif (tmp == 3) then
	msg = msg .. "Diamond"
else msg = msg .. "Club"
end

return msg
end

local function getTotal(data)
local total = 0
local ace = false

for all = 1, #data.values do
	if getValue( data.values[all] ) == 1 then
		ace = true
	end
	total = total + getValue( data.values[all] )
end

if ace and total <= 11 then
	return { total, total + 10 } 
end

return {total}
end

local function addCard(data, number)
table.insert(data.values, number)

selfSay("You got a card: " .. getName( number ) .. ".", data.id)
end

local function addRandomCard(data)
local rand
local lower = math.huge

for all = 0, data.level * 2 do
	rand = math.random(1, 52)
	if getTotal(data)[1] + getValue(rand) > 21 then
		lower = rand
		break
	elseif getTotal(data)[1] + getValue(rand) < lower + getTotal(data)[1] and getValue(rand) ~= 1 then
		lower = rand
	end
end

if lower == math.huge then
	lower = 1
end

addCard(data, lower)
end

function blackjack(data, msg)
if type(msg) == "number" then
	if data.status == STATUS.win then
		return data.bet * data.level * 1.5
	elseif data.status == STATUS.draw then
		return data.bet * data.level * 0.85
	else return 0
	end
end

local stop = false

if #data.values == 0 then
	addRandomCard(data)
	addRandomCard(data)
elseif msgcontains(msg, "yes") then
	addRandomCard(data)
elseif msgcontains(msg, "no") then
	stop = true
end

local total = getTotal(data)
if #total == 2 then
	selfSay("You got " .. total[1] .. "/" .. total[2] .. " points.", data.id)
else selfSay("You got " .. total[1] .. " points.", data.id)
end
total = total[#total]

if total == 21 then
	stop = true
elseif total < 21 and not stop then
	selfSay("Wanna get annother card? Answer: {yes}, {no}.", data.id)
elseif total > 21 then
	return STATUS.lose
end

if stop == true then
	local best = math.random(1, 21)
	local rand

	for all = 1, data.level * 3 do
		rand = math.random(1, 33)

		if best < rand and rand <= 21 then
			best = rand
		end
	end

	selfSay("I got " .. best .. " points.", data.id)
	if best > total then
		return STATUS.lose
	elseif best == total then
		return STATUS.draw
	else return STATUS.win
	end
end

return STATUS.continue
end

 

 

Configuração

Os textos estão em inglês e não se referem a qualquer servidor ou especificidade, caso ainda sim necessite alterar as mensagens para exibição e outros valores, todos estão no arquivo npc/scripts/jocker.lua, sendo que mbet é a aposta mínima. A parte citada é a seguinte:

[spoiler=Configuração]

local words =
{
start = { "hi" },
finish = { "bye" }
}

local mbet = 100

local messages =
{
start = "Hello, i'm a joker. ",
finish = "Good bye...",
games = "Choose your game: ",
bet = "Place your bet, the minumum is " .. mbet ..".",
play = "Lets play!",
win = "Congratulations, you won the game!",
draw = "Draw...",
lose = "Sorry, you lost. Try again later.",
random = { "Time to JOKE!", "Psiu", "Come on, place your bets!" }
}

 

 

Edição:

Caso você queira adicionar novos jogos, é simples, em games adicione as informações como foi feito com o blackjack e observe como é feito o blackjack no arquivo npc/lib/blackjack.lua.

[spoiler=games]

local games =
{
{ name = "blackjack", func = blackjack, rules = "Rules:\nGet 21 points\nYou may get cards" }
}

 

 

Edit: O jogo é programado para o usuário perder, quanto maior o nível do jogo, maior o prêmio, mas maior a dificuldade de ganhar.

Espero que gostem, se gostarem acessem o link abaixo \/ (na minha assinatura) e comente. ( GCoders )

 

Atenciosamente, Pedro Silva Moreira - PeJuGe.

Editado por pejuge2

Compartilhar este post


Link para o post
Oneshot    24
Oneshot

Parabéns, Pejuge. Além de adaptar dois jogos de azar para um sistema LUA, você o fez de tal forma que o código se tornasse bastante limpo e aberto a modificações, como a adição de outros jogos. Ótimo trabalho!

 

Aprovado!

Seu script recebeu uma nota A-.

Você receberá V$ 65 como remuneração.

Editado por Garou

Compartilhar este post


Link para o post
pejuge2    1
pejuge2

Vlw Garou, dedico a facilidade de edição a mim mesmo, kkk. Algumas coisas a prática ajuda bastante ^^.

Para quem gostou acompanhe os tópicos posteriores do jocker que postarei novos jogos para ele.

Compartilhar este post


Link para o post
Victinh0    0
Victinh0

Otimo! KK apesar de nao entender muito de scripting o codigo que parece muito bom, bem limpo. V$ bem merecidos :)

Compartilhar este post


Link para o post
Entre para seguir isso  
  • Quem Está Navegando   0 membros estão online

    Nenhum usuário registrado visualizando esta página.

×