Module: Core::Parse

Defined in:
lib/parse.rb,
lib/parse_tmx.rb

Overview

TODO function to look for key in a line and return it; then refactor

Defined Under Namespace

Modules: TMX Classes: Parser

Class Method Summary collapse

Class Method Details

.animationsObject

FIXME bit hacked



565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/parse.rb', line 565

def self.animations
  p = {
    "length" => :int,
    "color" => :color,
    "x" => :float,
    "y" => :float,
    "mode" => :symbol,
  }
  anims = {}
  Dir.new("#{Core::LIBRARY_PATH}/def/animations").each { |file|
    next if file == "." or file == ".."
    frames = Parser.new("def/animations/#{file}", p, Core::Frame, true).parse
    block = false
    @graphic = @split_x = @split_y = @repeat = nil
    f = File.open("#{Core::LIBRARY_PATH}/def/animations/#{file}")
    f.each_line { |line|
      line.gsub!(" ", "")
      if line[0] == '('
        block = true
        next
      end
      if block
        if line[0] == ')'
          anims.store(file.gsub(".anim", "").to_sym, Core::Animation.new(@graphic, @split_x.to_i, @split_y.to_i, @repeat.to_b, frames))
          break
        else  
          line.gsub!("\"", "")
          line =~ /(.+)\=(.+)/
          key = $1
          val = $2
          ivs("@#{key}", val)
        end
      end
    }
    f.close
  }
  puts("INFO: Parsed #{anims.size} animations")
  return anims
end

.behaviour(map, npc) ⇒ Object



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
# File 'lib/parse.rb', line 282

