laneya2

cave exploration game
git clone https://git.ce9e.org/laneya2.git

commit
53953138c7716203136acb82667ef3ff1019e42f
parent
7a06c482744ac70993ff0a2b43c2309ef739bcbd
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-10-05 07:13
monster drop random items

Diffstat

M items.go 39 +++++++++++++++++++++++++++++++++++++++
M monster.go 2 +-

2 files changed, 40 insertions, 1 deletions


diff --git a/items.go b/items.go

@@ -1,5 +1,7 @@
    1     1 package main
    2     2 
   -1     3 import "math/rand"
   -1     4 
    3     5 const (
    4     6 	CONSUMABLE uint = 1
    5     7 	WEAPON          = 2
@@ -8,6 +10,7 @@ const (
    8    10 
    9    11 type Item struct {
   10    12 	Type        uint
   -1    13 	Value       uint
   11    14 	Health      uint
   12    15 	HealthTotal uint
   13    16 	Attack      uint
@@ -20,90 +23,126 @@ var Items = map[string]Item{
   20    23 	// consumables
   21    24 	"Small Potion": Item{
   22    25 		Type:   CONSUMABLE,
   -1    26 		Value:  10,
   23    27 		Health: 10,
   24    28 	},
   25    29 	"Potion": Item{
   26    30 		Type:   CONSUMABLE,
   -1    31 		Value:  50,
   27    32 		Health: 25,
   28    33 	},
   29    34 	"Great Potion": Item{
   30    35 		Type:   CONSUMABLE,
   -1    36 		Value:  400,
   31    37 		Health: 100,
   32    38 	},
   33    39 	"Small Life Elixir": Item{
   34    40 		Type:        CONSUMABLE,
   -1    41 		Value:       50,
   35    42 		HealthTotal: 1,
   36    43 	},
   37    44 	"Life Elixir": Item{
   38    45 		Type:        CONSUMABLE,
   -1    46 		Value:       250,
   39    47 		HealthTotal: 5,
   40    48 	},
   41    49 	"Great Life Elixir": Item{
   42    50 		Type:        CONSUMABLE,
   -1    51 		Value:       1000,
   43    52 		HealthTotal: 20,
   44    53 	},
   45    54 
   46    55 	// weapons
   47    56 	"Butterknive": {
   48    57 		Type:   WEAPON,
   -1    58 		Value:  30,
   49    59 		Attack: 1,
   50    60 	},
   51    61 	"Sword": {
   52    62 		Type:   WEAPON,
   -1    63 		Value:  100,
   53    64 		Attack: 3,
   54    65 	},
   55    66 	"Battleaxe": Item{
   56    67 		Type:   WEAPON,
   -1    68 		Value:  500,
   57    69 		Attack: 4,
   58    70 		Speed:  -5,
   59    71 	},
   60    72 	"Daggers": Item{
   61    73 		Type:   WEAPON,
   -1    74 		Value:  300,
   62    75 		Attack: 2,
   63    76 		Speed:  5,
   64    77 	},
   65    78 	"Sting": Item{
   66    79 		Type:        WEAPON,
   -1    80 		Value:       400,
   67    81 		Attack:      2,
   68    82 		LineOfSight: 2,
   69    83 	},
   70    84 	"Shield": Item{
   71    85 		Type:    WEAPON,
   -1    86 		Value:   300,
   72    87 		Defense: 3,
   73    88 	},
   74    89 
   75    90 	// armor
   76    91 	"Leather Armor": Item{
   77    92 		Type:    ARMOR,
   -1    93 		Value:   100,
   78    94 		Defense: 2,
   79    95 		Speed:   -5,
   80    96 	},
   81    97 	"Shining Armor": Item{
   82    98 		Type:        ARMOR,
   -1    99 		Value:       500,
   83   100 		Defense:     2,
   84   101 		LineOfSight: 3,
   85   102 		Speed:       -5,
   86   103 	},
   87   104 	"Heavy Armor": Item{
   88   105 		Type:    ARMOR,
   -1   106 		Value:   400,
   89   107 		Defense: 3,
   90   108 		Speed:   -10,
   91   109 	},
   92   110 	"Spiked Armor": Item{
   93   111 		Type:    ARMOR,
   -1   112 		Value:   1000,
   94   113 		Attack:  1,
   95   114 		Defense: 2,
   96   115 		Speed:   -10,
   97   116 	},
   98   117 	"Cloak": Item{
   99   118 		Type:        ARMOR,
   -1   119 		Value:       400,
  100   120 		Defense:     1,
  101   121 		LineOfSight: 1,
  102   122 		Speed:       5,
  103   123 	},
  104   124 	"Body Oil": Item{
  105   125 		Type:   ARMOR,
   -1   126 		Value:  750,
  106   127 		Attack: 1,
  107   128 		Speed:  10,
  108   129 	},
  109   130 }
   -1   131 
   -1   132 func RandomItem() string {
   -1   133 	total := 0.0
   -1   134 	for _, item := range Items {
   -1   135 		total += 1 / float64(item.Value)
   -1   136 	}
   -1   137 
   -1   138 	x := rand.Float64()
   -1   139 	for name, item := range Items {
   -1   140 		p := 1 / float64(item.Value) / total
   -1   141 		if x < p {
   -1   142 			return name
   -1   143 		} else {
   -1   144 			x -= p
   -1   145 		}
   -1   146 	}
   -1   147 	return ""
   -1   148 }

diff --git a/monster.go b/monster.go

@@ -77,7 +77,7 @@ func (monster *Monster) TakeDamage(amount uint) {
   77    77 	if amount > monster.Health {
   78    78 		monster.quit <- true
   79    79 		delete(monster.Game.Monsters, monster)
   80    -1 		monster.Game.addToPile(monster.Pos, "Small Potion", 1)
   -1    80 		monster.Game.addToPile(monster.Pos, RandomItem(), 1)
   81    81 		monster.Game.Enqueue(Message{
   82    82 			"action": "remove",
   83    83 			"id":     monster.Id,