Passed
Pull Request — master (#291)
by Marek
02:10
created

osci.res.OLDgetFFColorCode()   B

Complexity

Conditions 7

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 18
nop 1
dl 0
loc 22
rs 8
c 0
b 0
f 0
1
#
2
#  Copyright 2001 - 2016 Ludek Smid [http://www.ospace.net/]
3
#
4
#  This file is part of Outer Space.
5
#
6
#  Outer Space is free software; you can redistribute it and/or modify
7
#  it under the terms of the GNU General Public License as published by
8
#  the Free Software Foundation; either version 2 of the License, or
9
#  (at your option) any later version.
10
#
11
#  Outer Space is distributed in the hope that it will be useful,
12
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
#  GNU General Public License for more details.
15
#
16
#  You should have received a copy of the GNU General Public License
17
#  along with Outer Space; if not, write to the Free Software
18
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
#
20
21
import bisect
22
import glob
23
import math
24
import os
25
import re
26
27
import gdata, client
28
import pygame, pygame.image
29
from osci import gdata
30
import ige.ospace.Const as Const
31
from ige.ospace import Rules
32
from ige import log
33
34
import resources
35
36
whiteShift = 80000
37
redShift = 90000
38
39
smallStarImgs = None
40
techImgs = None
41
bigStarImgs = None
42
planetImgs = None
43
planetImgCnt = None
44
buttonImgs = None
45
cmdInProgressImg = None
46
loginLogoImg = None
47
structProblemImg = None
48
structOffImg = None
49
icons = {}
50
ui_icons = {}
51
52
def initialize():
53
    # needed for progress dlg
54
    global loginLogoImg
55
    loginLogoImg = pygame.image.load(resources.get('logo-login.png')).convert_alpha()
56
57
58
def updateProgress(curr, progress_dlg):
59
    if not progress_dlg: return
60
    if curr % 30 == 0:
61
        periods = '.' * (curr / 30 % 4)
62
        progress_dlg.setProgress(_('Loading resources' + periods), curr)
63
64
65
def loadResources(progress_dlg=None):
66
    curr = 0
67
    max = len(glob.glob(resources.get('galaxy/*.png'))) \
68
        + len(glob.glob(resources.get('techs/*.png'))) \
69
        + len(glob.glob(resources.get('system/*.png'))) \
70
        + len(glob.glob(resources.get('icons/*.png'))) \
71
        + len(glob.glob(resources.get('buttons/*.png')))
72
    if progress_dlg:
73
        progress_dlg.display(_('Loading resources'), 0, max)
74
    # load star imgs
75
    global smallStarImgs
76
    smallStarImgs = {}
77
    for filename in glob.glob(resources.get('galaxy/star_*.png')):
78
        name = re.search("star_([^.]+).png", filename).group(1)
79
        smallStarImgs[name] = pygame.image.load(filename).convert_alpha()
80
        curr += 1
81
        updateProgress(curr, progress_dlg)
82
    # load tech imgs
83
    global techImgs
84
    techImgs = {}
85
    white = pygame.Surface((37, 37))
86
    white.fill((255, 255, 255))
87
    white.set_alpha(64)
88
    red = pygame.Surface((37, 37))
89
    red.fill((255, 0, 0))
90
    red.set_alpha(64)
91
    for filename in glob.glob(resources.get('techs/????.png')):
92
        name = os.path.splitext(os.path.basename(filename))[0]
93
        imgID = int(name)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable int does not seem to be defined.
Loading history...
94
        techImgs[imgID] = pygame.image.load(filename).convert_alpha()
95
        copyImg = techImgs[imgID].convert_alpha()
96
        copyImg.blit(white, (0, 0))
97
        techImgs[imgID + whiteShift] = copyImg
98
        copyImg = techImgs[imgID].convert_alpha()
99
        copyImg.blit(red, (0, 0))
100
        techImgs[imgID + redShift] = copyImg
101
        curr += 1
102
        updateProgress(curr, progress_dlg)
103
    # load big star imgs
104
    global bigStarImgs
105
    bigStarImgs = {}
106
    for filename in glob.glob(resources.get('system/star_*.png')):
107
        name = re.search("star_([^.]+).png", filename).group(1)
108
        bigStarImgs[name] = pygame.image.load(filename).convert_alpha()
109
        curr += 1
110
        updateProgress(curr, progress_dlg)
111
    # load planet images
112
    global planetImgs
113
    global planetImgCnt
114
    planetImgs = {}
115
    planetImgCnt = {}
116
    for filename in glob.glob(resources.get('system/planet_*.png')):
117
        matchobj = re.search("planet_((.)[^.]+).png", filename)
118
        name = matchobj.group(1)
119
        pltype = matchobj.group(2)
120
        if pltype in planetImgCnt:
121
            planetImgCnt[pltype] += 1
122
        else:
123
            planetImgCnt[pltype] = 1
124
        planetImgs[name] = pygame.image.load(filename).convert_alpha()
125
        curr += 1
126
        updateProgress(curr, progress_dlg)
127
    # load ship imgs
128
    global shipImgs
129
    shipImgs = {}
130
    for filename in glob.glob(resources.get('ships/??.png')):
131
        name = os.path.splitext(os.path.basename(filename))[0]
132
        shipImgs[int(name)] = pygame.image.load(filename).convert_alpha()
133
        curr += 1
134
        updateProgress(curr, progress_dlg)
135
    # load star imgs
136
    global icons
137
    icons = {}
138
    for filename in glob.glob(resources.get('icons/[!ui_]*.png')):
139
        name = os.path.splitext(os.path.basename(filename))[0]
140
        icons[name] = pygame.image.load(filename).convert_alpha()
141
        curr += 1
142
        updateProgress(curr, progress_dlg)
143
    # load UI icons
144
    global ui_icons
145
    ui_icons = {}
146
    for filename in glob.glob(resources.get('icons/ui_*.png')):
147
        name = os.path.splitext(os.path.basename(filename))[0]
148
        ui_icons[name] = pygame.image.load(filename).convert_alpha()
149
        curr += 1
150
        updateProgress(curr, progress_dlg)
151
    # load buttons
152
    global buttonImgs
153
    buttonImgs = {}
154
    for filename in glob.glob(resources.get('buttons/*.png')):
155
        name = os.path.splitext(os.path.basename(filename))[0]
156
        buttonImgs[name] = pygame.image.load(filename).convert_alpha()
157
        curr += 1
158
        updateProgress(curr, progress_dlg)
159
    # other icons
160
    global cmdInProgressImg
161
    cmdInProgressImg = pygame.image.load(resources.get('cmdInProgress.png')).convert_alpha()
162
    global structProblemImg
163
    structProblemImg = pygame.image.load(resources.get('struct_problem.png')).convert_alpha()
164
    global structOffImg
165
    structOffImg = pygame.image.load(resources.get('struct_off.png')).convert_alpha()
166
167
168
def prepareUIIcons(color):
169
    for image in ui_icons.values():
170
        image.fill((0, 0, 0, 255), None, pygame.BLEND_RGBA_MULT)
171
        image.fill(color[0:3] + (0, ), None, pygame.BLEND_RGBA_ADD)
172
173
174
def getUIIcon(icon_name):
175
    return ui_icons["ui_" + str(icon_name)]
176
177
178
def getTechImg(techID):
179
    return techImgs.get(techID, techImgs[0])
180
181
182
def getShipImg(combatClass, isMilitary):
183
    return shipImgs.get(int(combatClass) * 10 + int(isMilitary), shipImgs[99])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable shipImgs does not seem to be defined.
Loading history...
184
185
186
def getSmallStarImg(name):
187
    return smallStarImgs[name]
188
189
190
def getBigStarImg(name):
191
    return bigStarImgs[name]
192
193
194
def getPlanetImg(pltype, plid):
195
    global planetImgCnt
196
    name = '%s%d' % (pltype, plid % planetImgCnt[pltype])
197
    return planetImgs[name]
198
199
200
def getButton(name, state):
201
    if state:
202
        name = "%s_%s" % (name, 'active')
203
    else:
204
        name = "%s_%s" % (name, 'inactive')
205
    return buttonImgs[name]
206
207
208
def getUnknownName():
209
    return _('[Unknown]')
210
211
212
def getNA():
213
    return _('N/A')
214
215
216
def getSystemOverviewProblemColor(owner, problem):
217
    if problem:
218
        return gdata.sevColors[gdata.CRI]
219
    else:
220
        return getPlayerColor(owner)
221
222
223
def getFFColorCode(relationship):
224
    if relationship == Const.REL_UNDEF:
225
        return (0xc0, 0xc0, 0xc0)
226
    if relationship == Const.REL_UNITY:
227
        return (0x00, 0xff, 0x00)
228
    relColors = [(0xff, 0x80, 0x80),
229
                 (0xff, 0x90, 0x01),
230
                 (0xff, 0xff, 0x00),
231
                 (0xb0, 0xb0, 0xff),
232
                 (0x80, 0xff, 0xff)]
233
    return relColors[bisect.bisect(Const.REL_BOUNDARIES, relationship)]
234
235
236
def getHabitabilityColorCode(bio):
237
    if bio < 0:
238
        return (0xff, 0x00, 0xff)
239
    colorCodes = [(255, 5 * bio, 0),  # end 255, 125, 0
240
                  (255 - 2 * (bio - 25), 125 + 2 * (bio - 25), 0),  # end 155, 225, 0
241
                  (155 - 3 * (bio - 75), 225, 0),  # end 5, 225, 0
242
                  (0, 255, 2 * (bio - 125)),  # end 0, 225, 250
243
                  (0, 255, 255)]
244
    boundaries = [25, 75, 125, 200]
245
    return colorCodes[bisect.bisect(boundaries, bio)]
246
247
248
def getPirateColonyColorCode(pirate):
249
    if pirate is False:
250
        return (0xc0, 0xc0, 0xc0)
251
    if pirate <= 0:
252
        return (0xff, 0x00, 0xff)
253
    colorCodes = [(255, 5 * pirate, 0),  # end 255, 125, 0
254
                  (255 - 2 * (pirate - 25), 125 + 2 * (pirate - 25), 0),  # end 155, 225, 0
255
                  (155 - 3 * (pirate - 75), 225, 0),  # end 5, 225, 0
256
                  (0, 255, 2 * (pirate - 125)),  # end 0, 225, 250
257
                  (0, 255, 255)]
258
    boundaries = [25, 75, 125, 200]
259
    return colorCodes[bisect.bisect(boundaries, pirate)]
260
261
262
def getFameColorCode(fame):
263
    if fame > 0 and fame < 200:
264
        # log.debug(fame, (0xff, 255 - int(255*(fame/200)), 0x00))
265
        return (0xff, 255 - int(255 * (fame / 200.0)), 0x00)
266
    if fame == 200:
267
        return (0xff, 0x00, 0xff)  # pirate colony
268
    return (0xc0, 0xc0, 0xc0)
269
270
271
def getMineralColorCode(minerals):
272
    if minerals >= 0:
273
        # use min, since it we occasionally get 201+ mineral levels,
274
        # but it is so rare that we can ignore it for colors.
275
        return (0xff, max(0, min(255, 255 - int(255 * (minerals / 200.0)))), 0x0)
276
    return (0xc0, 0xc0, 0xc0)
277
278
279
def getSlotColorCode(slots):
280
    if slots > 20:
281
        # in case sometime in the future we have larger worlds available
282
        return (0x0, 0xFF, min(255, (slots - 20) * 10))
283
    if slots > 0:
284
        return (int(255 * (slots / 20.0)), 255 - int(255 * (slots / 20.0)), 0x0)
285
    return (0xc0, 0xc0, 0xc0)
286
287
288
def getSlotSystemColorCode(slots, planets):
289
    if planets > 0:
290
        slots = int(float(slots) / planets)
291
        if slots > 20:
292
            # in case sometime in the future we have larger worlds available
293
            return (0x0, 0xFF, min(255, (slots - 20) * 10))
294
        if slots > 0:
295
            return (int(255 * (slots / 20.0)), 255 - int(255 * (slots / 20.0)), 0x0)
296
        return (0xc0, 0xc0, 0xc0)
297
    else:
298
        return (0xc0, 0xc0, 0xc0)
299
300
301
def getStargateColorCode(accel):
302
    accel = accel * 100 - 100
303
    if accel < 1:
304
        return (0xc0, 0xc0, 0xc0)
305
    if accel < 50:
306
        return (0xff, 0xff, 0x00)
307
    if accel < 150:
308
        return (0xff, 0xc0, 0x00)
309
    if accel < 250:
310
        return (0xff, 0x60, 0x00)
311
    if accel < 350:
312
        return (0xff, 0x00, 0x00)
313
    if accel < 450:
314
        return (0xff, 0x00, 0x80)
315
    if accel > 449:
316
        return (0xff, 0x00, 0xff)
317
    return (0xc0, 0xc0, 0xc0)
318
319
320
def getDockColorCode(refuel, upgrades):
321
    # refuel is based on best dock; upgrades are based on sum of all docks
322
    # refuel range: 0...6
323
    # upgrade range: 0...100 (cap 100)
324
    if (refuel > 1 and upgrades > 0) or (refuel > 2):
325
        # refuel > 2 for other player docks since they always read upgrades of 0
326
        # this probably should be corrected for allies
327
        refuelScale = max(0, min(1, (refuel - 1) / 5.0))
328
        upgradeScale = max(0, min(1, upgrades / 50))
329
        return (0xFF, int(refuelScale * (1 - upgradeScale) * 255), int(refuelScale * (upgradeScale) * 255))
330
    if refuel > 0:
331
        refuelScale = max(0, min(1, refuel / 2.0))
332
        return (0x00, 100 + 100 * int(refuelScale), 100 + 100 * int(refuelScale))  # cyan
333
    return (0xc0, 0xc0, 0xc0)
334
335
336
def getMoraleColors(morale):
337
    if morale >= 0:
338
        return (255 - int(255 * (morale / 100.0)), int(255 * (morale / 100.0)), 0x0)
339
    return (0xc0, 0xc0, 0xc0)
340
341
342
def getPlayerColor(owner, onlyDiplo = False):
343
    if owner == Const.OID_NONE:
344
        return getFFColorCode(Const.REL_UNDEF)
345
    if not onlyDiplo:
346
        if gdata.config.defaults.highlights == 'yes':
347
            if gdata.playersHighlightColors.has_key(owner):
348
                return gdata.playersHighlightColors[owner]
349
    rel = min(Const.REL_UNDEF, client.getRelationTo(owner))
350
    return getFFColorCode(rel)
351
352
353
def getControlColor(owner, onlyDiplo = False):
354
    if owner == Const.OID_NONE:
355
        return False
356
    if not onlyDiplo:
357
        if gdata.config.defaults.highlights == 'yes':
358
            if gdata.playersHighlightColors.has_key(owner):
359
                return fadeDarkColor(gdata.playersHighlightColors[owner])
360
    rel = min(Const.REL_UNDEF, client.getRelationTo(owner))
361
    return fadeDarkColor(getFFColorCode(rel))
362
363
364
def getGateLineWidth(owner):
365
    if owner == Const.OID_NONE:
366
        return 1
367
    rel = min(Const.REL_UNDEF, client.getRelationTo(owner))
368
    if rel == 1250:
369
        return 2
370
    return 1
371
372
373
def getStarmapWidgetPlanetColor(ownerid, bio, mineral, slot,
374
                                stargate, dockfuel, dockupgrade, fame,
375
                                stratres, morale, pirate=False):
376
    colors = {}
377
    for datatype in gdata.OVERLAY_TYPES:
378
        colors[datatype] = getStarmapWidgetPlanetColorPerDatatype(datatype, ownerid, bio, mineral, slot,
379
                                                                  stargate, dockfuel, dockupgrade, fame,
380
                                                                  stratres, morale, pirate)
381
    return colors
382
383
384
def getStarmapWidgetSystemColor(ownerid, bio, mineral, slot,
385
                                num_planets, stargate, dockfuel, dockupgrade, fame,
386
                                stratres, morale, pirate=False):
387
    colors = {}
388
    for datatype in gdata.OVERLAY_TYPES:
389
        colors[datatype] = getStarmapWidgetSystemColorPerDatatype(datatype, ownerid, bio, mineral, slot,
390
                                                                  num_planets, stargate, dockfuel, dockupgrade, fame,
391
                                                                  stratres, morale, pirate)
392
    return colors
393
394
395 View Code Duplication
def getStarmapWidgetPlanetColorPerDatatype(datatype, ownerid, bio, mineral, slot,
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
396
                                           stargate, dockfuel, dockupgrade, fame,
397
                                           stratres, morale, pirate=False):
398
    if (datatype == gdata.OVERLAY_OWNER):
399
        return getPlayerColor(ownerid)
400
    if (datatype == gdata.OVERLAY_DIPLO):
401
        return getPlayerColor(ownerid, True)
402
    if (datatype == gdata.OVERLAY_BIO):
403
        return getHabitabilityColorCode(bio)
404
    if (datatype == gdata.OVERLAY_MIN):
405
        return getMineralColorCode(mineral)
406
    if (datatype == gdata.OVERLAY_SLOT):
407
        return getSlotColorCode(slot)
408
    if (datatype == gdata.OVERLAY_STARGATE):
409
        return getStargateColorCode(stargate)
410
    if (datatype == gdata.OVERLAY_DOCK):
411
        return getDockColorCode(dockfuel, dockupgrade)
412
    if (datatype == gdata.OVERLAY_FAME):
413
        return getFameColorCode(fame)
414
    if (datatype == gdata.OVERLAY_PIRATECOLONYCOST):
415
        return getPirateColonyColorCode(pirate)
416
#    if (datatype == "stratres"):
417
#        return getMoraleColors(stratres)
418
    if (datatype == gdata.OVERLAY_MORALE):
419
        return getMoraleColors(morale)
420
    return getPlayerColor(ownerid)  # default
421
422
423 View Code Duplication
def getStarmapWidgetSystemColorPerDatatype(datatype, ownerid, bio, mineral, slot,
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
424
                                           num_planets, stargate, dockfuel, dockupgrade, fame,
425
                                           stratres, morale, pirate=False):
426
    if (datatype == gdata.OVERLAY_OWNER):
427
        return getPlayerColor(ownerid)
428
    if (datatype == gdata.OVERLAY_DIPLO):
429
        return getPlayerColor(ownerid, True)
430
    if (datatype == gdata.OVERLAY_BIO):
431
        return getHabitabilityColorCode(bio)
432
    if (datatype == gdata.OVERLAY_MIN):
433
        return getMineralColorCode(mineral)
434
    if (datatype == gdata.OVERLAY_SLOT):
435
        return getSlotSystemColorCode(slot, num_planets)
436
    if (datatype == gdata.OVERLAY_STARGATE):
437
        return getStargateColorCode(stargate)
438
    if (datatype == gdata.OVERLAY_DOCK):
439
        return getDockColorCode(dockfuel, dockupgrade)
440
    if (datatype == gdata.OVERLAY_FAME):
441
        return getFameColorCode(fame)
442
    if (datatype == gdata.OVERLAY_PIRATECOLONYCOST):
443
        return getPirateColonyColorCode(pirate)
444
#    if (datatype == "stratres"):
445
#        return getMoraleColors(stratres)
446
    if (datatype == gdata.OVERLAY_MORALE):
447
        return getMoraleColors(morale)
448
    return getPlayerColor(ownerid)  # default
449
450
451
def fadeColor(triplet):
452
    return ((triplet[0] + 0xc0) / 2, (triplet[1] + 0xc0) / 2, (triplet[2] + 0xc0) / 2)
453
454
455
def fadeDarkColor(triplet):
456
    return ((triplet[0] + 0x00 * 2) / 3, (triplet[1] + 0x00 * 2) / 3, (triplet[2] + 0x00 * 2) / 3)
457
458
459
def formatTime(time, separator=':'):
460
    time = int(math.ceil(time))
461
    sign = ''
462
    if time < 0:
463
        time = - time
464
        sign = '-'
465
    days = time / Rules.turnsPerDay
466
    hours = time % Rules.turnsPerDay
467
    return '%s%d%s%02d' % (sign, days, separator, hours)
468
469
470
def formatBE(b, e):
471
    return '%d / %d' % (b, e)
472
473
474
def globalQueueName(index):
475
    return ['Default', 'Red', 'Blue', 'Yellow', 'Violet'][index]
476