def self.behaviour(map, npc)
  begin
    file = File.open("#{Core::LIBRARY_PATH}/maps/def/#{map}.bhv", "r")
    block = ary = false
    
    b = nil
    
    name = array = type = ""
    init = trigger = motion = update = Core::Game::NPC::Task.new([])
    
    file.each_line { |line|
      line.sub!("\n", "")
      if line[0] == "#" or line.length == 0
        if line == "#EOF"
          break
        end
        next
      end
      line.lstrip!
      if ary
        if line == "]"
          array.gsub!("<", "")
          array.gsub!(">", "")
          ret = self.task_array(array.split("?"), npc)
          # TODO needs some magic
          case type
          when "init"
            init = ret
          when "trigger"
            trigger = ret
          when "motion"
            motion = ret
          when "update"
            update = ret
          end
          array = ""
          ary = false
          next
        end
        array += "#{line}?"
        next
      end
      if block
        if line.include?("}")
          block = false
          b = Core::Game::NPC::Behaviour.new(npc, init, trigger, motion, update)
          init = trigger = motion = update = Core::Game::NPC::Task.new([])
          break
        end
        type = line.gsub(/\s?=\s?\[/, "")
        ary = true
      else
        if line.include?("{")
          block = true
          name = line.gsub(/\s?\{/, "")
          if npc.properties[:id] != name
            ary = block = false
            next
          end
        end
      end
    }
    file.close
  rescue Errno::ENOENT
    # we dont care about missing definition files
  end
  return b
end

.charactersObject



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/parse.rb', line 428

def self.characters
  p = {
    "name" => :string,
    "age" => :int,
    "strength" => :int,
    "weight" => :int,
    "agility" => :int,
    "file" => :string,
    "race" => :string,
    "gender" => :symbol,
    "healing" => :int,
    "crafting" => :int,
    "botany" => :int,
    "healing_magic" => :int,
    "mind" => :int,
    "elemental" => :int,
    "ranged" => :int,
    "melee" => :int,
  }
  chars = Parser.new("def/characters.def", p, Core::Game::Character).parse
  chars.each { |char|
    char.setup
    char.validate
  }
  return chars
end

.enemiesObject



502
503
504
505
506
507
508
509
510
511
# File 'lib/parse.rb', line 502

def self.enemies
  p = {
    "name" => :symbol,
    "file" => :image,
    "type" => :symbol,
    "strength" => :int,
    "weapons" => :symarray,
  }
  return Parser.new("def/enemies.def", p, Core::Game::Combat::Enemy).parse
end

.enemy_behaviour(name) ⇒ Object

TODO create special parser



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
# File 'lib/parse.rb', line 378

def self.enemy_behaviour(name)
  hash = {}
  begin
    file = File.open("#{Core::LIBRARY_PATH}/def/enemies/#{name}.bhv")
  rescue
    warn("ERROR: Couldn't find enemies/#{name}.bhv")
    return hash
  end
  
  actions = [:on_turn, :on_defeat]
  number = 0
  block = false
  
  action = nil
  ary = []
  
  file.each_line { |line|
    number += 1
    line.gsub!("\n", "")
    if line[0] == "#" or line.length == 0
      if line == "#EOF"
        break
      end
      next
    end
    if line[0] == "{"
      block = true
    elsif line[0] == "}" and block and action
      hash.store(action, ary)
      action = nil
      ary = []
      block = false
      next
    else
      if !action and !block
        action = line.gsub("\"", "").to_sym
        if !actions.include?(action)
          warn("WARNING: Unknown action key #{action} at enemies/#{name}.bhv, line #{number}")
          action = nil
          block = false
        end
      elsif block and action
        ary.push(line.gsub("\"", ""))
      end
    end
  }
  file.close
  return Core::Game::Combat::Behaviour.new(hash)
end

.itemsObject



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/parse.rb', line 474

def self.items
  p = {
    "name" => :symbol,
    "icon" => :string,
    "weight" => :float,
    "effects" => :symarray,
    "type" => :symbol,
  }
  items = Parser.new("def/items.def", p, Core::Game::Item).parse
  items.each { |item|
    icon = item.icon
    item.ivs("@icon".to_sym, Core.sprite("icons/items/#{icon}"))
    item.ivs("@icon_file".to_sym, "icons/items/#{icon}")
    item.ivs("@img".to_sym, Core.sprite("items/#{icon}"))
  }
  return items
end

.map(file, tiny = false) ⇒ Object



161
162
163
# File 'lib/parse.rb', line 161

def self.map(file, tiny=false)
  return Core::Parse::TMX.parse(file)
end

.parse_int_array(str) ⇒ Object



271
272
273
274
275
276
277
278
279
280
# File 'lib/parse.rb', line 271

def self.parse_int_array(str)
  ary = []
  str.gsub!("[", "")
  str.gsub!("]", "")
  str.gsub!(" ", "")
  str.each_line(",") { |c|
    ary.push(c.sub(",", "").to_i)
  }
  return ary
end

.parse_range(str) ⇒ Object



249
250
251
252
253
254
255
256
257
258
# File 'lib/parse.rb', line 249

def self.parse_range(str)
  r = (0..0)
  str.gsub!("(", "")
  str.gsub!(")", "")
  first = last = 0
  first = str.gsub(/\D{2,}.+/, "").to_i
  last = str.gsub(/-?\d+\D{2}/, "").to_i
  r = (first..last)
  return r
end

.parse_sym_array(str) ⇒ Object



260
261
262
263
264
265
266
267
268
269
# File 'lib/parse.rb', line 260

def self.parse_sym_array(str)
  ary = []
  str.gsub!("[", "")
  str.gsub!("]", "")
  str.gsub!(" ", "")
  str.each_line(",") { |c|
    ary.push(c.sub(",", "").to_sym)
  }
  return ary
end

.particlesObject

TODO create special parser



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
# File 'lib/parse.rb', line 166

def self.particles
  hash = {}
  begin
    file = File.open("#{Core::LIBRARY_PATH}/def/particles.def")
  rescue
    warn("ERROR: Couldn't find particles.def")
    return hash
  end
  block = false
  
  name = filename = ""
  mode = :default
  delay = time = fadein = fadeout = 0
  color = 0xffffffff
  xrange = yrange = xoffset = yoffset = (0..0)
  angle = false
  
  file.each_line { |line|
    line.sub!("\n", "")
    if line[0] == "#" or line.length == 0
      break if line.index("#EOF")
      next
    end
    if line[0] == "{"
      block = true
    end
    if line[0] == "}"
      block = false
      hash.store(name.to_sym, Core::ParticleEmitter.new(filename, time, fadein, fadeout, color, delay, angle, mode, xrange, yrange, xoffset, yoffset))
      name = filename = ""
      mode = :default
      delay = time = fadein = fadeout = 0
      color = 0xffffffff
      xrange = yrange = xoffset = yoffset = (0..0)
      angle = false
    end
    if block
      if line.start_with?("name")
        line.gsub!(/name *= */, "")
        name = line.gsub("\"", "")
      elsif line.start_with?("file")
        line.gsub!(/file *= */, "")
        filename = line.gsub("\"", "")
      elsif line.start_with?("delay")
        line.gsub!(/delay *= */, "")
        delay = line.gsub("\"", "").to_i
      elsif line.start_with?("time")
        line.gsub!(/time *= */, "")
        time = line.gsub("\"", "").to_i
      elsif line.start_with?("fadein")
        line.gsub!(/fadein *= */, "")
        fadein = line.gsub("\"", "").to_i
      elsif line.start_with?("fadeout")
        line.gsub!(/fadeout *= */, "")
        fadeout = line.gsub("\"", "").to_i
      elsif line.start_with?("mode")
        line.gsub!(/mode *= *:/, "")
        mode = line.gsub("\"", "").to_sym
      elsif line.start_with?("color")
        line.gsub!(/color *= */, "")
        color = parse_int_array(line.gsub("\"", "")).to_color
      elsif line.start_with?("angle")
        line.gsub!(/angle *= */, "")
        angle = line.gsub("\"", "").to_b
      elsif line.start_with?("xrange")
        line.gsub!(/xrange *= */, "")
        xrange = parse_range(line.gsub("\"", ""))
      elsif line.start_with?("yrange")
        line.gsub!(/yrange *= */, "")
        yrange = parse_range(line.gsub("\"", ""))
      elsif line.start_with?("xoffset")
        line.gsub!(/xoffset *= */, "")
        xoffset = parse_range(line.gsub("\"", ""))
      elsif line.start_with?("yoffset")
        line.gsub!(/yoffset *= */, "")
        yoffset = parse_range(line.gsub("\"", ""))
      end
    end
  }
  puts("INFO: Parsed #{hash.length} particles")
  return hash
end

.recipiesObject



463
464
465
466
467
468
469
470
471
472
# File 'lib/parse.rb', line 463

def self.recipies
  p = {
    "name" => :symbol,
    "icon" => :image,
    "output" => :symbol,
    "input" => :symarray,
    "level" => :int,
  }
  return Parser.new("def/recipies.def", p, Core::Game::Alchemy::Recipe).parse
end

.skillsObject



455
456
457
458
459
460
461
# File 'lib/parse.rb', line 455

def self.skills
  p = {
    "name" => :symbol,
    "icon" => :image,
  }
  return Parser.new("def/skills.def", p, Core::Game::Skill).parse
end

.spellsObject



492
493
494
495
496
497
498
499
500
# File 'lib/parse.rb', line 492

def self.spells
  p = {
    "name" => :symbol,
    "icon" => :image,
    "type" => :symbol,
    "cost" => :int,
  }
  return Parser.new("def/spells.def", p, Core::Game::Spell).parse
end

.task_array(inp, npc) ⇒ Object



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
# File 'lib/parse.rb', line 351

def self.task_array(inp, npc)
  ret = []
  inp.each { |el|
    delete = []
    ary = el.split(" ")
    ary.each { |parm|
      if parm.include?("|")
        if parm.include?("@")
          parm.sub!("@x", "#{npc.x}")
          parm.sub!("@y", "#{npc.y}")
        end
        ary[ary.index(parm)] = parm.to_pos
        next
      elsif ary.first == :msg and parm.include?(":")
        ary[ary.index(parm)] = Core::Trans.dialogue(parm.gsub(":", "").to_sym)
        next
      end
      ary[ary.index(parm)] = parm.to_sym
    }
    ary.compact!
    ret.push(ary)
  }
  ret.compact!
  return Core::Game::NPC::Task.new(ret)
end

.weaponsObject

TODO create special parser



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/parse.rb', line 514

def self.weapons
  hash = {}
  begin
    file = File.open("#{Core::LIBRARY_PATH}/def/weapons.def")
  rescue
    warn("ERROR: Couldn't find weapons.def")
    return hash
  end
  block = false
  
  name = icon = ""
  type = :melee
  effects = []
  
  file.each_line { |line|
    line.gsub!("\n", "")
    if line[0] == "#" or line.length == 0
      if line == "#EOF"
        break
      end
      next
    end
    if line[0] == "{"
      block = true
    end
    if line[0] == "}"
      block = false
      hash.store(name, Core::Game::Combat::Weapon.new(name, type, effects, icon))
      next
    end
    if block
      if line.start_with?("icon")
        line.gsub!(/icon *= */, "")
        icon = line.gsub("\"", "")
      elsif line.start_with?("type")
        line.gsub!(/type *= */, "")
        type = line.gsub("\"", "").to_sym
      elsif line.start_with?("effects")
        line.gsub!(/effects *= */, "")
        effects = parse_sym_array(line.gsub("\"", ""))
      end
    else
      name = line.gsub("\"", "").to_sym
    end
  }
  file.close
  puts("INFO: Parsed #{hash.size} weapons")
  return hash
end