aduermael commited on
Commit
034803e
1 Parent(s): cf3df7c

update script

Browse files
Files changed (1) hide show
  1. cubzh.lua +512 -7
cubzh.lua CHANGED
@@ -1,11 +1,516 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  Client.OnStart = function()
2
- Camera:SetParent(World)
 
 
 
3
 
4
- local ui = require("uikit")
 
5
 
6
- local text = ui:createText("hello world!", Color.Black)
7
- text.parentDidResize = function(self)
8
- self.pos = { Screen.Width * 0.5 - self.Width * 0.5, Screen.Height * 0.5 - self.Height * 0.5 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  end
10
- text:parentDidResize()
11
- end
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Modules = {
2
+ gigax = "github.com/GigaxGames/integrations/cubzh:cdfd9a2",
3
+ pathfinding = "github.com/caillef/cubzh-library/pathfinding:f8c4315",
4
+ }
5
+
6
+ Config = {
7
+ Items = { "pratamacam.squirrel" },
8
+ }
9
+
10
+ -- Function to spawn a squirrel above the player
11
+ function spawnSquirrelAbovePlayer(player)
12
+ local squirrel = Shape(Items.pratamacam.squirrel)
13
+ squirrel:SetParent(World)
14
+ squirrel.Position = player.Position + Number3(0, 20, 0)
15
+ -- make scale smaller
16
+ squirrel.LocalScale = 0.5
17
+ -- remove collision
18
+ squirrel.Physics = PhysicsMode.Dynamic
19
+ -- rotate it 90 degrees to the right
20
+ squirrel.Rotation = { 0, math.pi * 0.5, 0 }
21
+ -- this would make squirrel.Rotation = player.Rotation
22
+ World:AddChild(squirrel)
23
+ return squirrel
24
+ end
25
+
26
+ local SIMULATION_NAME = "Islands" .. tostring(math.random())
27
+ local SIMULATION_DESCRIPTION = "Three floating islands."
28
+
29
+ local occupiedPositions = {}
30
+
31
+ local skills = {
32
+ {
33
+ name = "SAY",
34
+ description = "Say smthg out loud",
35
+ parameter_types = { "character", "content" },
36
+ callback = function(client, action)
37
+ local npc = client:getNpc(action.character_id)
38
+ if not npc then
39
+ print("Can't find npc")
40
+ return
41
+ end
42
+ dialog:create(action.content, npc.avatar)
43
+ print(string.format("%s: %s", npc.name, action.content))
44
+ end,
45
+ action_format_str = "{protagonist_name} said '{content}' to {target_name}",
46
+ },
47
+ {
48
+ name = "MOVE",
49
+ description = "Move to a new location",
50
+ parameter_types = { "location" },
51
+ callback = function(client, action, config)
52
+ local targetName = action.target_name
53
+ local targetPosition = findLocationByName(targetName, config)
54
+ if not targetPosition then
55
+ print("tried to move to an unknown place", targetName)
56
+ return
57
+ end
58
+ local npc = client:getNpc(action.character_id)
59
+ dialog:create("I'm going to " .. targetName, npc.avatar)
60
+ print(string.format("%s: %s", npc.name, "I'm going to " .. targetName))
61
+ local origin = Map:WorldToBlock(npc.object.Position)
62
+ local destination = Map:WorldToBlock(targetPosition) + Number3(math.random(-1, 1), 0, math.random(-1, 1))
63
+ local canMove = pathfinding:moveObjectTo(npc.object, origin, destination)
64
+ if not canMove then
65
+ dialog:create("I can't go there", npc.avatar)
66
+ return
67
+ end
68
+ end,
69
+ action_format_str = "{protagonist_name} moved to {target_name}",
70
+ },
71
+ {
72
+ name = "GREET",
73
+ description = "Greet a character by waving your hand at them",
74
+ parameter_types = { "character" },
75
+ callback = function(client, action)
76
+ local npc = client:getNpc(action.character_id)
77
+ if not npc then
78
+ print("Can't find npc")
79
+ return
80
+ end
81
+
82
+ dialog:create("<Greets you warmly!>", npc.avatar)
83
+ print(string.format("%s: %s", npc.name, "<Greets you warmly!>"))
84
+
85
+ npc.avatar.Animations.SwingRight:Play()
86
+ end,
87
+ action_format_str = "{protagonist_name} waved their hand at {target_name} to greet them",
88
+ },
89
+ {
90
+ name = "JUMP",
91
+ description = "Jump in the air",
92
+ parameter_types = {},
93
+ callback = function(client, action)
94
+ local npc = client:getNpc(action.character_id)
95
+ if not npc then
96
+ print("Can't find npc")
97
+ return
98
+ end
99
+
100
+ dialog:create("<Jumps in the air!>", npc.avatar)
101
+ print(string.format("%s: %s", npc.name, "<Jumps in the air!>"))
102
+
103
+ npc.object.avatarContainer.Physics = PhysicsMode.Dynamic
104
+ npc.object.avatarContainer.Velocity.Y = 50
105
+ Timer(3, function()
106
+ npc.object.avatarContainer.Physics = PhysicsMode.Trigger
107
+ end)
108
+ end,
109
+ action_format_str = "{protagonist_name} jumped up in the air for a moment.",
110
+ },
111
+ {
112
+ name = "FOLLOW",
113
+ description = "Follow a character around for a while",
114
+ parameter_types = { "character" },
115
+ callback = function(client, action)
116
+ local npc = client:getNpc(action.character_id)
117
+ if not npc then
118
+ print("Can't find npc")
119
+ return
120
+ end
121
+
122
+ dialog:create("I'm following you", npc.avatar)
123
+ print(string.format("%s: %s", npc.name, "I'm following you"))
124
+
125
+ followHandler = pathfinding:followObject(npc.object, Player)
126
+ return {
127
+ followHandler = followHandler,
128
+ }
129
+ end,
130
+ onEndCallback = function(_, data)
131
+ data.followHandler:Stop()
132
+ end,
133
+ action_format_str = "{protagonist_name} followed {target_name} for a while.",
134
+ },
135
+ {
136
+ name = "EXPLODE",
137
+ description = "Explodes in a fireball - Hell yeah!",
138
+ parameter_types = { "character" },
139
+ callback = function(client, action)
140
+ local npc = client:getNpc(action.character_id)
141
+ if not npc then
142
+ print("Can't find npc")
143
+ return
144
+ end
145
+
146
+ require("explode"):shapes(npc.avatar)
147
+ dialog:create("*boom*", npc.avatar)
148
+ --print(string.format("%s: %s", npc.name, "EXPLODING"))
149
+ npc.avatar.IsHidden = true
150
+ Timer(5, function()
151
+ dialog:create("Aaaaand... I'm back!", npc.avatar)
152
+ npc.avatar.IsHidden = false
153
+ end)
154
+ end,
155
+ action_format_str = "{protagonist_name} exploded!",
156
+ },--[[
157
+ {
158
+ name = "GIVEAPPLE",
159
+ description = "Give a pice of bread (or a baguette) to someone",
160
+ parameter_types = {"character"},
161
+ callback = function(client, action)
162
+ local npc = client:getNpc(action.character_id)
163
+ if not npc then print("Can't find npc") return end
164
+
165
+ local shape = MutableShape()
166
+ shape:AddBlock(Color.Red, 0, 0, 0)
167
+ shape.Scale = 4
168
+ Player:EquipRightHand(shape)
169
+
170
+ dialog:create("Here is an apple for you!", npc.avatar)
171
+ end,
172
+ action_format_str = "{protagonist_name} gave you a piece of bread!"
173
+ },
174
+ {
175
+ name = "SCALEUP",
176
+ description = "Double your height",
177
+ parameter_types = {"character"},
178
+ callback = function(client, action)
179
+ local npc = client:getNpc(action.character_id)
180
+ if not npc then print("Can't find npc") return end
181
+
182
+ npc.object.Scale = npc.object.Scale * 2
183
+ dialog:create("I am taller than you now!", npc.avatar)
184
+ end,
185
+ action_format_str = "{protagonist_name} doubled his height!"
186
+ },--]]
187
+ {
188
+ name = "GIVEHAT",
189
+ description = "Give a party hat to someone",
190
+ parameter_types = { "character" },
191
+ callback = function(client, action)
192
+ local npc = client:getNpc(action.character_id)
193
+ if not npc then
194
+ print("Can't find npc")
195
+ return
196
+ end
197
+
198
+ Object:Load("claire.party_hat", function(obj)
199
+ require("hierarchyactions"):apply(obj, { includeRoot = true }, function(o)
200
+ o.Physics = PhysicsMode.Disabled
201
+ end)
202
+ Player:EquipHat(obj)
203
+ end)
204
+ dialog:create("Let's get the party started!", npc.avatar)
205
+ end,
206
+ action_format_str = "{protagonist_name} gave you a piece of bread!",
207
+ },
208
+ {
209
+ name = "FLYINGSQUIRREL",
210
+ description = "Summon a flying squirrel - only the scientist can do this!!",
211
+ parameter_types = {},
212
+ callback = function(client, action)
213
+ local npc = client:getNpc(action.character_id)
214
+ if not npc then
215
+ print("Can't find npc")
216
+ return
217
+ end
218
+
219
+ local squirrel = spawnSquirrelAbovePlayer(Player)
220
+ dialog:create("Wooh, squirrel!", npc.avatar)
221
+ -- make it disappear after a while
222
+ Timer(5, function()
223
+ squirrel:RemoveFromParent()
224
+ squirrel = nil
225
+ end)
226
+ end,
227
+ action_format_str = "{protagonist_name} summoned a flying squirrel! It's vibrating with excitement!",
228
+ },
229
+ }
230
+
231
+ local locations = {
232
+ {
233
+ name = "Scientist Island",
234
+ description = "A small island with a scientist and its pet chilling.",
235
+ },
236
+ {
237
+ name = "Baker Island",
238
+ description = "A small bakery on a floating island in the sky.",
239
+ },
240
+ {
241
+ name = "Pirate Island",
242
+ description = "A small floating island in the sky with a pirate and its ship.",
243
+ },
244
+ {
245
+ name = "Center",
246
+ description = "Center point between the three islands.",
247
+ },
248
+ }
249
+
250
+ local NPCs = {
251
+ {
252
+ name = "npcscientist",
253
+ physicalDescription = "Short, with a stern expression and sharp eyes",
254
+ psychologicalProfile = "Grumpy but insightful, suspicious yet intelligent",
255
+ currentLocationName = "Scientist Island",
256
+ initialReflections = {
257
+ "I just arrived on this island to feed my pet, he loves tulips so much.",
258
+ "I was just eating before I stood up to start the radio, I don't know which song I should start",
259
+ "I am a scientist that works on new pets for everyone, so that each individual can have the pet of their dreams",
260
+ "I am a bit allergic to the tulip but Fredo my pet loves it so much, I have to dock here with my vehicle. The pet is placed at the back of my flying scooter when we move to another place.",
261
+ },
262
+ },
263
+ {
264
+ name = "npcbaker",
265
+ physicalDescription = "Tall, with a solemn demeanor and thoughtful eyes",
266
+ psychologicalProfile = "Wise and mysterious, calm under pressure",
267
+ currentLocationName = "Baker Island",
268
+ initialReflections = {
269
+ "I am a baker and I make food for everyone that pass by.",
270
+ "I am a bit stressed that the flour didn't arrived yet, my cousin Joe should arrive soon with the delivery but he is late and I worry a bit.",
271
+ "I love living here on these floating islands, the view is amazing from my wind mill.",
272
+ "I like to talk to strangers like the pirate that just arrived or the scientist coming time to time to feed his pet.",
273
+ },
274
+ },
275
+ {
276
+ name = "npcpirate",
277
+ physicalDescription = "Average height, with bright green eyes and a warm smile",
278
+ psychologicalProfile = "Friendly and helpful, quick-witted and resourceful",
279
+ currentLocationName = "Pirate Island",
280
+ initialReflections = {
281
+ "Ahoy, matey! I'm Captain Ruby Storm, a fearless lass from the seven skies.",
282
+ "I've docked me floating ship on this here floating isle to sell me wares (almost legally) retrieved treasures from me last daring adventure.",
283
+ "So, who be lookin' to trade with a swashbuckler like meself?",
284
+ },
285
+ },
286
+ }
287
+
288
+ local gigaxWorldConfig = {
289
+ simulationName = SIMULATION_NAME,
290
+ simulationDescription = SIMULATION_DESCRIPTION,
291
+ startingLocationName = "Center",
292
+ skills = skills,
293
+ locations = locations,
294
+ NPCs = NPCs,
295
+ }
296
+
297
+ findLocationByName = function(targetName, config)
298
+ for _, node in ipairs(config.locations) do
299
+ if string.lower(node.name) == string.lower(targetName) then
300
+ return node.position
301
+ end
302
+ end
303
+ end
304
+
305
+ function generateWorld()
306
+ local nbIslands = 20
307
+ local minSize = 4
308
+ local maxSize = 7
309
+ local dist = 750
310
+ local safearea = 200
311
+ floating_islands_generator:onReady(function()
312
+ for i = 1, nbIslands do
313
+ local island = floating_islands_generator:create(math.random(minSize, maxSize))
314
+ island:SetParent(World)
315
+ island.Scale = Map.Scale
316
+ island.Physics = PhysicsMode.Disabled
317
+ local x = math.random(-dist, dist)
318
+ local z = math.random(-dist, dist)
319
+ while (x >= -safearea and x <= safearea) and (z >= -safearea and z <= safearea) do
320
+ x = math.random(-dist, dist)
321
+ z = math.random(-dist, dist)
322
+ end
323
+ island.Position = {
324
+ x + (Map.Width * 0.5) * Map.Scale.X,
325
+ math.random(300) - 150,
326
+ z + (Map.Depth * 0.5) * Map.Scale.Z,
327
+ }
328
+ local t = x + z
329
+ LocalEvent:Listen(LocalEvent.Name.Tick, function(dt)
330
+ t = t + dt
331
+ island.Position.Y = island.Position.Y + math.sin(t) * 0.02
332
+ end)
333
+ end
334
+ end)
335
+ end
336
+
337
+ Client.OnWorldObjectLoad = function(obj)
338
+ if obj.Name == "pirate_ship" then
339
+ obj.Scale = 1
340
+ end
341
+ if obj.Name == "NPC_scientist" then
342
+ local pos = obj.Position:Copy()
343
+ gigaxWorldConfig.locations[1].position = pos
344
+ gigaxWorldConfig.NPCs[1].position = pos
345
+ gigaxWorldConfig.NPCs[1].rotation = obj.Rotation:Copy()
346
+ obj:RemoveFromParent()
347
+ elseif obj.Name == "NPC_baker" then
348
+ local pos = obj.Position:Copy()
349
+ gigaxWorldConfig.locations[2].position = pos
350
+ gigaxWorldConfig.NPCs[2].position = pos
351
+ gigaxWorldConfig.NPCs[2].rotation = obj.Rotation:Copy()
352
+ obj:RemoveFromParent()
353
+ elseif obj.Name == "NPC_pirate" then
354
+ local pos = obj.Position:Copy()
355
+ gigaxWorldConfig.locations[3].position = pos
356
+ gigaxWorldConfig.NPCs[3].position = pos
357
+ gigaxWorldConfig.NPCs[3].rotation = obj.Rotation:Copy()
358
+ obj:RemoveFromParent()
359
+ end
360
+ end
361
+
362
  Client.OnStart = function()
363
+ require("object_skills").addStepClimbing(Player, {
364
+ mapScale = MAP_SCALE,
365
+ collisionGroups = Map.CollisionGroups,
366
+ })
367
 
368
+ gigaxWorldConfig.locations[4].position = Number3(Map.Width * 0.5, Map.Height - 2, Map.Depth * 0.5) * Map.Scale
369
+ generateWorld()
370
 
371
+ local ambience = require("ambience")
372
+ ambience:set(ambience.dusk)
373
+
374
+ sfx = require("sfx")
375
+ Player.Head:AddChild(AudioListener)
376
+
377
+ dropPlayer = function()
378
+ Player.Position = Number3(Map.Width * 0.5, Map.Height + 10, Map.Depth * 0.5) * Map.Scale
379
+ Player.Rotation = { 0, 0, 0 }
380
+ Player.Velocity = { 0, 0, 0 }
381
+ end
382
+ World:AddChild(Player)
383
+ dropPlayer()
384
+
385
+ dialog = require("dialog")
386
+ dialog:setMaxWidth(400)
387
+
388
+ pathfinding:createPathfindingMap()
389
+ end
390
+
391
+ Client.OnPlayerJoin = function(player)
392
+ if player ~= Player then
393
+ return
394
+ end
395
+ gigax:setConfig(gigaxWorldConfig)
396
+ end
397
+
398
+ Client.Action1 = function()
399
+ if Player.IsOnGround then
400
+ sfx("hurtscream_1", { Position = Player.Position, Volume = 0.4 })
401
+ Player.Velocity.Y = 100
402
+ if Player.Motion.X == 0 and Player.Motion.Z == 0 then
403
+ -- only play jump action when jumping without moving to avoid wandering around to trigger NPCs
404
+ gigax:action({
405
+ name = "JUMP",
406
+ description = "Jump in the air",
407
+ parameter_types = {},
408
+ action_format_str = "{protagonist_name} jumped up in the air for a moment.",
409
+ })
410
+ end
411
+ end
412
+ end
413
+
414
+ Client.Tick = function(dt)
415
+ if Player.Position.Y < -500 then
416
+ dropPlayer()
417
+ end
418
+ end
419
+
420
+ Client.OnChat = function(payload)
421
+ local msg = payload.message
422
+
423
+ Player:TextBubble(msg, 3, true)
424
+ sfx("waterdrop_2", { Position = Player.Position, Pitch = 1.1 + math.random() * 0.5 })
425
+
426
+ gigax:action({
427
+ name = "SAY",
428
+ description = "Say smthg out loud",
429
+ parameter_types = { "character", "content" },
430
+ action_format_str = "{protagonist_name} said '{content}' to {target_name}",
431
+ content = msg,
432
+ })
433
+ end
434
+
435
+ -- Module floating islands
436
+
437
+ floating_islands_generator = {}
438
+
439
+ local cachedTree
440
+
441
+ local COLORS = {
442
+ GRASS = Color(19, 133, 16),
443
+ DIRT = Color(107, 84, 40),
444
+ STONE = Color.Grey,
445
+ }
446
+
447
+ local function islandHeight(x, z, radius)
448
+ local distance = math.sqrt(x * x + z * z)
449
+ local normalizedDistance = distance / radius
450
+ local maxy = -((1 + radius) * 2 - (normalizedDistance ^ 4) * distance)
451
+ return maxy
452
+ end
453
+
454
+ floating_islands_generator.onReady = function(_, callback)
455
+ Object:Load("knosvoxel.oak_tree", function(obj)
456
+ cachedTree = obj
457
+ callback()
458
+ end)
459
+ end
460
+
461
+ floating_islands_generator.create = function(_, radius)
462
+ local shape = MutableShape()
463
+ shape.Pivot = { 0.5, 0.5, 0.5 }
464
+ for z = -radius, radius do
465
+ for x = -radius, radius do
466
+ local maxy = islandHeight(x, z, radius)
467
+ shape:AddBlock(COLORS.DIRT, x, -2, z)
468
+ shape:AddBlock(COLORS.GRASS, x, -1, z)
469
+ shape:AddBlock(COLORS.GRASS, x, 0, z)
470
+ if maxy <= -3 then
471
+ shape:AddBlock(COLORS.DIRT, x, -3, z)
472
+ end
473
+ for y = maxy, -3 do
474
+ shape:AddBlock(COLORS.STONE, x, y, z)
475
+ end
476
+ end
477
+ end
478
+
479
+ xShift = math.random(-radius, radius)
480
+ zShift = math.random(-radius, radius)
481
+ for z = -radius, radius do
482
+ for x = -radius, radius do
483
+ local maxy = islandHeight(x, z, radius) - 2
484
+ shape:AddBlock(COLORS.DIRT, x + xShift, -2 + 2, z + zShift)
485
+ shape:AddBlock(COLORS.GRASS, x + xShift, -1 + 2, z + zShift)
486
+ shape:AddBlock(COLORS.GRASS, x + xShift, 0 + 2, z + zShift)
487
+ if maxy <= -3 + 2 then
488
+ shape:AddBlock(COLORS.DIRT, x + xShift, -3 + 2, z + zShift)
489
+ end
490
+ for y = maxy, -3 + 2 do
491
+ shape:AddBlock(COLORS.STONE, x + xShift, y, z + zShift)
492
+ end
493
+ end
494
  end
495
+
496
+ for i = 1, math.random(1, 2) do
497
+ local obj = Shape(cachedTree, { includeChildren = true })
498
+ obj.Position = { 0, 0, 0 }
499
+ local box = Box()
500
+ box:Fit(obj, true)
501
+ obj.Pivot = Number3(obj.Width / 2, box.Min.Y + obj.Pivot.Y + 4, obj.Depth / 2)
502
+ obj:SetParent(shape)
503
+ require("hierarchyactions"):applyToDescendants(obj, { includeRoot = true }, function(o)
504
+ o.Physics = PhysicsMode.Disabled
505
+ end)
506
+ local coords = Number3(math.random(-radius + 1, radius - 1), 0, math.random(-radius + 1, radius - 1))
507
+ while shape:GetBlock(coords) do
508
+ coords.Y = coords.Y + 1
509
+ end
510
+ obj.Scale = math.random(70, 150) / 1000
511
+ obj.Rotation.Y = math.random(1, 4) * math.pi * 0.25
512
+ obj.LocalPosition = coords
513
+ end
514
+
515
+ return shape
516
+ end