File: classical.rules

package info (click to toggle)
xmoto 0.6.1%2Brepack-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 79,648 kB
  • sloc: cpp: 106,826; ansic: 50,210; xml: 7,832; sh: 465; ruby: 26; makefile: 3
file content (146 lines) | stat: -rw-r--r-- 6,333 bytes parent folder | download | duplicates (3)
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
---------------------------------
-- Begining of classical rules --
---------------------------------

------------------------
-- Callable functions --
------------------------
-- Rules.Log(msg)                       -- log a message on the server
-- x = Rules.Player_getPoints(playerId) -- get the currrent number of points of a player
-- Rules.Player_setPoints(playerId, x)  -- set the number of points of a player
-- Rules.Player_addPoints(playerId, x)  -- add points to a player
-- Rules.SendPointsToPlayers()          -- send points information to players
-- Rules.GetTime()                      -- return the time in the scene
-- Rules.GetNbRemainingEntitiesToTake() -- return the number of entities remaining to take
------------------------
------------------------
-- Called functions ----
------------------------
-- Global_init()                       		  	    -- rules initialisation function / called once at the server start
-- Global_whenPlayer_added(playerId)   		  	    -- a new player joins -- players are never added between Round_whenRound_begin and Round_whenRound_end
-- Global_whenPlayer_removed(playerId) 		  	    -- a player go out -- as consequence, he will die too
-- Round_whenRound_begins()           		  	    -- the level begins
-- Round_whenRound_ends()              		  	    -- the level ends -- called after all players died or a player win
-- Round_whenPlayer_onEntityToTakeTaken(playerId) 	    -- the player takes a strawberry
-- Round_whenExternal_onEntityToTakeTaken()       	    -- something external (ie script) takes a strawberry
-- Round_whenPlayer_wins(playerId)                	    -- a player wins
-- Round_whenPlayer_dies(playerId)                	    -- a player dies
-- Round_whenPlayer_DoesASomersault(playerId, counterclock) -- a player does a somersault ; counterclock is 0 or 1

function Global_init()
  Rules.Log("Classical rules initialisation")
  g_bank     = { points = 10000 ; raise = 0 } -- points = maximum number of points to distributes
  g_players  = { }                            -- keep information about players ; n = number of players
  g_nPlayers = 0
end

function Global_whenPlayer_added(playerId)
  -- players starts with 0 points
  g_nPlayers          = g_nPlayers + 1
  g_players[playerId] = { points = 0 }
  Rules.Player_setPoints(playerId, g_players[playerId].points)
  Rules.SendPointsToPlayers()
end

function Global_whenPlayer_removed(playerId)
  -- returns the points to the bank
  g_bank.points = g_bank.points + g_players[playerId].points
  g_players[playerId] = nil
  g_nPlayers = g_nPlayers - 1
end

function Round_whenRound_begins()
  -- every player + the bank must participate in the pot
  g_round = { initialPot = 0 ; pot = 0 ; nLivingPlayers = g_nPlayers }

  -- bank participation : 10%
  g_bank.raise = math.floor(g_bank.points / 10) -- compute to be sure you remove exactly what you add
  if g_bank.points > 0 and g_bank.raise < 1 -- minimum participation
  then
    g_bank.raise = 1
  end
  g_bank.points = g_bank.points - g_bank.raise
  g_round.pot   = g_round.pot   + g_bank.raise

  -- players participation : 10%
  for i,v in pairs(g_players) do
    g_players[i].raise = math.floor(g_players[i].points / 10)
    if g_players[i].points > 0 and g_players[i].raise < 1 -- minimum participation
    then
      g_players[i].raise = 1
    end
    g_players[i].points = g_players[i].points - g_players[i].raise
    g_round.pot         = g_round.pot         + g_players[i].raise
    Rules.Player_setPoints(i, g_players[i].points)
  end
  Rules.SendPointsToPlayers()

  -- remains the initial number of entity to take
  g_round.pointsByEntity = math.floor(g_round.pot / (Rules.GetNbRemainingEntitiesToTake()+1)) -- +1 for the end of level ; floor to let points for the end of level
  g_round.initialPot = g_round.pot
end

function Round_whenRound_ends()
  -- close the round
  -- retrive points to players according to their participation in the pot
  for i,v in pairs(g_players) do
    local back = math.floor((g_players[i].raise / g_round.initialPot) * g_round.pot) -- floor to be sure to not retrieve more
    g_players[i].points = g_players[i].points + back
    g_round.pot         = g_round.pot         - back
    Rules.Player_setPoints(i, g_players[i].points)
  end
  Rules.SendPointsToPlayers()

  -- retrieve the rest to the bank (its participation + some rounding points (floor))
  g_bank.points = g_bank.points + g_round.pot
  g_round.pot = 0
end

function Round_whenPlayer_onEntityToTakeTaken(playerId)
  -- takes some points for an entity to take
  if g_round.nLivingPlayers > 1 -- points are distributed only when there are at least 2 players
  then
    if g_round.pot >= g_round.pointsByEntity then -- entities gave not more points if no more enough points remaining
      g_players[playerId].points = g_players[playerId].points + g_round.pointsByEntity
      g_round.pot                = g_round.pot                - g_round.pointsByEntity
      Rules.Player_setPoints(playerId, g_players[playerId].points)
      Rules.SendPointsToPlayers() -- update
    end
  end
end

function Round_whenExternal_onEntityToTakeTaken()
  -- nothing to do
end

function Round_whenPlayer_wins(playerId)
  -- takes the remaining pot (if still something is remaining)
  if g_round.nLivingPlayers > 1 -- points are distributed only when there are at least 2 players
  then
    g_players[playerId].points = g_players[playerId].points + g_round.pot -- get the remaining pot
    g_round.pot                = 0
    Rules.Player_setPoints(playerId, g_players[playerId].points) -- update the points
    Rules.SendPointsToPlayers() 
  end
end

function Round_whenPlayer_dies(playerId)
  g_round.nLivingPlayers = g_round.nLivingPlayers -1
end

function Round_whenPlayer_DoesASomersault(playerId, counterclock)
  -- takes 1 point in the pot
  if g_round.nLivingPlayers > 1 -- points are distributed only when there are at least 2 players
  then
    if g_round.pot > 0 then -- give 1 point by somersault
      g_round.pot                = g_round.pot                -1
      g_players[playerId].points = g_players[playerId].points + 1
      Rules.Player_setPoints(playerId, g_players[playerId].points) -- update the points
      Rules.SendPointsToPlayers()
    end
  end
end

----------------------------
-- End of classical rules --
----------------------------